Skip to content
This repository was archived by the owner on Apr 1, 2020. It is now read-only.

Commit 262f653

Browse files
committed
test: refactor tests
1 parent d3486b6 commit 262f653

File tree

2 files changed

+56
-22
lines changed

2 files changed

+56
-22
lines changed

test/bot.test.js

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ const events = require('./events')
1010
// Constants
1111
const baseStatus = {
1212
sha: '123456789',
13-
repo: 'test-commitlint-bot',
14-
owner: 'ahmed-taj',
13+
repo: 'repo',
14+
owner: 'user',
1515
context: 'commitlint-bot',
1616
target_url: 'http://npm.im/@commitlint/config-angular#problems'
1717
}
@@ -31,20 +31,56 @@ describe('commitlint-bot', () => {
3131
robot.auth = () => Promise.resolve(github)
3232
})
3333

34-
describe('update status to pending', () => {
35-
it('calls createStatus with "pending"', async () => {
36-
const pending = {
37-
...baseStatus,
38-
state: 'pending',
39-
description: 'Waiting for the status to be reported'
40-
}
41-
// Simulates delivery of a payload
42-
// New PR
34+
describe('updates status to pending', () => {
35+
const pending = {
36+
...baseStatus,
37+
state: 'pending',
38+
description: 'Waiting for the status to be reported'
39+
}
40+
41+
it('works with new PRs', async () => {
4342
await robot.receive(events.opened)
4443
expect(github.repos.createStatus).toHaveBeenCalledWith(pending)
45-
// Updated PR
44+
})
45+
46+
it('works with updated PRs', async () => {
4647
await robot.receive(events.synchronize)
4748
expect(github.repos.createStatus).toHaveBeenCalledWith(pending)
4849
})
4950
})
51+
52+
describe('gets the list of commits for PRs', () => {
53+
const info = { repo: 'repo', owner: 'user', number: 1 }
54+
55+
it('works with new PRs', async () => {
56+
await robot.receive(events.opened)
57+
expect(github.pullRequests.getCommits).toHaveBeenCalledWith(info)
58+
})
59+
60+
it('works with updated PRs', async () => {
61+
await robot.receive(events.synchronize)
62+
expect(github.pullRequests.getCommits).toHaveBeenCalledWith(info)
63+
})
64+
})
65+
66+
describe('sends success when messages are valid', () => {
67+
const success = {
68+
...baseStatus,
69+
state: 'success',
70+
// description: 'found 0 problems, 0 warnings'
71+
description: 'ok'
72+
}
73+
74+
it('works with new PRs', async () => {
75+
// Prepare
76+
// github = githubMock(['fix: me'])
77+
// robot.auth = () => Promise.resolve(github)
78+
await robot.receive(events.opened)
79+
expect(github.repos.createStatus).toHaveBeenCalledWith(success)
80+
})
81+
it('works with updated PRs', async () => {
82+
await robot.receive(events.synchronize)
83+
expect(github.repos.createStatus).toHaveBeenCalledWith(success)
84+
})
85+
})
5086
})

test/mocks/github.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,20 @@
22
const expect = require('expect')
33

44
// Mock necessary GitHub APIs here
5-
module.exports = data => {
6-
const commits = data || [
7-
{ commit: { message: 'fix: issue #1' } },
8-
{ commit: { message: 'fix: issue #2' } },
9-
{ commit: { message: 'fix: issue #3' } },
10-
{ commit: { message: 'fix: issue #4' } }
11-
]
5+
module.exports = (data = ['fix: issue #1']) => {
126
return {
137
repos: {
148
createStatus: expect.createSpy()
159
},
1610
pullRequests: {
17-
getCommits: expect.createSpy().andReturn(Promise.resolve({ commits }))
11+
getCommits: expect.createSpy().andReturn({
12+
data: data.map(e => {
13+
return { commit: { message: e } }
14+
})
15+
})
1816
},
19-
paginate: (fn, callback) => {
20-
callback(fn)
17+
paginate: async (fn, callback) => {
18+
callback(await fn)
2119
}
2220
}
2321
}

0 commit comments

Comments
 (0)