Skip to content

Commit

Permalink
fix(addLabel): update Label type & add some integration test (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
evenchange4 authored Nov 4, 2017
1 parent c2d3de5 commit e29460a
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ before_install:
script:
- yarn run eslint
- yarn run test
- yarn run flow
- yarn run flow-coverage

after_success:
- ./node_modules/.bin/codecov
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"test": "NODE_ENV='test' TZ='UTC' jest --coverage --runInBand --forceExit",
"test:watch": "npm run test -- --watch",
"eslint": "eslint ./",
"format": "prettier --write 'src/**/*.js'",
"format": "prettier --write 'src/**/*.{js,json}'",
"flow": "flow",
"flow-coverage": "flow-coverage-report",
"changelog": "github-changes -o evenchange4 -r gh-polls-bot -b master -f ./CHANGELOG.md --order-semver --use-commit-body"
Expand Down
28 changes: 28 additions & 0 deletions src/__tests__/fixtures/issue.edited.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"event": "issues",
"payload": {
"action": "edited",
"issue": {
"number": 1234,
"labels": [
{
"id": 737406747,
"url": "url",
"name": "Polls",
"color": "ededed",
"default": false
}
],
"body": "/polls Option4 'Option 5' \"Option 6\""
},
"repository": {
"name": "test",
"owner": {
"login": "evenchange4"
}
},
"installation": {
"id": 1234
}
}
}
20 changes: 20 additions & 0 deletions src/__tests__/fixtures/issue.opened.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"event": "issues",
"payload": {
"action": "opened",
"issue": {
"number": 1234,
"labels": [],
"body": "/polls Option1 'Option 2' \"Option 3\""
},
"repository": {
"name": "test",
"owner": {
"login": "evenchange4"
}
},
"installation": {
"id": 1234
}
}
}
68 changes: 60 additions & 8 deletions src/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,62 @@
// @flow
const main = require('../index');

