Repository navigation
Expand file tree
/
Copy pathnode-repo.test.js
More file actions
124 lines (104 loc) · 4.22 KB
/
Copy pathnode-repo.test.js
File metadata and controls
124 lines (104 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
'use strict'
const lolex = require('lolex')
const proxyquire = require('proxyquire')
const sinon = require('sinon')
const tap = require('tap')
const logger = require('../../lib/logger')
const githubClient = require('../../lib/github-client')
const readFixture = require('../read-fixture')
tap.test('fetchExistingLabels(): caches existing repository labels', (t) => {
sinon.stub(githubClient.issues, 'getLabels').yields(null, [])
sinon.stub(githubClient, 'hasNextPage', () => false)
const nodeRepo = proxyquire('../../lib/node-repo', {
'./github-client': githubClient
})
t.plan(1)
t.tearDown(() => {
githubClient.issues.getLabels.restore()
githubClient.hasNextPage.restore()
})
nodeRepo._fetchExistingLabels({ owner: 'nodejs', repo: 'node', logger }, () => {
nodeRepo._fetchExistingLabels({ owner: 'nodejs', repo: 'node', logger }, () => {
t.ok(githubClient.issues.getLabels.calledOnce)
})
})
})
tap.test('fetchExistingLabels(): cache expires after one hour', (t) => {
const clock = lolex.install()
sinon.stub(githubClient.issues, 'getLabels').yields(null, [])
sinon.stub(githubClient, 'hasNextPage', () => false)
const nodeRepo = proxyquire('../../lib/node-repo', {
'./github-client': githubClient
})
t.plan(1)
t.tearDown(() => {
githubClient.issues.getLabels.restore()
githubClient.hasNextPage.restore()
clock.uninstall()
})
nodeRepo._fetchExistingLabels({ owner: 'nodejs', repo: 'node', logger }, () => {
// fetch labels again after 1 hour and 1 minute
clock.tick(1000 * 60 * 61)
nodeRepo._fetchExistingLabels({ owner: 'nodejs', repo: 'node', logger }, () => {
t.equal(githubClient.issues.getLabels.callCount, 2)
})
})
})
tap.test('fetchExistingLabels(): yields an array of existing label names', (t) => {
const labelsFixture = readFixture('repo-labels.json')
sinon.stub(githubClient.issues, 'getLabels').yields(null, labelsFixture)
sinon.stub(githubClient, 'hasNextPage', () => false)
const nodeRepo = proxyquire('../../lib/node-repo', {
'./github-client': githubClient
})
t.plan(2)
t.tearDown(() => {
githubClient.issues.getLabels.restore()
githubClient.hasNextPage.restore()
})
nodeRepo._fetchExistingLabels({ owner: 'nodejs', repo: 'node', logger }, (err, existingLabels) => {
t.equal(err, null)
t.ok(existingLabels.includes('cluster'))
})
})
tap.test('fetchExistingLabels(): can retrieve more than 100 labels', (t) => {
const labelsFixturePage1 = readFixture('repo-labels.json')
const labelsFixturePage2 = readFixture('repo-labels-page-2.json')
sinon.stub(githubClient.issues, 'getLabels', (options, cb) => cb(null, options.page === 1 ? labelsFixturePage1 : labelsFixturePage2))
sinon.stub(githubClient, 'hasNextPage', (listing) => listing === labelsFixturePage1)
const nodeRepo = proxyquire('../../lib/node-repo', {'./github-client': githubClient})
t.plan(3)
t.tearDown(() => {
githubClient.issues.getLabels.restore()
githubClient.hasNextPage.restore()
})
nodeRepo._fetchExistingLabels({ owner: 'nodejs', repo: 'node', logger }, (err, existingLabels) => {
t.equal(err, null)
t.ok(existingLabels.includes('cluster'))
t.ok(existingLabels.includes('windows'))
})
})
tap.test('getBotPrLabels(): returns labels added by nodejs-github-bot', (t) => {
const events = readFixture('pull-request-events.json')
sinon.stub(githubClient.issues, 'getEvents', (options, cb) => { cb(null, events) })
const nodeRepo = proxyquire('../../lib/node-repo', {'./github-client': githubClient})
t.plan(1)
t.tearDown(() => {
githubClient.issues.getEvents.restore()
})
nodeRepo.getBotPrLabels({ owner: 'nodejs', repo: 'node', prId: '1' }, (_, labels) => {
t.same(labels, ['testlabel'])
})
})
tap.test('getBotPrLabels(): returns net labels added/removed by nodejs-github-bot', (t) => {
const events = readFixture('pull-request-events-2.json')
sinon.stub(githubClient.issues, 'getEvents', (options, cb) => { cb(null, events) })
const nodeRepo = proxyquire('../../lib/node-repo', {'./github-client': githubClient})
t.plan(1)
t.tearDown(() => {
githubClient.issues.getEvents.restore()
})
nodeRepo.getBotPrLabels({ owner: 'nodejs', repo: 'node', prId: '1' }, (_, labels) => {
t.same(labels, [])
})
})