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 408
Expand file tree
/
Copy pathmulti-file-patch-view.test.js
More file actions
1942 lines (1615 loc) · 76 KB
/
multi-file-patch-view.test.js
File metadata and controls
1942 lines (1615 loc) · 76 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
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import React from 'react';
import {shallow, mount} from 'enzyme';
import * as reporterProxy from '../../lib/reporter-proxy';
import {cloneRepository, buildRepository} from '../helpers';
import {EXPANDED, COLLAPSED, DEFERRED, REMOVED} from '../../lib/models/patch/patch';
import MultiFilePatchView from '../../lib/views/multi-file-patch-view';
import {multiFilePatchBuilder} from '../builder/patch';
import {aggregatedReviewsBuilder} from '../builder/graphql/aggregated-reviews-builder';
import {nullFile} from '../../lib/models/patch/file';
import FilePatch from '../../lib/models/patch/file-patch';
import RefHolder from '../../lib/models/ref-holder';
import CommentGutterDecorationController from '../../lib/controllers/comment-gutter-decoration-controller';
import CommitPreviewItem from '../../lib/items/commit-preview-item';
import ChangedFileItem from '../../lib/items/changed-file-item';
import IssueishDetailItem from '../../lib/items/issueish-detail-item';
describe('MultiFilePatchView', function() {
let atomEnv, workspace, repository, filePatches;
beforeEach(async function() {
atomEnv = global.buildAtomEnvironment();
workspace = atomEnv.workspace;
const workdirPath = await cloneRepository();
repository = await buildRepository(workdirPath);
const {multiFilePatch} = multiFilePatchBuilder()
.addFilePatch(fp => {
fp.setOldFile(f => f.path('path.txt'));
fp.addHunk(h => {
h.oldRow(4);
h.unchanged('0000').added('0001', '0002').deleted('0003').unchanged('0004');
});
fp.addHunk(h => {
h.oldRow(8);
h.unchanged('0005').added('0006').deleted('0007').unchanged('0008');
});
}).build();
filePatches = multiFilePatch;
});
afterEach(function() {
atomEnv.destroy();
});
function buildApp(overrideProps = {}) {
const props = {
relPath: 'path.txt',
stagingStatus: 'unstaged',
isPartiallyStaged: false,
multiFilePatch: filePatches,
hasUndoHistory: false,
selectionMode: 'line',
selectedRows: new Set(),
hasMultipleFileSelections: false,
repository,
isActive: true,
workspace,
config: atomEnv.config,
commands: atomEnv.commands,
keymaps: atomEnv.keymaps,
tooltips: atomEnv.tooltips,
selectedRowsChanged: () => {},
switchToIssueish: () => {},
diveIntoMirrorPatch: () => {},
surface: () => {},
openFile: () => {},
toggleFile: () => {},
toggleRows: () => {},
toggleModeChange: () => {},
toggleSymlinkChange: () => {},
undoLastDiscard: () => {},
discardRows: () => {},
itemType: CommitPreviewItem,
...overrideProps,
};
return <MultiFilePatchView {...props} />;
}
it('renders the file header', function() {
const wrapper = shallow(buildApp());
assert.isTrue(wrapper.find('FilePatchHeaderView').exists());
});
it('passes the new path of renamed files', function() {
const {multiFilePatch} = multiFilePatchBuilder()
.addFilePatch(fp => {
fp.status('renamed');
fp.setOldFile(f => f.path('old.txt'));
fp.setNewFile(f => f.path('new.txt'));
})
.build();
const wrapper = shallow(buildApp({multiFilePatch}));
assert.strictEqual(wrapper.find('FilePatchHeaderView').prop('newPath'), 'new.txt');
});
it('populates an externally provided refEditor', async function() {
const refEditor = new RefHolder();
mount(buildApp({refEditor}));
assert.isDefined(await refEditor.getPromise());
});
describe('file header decoration positioning', function() {
let wrapper;
function decorationForFileHeader(fileName) {
return wrapper.find('Decoration').filterWhere(dw => dw.exists(`FilePatchHeaderView[relPath="${fileName}"]`));
}
it('renders visible file headers with position: before', function() {
const {multiFilePatch} = multiFilePatchBuilder()
.addFilePatch(fp => {
fp.setOldFile(f => f.path('0.txt'));
fp.addHunk(h => h.unchanged('a-0').deleted('a-1').unchanged('a-2'));
})
.addFilePatch(fp => {
fp.setOldFile(f => f.path('1.txt'));
fp.addHunk(h => h.unchanged('b-0').added('b-1').unchanged('b-2'));
})
.addFilePatch(fp => {
fp.setOldFile(f => f.path('2.txt'));
fp.addHunk(h => h.unchanged('c-0').deleted('c-1').unchanged('c-2'));
})
.build();
wrapper = shallow(buildApp({multiFilePatch}));
assert.strictEqual(decorationForFileHeader('0.txt').prop('position'), 'before');
assert.strictEqual(decorationForFileHeader('1.txt').prop('position'), 'before');
assert.strictEqual(decorationForFileHeader('2.txt').prop('position'), 'before');
});
it('renders a final collapsed file header with position: after', function() {
const {multiFilePatch} = multiFilePatchBuilder()
.addFilePatch(fp => {
fp.renderStatus(COLLAPSED);
fp.setOldFile(f => f.path('0.txt'));
fp.addHunk(h => h.unchanged('a-0').added('a-1').unchanged('a-2'));
})
.addFilePatch(fp => {
fp.setOldFile(f => f.path('1.txt'));
fp.addHunk(h => h.unchanged('b-0').added('b-1').unchanged('b-2'));
})
.addFilePatch(fp => {
fp.renderStatus(COLLAPSED);
fp.setOldFile(f => f.path('2.txt'));
fp.addHunk(h => h.unchanged('c-0').added('c-1').unchanged('c-2'));
})
.addFilePatch(fp => {
fp.setOldFile(f => f.path('3.txt'));
fp.addHunk(h => h.unchanged('d-0').added('d-1').unchanged('d-2'));
})
.addFilePatch(fp => {
fp.renderStatus(COLLAPSED);
fp.setOldFile(f => f.path('4.txt'));
fp.addHunk(h => h.unchanged('e-0').added('e-1').unchanged('e-2'));
})
.build();
wrapper = shallow(buildApp({multiFilePatch}));
assert.strictEqual(decorationForFileHeader('0.txt').prop('position'), 'before');
assert.strictEqual(decorationForFileHeader('1.txt').prop('position'), 'before');
assert.strictEqual(decorationForFileHeader('2.txt').prop('position'), 'before');
assert.strictEqual(decorationForFileHeader('3.txt').prop('position'), 'before');
assert.strictEqual(decorationForFileHeader('4.txt').prop('position'), 'after');
});
it('renders a final mode change-only file header with position: after', function() {
const {multiFilePatch} = multiFilePatchBuilder()
.addFilePatch(fp => {
fp.setOldFile(f => f.path('0.txt'));
fp.setNewFile(f => f.path('0.txt').executable());
fp.empty();
})
.addFilePatch(fp => {
fp.setOldFile(f => f.path('1.txt'));
fp.addHunk(h => h.unchanged('b-0').added('b-1').unchanged('b-2'));
})
.addFilePatch(fp => {
fp.setOldFile(f => f.path('2.txt').executable());
fp.setNewFile(f => f.path('2.txt'));
fp.empty();
})
.build();
wrapper = shallow(buildApp({multiFilePatch}));
assert.strictEqual(decorationForFileHeader('0.txt').prop('position'), 'before');
assert.strictEqual(decorationForFileHeader('1.txt').prop('position'), 'before');
assert.strictEqual(decorationForFileHeader('2.txt').prop('position'), 'after');
});
});
it('undoes the last discard from the file header button', function() {
const undoLastDiscard = sinon.spy();
const wrapper = shallow(buildApp({undoLastDiscard}));
wrapper.find('FilePatchHeaderView').first().prop('undoLastDiscard')();
assert.lengthOf(filePatches.getFilePatches(), 1);
const [filePatch] = filePatches.getFilePatches();
assert.isTrue(undoLastDiscard.calledWith(filePatch, {eventSource: 'button'}));
});
it('dives into the mirror patch from the file header button', function() {
const diveIntoMirrorPatch = sinon.spy();
const wrapper = shallow(buildApp({diveIntoMirrorPatch}));
wrapper.find('FilePatchHeaderView').prop('diveIntoMirrorPatch')();
assert.lengthOf(filePatches.getFilePatches(), 1);
const [filePatch] = filePatches.getFilePatches();
assert.isTrue(diveIntoMirrorPatch.calledWith(filePatch));
});
it('toggles a file from staged to unstaged from the file header button', function() {
const toggleFile = sinon.spy();
const wrapper = shallow(buildApp({toggleFile}));
wrapper.find('FilePatchHeaderView').prop('toggleFile')();
assert.lengthOf(filePatches.getFilePatches(), 1);
const [filePatch] = filePatches.getFilePatches();
assert.isTrue(toggleFile.calledWith(filePatch));
});
it('passes hasMultipleFileSelections to all file headers', function() {
const {multiFilePatch} = multiFilePatchBuilder()
.addFilePatch(fp => fp.setOldFile(f => f.path('0')))
.addFilePatch(fp => fp.setOldFile(f => f.path('1')))
.build();
const wrapper = shallow(buildApp({multiFilePatch, hasMultipleFileSelections: true}));
assert.isTrue(wrapper.find('FilePatchHeaderView[relPath="0"]').prop('hasMultipleFileSelections'));
assert.isTrue(wrapper.find('FilePatchHeaderView[relPath="1"]').prop('hasMultipleFileSelections'));
wrapper.setProps({hasMultipleFileSelections: false});
assert.isFalse(wrapper.find('FilePatchHeaderView[relPath="0"]').prop('hasMultipleFileSelections'));
assert.isFalse(wrapper.find('FilePatchHeaderView[relPath="1"]').prop('hasMultipleFileSelections'));
});
it('triggers a FilePatch collapse from file headers', function() {
const {multiFilePatch} = multiFilePatchBuilder()
.addFilePatch(fp => fp.setOldFile(f => f.path('0')))
.addFilePatch(fp => fp.setOldFile(f => f.path('1')))
.build();
const fp1 = multiFilePatch.getFilePatches()[1];
const wrapper = shallow(buildApp({multiFilePatch}));
sinon.stub(multiFilePatch, 'collapseFilePatch');
wrapper.find('FilePatchHeaderView[relPath="1"]').prop('triggerCollapse')();
assert.isTrue(multiFilePatch.collapseFilePatch.calledWith(fp1));
});
it('triggers a FilePatch expansion from file headers', function() {
const {multiFilePatch} = multiFilePatchBuilder()
.addFilePatch(fp => fp.setOldFile(f => f.path('0')))
.addFilePatch(fp => fp.setOldFile(f => f.path('1')))
.build();
const fp0 = multiFilePatch.getFilePatches()[0];
const wrapper = shallow(buildApp({multiFilePatch}));
sinon.stub(multiFilePatch, 'expandFilePatch');
wrapper.find('FilePatchHeaderView[relPath="0"]').prop('triggerExpand')();
assert.isTrue(multiFilePatch.expandFilePatch.calledWith(fp0));
});
describe('review comments', function() {
it('renders a gutter decoration for each review thread if itemType is IssueishDetailItem', function() {
const {multiFilePatch} = multiFilePatchBuilder()
.addFilePatch(fp => {
fp.setOldFile(f => f.path('file0.txt'));
fp.addHunk(h => h.unchanged('0', '1', '2').added('3').unchanged('4', '5'));
})
.addFilePatch(fp => {
fp.setOldFile(f => f.path('file1.txt'));
fp.addHunk(h => h.unchanged('6').deleted('7', '8').unchanged('9'));
})
.build();
const payload = aggregatedReviewsBuilder()
.addReviewThread(b => {
b.thread(t => t.id('thread0').isResolved(true));
b.addComment(c => c.path('file0.txt').position(1));
})
.addReviewThread(b => {
b.thread(t => t.id('thread1').isResolved(false));
b.addComment(c => c.path('file1.txt').position(2));
b.addComment(c => c.path('ignored').position(999));
b.addComment(c => c.path('file1.txt').position(0));
})
.addReviewThread(b => {
b.thread(t => t.id('thread2').isResolved(false));
b.addComment(c => c.path('file0.txt').position(4));
b.addComment(c => c.path('file0.txt').position(4));
})
.build();
const wrapper = shallow(buildApp({
multiFilePatch,
reviewCommentsLoading: false,
reviewCommentsTotalCount: 3,
reviewCommentsResolvedCount: 1,
reviewCommentThreads: payload.commentThreads,
selectedRows: new Set([7]),
itemType: IssueishDetailItem,
}));
const controllers = wrapper.find(CommentGutterDecorationController);
assert.lengthOf(controllers, 3);
const controller0 = controllers.at(0);
assert.strictEqual(controller0.prop('commentRow'), 0);
assert.strictEqual(controller0.prop('threadId'), 'thread0');
assert.lengthOf(controller0.prop('extraClasses'), 0);
const controller1 = controllers.at(1);
assert.strictEqual(controller1.prop('commentRow'), 7);
assert.strictEqual(controller1.prop('threadId'), 'thread1');
assert.deepEqual(controller1.prop('extraClasses'), ['github-FilePatchView-line--selected']);
const controller2 = controllers.at(2);
assert.strictEqual(controller2.prop('commentRow'), 3);
assert.strictEqual(controller2.prop('threadId'), 'thread2');
assert.lengthOf(controller2.prop('extraClasses'), 0);
});
it('does not render threads until they finish loading', function() {
const payload = aggregatedReviewsBuilder()
.addReviewThread(b => {
b.thread(t => t.isResolved(true));
b.addComment();
})
.addReviewThread(b => {
b.thread(t => t.isResolved(false));
b.addComment();
b.addComment();
b.addComment();
})
.build();
const wrapper = shallow(buildApp({
reviewCommentsLoading: true,
reviewCommentsTotalCount: 3,
reviewCommentsResolvedCount: 1,
reviewCommentThreads: payload.commentThreads,
itemType: IssueishDetailItem,
}));
assert.isFalse(wrapper.find(CommentGutterDecorationController).exists());
});
it('omit threads that have an invalid path or position', function() {
sinon.stub(console, 'error');
const {multiFilePatch} = multiFilePatchBuilder()
.addFilePatch(fp => {
fp.setOldFile(f => f.path('file.txt'));
fp.addHunk(h => h.unchanged('0').added('1').unchanged('2'));
})
.build();
const payload = aggregatedReviewsBuilder()
.addReviewThread(b => {
b.addComment(c => c.path('bad-path.txt').position(1));
})
.addReviewThread(b => {
b.addComment(c => c.path('file.txt').position(100));
})
.build();
const wrapper = shallow(buildApp({
multiFilePatch,
reviewCommentsLoading: false,
reviewCommentsTotalCount: 2,
reviewCommentsResolvedCount: 0,
reviewCommentThreads: payload.commentThreads,
itemType: IssueishDetailItem,
}));
assert.isFalse(wrapper.find(CommentGutterDecorationController).exists());
// eslint-disable-next-line no-console
assert.strictEqual(console.error.callCount, 1);
});
});
it('renders the file patch within an editor', function() {
const wrapper = mount(buildApp());
const editor = wrapper.find('AtomTextEditor');
assert.strictEqual(editor.instance().getModel().getText(), filePatches.getBuffer().getText());
});
it('sets the root class when in hunk selection mode', function() {
const wrapper = shallow(buildApp({selectionMode: 'line'}));
assert.isFalse(wrapper.find('.github-FilePatchView--hunkMode').exists());
wrapper.setProps({selectionMode: 'hunk'});
assert.isTrue(wrapper.find('.github-FilePatchView--hunkMode').exists());
});
describe('initial selection', function() {
it('selects the origin with an empty FilePatch', function() {
const {multiFilePatch} = multiFilePatchBuilder()
.addFilePatch(fp => fp.empty())
.build();
const wrapper = mount(buildApp({multiFilePatch}));
const editor = wrapper.find('AtomTextEditor').instance().getModel();
assert.deepEqual(editor.getSelectedBufferRanges().map(r => r.serialize()), [[[0, 0], [0, 0]]]);
});
it('selects the first hunk with a populated file patch', function() {
const {multiFilePatch} = multiFilePatchBuilder()
.addFilePatch(fp => {
fp.setOldFile(f => f.path('file-0'));
fp.addHunk(h => h.unchanged('0').added('1', '2').deleted('3').unchanged('4'));
fp.addHunk(h => h.added('5', '6'));
})
.addFilePatch(fp => {
fp.setOldFile(f => f.path('file-1'));
fp.addHunk(h => h.deleted('7', '8', '9'));
})
.build();
const wrapper = mount(buildApp({multiFilePatch}));
const editor = wrapper.find('AtomTextEditor').instance().getModel();
assert.deepEqual(editor.getSelectedBufferRanges().map(r => r.serialize()), [[[0, 0], [4, 1]]]);
});
});
it('preserves the selection index when a new file patch arrives in line selection mode', function() {
const selectedRowsChanged = sinon.spy();
let willUpdate, didUpdate;
const onWillUpdatePatch = cb => {
willUpdate = cb;
return {dispose: () => {}};
};
const onDidUpdatePatch = cb => {
didUpdate = cb;
return {dispose: () => {}};
};
const wrapper = mount(buildApp({
selectedRows: new Set([2]),
selectionMode: 'line',
selectedRowsChanged,
onWillUpdatePatch,
onDidUpdatePatch,
}));
const {multiFilePatch} = multiFilePatchBuilder()
.addFilePatch(fp => {
fp.setOldFile(f => f.path('path.txt'));
fp.addHunk(h => {
h.oldRow(5);
h.unchanged('0000').added('0001').unchanged('0002').deleted('0003').unchanged('0004');
});
}).build();
willUpdate();
wrapper.setProps({multiFilePatch});
didUpdate(multiFilePatch);
assert.sameMembers(Array.from(selectedRowsChanged.lastCall.args[0]), [3]);
assert.strictEqual(selectedRowsChanged.lastCall.args[1], 'line');
const editor = wrapper.find('AtomTextEditor').instance().getModel();
assert.deepEqual(editor.getSelectedBufferRanges().map(r => r.serialize()), [
[[3, 0], [3, 4]],
]);
selectedRowsChanged.resetHistory();
wrapper.setProps({isPartiallyStaged: true});
assert.isFalse(selectedRowsChanged.called);
});
it('selects the next full hunk when a new file patch arrives in hunk selection mode', function() {
const {multiFilePatch} = multiFilePatchBuilder()
.addFilePatch(fp => {
fp.setOldFile(f => f.path('path.txt'));
fp.addHunk(h => {
h.oldRow(10);
h.unchanged('0000').added('0001').unchanged('0002').deleted('0003').unchanged('0004');
});
fp.addHunk(h => {
h.oldRow(20);
h.unchanged('0005').added('0006').added('0007').deleted('0008').unchanged('0009');
});
fp.addHunk(h => {
h.oldRow(30);
h.unchanged('0010').added('0011').deleted('0012').unchanged('0013');
});
fp.addHunk(h => {
h.oldRow(40);
h.unchanged('0014').deleted('0015').unchanged('0016').added('0017').unchanged('0018');
});
}).build();
let willUpdate, didUpdate;
const onWillUpdatePatch = cb => {
willUpdate = cb;
return {dispose: () => {}};
};
const onDidUpdatePatch = cb => {
didUpdate = cb;
return {dispose: () => {}};
};
const selectedRowsChanged = sinon.spy();
const wrapper = mount(buildApp({
multiFilePatch,
selectedRows: new Set([6, 7, 8]),
selectionMode: 'hunk',
selectedRowsChanged,
onWillUpdatePatch,
onDidUpdatePatch,
}));
const {multiFilePatch: nextMfp} = multiFilePatchBuilder()
.addFilePatch(fp => {
fp.setOldFile(f => f.path('path.txt'));
fp.addHunk(h => {
h.oldRow(10);
h.unchanged('0000').added('0001').unchanged('0002').deleted('0003').unchanged('0004');
});
fp.addHunk(h => {
h.oldRow(30);
h.unchanged('0010').added('0011').deleted('0012').unchanged('0013');
});
fp.addHunk(h => {
h.oldRow(40);
h.unchanged('0014').deleted('0015').unchanged('0016').added('0017').unchanged('0018');
});
}).build();
willUpdate();
wrapper.setProps({multiFilePatch: nextMfp});
didUpdate(nextMfp);
assert.sameMembers(Array.from(selectedRowsChanged.lastCall.args[0]), [6, 7]);
assert.strictEqual(selectedRowsChanged.lastCall.args[1], 'hunk');
const editor = wrapper.find('AtomTextEditor').instance().getModel();
assert.deepEqual(editor.getSelectedBufferRanges().map(r => r.serialize()), [
[[5, 0], [8, 4]],
]);
});
describe('when the last line in a non-last file patch is staged', function() {
it('updates the selected row to be the first changed line in the next file patch', function() {
const selectedRowsChanged = sinon.spy();
let willUpdate, didUpdate;
const onWillUpdatePatch = cb => {
willUpdate = cb;
return {dispose: () => {}};
};
const onDidUpdatePatch = cb => {
didUpdate = cb;
return {dispose: () => {}};
};
const {multiFilePatch} = multiFilePatchBuilder()
.addFilePatch(fp => {
fp.setOldFile(f => f.path('path.txt'));
fp.addHunk(h => {
h.oldRow(5);
h.unchanged('0000').added('0001').unchanged('0002').deleted('0003').unchanged('0004');
});
})
.addFilePatch(fp => {
fp.setOldFile(f => f.path('another-path.txt'));
fp.addHunk(h => {
h.oldRow(5);
h.unchanged('0000').added('0001').unchanged('0002').deleted('0003').unchanged('0004');
});
}).build();
const wrapper = mount(buildApp({
selectedRows: new Set([3]),
selectionMode: 'line',
selectedRowsChanged,
onWillUpdatePatch,
onDidUpdatePatch,
multiFilePatch,
}));
assert.deepEqual([...wrapper.prop('selectedRows')], [3]);
const {multiFilePatch: multiFilePatch2} = multiFilePatchBuilder()
.addFilePatch(fp => {
fp.setOldFile(f => f.path('path.txt'));
fp.addHunk(h => {
h.oldRow(5);
h.unchanged('0000').added('0001').unchanged('0002').unchanged('0003').unchanged('0004');
});
})
.addFilePatch(fp => {
fp.setOldFile(f => f.path('another-path.txt'));
fp.addHunk(h => {
h.oldRow(5);
h.unchanged('0000').added('0001').unchanged('0002').deleted('0003').unchanged('0004');
});
}).build();
selectedRowsChanged.resetHistory();
willUpdate();
wrapper.setProps({multiFilePatch: multiFilePatch2});
didUpdate(multiFilePatch2);
assert.strictEqual(selectedRowsChanged.callCount, 1);
assert.sameMembers(Array.from(selectedRowsChanged.lastCall.args[0]), [6]);
assert.strictEqual(selectedRowsChanged.lastCall.args[1], 'line');
const editor = wrapper.find('AtomTextEditor').instance().getModel();
assert.deepEqual(editor.getSelectedBufferRanges().map(r => r.serialize()), [
[[6, 0], [6, 4]],
]);
});
});
it('unregisters the mouseup handler on unmount', function() {
sinon.spy(window, 'addEventListener');
sinon.spy(window, 'removeEventListener');
const wrapper = shallow(buildApp());
assert.strictEqual(window.addEventListener.callCount, 1);
const addCall = window.addEventListener.getCall(0);
assert.strictEqual(addCall.args[0], 'mouseup');
const handler = window.addEventListener.getCall(0).args[1];
wrapper.unmount();
assert.isTrue(window.removeEventListener.calledWith('mouseup', handler));
});
describe('refInitialFocus', function() {
it('is set to its editor', function() {
const refInitialFocus = new RefHolder();
const wrapper = mount(buildApp({refInitialFocus}));
assert.isFalse(refInitialFocus.isEmpty());
assert.strictEqual(
refInitialFocus.get(),
wrapper.find('AtomTextEditor').getDOMNode().querySelector('atom-text-editor'),
);
});
it('may be swapped out for a new RefHolder', function() {
const refInitialFocus0 = new RefHolder();
const wrapper = mount(buildApp({refInitialFocus: refInitialFocus0}));
const editorElement = wrapper.find('AtomTextEditor').getDOMNode().querySelector('atom-text-editor');
assert.strictEqual(refInitialFocus0.getOr(null), editorElement);
const refInitialFocus1 = new RefHolder();
wrapper.setProps({refInitialFocus: refInitialFocus1});
assert.isTrue(refInitialFocus0.isEmpty());
assert.strictEqual(refInitialFocus1.getOr(null), editorElement);
wrapper.setProps({refInitialFocus: null});
assert.isTrue(refInitialFocus0.isEmpty());
assert.isTrue(refInitialFocus1.isEmpty());
wrapper.setProps({refInitialFocus: refInitialFocus0});
assert.strictEqual(refInitialFocus0.getOr(null), editorElement);
assert.isTrue(refInitialFocus1.isEmpty());
});
});
describe('executable mode changes', function() {
it('does not render if the mode has not changed', function() {
const [fp] = filePatches.getFilePatches();
const mfp = filePatches.clone({
filePatches: [fp.clone({
oldFile: fp.getOldFile().clone({mode: '100644'}),
newFile: fp.getNewFile().clone({mode: '100644'}),
})],
});
const wrapper = shallow(buildApp({multiFilePatch: mfp}));
assert.isFalse(wrapper.find('FilePatchMetaView[title="Mode change"]').exists());
});
it('renders change details within a meta container', function() {
const [fp] = filePatches.getFilePatches();
const mfp = filePatches.clone({
filePatches: [fp.clone({
oldFile: fp.getOldFile().clone({mode: '100644'}),
newFile: fp.getNewFile().clone({mode: '100755'}),
})],
});
const wrapper = mount(buildApp({multiFilePatch: mfp, stagingStatus: 'unstaged'}));
const meta = wrapper.find('FilePatchMetaView[title="Mode change"]');
assert.strictEqual(meta.prop('actionIcon'), 'icon-move-down');
assert.strictEqual(meta.prop('actionText'), 'Stage Mode Change');
const details = meta.find('.github-FilePatchView-metaDetails');
assert.strictEqual(details.text(), 'File changed modefrom non executable 100644to executable 100755');
});
it("stages or unstages the mode change when the meta container's action is triggered", function() {
const [fp] = filePatches.getFilePatches();
const mfp = filePatches.clone({
filePatches: [fp.clone({
oldFile: fp.getOldFile().clone({mode: '100644'}),
newFile: fp.getNewFile().clone({mode: '100755'}),
})],
});
const toggleModeChange = sinon.stub();
const wrapper = mount(buildApp({multiFilePatch: mfp, stagingStatus: 'staged', toggleModeChange}));
const meta = wrapper.find('FilePatchMetaView[title="Mode change"]');
assert.isTrue(meta.exists());
assert.strictEqual(meta.prop('actionIcon'), 'icon-move-up');
assert.strictEqual(meta.prop('actionText'), 'Unstage Mode Change');
meta.prop('action')();
assert.isTrue(toggleModeChange.called);
});
});
describe('symlink changes', function() {
it('does not render if the symlink status is unchanged', function() {
const [fp] = filePatches.getFilePatches();
const mfp = filePatches.clone({
filePatches: [fp.clone({
oldFile: fp.getOldFile().clone({mode: '100644'}),
newFile: fp.getNewFile().clone({mode: '100755'}),
})],
});
const wrapper = mount(buildApp({multiFilePatch: mfp}));
assert.lengthOf(wrapper.find('FilePatchMetaView').filterWhere(v => v.prop('title').startsWith('Symlink')), 0);
});
it('renders symlink change information within a meta container', function() {
const [fp] = filePatches.getFilePatches();
const mfp = filePatches.clone({
filePatches: [fp.clone({
oldFile: fp.getOldFile().clone({mode: '120000', symlink: '/old.txt'}),
newFile: fp.getNewFile().clone({mode: '120000', symlink: '/new.txt'}),
})],
});
const wrapper = mount(buildApp({multiFilePatch: mfp, stagingStatus: 'unstaged'}));
const meta = wrapper.find('FilePatchMetaView[title="Symlink changed"]');
assert.isTrue(meta.exists());
assert.strictEqual(meta.prop('actionIcon'), 'icon-move-down');
assert.strictEqual(meta.prop('actionText'), 'Stage Symlink Change');
assert.strictEqual(
meta.find('.github-FilePatchView-metaDetails').text(),
'Symlink changedfrom /old.txtto /new.txt.',
);
});
it('stages or unstages the symlink change', function() {
const toggleSymlinkChange = sinon.stub();
const [fp] = filePatches.getFilePatches();
const mfp = filePatches.clone({
filePatches: [fp.clone({
oldFile: fp.getOldFile().clone({mode: '120000', symlink: '/old.txt'}),
newFile: fp.getNewFile().clone({mode: '120000', symlink: '/new.txt'}),
})],
});
const wrapper = mount(buildApp({multiFilePatch: mfp, stagingStatus: 'staged', toggleSymlinkChange}));
const meta = wrapper.find('FilePatchMetaView[title="Symlink changed"]');
assert.isTrue(meta.exists());
assert.strictEqual(meta.prop('actionIcon'), 'icon-move-up');
assert.strictEqual(meta.prop('actionText'), 'Unstage Symlink Change');
meta.find('button.icon-move-up').simulate('click');
assert.isTrue(toggleSymlinkChange.called);
});
it('renders details for a symlink deletion', function() {
const [fp] = filePatches.getFilePatches();
const mfp = filePatches.clone({
filePatches: [fp.clone({
oldFile: fp.getOldFile().clone({mode: '120000', symlink: '/old.txt'}),
newFile: nullFile,
})],
});
const wrapper = mount(buildApp({multiFilePatch: mfp}));
const meta = wrapper.find('FilePatchMetaView[title="Symlink deleted"]');
assert.isTrue(meta.exists());
assert.strictEqual(
meta.find('.github-FilePatchView-metaDetails').text(),
'Symlinkto /old.txtdeleted.',
);
});
it('renders details for a symlink creation', function() {
const [fp] = filePatches.getFilePatches();
const mfp = filePatches.clone({
filePatches: [fp.clone({
oldFile: nullFile,
newFile: fp.getOldFile().clone({mode: '120000', symlink: '/new.txt'}),
})],
});
const wrapper = mount(buildApp({multiFilePatch: mfp}));
const meta = wrapper.find('FilePatchMetaView[title="Symlink created"]');
assert.isTrue(meta.exists());
assert.strictEqual(
meta.find('.github-FilePatchView-metaDetails').text(),
'Symlinkto /new.txtcreated.',
);
});
});
describe('hunk headers', function() {
it('renders one for each hunk', function() {
const {multiFilePatch: mfp} = multiFilePatchBuilder()
.addFilePatch(fp => {
fp.setOldFile(f => f.path('path.txt'));
fp.addHunk(h => {
h.oldRow(1);
h.unchanged('0000').added('0001').unchanged('0002');
});
fp.addHunk(h => {
h.oldRow(10);
h.unchanged('0003').deleted('0004').unchanged('0005');
});
}).build();
const hunks = mfp.getFilePatches()[0].getHunks();
const wrapper = mount(buildApp({multiFilePatch: mfp}));
assert.isTrue(wrapper.find('HunkHeaderView').someWhere(h => h.prop('hunk') === hunks[0]));
assert.isTrue(wrapper.find('HunkHeaderView').someWhere(h => h.prop('hunk') === hunks[1]));
});
it('pluralizes the toggle and discard button labels', function() {
const wrapper = shallow(buildApp({selectedRows: new Set([2]), selectionMode: 'line'}));
assert.strictEqual(wrapper.find('HunkHeaderView').at(0).prop('toggleSelectionLabel'), 'Stage Selected Line');
assert.strictEqual(wrapper.find('HunkHeaderView').at(0).prop('discardSelectionLabel'), 'Discard Selected Line');
assert.strictEqual(wrapper.find('HunkHeaderView').at(1).prop('toggleSelectionLabel'), 'Stage Hunk');
assert.strictEqual(wrapper.find('HunkHeaderView').at(1).prop('discardSelectionLabel'), 'Discard Hunk');
wrapper.setProps({selectedRows: new Set([1, 2, 3]), selectionMode: 'line'});
assert.strictEqual(wrapper.find('HunkHeaderView').at(0).prop('toggleSelectionLabel'), 'Stage Selected Lines');
assert.strictEqual(wrapper.find('HunkHeaderView').at(0).prop('discardSelectionLabel'), 'Discard Selected Lines');
assert.strictEqual(wrapper.find('HunkHeaderView').at(1).prop('toggleSelectionLabel'), 'Stage Hunk');
assert.strictEqual(wrapper.find('HunkHeaderView').at(1).prop('discardSelectionLabel'), 'Discard Hunk');
wrapper.setProps({selectedRows: new Set([1, 2, 3]), selectionMode: 'hunk'});
assert.strictEqual(wrapper.find('HunkHeaderView').at(0).prop('toggleSelectionLabel'), 'Stage Hunk');
assert.strictEqual(wrapper.find('HunkHeaderView').at(0).prop('discardSelectionLabel'), 'Discard Hunk');
assert.strictEqual(wrapper.find('HunkHeaderView').at(1).prop('toggleSelectionLabel'), 'Stage Hunk');
assert.strictEqual(wrapper.find('HunkHeaderView').at(1).prop('discardSelectionLabel'), 'Discard Hunk');
wrapper.setProps({selectedRows: new Set([1, 2, 3, 6, 7]), selectionMode: 'hunk'});
assert.strictEqual(wrapper.find('HunkHeaderView').at(0).prop('toggleSelectionLabel'), 'Stage Hunks');
assert.strictEqual(wrapper.find('HunkHeaderView').at(0).prop('discardSelectionLabel'), 'Discard Hunks');
assert.strictEqual(wrapper.find('HunkHeaderView').at(1).prop('toggleSelectionLabel'), 'Stage Hunks');
assert.strictEqual(wrapper.find('HunkHeaderView').at(1).prop('discardSelectionLabel'), 'Discard Hunks');
});
it('uses the appropriate staging action verb in hunk header button labels', function() {
const wrapper = shallow(buildApp({
selectedRows: new Set([2]),
stagingStatus: 'unstaged',
selectionMode: 'line',
}));
assert.strictEqual(wrapper.find('HunkHeaderView').at(0).prop('toggleSelectionLabel'), 'Stage Selected Line');
assert.strictEqual(wrapper.find('HunkHeaderView').at(1).prop('toggleSelectionLabel'), 'Stage Hunk');
wrapper.setProps({stagingStatus: 'staged'});
assert.strictEqual(wrapper.find('HunkHeaderView').at(0).prop('toggleSelectionLabel'), 'Unstage Selected Line');
assert.strictEqual(wrapper.find('HunkHeaderView').at(1).prop('toggleSelectionLabel'), 'Unstage Hunk');
});
it('uses the appropriate staging action noun in hunk header button labels', function() {
const wrapper = shallow(buildApp({
selectedRows: new Set([1, 2, 3]),
stagingStatus: 'unstaged',
selectionMode: 'line',
}));
assert.strictEqual(wrapper.find('HunkHeaderView').at(0).prop('toggleSelectionLabel'), 'Stage Selected Lines');
assert.strictEqual(wrapper.find('HunkHeaderView').at(1).prop('toggleSelectionLabel'), 'Stage Hunk');
wrapper.setProps({selectionMode: 'hunk'});
assert.strictEqual(wrapper.find('HunkHeaderView').at(0).prop('toggleSelectionLabel'), 'Stage Hunk');
assert.strictEqual(wrapper.find('HunkHeaderView').at(1).prop('toggleSelectionLabel'), 'Stage Hunk');
});
it('handles mousedown as a selection event', function() {
const {multiFilePatch: mfp} = multiFilePatchBuilder()
.addFilePatch(fp => {
fp.setOldFile(f => f.path('path.txt'));
fp.addHunk(h => {
h.oldRow(1);
h.unchanged('0000').added('0001').unchanged('0002');
});
fp.addHunk(h => {
h.oldRow(10);
h.unchanged('0003').deleted('0004').unchanged('0005');
});
}).build();
const selectedRowsChanged = sinon.spy();
const wrapper = mount(buildApp({multiFilePatch: mfp, selectedRowsChanged, selectionMode: 'line'}));
wrapper.find('HunkHeaderView').at(1).prop('mouseDown')({button: 0}, mfp.getFilePatches()[0].getHunks()[1]);
assert.sameMembers(Array.from(selectedRowsChanged.lastCall.args[0]), [4]);
assert.strictEqual(selectedRowsChanged.lastCall.args[1], 'hunk');
});
it('handles a toggle click on a hunk containing a selection', function() {
const toggleRows = sinon.spy();
const wrapper = mount(buildApp({selectedRows: new Set([2]), toggleRows, selectionMode: 'line'}));
wrapper.find('HunkHeaderView').at(0).prop('toggleSelection')();
assert.sameMembers(Array.from(toggleRows.lastCall.args[0]), [2]);
assert.strictEqual(toggleRows.lastCall.args[1], 'line');
});
it('handles a toggle click on a hunk not containing a selection', function() {
const toggleRows = sinon.spy();
const wrapper = mount(buildApp({selectedRows: new Set([2]), toggleRows, selectionMode: 'line'}));
wrapper.find('HunkHeaderView').at(1).prop('toggleSelection')();
assert.sameMembers(Array.from(toggleRows.lastCall.args[0]), [6, 7]);
assert.strictEqual(toggleRows.lastCall.args[1], 'hunk');
});
it('handles a discard click on a hunk containing a selection', function() {
const discardRows = sinon.spy();
const wrapper = mount(buildApp({selectedRows: new Set([2]), discardRows, selectionMode: 'line'}));
wrapper.find('HunkHeaderView').at(0).prop('discardSelection')();
assert.sameMembers(Array.from(discardRows.lastCall.args[0]), [2]);
assert.strictEqual(discardRows.lastCall.args[1], 'line');
});
it('handles a discard click on a hunk not containing a selection', function() {
const discardRows = sinon.spy();
const wrapper = mount(buildApp({selectedRows: new Set([2]), discardRows, selectionMode: 'line'}));
wrapper.find('HunkHeaderView').at(1).prop('discardSelection')();
assert.sameMembers(Array.from(discardRows.lastCall.args[0]), [6, 7]);
assert.strictEqual(discardRows.lastCall.args[1], 'hunk');
});
});
describe('custom gutters', function() {
let wrapper, instance, editor;
beforeEach(function() {
wrapper = mount(buildApp());
instance = wrapper.instance();
editor = wrapper.find('AtomTextEditor').instance().getModel();
});
it('computes the old line number for a buffer row', function() {
assert.strictEqual(instance.oldLineNumberLabel({bufferRow: 5, softWrapped: false}), '\u00a08');
assert.strictEqual(instance.oldLineNumberLabel({bufferRow: 6, softWrapped: false}), '\u00a0\u00a0');
assert.strictEqual(instance.oldLineNumberLabel({bufferRow: 6, softWrapped: true}), '\u00a0\u00a0');
assert.strictEqual(instance.oldLineNumberLabel({bufferRow: 7, softWrapped: false}), '\u00a09');
assert.strictEqual(instance.oldLineNumberLabel({bufferRow: 8, softWrapped: false}), '10');
assert.strictEqual(instance.oldLineNumberLabel({bufferRow: 8, softWrapped: true}), '\u00a0•');
assert.strictEqual(instance.oldLineNumberLabel({bufferRow: 999, softWrapped: false}), '\u00a0\u00a0');
});
it('computes the new line number for a buffer row', function() {
assert.strictEqual(instance.newLineNumberLabel({bufferRow: 5, softWrapped: false}), '\u00a09');
assert.strictEqual(instance.newLineNumberLabel({bufferRow: 6, softWrapped: false}), '10');
assert.strictEqual(instance.newLineNumberLabel({bufferRow: 6, softWrapped: true}), '\u00a0•');
assert.strictEqual(instance.newLineNumberLabel({bufferRow: 7, softWrapped: false}), '\u00a0\u00a0');