it('should call on', () => {
const mockRobot = {
on: jest.fn(),
};
main(mockRobot);
expect(mockRobot.on).toHaveBeenCalled();
const { createRobot } = require('probot');
const app = require('../index');
const issueOpenedPayload /* : Object */ = require('./fixtures/issue.opened.json');
const issueEditedPayload /* : Object */ = require('./fixtures/issue.edited.json');

jest.mock('../utils/API', () => ({
addPoll: () => new Promise(resolve => resolve('ID')),
}));

/**
* ref: https://probot.github.io/docs/testing/
*/
describe('App Integration Test', () => {
let robot /* : Robot */;
let mockGitHubAPI;

beforeEach(() => {
robot /* : Robot */ = createRobot();
app(robot);
mockGitHubAPI = {
issues: {
addLabels: jest.fn(),
edit: jest.fn(),
},
};
robot.auth = () => Promise.resolve(mockGitHubAPI);
});

it('should performs actions when issue opened', async () => {
await robot.receive(issueOpenedPayload);

expect(mockGitHubAPI.issues.addLabels).toHaveBeenCalledWith({
labels: ['Polls'],
number: 1234,
owner: 'evenchange4',
repo: 'test',
});
expect(mockGitHubAPI.issues.edit).toHaveBeenCalledWith({
body: `[![](https://m131jyck4m.execute-api.us-west-2.amazonaws.com/prod/poll/ID/Option1)](https://m131jyck4m.execute-api.us-west-2.amazonaws.com/prod/poll/ID/Option1/vote)
[![](https://m131jyck4m.execute-api.us-west-2.amazonaws.com/prod/poll/ID/Option%202)](https://m131jyck4m.execute-api.us-west-2.amazonaws.com/prod/poll/ID/Option%202/vote)
[![](https://m131jyck4m.execute-api.us-west-2.amazonaws.com/prod/poll/ID/Option%203)](https://m131jyck4m.execute-api.us-west-2.amazonaws.com/prod/poll/ID/Option%203/vote)`,
number: 1234,
owner: 'evenchange4',
repo: 'test',
});
});

it('should performs actions when issue edited', async () => {
await robot.receive(issueEditedPayload);

expect(mockGitHubAPI.issues.addLabels).not.toHaveBeenCalled();
expect(mockGitHubAPI.issues.edit).toHaveBeenCalledWith({
body: `[![](https://m131jyck4m.execute-api.us-west-2.amazonaws.com/prod/poll/ID/Option4)](https://m131jyck4m.execute-api.us-west-2.amazonaws.com/prod/poll/ID/Option4/vote)
[![](https://m131jyck4m.execute-api.us-west-2.amazonaws.com/prod/poll/ID/Option%205)](https://m131jyck4m.execute-api.us-west-2.amazonaws.com/prod/poll/ID/Option%205/vote)
[![](https://m131jyck4m.execute-api.us-west-2.amazonaws.com/prod/poll/ID/Option%206)](https://m131jyck4m.execute-api.us-west-2.amazonaws.com/prod/poll/ID/Option%206/vote)`,
number: 1234,
owner: 'evenchange4',
repo: 'test',
});
});
});
11 changes: 7 additions & 4 deletions src/__tests__/listener.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ it('should handle addPollListener', async () => {
const mockContext = {
payload: {
issue: {
number: 1234,
body: 'H1H3\n/polls 1 2 3',
labels: [],
},
Expand All @@ -36,7 +37,7 @@ it('should handle addPollListener', async () => {
});
});

it('should not addLabels if there is one', async () => {
it('should not perform addLabels action when there is one', async () => {
const mockGitHubAPI = {
issues: {
addLabels: jest.fn(),
Expand All @@ -46,8 +47,9 @@ it('should not addLabels if there is one', async () => {
const mockContext = {
payload: {
issue: {
number: 1234,
body: 'H1H3\n/polls 1 2 3',
labels: ['Polls'],
labels: [{ name: 'Polls' }],
},
},
github: mockGitHubAPI,
Expand All @@ -64,7 +66,7 @@ it('should not addLabels if there is one', async () => {
});
});

it('should handle commands without any arguments', async () => {
it('should not performs actions with empty arguments', async () => {
const mockGitHubAPI = {
issues: {
addLabels: jest.fn(),
Expand All @@ -74,8 +76,9 @@ it('should handle commands without any arguments', async () => {
const mockContext = {
payload: {
issue: {
number: 1234,
body: 'H1H3\n/polls',
labels: ['Polls'],
labels: [{ name: 'Polls' }],
},
},
github: mockGitHubAPI,
Expand Down
3 changes: 2 additions & 1 deletion src/listener.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @flow
const split /* : string => string[] */ = require('argv-split');
const R = require('ramda');
const getCommand = require('./utils/getCommand');
const { addPoll } = require('./utils/API');
const toMarkdown = require('./utils/toMarkdown');
Expand All @@ -18,7 +19,7 @@ const addPollListener /* : Listener */ = async context => {
const id = await addPoll(options);

// 2. Add Label
if (!labels.includes(LABEL)) {
if (!R.any(R.propEq('name', LABEL))(labels)) {
await context.github.issues.addLabels(context.issue({ labels: [LABEL] }));
}

Expand Down
22 changes: 15 additions & 7 deletions src/type/index.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@
/**
* For Probot types
*/

declare type Label = {
name: string,
};
declare type GitHubApi = {
issues: {
addLabels: Object => Promise<void>,
edit: Object => Promise<void>,
},
};
declare type Context = {
payload: {
issue: {
number: number,
body: string,
labels: string[],
labels: Label[],
},
},
issue: Object => Object,
github: {
issues: {
addLabels: Object => Promise<void>,
edit: Object => Promise<void>,
},
},
github: GitHubApi,
};
declare type Listener = (Context) => Promise<void>;
declare type Robot = {
on: (string | string[], Listener) => void,
auth: () => Promise<GitHubApi>,
receive: Object => void,
};

0 comments on commit e29460a

Please sign in to comment.