forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstaging-view.test.js
897 lines (731 loc) · 32.7 KB
/
staging-view.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
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
import path from 'path';
import React from 'react';
import {mount} from 'enzyme';
import StagingView from '../../lib/views/staging-view';
import CommitView from '../../lib/views/commit-view';
import CommitPreviewItem from '../../lib/items/commit-preview-item';
import ResolutionProgress from '../../lib/models/conflicts/resolution-progress';
import * as reporterProxy from '../../lib/reporter-proxy';
import {assertEqualSets} from '../helpers';
describe('StagingView', function() {
const workingDirectoryPath = '/not/real/';
let atomEnv, commands, workspace, notificationManager;
let app;
beforeEach(function() {
atomEnv = global.buildAtomEnvironment();
commands = atomEnv.commands;
workspace = atomEnv.workspace;
notificationManager = atomEnv.notifications;
sinon.stub(workspace, 'open');
sinon.stub(workspace, 'paneForItem').returns({activateItem: () => { }});
const noop = () => { };
app = (
<StagingView
unstagedChanges={[]}
stagedChanges={[]}
workingDirectoryPath={workingDirectoryPath}
hasUndoHistory={false}
commands={commands}
notificationManager={notificationManager}
workspace={workspace}
openFiles={noop}
attemptFileStageOperation={noop}
discardWorkDirChangesForPaths={noop}
undoLastDiscard={noop}
attemptStageAllOperation={noop}
resolveAsOurs={noop}
resolveAsTheirs={noop}
/>
);
});
afterEach(function() {
atomEnv.destroy();
});
describe('staging and unstaging files', function() {
it('renders staged and unstaged files', function() {
const filePatches = [
{filePath: 'a.txt', status: 'modified'},
{filePath: 'b.txt', status: 'deleted'},
];
const wrapper = mount(React.cloneElement(app, {unstagedChanges: filePatches}));
assert.deepEqual(
wrapper.find('.github-UnstagedChanges .github-FilePatchListView-item').map(n => n.text()),
['a.txt', 'b.txt'],
);
});
it('renders staged files', function() {
const filePatches = [
{filePath: 'a.txt', status: 'modified'},
{filePath: 'b.txt', status: 'deleted'},
];
const wrapper = mount(React.cloneElement(app, {stagedChanges: filePatches}));
assert.deepEqual(
wrapper.find('.github-StagedChanges .github-FilePatchListView-item').map(n => n.text()),
['a.txt', 'b.txt'],
);
});
describe('confirmSelectedItems()', function() {
let filePatches, attemptFileStageOperation;
beforeEach(function() {
filePatches = [
{filePath: 'a.txt', status: 'modified'},
{filePath: 'b.txt', status: 'deleted'},
];
attemptFileStageOperation = sinon.spy();
});
it('calls attemptFileStageOperation with the paths to stage and the staging status', async function() {
const wrapper = mount(React.cloneElement(app, {
unstagedChanges: filePatches,
attemptFileStageOperation,
}));
wrapper.find('.github-StagingView-unstaged').find('.github-FilePatchListView-item').at(1)
.simulate('mousedown', {button: 0});
await wrapper.instance().mouseup();
commands.dispatch(wrapper.getDOMNode(), 'core:confirm');
await assert.async.isTrue(attemptFileStageOperation.calledWith(['b.txt'], 'unstaged'));
});
it('calls attemptFileStageOperation with the paths to unstage and the staging status', async function() {
const wrapper = mount(React.cloneElement(app, {
stagedChanges: filePatches,
attemptFileStageOperation,
}));
wrapper.find('.github-StagingView-staged').find('.github-FilePatchListView-item').at(1)
.simulate('mousedown', {button: 0});
await wrapper.instance().mouseup();
commands.dispatch(wrapper.getDOMNode(), 'core:confirm');
await assert.async.isTrue(attemptFileStageOperation.calledWith(['b.txt'], 'staged'));
});
});
});
describe('merge conflicts list', function() {
const mergeConflicts = [
{
filePath: 'conflicted-path',
status: {
file: 'modified',
ours: 'deleted',
theirs: 'modified',
},
},
];
it('is not visible when no conflicted paths are passed', function() {
const wrapper = mount(app);
assert.isFalse(wrapper.find('.github-MergeConflictPaths').exists());
});
it('is visible when conflicted paths are passed', function() {
const wrapper = mount(React.cloneElement(app, {mergeConflicts}));
assert.isTrue(wrapper.find('.github-MergeConflictPaths').exists());
});
it('shows "calculating" while calculating the number of conflicts', function() {
const resolutionProgress = new ResolutionProgress();
const wrapper = mount(React.cloneElement(app, {
mergeConflicts,
resolutionProgress,
}));
assert.lengthOf(wrapper.find('.github-RemainingConflicts'), 1);
assert.strictEqual(wrapper.find('.github-RemainingConflicts').text(), 'calculating');
});
it('shows the number of remaining conflicts', function() {
const resolutionProgress = new ResolutionProgress();
resolutionProgress.reportMarkerCount(path.join(workingDirectoryPath, 'conflicted-path'), 10);
const wrapper = mount(React.cloneElement(app, {
mergeConflicts,
resolutionProgress,
}));
assert.strictEqual(wrapper.find('.github-RemainingConflicts').text(), '10 conflicts remaining');
});
it('shows a checkmark when there are no remaining conflicts', function() {
const resolutionProgress = new ResolutionProgress();
resolutionProgress.reportMarkerCount(path.join(workingDirectoryPath, 'conflicted-path'), 0);
const wrapper = mount(React.cloneElement(app, {
mergeConflicts,
resolutionProgress,
}));
assert.lengthOf(wrapper.find('.icon-check'), 1);
});
it('disables the "stage all" button while there are unresolved conflicts', function() {
const multiMergeConflicts = [
{
filePath: 'conflicted-path-0.txt',
status: {file: 'modified', ours: 'deleted', theirs: 'modified'},
},
{
filePath: 'conflicted-path-1.txt',
status: {file: 'modified', ours: 'modified', theirs: 'modified'},
},
];
const resolutionProgress = new ResolutionProgress();
resolutionProgress.reportMarkerCount(path.join(workingDirectoryPath, 'conflicted-path-0.txt'), 2);
resolutionProgress.reportMarkerCount(path.join(workingDirectoryPath, 'conflicted-path-1.txt'), 0);
const wrapper = mount(React.cloneElement(app, {
mergeConflicts: multiMergeConflicts,
resolutionProgress,
}));
const conflictButton = wrapper.find('.github-MergeConflictPaths')
.find('.github-StagingView-headerButton');
assert.strictEqual(conflictButton.text(), 'Stage All');
assert.isTrue(conflictButton.prop('disabled'));
});
it('enables the "stage all" button when all conflicts are resolved', function() {
const resolutionProgress = new ResolutionProgress();
resolutionProgress.reportMarkerCount(path.join(workingDirectoryPath, 'conflicted-path'), 0);
const wrapper = mount(React.cloneElement(app, {
mergeConflicts,
resolutionProgress,
}));
const conflictButton = wrapper.find('.github-MergeConflictPaths')
.find('.github-StagingView-headerButton');
assert.strictEqual(conflictButton.text(), 'Stage All');
assert.isFalse(conflictButton.prop('disabled'));
});
});
describe('showFilePatchItem(filePath, stagingStatus, {activate})', function() {
describe('calls to workspace.open', function() {
it('passes activation options and focuses the returned item if activate is true', async function() {
const wrapper = mount(app);
const changedFileItem = {
getElement: () => changedFileItem,
querySelector: () => changedFileItem,
focus: sinon.spy(),
};
workspace.open.returns(changedFileItem);
await wrapper.instance().showFilePatchItem('file.txt', 'staged', {activate: true});
assert.equal(workspace.open.callCount, 1);
assert.deepEqual(workspace.open.args[0], [
`atom-github://file-patch/file.txt?workdir=${encodeURIComponent(workingDirectoryPath)}&stagingStatus=staged`,
{pending: true, activatePane: true, pane: undefined, activateItem: true},
]);
assert.isTrue(changedFileItem.focus.called);
});
it('makes the item visible if activate is false', async function() {
const wrapper = mount(app);
const focus = sinon.spy();
const changedFileItem = {focus};
workspace.open.returns(changedFileItem);
const activateItem = sinon.spy();
workspace.paneForItem.returns({activateItem});
await wrapper.instance().showFilePatchItem('file.txt', 'staged', {activate: false});
assert.equal(workspace.open.callCount, 1);
assert.deepEqual(workspace.open.args[0], [
`atom-github://file-patch/file.txt?workdir=${encodeURIComponent(workingDirectoryPath)}&stagingStatus=staged`,
{pending: true, activatePane: false, pane: undefined, activateItem: false},
]);
assert.isFalse(focus.called);
assert.equal(activateItem.callCount, 1);
assert.equal(activateItem.args[0][0], changedFileItem);
});
});
});
describe('showMergeConflictFileForPath(relativeFilePath, {activate})', function() {
it('passes activation options and focuses the returned item if activate is true', async function() {
const wrapper = mount(app);
sinon.stub(wrapper.instance(), 'fileExists').returns(true);
await wrapper.instance().showMergeConflictFileForPath('conflict.txt');
assert.equal(workspace.open.callCount, 1);
assert.deepEqual(workspace.open.args[0], [
path.join(workingDirectoryPath, 'conflict.txt'),
{pending: true, activatePane: false, activateItem: false},
]);
workspace.open.reset();
await wrapper.instance().showMergeConflictFileForPath('conflict.txt', {activate: true});
assert.equal(workspace.open.callCount, 1);
assert.deepEqual(workspace.open.args[0], [
path.join(workingDirectoryPath, 'conflict.txt'),
{pending: true, activatePane: true, activateItem: true},
]);
});
describe('when the file doesn\'t exist', function() {
it('shows an info notification and does not open the file', async function() {
sinon.spy(notificationManager, 'addInfo');
const wrapper = mount(app);
sinon.stub(wrapper.instance(), 'fileExists').returns(false);
notificationManager.clear(); // clear out notifications
await wrapper.instance().showMergeConflictFileForPath('conflict.txt');
assert.equal(notificationManager.getNotifications().length, 1);
assert.equal(workspace.open.callCount, 0);
assert.equal(notificationManager.addInfo.callCount, 1);
assert.deepEqual(notificationManager.addInfo.args[0], ['File has been deleted.']);
});
});
});
describe('getPanesWithStalePendingFilePatchItem', function() {
it('ignores CommitPreviewItems', function() {
const pane = workspace.getCenter().getPanes()[0];
const changedFileItem = new CommitPreviewItem({});
sinon.stub(pane, 'getPendingItem').returns({
getRealItem: () => changedFileItem,
});
const wrapper = mount(app);
assert.deepEqual(wrapper.instance().getPanesWithStalePendingFilePatchItem(), []);
});
});
describe('when the selection changes due to keyboard navigation', function() {
let showFilePatchItem, showMergeConflictFileForPath;
beforeEach(function() {
showFilePatchItem = sinon.stub(StagingView.prototype, 'showFilePatchItem');
showMergeConflictFileForPath = sinon.stub(StagingView.prototype, 'showMergeConflictFileForPath');
});
afterEach(function() {
showFilePatchItem.restore();
showMergeConflictFileForPath.restore();
});
describe('when github.keyboardNavigationDelay is 0', function() {
beforeEach(function() {
atom.config.set('github.keyboardNavigationDelay', 0);
});
it('synchronously calls showFilePatchItem if there is a pending file patch item open', async function() {
const filePatches = [
{filePath: 'a.txt', status: 'modified'},
{filePath: 'b.txt', status: 'deleted'},
];
const wrapper = mount(React.cloneElement(app, {
unstagedChanges: filePatches,
}));
sinon.stub(wrapper.instance(), 'hasFocus').returns(true);
const getPanesWithStalePendingFilePatchItem = sinon.stub(
wrapper.instance(),
'getPanesWithStalePendingFilePatchItem',
).returns([]);
await wrapper.instance().selectNext();
assert.isFalse(showFilePatchItem.called);
getPanesWithStalePendingFilePatchItem.returns(['item1', 'item2']);
await wrapper.instance().selectPrevious();
assert.isTrue(showFilePatchItem.calledTwice);
assert.strictEqual(showFilePatchItem.args[0][0], filePatches[0].filePath);
assert.strictEqual(showFilePatchItem.args[1][0], filePatches[0].filePath);
showFilePatchItem.reset();
await wrapper.instance().selectNext();
assert.isTrue(showFilePatchItem.calledTwice);
assert.strictEqual(showFilePatchItem.args[0][0], filePatches[1].filePath);
assert.strictEqual(showFilePatchItem.args[1][0], filePatches[1].filePath);
});
it('does not call showMergeConflictFileForPath', async function() {
// Currently we don't show merge conflict files while using keyboard nav. They can only be viewed via clicking.
// This behavior is different from the diff views because merge conflict files are regular editors with decorations
// We might change this in the future to also open pending items
const mergeConflicts = [
{
filePath: 'conflicted-path-1',
status: {
file: 'modified',
ours: 'deleted',
theirs: 'modified',
},
},
{
filePath: 'conflicted-path-2',
status: {
file: 'modified',
ours: 'deleted',
theirs: 'modified',
},
},
];
const wrapper = mount(React.cloneElement(app, {
mergeConflicts,
}));
await wrapper.instance().selectNext();
const selectedItems = wrapper.instance().getSelectedItems().map(item => item.filePath);
assert.deepEqual(selectedItems, ['conflicted-path-2']);
assert.isFalse(showMergeConflictFileForPath.called);
});
});
describe('when github.keyboardNavigationDelay is greater than 0', function() {
beforeEach(function() {
atom.config.set('github.keyboardNavigationDelay', 50);
});
it('asynchronously calls showFilePatchItem if there is a pending file patch item open', async function() {
const filePatches = [
{filePath: 'a.txt', status: 'modified'},
{filePath: 'b.txt', status: 'deleted'},
];
const wrapper = mount(React.cloneElement(app, {
unstagedChanges: filePatches,
}));
sinon.stub(wrapper.instance(), 'hasFocus').returns(true);
const getPanesWithStalePendingFilePatchItem = sinon.stub(
wrapper.instance(),
'getPanesWithStalePendingFilePatchItem',
).returns([]);
await wrapper.instance().selectNext();
assert.isFalse(showFilePatchItem.called);
getPanesWithStalePendingFilePatchItem.returns(['item1', 'item2', 'item3']);
await wrapper.instance().selectPrevious();
await assert.async.isTrue(showFilePatchItem.calledWith(filePatches[0].filePath));
assert.isTrue(showFilePatchItem.calledThrice);
showFilePatchItem.reset();
await wrapper.instance().selectNext();
await assert.async.isTrue(showFilePatchItem.calledWith(filePatches[1].filePath));
assert.isTrue(showFilePatchItem.calledThrice);
});
});
it('autoscrolls to the selected item if it is out of view', async function() {
const unstagedChanges = [
{filePath: 'a.txt', status: 'modified'},
{filePath: 'b.txt', status: 'modified'},
{filePath: 'c.txt', status: 'modified'},
{filePath: 'd.txt', status: 'modified'},
{filePath: 'e.txt', status: 'modified'},
{filePath: 'f.txt', status: 'modified'},
];
const root = document.createElement('div');
root.style.top = '75%';
document.body.appendChild(root);
const wrapper = mount(React.cloneElement(app, {
unstagedChanges,
}), {attachTo: root});
// Actually loading the style sheet is complicated and prone to timing
// issues, so this applies some minimal styling to allow the unstaged
// changes list to scroll.
const unstagedChangesList = wrapper.find('.github-StagingView-unstaged').getDOMNode();
unstagedChangesList.style.flex = 'inherit';
unstagedChangesList.style.overflow = 'scroll';
unstagedChangesList.style.height = '50px';
assert.equal(unstagedChangesList.scrollTop, 0);
await wrapper.instance().selectNext();
await wrapper.instance().selectNext();
await wrapper.instance().selectNext();
await wrapper.instance().selectNext();
assert.isAbove(unstagedChangesList.scrollTop, 0);
wrapper.unmount();
root.remove();
});
});
describe('when the selection changes due to a repo update', function() {
let showFilePatchItem;
beforeEach(function() {
atom.config.set('github.keyboardNavigationDelay', 0);
showFilePatchItem = sinon.stub(StagingView.prototype, 'showFilePatchItem');
});
afterEach(function() {
showFilePatchItem.restore();
});
// such as files being staged/unstaged, discarded or stashed
it('calls showFilePatchItem if there is a pending file patch item open', function() {
const filePatches = [
{filePath: 'a.txt', status: 'modified'},
{filePath: 'b.txt', status: 'deleted'},
];
const wrapper = mount(React.cloneElement(app, {
unstagedChanges: filePatches,
}));
sinon.stub(wrapper.instance(), 'hasFocus').returns(true);
let selectedItems = wrapper.instance().getSelectedItems();
assert.lengthOf(selectedItems, 1);
assert.strictEqual(selectedItems[0].filePath, 'a.txt');
sinon.stub(wrapper.instance(), 'getPanesWithStalePendingFilePatchItem').returns(['item1']);
const newFilePatches = filePatches.slice(1); // remove first item, as though it was staged or discarded
wrapper.setProps({unstagedChanges: newFilePatches});
selectedItems = wrapper.instance().getSelectedItems();
assert.lengthOf(selectedItems, 1);
assert.strictEqual(selectedItems[0].filePath, 'b.txt');
assert.isTrue(showFilePatchItem.calledWith('b.txt'));
});
it('does not call showFilePatchItem if a new set of file patches are being fetched', function() {
const wrapper = mount(React.cloneElement(app, {
unstagedChanges: [{filePath: 'a.txt', status: 'modified'}],
}));
sinon.stub(wrapper.instance(), 'hasFocus').returns(true);
sinon.stub(wrapper.instance(), 'getPanesWithStalePendingFilePatchItem').returns(['item1']);
wrapper.setProps({unstagedChanges: []}); // when repo is changed, lists are cleared out and data is fetched for new repo
assert.isFalse(showFilePatchItem.called);
wrapper.setProps({unstagedChanges: [{filePath: 'b.txt', status: 'deleted'}]}); // data for new repo is loaded
assert.isFalse(showFilePatchItem.called);
wrapper.setProps({unstagedChanges: [{filePath: 'c.txt', status: 'added'}]});
assert.isTrue(showFilePatchItem.called);
});
});
it('updates the selection when there is an `activeFilePatch`', function() {
const wrapper = mount(React.cloneElement(app, {
unstagedChanges: [{filePath: 'file.txt', status: 'modified'}],
}));
let selectedItems = wrapper.instance().getSelectedItems();
assert.lengthOf(selectedItems, 1);
assert.strictEqual(selectedItems[0].filePath, 'file.txt');
// view.activeFilePatch = {
// getFilePath() { return 'b.txt'; },
// getStagingStatus() { return 'unstaged'; },
// };
wrapper.setProps({
unstagedChanges: [
{filePath: 'a.txt', status: 'modified'},
{filePath: 'b.txt', status: 'deleted'},
],
});
selectedItems = wrapper.instance().getSelectedItems();
assert.lengthOf(selectedItems, 1);
});
describe('when dragging a mouse across multiple items', function() {
let showFilePatchItem;
beforeEach(function() {
showFilePatchItem = sinon.stub(StagingView.prototype, 'showFilePatchItem');
});
afterEach(function() {
showFilePatchItem.restore();
});
// https://github.com/atom/github/issues/352
it('selects the items', async function() {
const unstagedChanges = [
{filePath: 'a.txt', status: 'modified'},
{filePath: 'b.txt', status: 'modified'},
{filePath: 'c.txt', status: 'modified'},
];
const wrapper = mount(React.cloneElement(app, {
unstagedChanges,
}));
await wrapper.instance().mousedownOnItem({button: 0, persist: () => { }}, unstagedChanges[0]);
await wrapper.instance().mousemoveOnItem({}, unstagedChanges[0]);
await wrapper.instance().mousemoveOnItem({}, unstagedChanges[1]);
wrapper.instance().mouseup();
assertEqualSets(wrapper.state('selection').getSelectedItems(), new Set(unstagedChanges.slice(0, 2)));
assert.equal(showFilePatchItem.callCount, 0);
});
});
describe('when advancing and retreating activation', function() {
let wrapper, stagedChanges;
beforeEach(function() {
const unstagedChanges = [
{filePath: 'unstaged-1.txt', status: 'modified'},
{filePath: 'unstaged-2.txt', status: 'modified'},
{filePath: 'unstaged-3.txt', status: 'modified'},
];
const mergeConflicts = [
{filePath: 'conflict-1.txt', status: {file: 'modified', ours: 'deleted', theirs: 'modified'}},
{filePath: 'conflict-2.txt', status: {file: 'modified', ours: 'added', theirs: 'modified'}},
];
stagedChanges = [
{filePath: 'staged-1.txt', status: 'staged'},
{filePath: 'staged-2.txt', status: 'staged'},
];
wrapper = mount(React.cloneElement(app, {
unstagedChanges, stagedChanges, mergeConflicts,
}));
});
const assertSelected = expected => {
const actual = Array.from(wrapper.update().state('selection').getSelectedItems()).map(item => item.filePath);
assert.deepEqual(actual, expected);
};
it("selects the next list, retaining that list's selection", async function() {
await wrapper.instance().activateNextList();
assertSelected(['conflict-1.txt']);
await wrapper.instance().activateNextList();
assertSelected(['staged-1.txt']);
await wrapper.instance().activateNextList();
assertSelected(['staged-1.txt']);
});
it("selects the previous list, retaining that list's selection", async function() {
wrapper.instance().mousedownOnItem({button: 0, persist: () => { }}, stagedChanges[1]);
wrapper.instance().mouseup();
assertSelected(['staged-2.txt']);
await wrapper.instance().activatePreviousList();
assertSelected(['conflict-1.txt']);
await wrapper.instance().activatePreviousList();
assertSelected(['unstaged-1.txt']);
await wrapper.instance().activatePreviousList();
assertSelected(['unstaged-1.txt']);
});
it('selects the first item of the final list', async function() {
assertSelected(['unstaged-1.txt']);
await wrapper.instance().activateLastList();
assertSelected(['staged-1.txt']);
});
});
describe('when navigating with core:move-left', function() {
let wrapper, showFilePatchItem, showMergeConflictFileForPath;
beforeEach(function() {
const unstagedChanges = [
{filePath: 'unstaged-1.txt', status: 'modified'},
{filePath: 'unstaged-2.txt', status: 'modified'},
];
const mergeConflicts = [
{filePath: 'conflict-1.txt', status: {file: 'modified', ours: 'modified', theirs: 'modified'}},
{filePath: 'conflict-2.txt', status: {file: 'modified', ours: 'modified', theirs: 'modified'}},
];
wrapper = mount(React.cloneElement(app, {
unstagedChanges,
mergeConflicts,
}));
showFilePatchItem = sinon.stub(StagingView.prototype, 'showFilePatchItem');
showMergeConflictFileForPath = sinon.stub(StagingView.prototype, 'showMergeConflictFileForPath');
});
afterEach(function() {
showFilePatchItem.restore();
showMergeConflictFileForPath.restore();
});
it('invokes a callback only when a single file is selected', async function() {
await wrapper.instance().selectFirst();
commands.dispatch(wrapper.getDOMNode(), 'core:move-left');
assert.isTrue(showFilePatchItem.calledWith('unstaged-1.txt'), 'Callback invoked with unstaged-1.txt');
showFilePatchItem.reset();
await wrapper.instance().selectAll();
const selectedFilePaths = wrapper.instance().getSelectedItems().map(item => item.filePath).sort();
assert.deepEqual(selectedFilePaths, ['unstaged-1.txt', 'unstaged-2.txt']);
commands.dispatch(wrapper.getDOMNode(), 'core:move-left');
assert.equal(showFilePatchItem.callCount, 0);
});
it('invokes a callback with a single merge conflict selection', async function() {
await wrapper.instance().activateNextList();
await wrapper.instance().selectFirst();
commands.dispatch(wrapper.getDOMNode(), 'core:move-left');
assert.isTrue(showMergeConflictFileForPath.calledWith('conflict-1.txt'), 'Callback invoked with conflict-1.txt');
showMergeConflictFileForPath.reset();
await wrapper.instance().selectAll();
const selectedFilePaths = wrapper.instance().getSelectedItems().map(item => item.filePath).sort();
assert.deepEqual(selectedFilePaths, ['conflict-1.txt', 'conflict-2.txt']);
commands.dispatch(wrapper.getDOMNode(), 'core:move-left');
assert.equal(showMergeConflictFileForPath.callCount, 0);
});
});
// https://github.com/atom/github/issues/468
it('updates selection on mousedown', async function() {
const unstagedChanges = [
{filePath: 'a.txt', status: 'modified'},
{filePath: 'b.txt', status: 'modified'},
{filePath: 'c.txt', status: 'modified'},
];
const wrapper = mount(React.cloneElement(app, {
unstagedChanges,
}));
await wrapper.instance().mousedownOnItem({button: 0, persist: () => { }}, unstagedChanges[0]);
wrapper.instance().mouseup();
assertEqualSets(wrapper.state('selection').getSelectedItems(), new Set([unstagedChanges[0]]));
await wrapper.instance().mousedownOnItem({button: 0, persist: () => { }}, unstagedChanges[2]);
assertEqualSets(wrapper.state('selection').getSelectedItems(), new Set([unstagedChanges[2]]));
});
if (process.platform !== 'win32') {
// https://github.com/atom/github/issues/514
describe('mousedownOnItem', function() {
it('does not select item or set selection to be in progress if ctrl-key is pressed and not on windows', async function() {
const unstagedChanges = [
{filePath: 'a.txt', status: 'modified'},
{filePath: 'b.txt', status: 'modified'},
{filePath: 'c.txt', status: 'modified'},
];
const wrapper = mount(React.cloneElement(app, {
unstagedChanges,
}));
sinon.spy(wrapper.state('selection'), 'addOrSubtractSelection');
sinon.spy(wrapper.state('selection'), 'selectItem');
await wrapper.instance().mousedownOnItem({button: 0, ctrlKey: true, persist: () => { }}, unstagedChanges[0]);
assert.isFalse(wrapper.state('selection').addOrSubtractSelection.called);
assert.isFalse(wrapper.state('selection').selectItem.called);
assert.isFalse(wrapper.instance().mouseSelectionInProgress);
});
});
}
describe('focus management', function() {
let wrapper, instance;
beforeEach(function() {
const unstagedChanges = [
{filePath: 'unstaged-1.txt', status: 'modified'},
{filePath: 'unstaged-2.txt', status: 'modified'},
{filePath: 'unstaged-3.txt', status: 'modified'},
];
const mergeConflicts = [
{filePath: 'conflict-1.txt', status: {file: 'modified', ours: 'deleted', theirs: 'modified'}},
{filePath: 'conflict-2.txt', status: {file: 'modified', ours: 'added', theirs: 'modified'}},
];
const stagedChanges = [
{filePath: 'staged-1.txt', status: 'staged'},
{filePath: 'staged-2.txt', status: 'staged'},
];
wrapper = mount(React.cloneElement(app, {
unstagedChanges, stagedChanges, mergeConflicts,
}));
instance = wrapper.instance();
});
it('gets the current focus', function() {
const rootElement = wrapper.find('.github-StagingView').getDOMNode();
assert.strictEqual(instance.getFocus(rootElement), StagingView.focus.STAGING);
assert.isNull(instance.getFocus(document.body));
instance.refRoot.setter(null);
assert.isNull(instance.getFocus(rootElement));
});
it('sets a new focus', function() {
const rootElement = wrapper.find('.github-StagingView').getDOMNode();
sinon.stub(rootElement, 'focus');
assert.isFalse(instance.setFocus(Symbol('nope')));
assert.isFalse(rootElement.focus.called);
assert.isTrue(instance.setFocus(StagingView.focus.STAGING));
assert.isTrue(rootElement.focus.called);
instance.refRoot.setter(null);
rootElement.focus.resetHistory();
assert.isTrue(instance.setFocus(StagingView.focus.STAGING));
assert.isFalse(rootElement.focus.called);
});
it('keeps focus on this component if a non-last list is focused', async function() {
sinon.spy(instance, 'activateNextList');
assert.strictEqual(
await instance.advanceFocusFrom(StagingView.focus.STAGING),
StagingView.focus.STAGING,
);
assert.isTrue(await instance.activateNextList.lastCall.returnValue);
});
it('moves focus to the CommitView if the last list was focused', async function() {
await instance.activateLastList();
sinon.spy(instance, 'activateNextList');
assert.strictEqual(
await instance.advanceFocusFrom(StagingView.focus.STAGING),
CommitView.firstFocus,
);
assert.isFalse(await instance.activateNextList.lastCall.returnValue);
});
it('detects when the component does have focus', function() {
const rootElement = wrapper.find('.github-StagingView').getDOMNode();
sinon.stub(rootElement, 'contains');
rootElement.contains.returns(true);
assert.isTrue(wrapper.instance().hasFocus());
rootElement.contains.returns(false);
assert.isFalse(wrapper.instance().hasFocus());
rootElement.contains.returns(true);
wrapper.instance().refRoot.setter(null);
assert.isFalse(wrapper.instance().hasFocus());
});
});
describe('discardAll()', function() {
it('records an event', function() {
const filePatches = [
{filePath: 'a.txt', status: 'modified'},
{filePath: 'b.txt', status: 'deleted'},
];
const wrapper = mount(React.cloneElement(app, {unstagedChanges: filePatches}));
sinon.stub(reporterProxy, 'addEvent');
wrapper.instance().discardAll();
assert.isTrue(reporterProxy.addEvent.calledWith('discard-unstaged-changes', {
package: 'github',
component: 'StagingView',
fileCount: 2,
type: 'all',
eventSource: undefined,
}));
});
});
describe('discardChanges()', function() {
it('records an event', function() {
const wrapper = mount(app);
sinon.stub(reporterProxy, 'addEvent');
sinon.stub(wrapper.instance(), 'getSelectedItemFilePaths').returns(['a.txt', 'b.txt']);
wrapper.instance().discardChanges();
assert.isTrue(reporterProxy.addEvent.calledWith('discard-unstaged-changes', {
package: 'github',
component: 'StagingView',
fileCount: 2,
type: 'selected',
eventSource: undefined,
}));
});
});
describe('undoLastDiscard()', function() {
it('records an event', function() {
const wrapper = mount(React.cloneElement(app, {hasUndoHistory: true}));
sinon.stub(reporterProxy, 'addEvent');
sinon.stub(wrapper.instance(), 'getSelectedItemFilePaths').returns(['a.txt', 'b.txt']);
wrapper.instance().undoLastDiscard();
assert.isTrue(reporterProxy.addEvent.calledWith('undo-last-discard', {
package: 'github',
component: 'StagingView',
eventSource: undefined,
}));
});
});
});