forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpr-patch-container.test.js
357 lines (286 loc) · 10.7 KB
/
pr-patch-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
import React from 'react';
import {shallow} from 'enzyme';
import path from 'path';
import PullRequestPatchContainer from '../../lib/containers/pr-patch-container';
import {rawDiff, rawDiffWithPathPrefix, rawAdditionDiff, rawDeletionDiff} from '../fixtures/diffs/raw-diff';
import {getEndpoint} from '../../lib/models/endpoint';
describe('PullRequestPatchContainer', function() {
function buildApp(override = {}) {
const props = {
owner: 'atom',
repo: 'github',
number: 1995,
endpoint: getEndpoint('github.com'),
token: '1234',
refetch: false,
children: () => null,
...override,
};
return <PullRequestPatchContainer {...props} />;
}
function setDiffResponse(body, options) {
const opts = {
status: 200,
statusText: 'OK',
getResolver: cb => cb(),
etag: null,
callNum: null,
...options,
};
let stub = window.fetch;
if (!stub.restore) {
stub = sinon.stub(window, 'fetch');
}
let call = stub;
if (opts.callNum !== null) {
call = stub.onCall(opts.callNum);
}
call.callsFake(() => {
const headers = {
'Content-type': 'text/plain',
};
if (opts.etag !== null) {
headers.ETag = opts.etag;
}
const resp = new window.Response(body, {
status: opts.status,
statusText: opts.statusText,
headers,
});
let resolveResponsePromise = null;
const promise = new Promise(resolve => {
resolveResponsePromise = resolve;
});
opts.getResolver(() => resolveResponsePromise(resp));
return promise;
});
return stub;
}
function createChildrenCallback() {
const calls = [];
const waitingCallbacks = [];
const fn = function(error, mfp) {
if (waitingCallbacks.length > 0) {
waitingCallbacks.shift()({error, mfp});
return null;
}
calls.push({error, mfp});
return null;
};
fn.nextCall = function() {
if (calls.length > 0) {
return Promise.resolve(calls.shift());
}
return new Promise(resolve => waitingCallbacks.push(resolve));
};
return fn;
}
describe('while the patch is loading', function() {
it('renders its child prop with nulls', async function() {
setDiffResponse(rawDiff);
const children = createChildrenCallback();
shallow(buildApp({children}));
assert.deepEqual(await children.nextCall(), {error: null, mfp: null});
});
});
describe('when the patch has been fetched successfully', function() {
it('builds the correct request', async function() {
const stub = setDiffResponse(rawDiff);
const children = createChildrenCallback();
shallow(buildApp({
owner: 'smashwilson',
repo: 'pushbot',
number: 12,
endpoint: getEndpoint('github.com'),
token: 'swordfish',
children,
}));
assert.isTrue(stub.calledWith(
'https://api.github.com/repos/smashwilson/pushbot/pulls/12',
{
headers: {
Accept: 'application/vnd.github.v3.diff',
Authorization: 'bearer swordfish',
},
},
));
assert.deepEqual(await children.nextCall(), {error: null, mfp: null});
const {error, mfp} = await children.nextCall();
assert.isNull(error);
assert.lengthOf(mfp.getFilePatches(), 1);
const [fp] = mfp.getFilePatches();
assert.strictEqual(fp.getOldFile().getPath(), 'file.txt');
assert.strictEqual(fp.getNewFile().getPath(), 'file.txt');
assert.lengthOf(fp.getHunks(), 1);
const [h] = fp.getHunks();
assert.strictEqual(h.getSectionHeading(), 'class Thing {');
});
it('modifies the patch to exclude a/ and b/ prefixes on file paths', async function() {
setDiffResponse(rawDiffWithPathPrefix);
const children = createChildrenCallback();
shallow(buildApp({children}));
await children.nextCall();
const {error, mfp} = await children.nextCall();
assert.isNull(error);
assert.lengthOf(mfp.getFilePatches(), 1);
const [fp] = mfp.getFilePatches();
assert.notMatch(fp.getOldFile().getPath(), /^[a|b]\//);
assert.notMatch(fp.getNewFile().getPath(), /^[a|b]\//);
});
it('excludes a/ prefix on the old file of a deletion', async function() {
setDiffResponse(rawDeletionDiff);
const children = createChildrenCallback();
shallow(buildApp({children}));
await children.nextCall();
const {error, mfp} = await children.nextCall();
assert.isNull(error);
assert.lengthOf(mfp.getFilePatches(), 1);
const [fp] = mfp.getFilePatches();
assert.strictEqual(fp.getOldFile().getPath(), 'deleted');
assert.isFalse(fp.getNewFile().isPresent());
});
it('excludes b/ prefix on the new file of an addition', async function() {
setDiffResponse(rawAdditionDiff);
const children = createChildrenCallback();
shallow(buildApp({children}));
await children.nextCall();
const {error, mfp} = await children.nextCall();
assert.isNull(error);
assert.lengthOf(mfp.getFilePatches(), 1);
const [fp] = mfp.getFilePatches();
assert.isFalse(fp.getOldFile().isPresent());
assert.strictEqual(fp.getNewFile().getPath(), 'added');
});
it('converts file paths to use native path separators', async function() {
setDiffResponse(rawDiffWithPathPrefix);
const children = createChildrenCallback();
shallow(buildApp({children}));
await children.nextCall();
const {error, mfp} = await children.nextCall();
assert.isNull(error);
assert.lengthOf(mfp.getFilePatches(), 1);
const [fp] = mfp.getFilePatches();
assert.strictEqual(fp.getNewFile().getPath(), path.join('bad/path.txt'));
assert.strictEqual(fp.getOldFile().getPath(), path.join('bad/path.txt'));
});
it('does not setState if the component has been unmounted', async function() {
let resolve = null;
setDiffResponse(rawDiff, {
getResolver(cb) { resolve = cb; },
});
const children = createChildrenCallback();
const wrapper = shallow(buildApp({children}));
const fetchDiffSpy = sinon.spy(wrapper.instance(), 'fetchDiff');
wrapper.setProps({refetch: true});
const setStateSpy = sinon.spy(wrapper.instance(), 'setState');
wrapper.unmount();
resolve();
await fetchDiffSpy.lastCall.returnValue;
assert.isFalse(setStateSpy.called);
});
it('respects a custom largeDiffThreshold', async function() {
setDiffResponse(rawDiff);
const children = createChildrenCallback();
shallow(buildApp({
largeDiffThreshold: 1,
children,
}));
await children.nextCall();
const {error, mfp} = await children.nextCall();
assert.isNull(error);
assert.lengthOf(mfp.getFilePatches(), 1);
const [fp] = mfp.getFilePatches();
assert.isFalse(fp.getRenderStatus().isVisible());
});
});
describe('when there has been an error', function() {
it('reports an error when the network request fails', async function() {
const output = sinon.stub(console, 'error');
sinon.stub(window, 'fetch').rejects(new Error('kerPOW'));
const children = createChildrenCallback();
shallow(buildApp({children}));
await children.nextCall();
const {error, mfp} = await children.nextCall();
assert.strictEqual(error, 'Network error encountered fetching the patch: kerPOW.');
assert.isNull(mfp);
assert.isTrue(output.called);
});
it('reports an error if the fetch returns a non-OK response', async function() {
setDiffResponse('ouch', {
status: 404,
statusText: 'Not found',
});
const children = createChildrenCallback();
shallow(buildApp({children}));
await children.nextCall();
const {error, mfp} = await children.nextCall();
assert.strictEqual(error, 'Unable to fetch the diff for this pull request: Not found.');
assert.isNull(mfp);
});
it('reports an error if the patch cannot be parsed', async function() {
const output = sinon.stub(console, 'error');
setDiffResponse('bad diff no treat for you');
const children = createChildrenCallback();
shallow(buildApp({children}));
await children.nextCall();
const {error, mfp} = await children.nextCall();
assert.strictEqual(error, 'Unable to parse the diff for this pull request.');
assert.isNull(mfp);
assert.isTrue(output.called);
});
});
describe('when a refetch is requested', function() {
it('refetches patch data on the next render', async function() {
const fetch = setDiffResponse(rawDiff);
const children = createChildrenCallback();
const wrapper = shallow(buildApp({children}));
assert.strictEqual(fetch.callCount, 1);
assert.deepEqual(await children.nextCall(), {error: null, mfp: null});
await children.nextCall();
wrapper.setProps({refetch: true});
assert.strictEqual(fetch.callCount, 2);
});
it('does not refetch data on additional renders', async function() {
const fetch = setDiffResponse(rawDiff);
const children = createChildrenCallback();
const wrapper = shallow(buildApp({children, refetch: true}));
assert.strictEqual(fetch.callCount, 1);
await children.nextCall();
await children.nextCall();
wrapper.setProps({refetch: true});
assert.strictEqual(fetch.callCount, 1);
});
it('does not reparse data if the diff has not been modified', async function() {
const stub = setDiffResponse(rawDiff, {callNum: 0, etag: '12345'});
setDiffResponse(null, {callNum: 1, status: 304});
const children = createChildrenCallback();
const wrapper = shallow(buildApp({
owner: 'smashwilson',
repo: 'pushbot',
number: 12,
endpoint: getEndpoint('github.com'),
token: 'swordfish',
children,
}));
assert.deepEqual(await children.nextCall(), {error: null, mfp: null});
const {error: error0, mfp: mfp0} = await children.nextCall();
assert.isNull(error0);
wrapper.setProps({refetch: true});
assert.deepEqual(await children.nextCall(), {error: null, mfp: mfp0});
assert.deepEqual(await children.nextCall(), {error: null, mfp: null});
const {error: error1, mfp: mfp1} = await children.nextCall();
assert.isNull(error1);
assert.strictEqual(mfp1, mfp0);
assert.isTrue(stub.calledWith(
'https://api.github.com/repos/smashwilson/pushbot/pulls/12',
{
headers: {
'Accept': 'application/vnd.github.v3.diff',
'Authorization': 'bearer swordfish',
'If-None-Match': '12345',
},
},
));
});
});
});