-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.js
187 lines (172 loc) · 5.55 KB
/
test.js
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
'use strict'
const tap = require('tap')
const sinon = require('sinon')
const { fetchIssues } = require('./fetchIssues')
const mockIssue = {
url: 'https://api.github.com/repos/fastify/help/issues/478',
labels_url:
'https://api.github.com/repos/fastify/help/issues/478/labels{/name}',
comments_url: 'https://api.github.com/repos/fastify/help/issues/478/comments',
events_url: 'https://api.github.com/repos/fastify/help/issues/478/events',
id: 933476766,
node_id: 'MDU6SXNzdWU5MzM0NzY3NjY=',
number: 478,
title: 'Full text search with mongoDB',
user: {
login: 'Dubask',
id: 30698018,
node_id: 'MDQ6VXNlcjMwNjk4MDE4',
avatar_url: 'https://avatars.githubusercontent.com/u/30698018?v=4',
gravatar_id: '',
url: 'https://api.github.com/users/Dubask',
html_url: 'https://github.com/Dubask',
followers_url: 'https://api.github.com/users/Dubask/followers',
following_url: 'https://api.github.com/users/Dubask/following{/other_user}',
gists_url: 'https://api.github.com/users/Dubask/gists{/gist_id}',
starred_url: 'https://api.github.com/users/Dubask/starred{/owner}{/repo}',
subscriptions_url: 'https://api.github.com/users/Dubask/subscriptions',
organizations_url: 'https://api.github.com/users/Dubask/orgs',
repos_url: 'https://api.github.com/users/Dubask/repos',
events_url: 'https://api.github.com/users/Dubask/events{/privacy}',
received_events_url: 'https://api.github.com/users/Dubask/received_events',
type: 'User',
site_admin: false
},
labels: [{ name: 'testLabel' }],
state: 'open',
locked: false,
assignee: null,
assignees: [],
milestone: null,
comments: 3,
created_at: '2021-06-30T09:12:31Z',
updated_at: '2021-06-30T13:17:56Z',
closed_at: null,
author_association: 'NONE',
active_lock_reason: null,
body: 'test_body',
reactions: {
url: 'https://api.github.com/repos/fastify/help/issues/478/reactions',
total_count: 0,
'+1': 0,
'-1': 0,
laugh: 0,
hooray: 0,
confused: 0,
heart: 0,
rocket: 0,
eyes: 0
},
timeline_url: 'https://api.github.com/repos/fastify/help/issues/478/timeline',
performed_via_github_app: null,
score: 1
}
const mockIssue1 = {
...mockIssue,
repository_url: 'https://api.github.com/repos/fastify/test1',
html_url: 'https://github.com/fastify/help/issues/478'
}
// app was falling over because of incorrectly parsing this url previously
const mockIssue2 = {
...mockIssue,
html_url: 'https://github.com/fastify/help/issues/4781',
repository_url: 'https://api.github.com/repos/fastify/.test2'
}
const mockIssuesData = {
total_count: 40,
incomplete_results: false
}
const mockSearchIssuesAndPullRequests = {
status: 200
}
tap.test('tests the "/api/find-issues" route', async t => {
const searchIssuesStub = sinon.stub()
searchIssuesStub.onCall(0).returns({
...mockSearchIssuesAndPullRequests,
data: { ...mockIssuesData, items: [mockIssue1] }
})
searchIssuesStub.onCall(1).returns({
...mockSearchIssuesAndPullRequests,
data: { ...mockIssuesData, items: [mockIssue2] }
})
searchIssuesStub.onCall(2).returns({
...mockSearchIssuesAndPullRequests,
data: { ...mockIssuesData, items: [mockIssue1] }
})
searchIssuesStub.onCall(3).returns({
...mockSearchIssuesAndPullRequests,
data: { ...mockIssuesData, items: [mockIssue2] }
})
const build = t.mock('./app', {
'./fetchIssues': {
fetchIssues,
getGithubClient: () => {
return {
search: { issuesAndPullRequests: () => searchIssuesStub() }
}
}
}
})
const app = build()
// test with defaults
const response = await app.inject({
method: 'GET',
url: '/api/find-issues'
})
const expectedResponseBody = {
results: [
{
url: 'https://github.com/fastify/help/issues/478',
title: 'Full text search with mongoDB',
comments: 3,
author: {
name: 'Dubask',
avatar_url: 'https://avatars.githubusercontent.com/u/30698018?v=4',
acc_url: 'https://github.com/Dubask'
},
project: {
org: 'fastify',
name: 'test1',
url: 'https://github.com/fastify/test1'
},
locked: false,
state: 'open',
created_at: '2021-06-30T09:12:31Z',
updated_at: '2021-06-30T13:17:56Z',
labels: ['testLabel']
},
{
url: 'https://github.com/fastify/help/issues/4781',
title: 'Full text search with mongoDB',
comments: 3,
author: {
name: 'Dubask',
avatar_url: 'https://avatars.githubusercontent.com/u/30698018?v=4',
acc_url: 'https://github.com/Dubask'
},
project: {
org: 'fastify',
name: '.test2',
url: 'https://github.com/fastify/.test2'
},
locked: false,
state: 'open',
created_at: '2021-06-30T09:12:31Z',
updated_at: '2021-06-30T13:17:56Z',
labels: ['testLabel']
}
]
}
tap.equal(response.statusCode, 200, 'returns a status code of 200')
tap.equal(response.body, JSON.stringify(expectedResponseBody))
expectedResponseBody.results[0].body = mockIssue.body
expectedResponseBody.results[1].body = mockIssue.body
// test with specifying query params
const response2 = await app.inject({
method: 'GET',
url: '/api/find-issues?org=test&includeBody=true&labels=1&labels=2'
})
tap.equal(response2.statusCode, 200, 'returns a status code of 200')
tap.equal(JSON.parse(response2.body).results[0].body, mockIssue.body)
tap.equal(JSON.parse(response2.body).results[1].body, mockIssue.body)
})