This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 402
/
Copy pathcomment-decorations-container.test.js
443 lines (380 loc) · 17.6 KB
/
comment-decorations-container.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import React from 'react';
import {shallow} from 'enzyme';
import {QueryRenderer} from 'react-relay';
import {cloneRepository, buildRepository} from '../helpers';
import {queryBuilder} from '../builder/graphql/query';
import {multiFilePatchBuilder} from '../builder/patch';
import Branch, {nullBranch} from '../../lib/models/branch';
import BranchSet from '../../lib/models/branch-set';
import GithubLoginModel from '../../lib/models/github-login-model';
import ObserveModel from '../../lib/views/observe-model';
import Remote from '../../lib/models/remote';
import RemoteSet from '../../lib/models/remote-set';
import {InMemoryStrategy, UNAUTHENTICATED, INSUFFICIENT} from '../../lib/shared/keytar-strategy';
import CommentDecorationsContainer from '../../lib/containers/comment-decorations-container';
import PullRequestPatchContainer from '../../lib/containers/pr-patch-container';
import CommentPositioningContainer from '../../lib/containers/comment-positioning-container';
import AggregatedReviewsContainer from '../../lib/containers/aggregated-reviews-container';
import CommentDecorationsController from '../../lib/controllers/comment-decorations-controller';
import rootQuery from '../../lib/containers/__generated__/commentDecorationsContainerQuery.graphql.js';
describe('CommentDecorationsContainer', function() {
let atomEnv, workspace, localRepository, loginModel;
beforeEach(async function() {
atomEnv = global.buildAtomEnvironment();
workspace = atomEnv.workspace;
localRepository = await buildRepository(await cloneRepository());
loginModel = new GithubLoginModel(InMemoryStrategy);
});
afterEach(function() {
atomEnv.destroy();
});
function buildApp(overrideProps = {}) {
return (
<CommentDecorationsContainer
workspace={workspace}
localRepository={localRepository}
loginModel={loginModel}
reportRelayError={() => {}}
commands={atomEnv.commands}
children={() => <div />}
{...overrideProps}
/>
);
}
it('renders nothing while repository data is being fetched', function() {
const wrapper = shallow(buildApp());
const localRepoWrapper = wrapper.find(ObserveModel).renderProp('children')(null);
assert.isTrue(localRepoWrapper.isEmptyRender());
});
it('renders nothing if no GitHub remotes exist', async function() {
const wrapper = shallow(buildApp());
const localRepoData = await wrapper.find(ObserveModel).prop('fetchData')(localRepository);
const localRepoWrapper = wrapper.find(ObserveModel).renderProp('children')(localRepoData);
const token = await localRepoWrapper.find(ObserveModel).prop('fetchData')(loginModel, localRepoData);
assert.isNull(token);
const tokenWrapper = localRepoWrapper.find(ObserveModel).renderProp('children')(null);
assert.isTrue(tokenWrapper.isEmptyRender());
});
it('renders nothing if no head branch is present', async function() {
const wrapper = shallow(buildApp({localRepository, loginModel}));
const origin = new Remote('origin', '[email protected]:atom/github.git');
const repoData = {
branches: new BranchSet(),
remotes: new RemoteSet([origin]),
currentRemote: origin,
workingDirectoryPath: 'path/path',
};
const localRepoWrapper = wrapper.find(ObserveModel).renderProp('children')(repoData);
const token = await localRepoWrapper.find(ObserveModel).prop('fetchData')(loginModel, repoData);
assert.isNull(token);
const tokenWrapper = localRepoWrapper.find(ObserveModel).renderProp('children')('1234');
assert.isTrue(tokenWrapper.isEmptyRender());
});
it('renders nothing if the head branch has no push upstream', function() {
const wrapper = shallow(buildApp({localRepository, loginModel}));
const origin = new Remote('origin', '[email protected]:atom/github.git');
const upstreamBranch = Branch.createRemoteTracking('refs/remotes/origin/master', 'origin', 'refs/heads/master');
const branch = new Branch('master', upstreamBranch, nullBranch, true);
const repoData = {
branches: new BranchSet([branch]),
remotes: new RemoteSet([origin]),
currentRemote: origin,
workingDirectoryPath: 'path/path',
};
const localRepoWrapper = wrapper.find(ObserveModel).renderProp('children')(repoData);
const tokenWrapper = localRepoWrapper.find(ObserveModel).renderProp('children')('1234');
assert.isTrue(tokenWrapper.isEmptyRender());
});
it('renders nothing if the push remote is invalid', function() {
const wrapper = shallow(buildApp({localRepository, loginModel}));
const origin = new Remote('origin', '[email protected]:atom/github.git');
const upstreamBranch = Branch.createRemoteTracking('refs/remotes/nope/master', 'nope', 'refs/heads/master');
const branch = new Branch('master', upstreamBranch, upstreamBranch, true);
const repoData = {
branches: new BranchSet([branch]),
remotes: new RemoteSet([origin]),
currentRemote: origin,
workingDirectoryPath: 'path/path',
};
const localRepoWrapper = wrapper.find(ObserveModel).renderProp('children')(repoData);
const tokenWrapper = localRepoWrapper.find(ObserveModel).renderProp('children')('1234');
assert.isTrue(tokenWrapper.isEmptyRender());
});
it('renders nothing if the push remote is not a GitHub remote', function() {
const wrapper = shallow(buildApp({localRepository, loginModel}));
const origin = new Remote('origin', '[email protected]:atom/github.git');
const upstreamBranch = Branch.createRemoteTracking('refs/remotes/origin/master', 'origin', 'refs/heads/master');
const branch = new Branch('master', upstreamBranch, upstreamBranch, true);
const repoData = {
branches: new BranchSet([branch]),
remotes: new RemoteSet([origin]),
currentRemote: origin,
workingDirectoryPath: 'path/path',
};
const localRepoWrapper = wrapper.find(ObserveModel).renderProp('children')(repoData);
const tokenWrapper = localRepoWrapper.find(ObserveModel).renderProp('children')('1234');
assert.isTrue(tokenWrapper.isEmptyRender());
});
describe('when GitHub remote exists', function() {
let localRepo, repoData, wrapper, localRepoWrapper;
beforeEach(async function() {
await loginModel.setToken('https://api.github.com', '1234');
sinon.stub(loginModel, 'getScopes').resolves(GithubLoginModel.REQUIRED_SCOPES);
localRepo = await buildRepository(await cloneRepository());
wrapper = shallow(buildApp({localRepository: localRepo, loginModel}));
const origin = new Remote('origin', '[email protected]:atom/github.git');
const upstreamBranch = Branch.createRemoteTracking('refs/remotes/origin/master', 'origin', 'refs/heads/master');
const branch = new Branch('master', upstreamBranch, upstreamBranch, true);
repoData = {
branches: new BranchSet([branch]),
remotes: new RemoteSet([origin]),
currentRemote: origin,
workingDirectoryPath: 'path/path',
};
localRepoWrapper = wrapper.find(ObserveModel).renderProp('children')(repoData);
});
it('renders nothing if token is UNAUTHENTICATED', function() {
const tokenWrapper = localRepoWrapper.find(ObserveModel).renderProp('children')(UNAUTHENTICATED);
assert.isTrue(tokenWrapper.isEmptyRender());
});
it('renders nothing if token is INSUFFICIENT', function() {
const tokenWrapper = localRepoWrapper.find(ObserveModel).renderProp('children')(INSUFFICIENT);
assert.isTrue(tokenWrapper.isEmptyRender());
});
it('makes a relay query if token works', async function() {
const token = await localRepoWrapper.find(ObserveModel).prop('fetchData')(loginModel, repoData);
assert.strictEqual(token, '1234');
const tokenWrapper = localRepoWrapper.find(ObserveModel).renderProp('children')('1234');
assert.lengthOf(tokenWrapper.find(QueryRenderer), 1);
});
it('renders nothing if query errors', function() {
const tokenWrapper = localRepoWrapper.find(ObserveModel).renderProp('children')('1234');
const resultWrapper = tokenWrapper.find(QueryRenderer).renderProp('render')({
error: 'oh noes', props: null, retry: () => {}});
assert.isTrue(resultWrapper.isEmptyRender());
});
it('renders nothing if query is loading', function() {
const tokenWrapper = localRepoWrapper.find(ObserveModel).renderProp('children')('1234');
const resultWrapper = tokenWrapper.find(QueryRenderer).renderProp('render')({
error: null, props: null, retry: () => {}});
assert.isTrue(resultWrapper.isEmptyRender());
});
it('renders nothing if query result does not include repository', function() {
const props = queryBuilder(rootQuery)
.nullRepository()
.build();
const tokenWrapper = localRepoWrapper.find(ObserveModel).renderProp('children')('1234');
const resultWrapper = tokenWrapper.find(QueryRenderer).renderProp('render')({
error: null, props, retry: () => {}});
assert.isTrue(resultWrapper.isEmptyRender());
});
it('renders nothing if query result does not include repository ref', function() {
const props = queryBuilder(rootQuery)
.repository(r => r.nullRef())
.build();
const tokenWrapper = localRepoWrapper.find(ObserveModel).renderProp('children')('1234');
const resultWrapper = tokenWrapper.find(QueryRenderer).renderProp('render')({
error: null, props, retry: () => {},
});
assert.isTrue(resultWrapper.isEmptyRender());
});
it('renders the AggregatedReviewsContainerContainer if result includes repository and ref', function() {
const props = queryBuilder(rootQuery)
.repository(r => {
r.ref(r0 => {
r0.associatedPullRequests(conn => {
conn.totalCount(1);
conn.addNode();
});
});
})
.build();
const tokenWrapper = localRepoWrapper.find(ObserveModel).renderProp('children')('1234');
const resultWrapper = tokenWrapper.find(QueryRenderer).renderProp('render')({
error: null, props, retry: () => {},
});
assert.lengthOf(resultWrapper.find(AggregatedReviewsContainer), 1);
});
it("renders nothing if there's an error aggregating reviews", function() {
const props = queryBuilder(rootQuery)
.repository(r => {
r.ref(r0 => {
r0.associatedPullRequests(conn => {
conn.totalCount(1);
conn.addNode();
});
});
})
.build();
const tokenWrapper = localRepoWrapper.find(ObserveModel).renderProp('children')('1234');
const resultWrapper = tokenWrapper.find(QueryRenderer).renderProp('render')({
error: null, props, retry: () => {},
});
const reviewsWrapper = resultWrapper.find(AggregatedReviewsContainer).renderProp('children')({
errors: [new Error('ahhhh')],
summaries: [],
commentThreads: [],
});
assert.isTrue(reviewsWrapper.isEmptyRender());
});
it('renders nothing if there are no review comment threads', function() {
const props = queryBuilder(rootQuery)
.repository(r => {
r.ref(r0 => {
r0.associatedPullRequests(conn => {
conn.totalCount(1);
conn.addNode();
});
});
})
.build();
const tokenWrapper = localRepoWrapper.find(ObserveModel).renderProp('children')('1234');
const resultWrapper = tokenWrapper.find(QueryRenderer).renderProp('render')({
error: null, props, retry: () => {},
});
const reviewsWrapper = resultWrapper.find(AggregatedReviewsContainer).renderProp('children')({
errors: [],
summaries: [],
commentThreads: [],
});
assert.isTrue(reviewsWrapper.isEmptyRender());
});
it('loads the patch once there is at least one loaded review comment thread', function() {
const props = queryBuilder(rootQuery)
.repository(r => {
r.ref(r0 => {
r0.associatedPullRequests(conn => {
conn.totalCount(1);
conn.addNode();
});
});
})
.build();
const tokenWrapper = localRepoWrapper.find(ObserveModel).renderProp('children')('1234');
const resultWrapper = tokenWrapper.find(QueryRenderer).renderProp('render')({
error: null, props, retry: () => {},
});
const reviewsWrapper = resultWrapper.find(AggregatedReviewsContainer).renderProp('children')({
errors: [],
summaries: [],
commentThreads: [
{thread: {id: 'thread0'}, comments: [{id: 'comment0', path: 'a.txt'}, {id: 'comment1', path: 'a.txt'}]},
],
});
const patchWrapper = reviewsWrapper.find(PullRequestPatchContainer).renderProp('children')(
null, null,
);
assert.isTrue(patchWrapper.isEmptyRender());
});
it('renders nothing if patch cannot be fetched', function() {
const props = queryBuilder(rootQuery)
.repository(r => {
r.ref(r0 => {
r0.associatedPullRequests(conn => {
conn.totalCount(1);
conn.addNode();
});
});
})
.build();
const tokenWrapper = localRepoWrapper.find(ObserveModel).renderProp('children')('1234');
const resultWrapper = tokenWrapper.find(QueryRenderer).renderProp('render')({
error: null, props, retry: () => {},
});
const reviewsWrapper = resultWrapper.find(AggregatedReviewsContainer).renderProp('children')({
errors: [],
summaries: [],
commentThreads: [
{thread: {id: 'thread0'}, comments: [{id: 'comment0', path: 'a.txt'}, {id: 'comment1', path: 'a.txt'}]},
],
});
const patchWrapper = reviewsWrapper.find(PullRequestPatchContainer).renderProp('children')(
new Error('oops'), null,
);
assert.isTrue(patchWrapper.isEmptyRender());
});
it('renders a CommentPositioningContainer when the patch and reviews arrive', function() {
const props = queryBuilder(rootQuery)
.repository(r => {
r.ref(r0 => {
r0.associatedPullRequests(conn => {
conn.totalCount(1);
conn.addNode();
});
});
})
.build();
const patch = multiFilePatchBuilder().build();
const tokenWrapper = localRepoWrapper.find(ObserveModel).renderProp('children')('1234');
const resultWrapper = tokenWrapper.find(QueryRenderer).renderProp('render')({
error: null, props, retry: () => {},
});
const reviewsWrapper = resultWrapper.find(AggregatedReviewsContainer).renderProp('children')({
errors: [],
summaries: [],
commentThreads: [
{thread: {id: 'thread0'}, comments: [{id: 'comment0', path: 'a.txt'}, {id: 'comment1', path: 'a.txt'}]},
],
});
const patchWrapper = reviewsWrapper.find(PullRequestPatchContainer).renderProp('children')(null, patch);
assert.isTrue(patchWrapper.find(CommentPositioningContainer).exists());
});
it('renders nothing while the comment positions are being calculated', function() {
const props = queryBuilder(rootQuery)
.repository(r => {
r.ref(r0 => {
r0.associatedPullRequests(conn => {
conn.totalCount(1);
conn.addNode();
});
});
})
.build();
const patch = multiFilePatchBuilder().build();
const tokenWrapper = localRepoWrapper.find(ObserveModel).renderProp('children')('1234');
const resultWrapper = tokenWrapper.find(QueryRenderer).renderProp('render')({
error: null, props, retry: () => {},
});
const reviewsWrapper = resultWrapper.find(AggregatedReviewsContainer).renderProp('children')({
errors: [],
summaries: [],
commentThreads: [
{thread: {id: 'thread0'}, comments: [{id: 'comment0', path: 'a.txt'}, {id: 'comment1', path: 'a.txt'}]},
],
});
const patchWrapper = reviewsWrapper.find(PullRequestPatchContainer).renderProp('children')(null, patch);
const positionedWrapper = patchWrapper.find(CommentPositioningContainer).renderProp('children')(null);
assert.isTrue(positionedWrapper.isEmptyRender());
});
it('renders a CommentDecorationsController with all of the results once comment positions arrive', function() {
const props = queryBuilder(rootQuery)
.repository(r => {
r.ref(r0 => {
r0.associatedPullRequests(conn => {
conn.totalCount(1);
conn.addNode();
});
});
})
.build();
const patch = multiFilePatchBuilder().build();
const tokenWrapper = localRepoWrapper.find(ObserveModel).renderProp('children')('1234');
const resultWrapper = tokenWrapper.find(QueryRenderer).renderProp('render')({
error: null, props, retry: () => {},
});
const reviewsWrapper = resultWrapper.find(AggregatedReviewsContainer).renderProp('children')({
errors: [],
summaries: [],
commentThreads: [
{thread: {id: 'thread0'}, comments: [{id: 'comment0', path: 'a.txt'}, {id: 'comment1', path: 'a.txt'}]},
],
});
const patchWrapper = reviewsWrapper.find(PullRequestPatchContainer).renderProp('children')(null, patch);
const translations = new Map();
const positionedWrapper = patchWrapper.find(CommentPositioningContainer).renderProp('children')(translations);
const controller = positionedWrapper.find(CommentDecorationsController);
assert.strictEqual(controller.prop('commentTranslations'), translations);
assert.strictEqual(controller.prop('pullRequests'), props.repository.ref.associatedPullRequests.nodes);
});
});
});