-
Notifications
You must be signed in to change notification settings - Fork 0
/
pane-spec.js
1699 lines (1446 loc) · 57.4 KB
/
pane-spec.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
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
const { extend } = require('underscore-plus');
const { Emitter } = require('event-kit');
const Grim = require('grim');
const Pane = require('../src/pane');
const PaneContainer = require('../src/pane-container');
const { conditionPromise, timeoutPromise } = require('./async-spec-helpers');
describe('Pane', () => {
let confirm, showSaveDialog, deserializerDisposable;
class Item {
static deserialize({ name, uri }) {
return new Item(name, uri);
}
constructor(name, uri) {
this.name = name;
this.uri = uri;
this.emitter = new Emitter();
this.destroyed = false;
}
getURI() {
return this.uri;
}
getPath() {
return this.path;
}
isEqual(other) {
return this.name === (other && other.name);
}
isPermanentDockItem() {
return false;
}
isDestroyed() {
return this.destroyed;
}
serialize() {
return { deserializer: 'Item', name: this.name, uri: this.uri };
}
copy() {
return new Item(this.name, this.uri);
}
destroy() {
this.destroyed = true;
return this.emitter.emit('did-destroy');
}
onDidDestroy(fn) {
return this.emitter.on('did-destroy', fn);
}
onDidTerminatePendingState(callback) {
return this.emitter.on('terminate-pending-state', callback);
}
terminatePendingState() {
return this.emitter.emit('terminate-pending-state');
}
}
beforeEach(() => {
confirm = spyOn(atom.applicationDelegate, 'confirm');
showSaveDialog = spyOn(atom.applicationDelegate, 'showSaveDialog');
deserializerDisposable = atom.deserializers.add(Item);
});
afterEach(() => {
deserializerDisposable.dispose();
});
function paneParams(params) {
return extend(
{
applicationDelegate: atom.applicationDelegate,
config: atom.config,
deserializerManager: atom.deserializers,
notificationManager: atom.notifications
},
params
);
}
describe('construction', () => {
it('sets the active item to the first item', () => {
const pane = new Pane(
paneParams({ items: [new Item('A'), new Item('B')] })
);
expect(pane.getActiveItem()).toBe(pane.itemAtIndex(0));
});
it('compacts the items array', () => {
const pane = new Pane(
paneParams({ items: [undefined, new Item('A'), null, new Item('B')] })
);
expect(pane.getItems().length).toBe(2);
expect(pane.getActiveItem()).toBe(pane.itemAtIndex(0));
});
});
describe('::activate()', () => {
let container, pane1, pane2;
beforeEach(() => {
container = new PaneContainer({
location: 'center',
config: atom.config,
applicationDelegate: atom.applicationDelegate
});
container.getActivePane().splitRight();
[pane1, pane2] = container.getPanes();
});
it('changes the active pane on the container', () => {
expect(container.getActivePane()).toBe(pane2);
pane1.activate();
expect(container.getActivePane()).toBe(pane1);
pane2.activate();
expect(container.getActivePane()).toBe(pane2);
});
it('invokes ::onDidChangeActivePane observers on the container', () => {
const observed = [];
container.onDidChangeActivePane(activePane => observed.push(activePane));
pane1.activate();
pane1.activate();
pane2.activate();
pane1.activate();
expect(observed).toEqual([pane1, pane2, pane1]);
});
it('invokes ::onDidChangeActive observers on the relevant panes', () => {
const observed = [];
pane1.onDidChangeActive(active => observed.push(active));
pane1.activate();
pane2.activate();
expect(observed).toEqual([true, false]);
});
it('invokes ::onDidActivate() observers', () => {
let eventCount = 0;
pane1.onDidActivate(() => eventCount++);
pane1.activate();
pane1.activate();
pane2.activate();
expect(eventCount).toBe(2);
});
});
describe('::addItem(item, index)', () => {
it('adds the item at the given index', () => {
const pane = new Pane(
paneParams({ items: [new Item('A'), new Item('B')] })
);
const [item1, item2] = pane.getItems();
const item3 = new Item('C');
pane.addItem(item3, { index: 1 });
expect(pane.getItems()).toEqual([item1, item3, item2]);
});
it('adds the item after the active item if no index is provided', () => {
const pane = new Pane(
paneParams({ items: [new Item('A'), new Item('B'), new Item('C')] })
);
const [item1, item2, item3] = pane.getItems();
pane.activateItem(item2);
const item4 = new Item('D');
pane.addItem(item4);
expect(pane.getItems()).toEqual([item1, item2, item4, item3]);
});
it('sets the active item after adding the first item', () => {
const pane = new Pane(paneParams());
const item = new Item('A');
pane.addItem(item);
expect(pane.getActiveItem()).toBe(item);
});
it('invokes ::onDidAddItem() observers', () => {
const pane = new Pane(
paneParams({ items: [new Item('A'), new Item('B')] })
);
const events = [];
pane.onDidAddItem(event => events.push(event));
const item = new Item('C');
pane.addItem(item, { index: 1 });
expect(events).toEqual([{ item, index: 1, moved: false }]);
});
it('throws an exception if the item is already present on a pane', () => {
const item = new Item('A');
const container = new PaneContainer({
config: atom.config,
applicationDelegate: atom.applicationDelegate
});
const pane1 = container.getActivePane();
pane1.addItem(item);
const pane2 = pane1.splitRight();
expect(() => pane2.addItem(item)).toThrow();
});
it("throws an exception if the item isn't an object", () => {
const pane = new Pane(paneParams({ items: [] }));
expect(() => pane.addItem(null)).toThrow();
expect(() => pane.addItem('foo')).toThrow();
expect(() => pane.addItem(1)).toThrow();
});
it('destroys any existing pending item', () => {
const pane = new Pane(paneParams({ items: [] }));
const itemA = new Item('A');
const itemB = new Item('B');
const itemC = new Item('C');
pane.addItem(itemA, { pending: false });
pane.addItem(itemB, { pending: true });
pane.addItem(itemC, { pending: false });
expect(itemB.isDestroyed()).toBe(true);
});
it('adds the new item before destroying any existing pending item', () => {
const eventOrder = [];
const pane = new Pane(paneParams({ items: [] }));
const itemA = new Item('A');
const itemB = new Item('B');
pane.addItem(itemA, { pending: true });
pane.onDidAddItem(function({ item }) {
if (item === itemB) eventOrder.push('add');
});
pane.onDidRemoveItem(function({ item }) {
if (item === itemA) eventOrder.push('remove');
});
pane.addItem(itemB);
waitsFor(() => eventOrder.length === 2);
runs(() => expect(eventOrder).toEqual(['add', 'remove']));
});
it('subscribes to be notified when item terminates its pending state', () => {
const fakeDisposable = { dispose: () => {} };
const spy = jasmine
.createSpy('onDidTerminatePendingState')
.andReturn(fakeDisposable);
const pane = new Pane(paneParams({ items: [] }));
const item = {
getTitle: () => '',
onDidTerminatePendingState: spy
};
pane.addItem(item);
expect(spy).toHaveBeenCalled();
});
it('subscribes to be notified when item is destroyed', () => {
const fakeDisposable = { dispose: () => {} };
const spy = jasmine.createSpy('onDidDestroy').andReturn(fakeDisposable);
const pane = new Pane(paneParams({ items: [] }));
const item = {
getTitle: () => '',
onDidDestroy: spy
};
pane.addItem(item);
expect(spy).toHaveBeenCalled();
});
describe('when using the old API of ::addItem(item, index)', () => {
beforeEach(() => spyOn(Grim, 'deprecate'));
it('supports the older public API', () => {
const pane = new Pane(paneParams({ items: [] }));
const itemA = new Item('A');
const itemB = new Item('B');
const itemC = new Item('C');
pane.addItem(itemA, 0);
pane.addItem(itemB, 0);
pane.addItem(itemC, 0);
expect(pane.getItems()).toEqual([itemC, itemB, itemA]);
});
it('shows a deprecation warning', () => {
const pane = new Pane(paneParams({ items: [] }));
pane.addItem(new Item(), 2);
expect(Grim.deprecate).toHaveBeenCalledWith(
'Pane::addItem(item, 2) is deprecated in favor of Pane::addItem(item, {index: 2})'
);
});
});
});
describe('::activateItem(item)', () => {
let pane = null;
beforeEach(() => {
pane = new Pane(paneParams({ items: [new Item('A'), new Item('B')] }));
});
it('changes the active item to the current item', () => {
expect(pane.getActiveItem()).toBe(pane.itemAtIndex(0));
pane.activateItem(pane.itemAtIndex(1));
expect(pane.getActiveItem()).toBe(pane.itemAtIndex(1));
});
it("adds the given item if it isn't present in ::items", () => {
const item = new Item('C');
pane.activateItem(item);
expect(pane.getItems().includes(item)).toBe(true);
expect(pane.getActiveItem()).toBe(item);
});
it('invokes ::onDidChangeActiveItem() observers', () => {
const observed = [];
pane.onDidChangeActiveItem(item => observed.push(item));
pane.activateItem(pane.itemAtIndex(1));
expect(observed).toEqual([pane.itemAtIndex(1)]);
});
describe('when the item being activated is pending', () => {
let itemC = null;
let itemD = null;
beforeEach(() => {
itemC = new Item('C');
itemD = new Item('D');
});
it('replaces the active item if it is pending', () => {
pane.activateItem(itemC, { pending: true });
expect(pane.getItems().map(item => item.name)).toEqual(['A', 'C', 'B']);
pane.activateItem(itemD, { pending: true });
expect(pane.getItems().map(item => item.name)).toEqual(['A', 'D', 'B']);
});
it('adds the item after the active item if it is not pending', () => {
pane.activateItem(itemC, { pending: true });
pane.activateItemAtIndex(2);
pane.activateItem(itemD, { pending: true });
expect(pane.getItems().map(item => item.name)).toEqual(['A', 'B', 'D']);
});
});
});
describe('::setPendingItem', () => {
let pane = null;
beforeEach(() => {
pane = atom.workspace.getActivePane();
});
it('changes the pending item', () => {
expect(pane.getPendingItem()).toBeNull();
pane.setPendingItem('fake item');
expect(pane.getPendingItem()).toEqual('fake item');
});
});
describe('::onItemDidTerminatePendingState callback', () => {
let pane = null;
let callbackCalled = false;
beforeEach(() => {
pane = atom.workspace.getActivePane();
callbackCalled = false;
});
it('is called when the pending item changes', () => {
pane.setPendingItem('fake item one');
pane.onItemDidTerminatePendingState(function(item) {
callbackCalled = true;
expect(item).toEqual('fake item one');
});
pane.setPendingItem('fake item two');
expect(callbackCalled).toBeTruthy();
});
it('has access to the new pending item via ::getPendingItem', () => {
pane.setPendingItem('fake item one');
pane.onItemDidTerminatePendingState(function(item) {
callbackCalled = true;
expect(pane.getPendingItem()).toEqual('fake item two');
});
pane.setPendingItem('fake item two');
expect(callbackCalled).toBeTruthy();
});
it("isn't called when a pending item is replaced with a new one", async () => {
pane = null;
const pendingSpy = jasmine.createSpy('onItemDidTerminatePendingState');
const destroySpy = jasmine.createSpy('onWillDestroyItem');
await atom.workspace.open('sample.txt', { pending: true });
pane = atom.workspace.getActivePane();
pane.onItemDidTerminatePendingState(pendingSpy);
pane.onWillDestroyItem(destroySpy);
await atom.workspace.open('sample.js', { pending: true });
expect(destroySpy).toHaveBeenCalled();
expect(pendingSpy).not.toHaveBeenCalled();
});
});
describe('::activateNextRecentlyUsedItem() and ::activatePreviousRecentlyUsedItem()', () => {
it('sets the active item to the next/previous item in the itemStack, looping around at either end', () => {
const pane = new Pane(
paneParams({
items: [
new Item('A'),
new Item('B'),
new Item('C'),
new Item('D'),
new Item('E')
]
})
);
const [item1, item2, item3, item4, item5] = pane.getItems();
pane.itemStack = [item3, item1, item2, item5, item4];
pane.activateItem(item4);
expect(pane.getActiveItem()).toBe(item4);
pane.activateNextRecentlyUsedItem();
expect(pane.getActiveItem()).toBe(item5);
pane.activateNextRecentlyUsedItem();
expect(pane.getActiveItem()).toBe(item2);
pane.activatePreviousRecentlyUsedItem();
expect(pane.getActiveItem()).toBe(item5);
pane.activatePreviousRecentlyUsedItem();
expect(pane.getActiveItem()).toBe(item4);
pane.activatePreviousRecentlyUsedItem();
expect(pane.getActiveItem()).toBe(item3);
pane.activatePreviousRecentlyUsedItem();
expect(pane.getActiveItem()).toBe(item1);
pane.activateNextRecentlyUsedItem();
expect(pane.getActiveItem()).toBe(item3);
pane.activateNextRecentlyUsedItem();
expect(pane.getActiveItem()).toBe(item4);
pane.activateNextRecentlyUsedItem();
pane.moveActiveItemToTopOfStack();
expect(pane.getActiveItem()).toBe(item5);
expect(pane.itemStack[4]).toBe(item5);
});
});
describe('::activateNextItem() and ::activatePreviousItem()', () => {
it('sets the active item to the next/previous item, looping around at either end', () => {
const pane = new Pane(
paneParams({ items: [new Item('A'), new Item('B'), new Item('C')] })
);
const [item1, item2, item3] = pane.getItems();
expect(pane.getActiveItem()).toBe(item1);
pane.activatePreviousItem();
expect(pane.getActiveItem()).toBe(item3);
pane.activatePreviousItem();
expect(pane.getActiveItem()).toBe(item2);
pane.activateNextItem();
expect(pane.getActiveItem()).toBe(item3);
pane.activateNextItem();
expect(pane.getActiveItem()).toBe(item1);
});
});
describe('::activateLastItem()', () => {
it('sets the active item to the last item', () => {
const pane = new Pane(
paneParams({ items: [new Item('A'), new Item('B'), new Item('C')] })
);
const [item1, , item3] = pane.getItems();
expect(pane.getActiveItem()).toBe(item1);
pane.activateLastItem();
expect(pane.getActiveItem()).toBe(item3);
});
});
describe('::moveItemRight() and ::moveItemLeft()', () => {
it('moves the active item to the right and left, without looping around at either end', () => {
const pane = new Pane(
paneParams({ items: [new Item('A'), new Item('B'), new Item('C')] })
);
const [item1, item2, item3] = pane.getItems();
pane.activateItemAtIndex(0);
expect(pane.getActiveItem()).toBe(item1);
pane.moveItemLeft();
expect(pane.getItems()).toEqual([item1, item2, item3]);
pane.moveItemRight();
expect(pane.getItems()).toEqual([item2, item1, item3]);
pane.moveItemLeft();
expect(pane.getItems()).toEqual([item1, item2, item3]);
pane.activateItemAtIndex(2);
expect(pane.getActiveItem()).toBe(item3);
pane.moveItemRight();
expect(pane.getItems()).toEqual([item1, item2, item3]);
});
});
describe('::activateItemAtIndex(index)', () => {
it('activates the item at the given index', () => {
const pane = new Pane(
paneParams({ items: [new Item('A'), new Item('B'), new Item('C')] })
);
const [item1, item2, item3] = pane.getItems();
pane.activateItemAtIndex(2);
expect(pane.getActiveItem()).toBe(item3);
pane.activateItemAtIndex(1);
expect(pane.getActiveItem()).toBe(item2);
pane.activateItemAtIndex(0);
expect(pane.getActiveItem()).toBe(item1);
// Doesn't fail with out-of-bounds indices
pane.activateItemAtIndex(100);
expect(pane.getActiveItem()).toBe(item1);
pane.activateItemAtIndex(-1);
expect(pane.getActiveItem()).toBe(item1);
});
});
describe('::destroyItem(item)', () => {
let pane, item1, item2, item3;
beforeEach(() => {
pane = new Pane(
paneParams({ items: [new Item('A'), new Item('B'), new Item('C')] })
);
[item1, item2, item3] = pane.getItems();
});
it('removes the item from the items list and destroys it', () => {
expect(pane.getActiveItem()).toBe(item1);
pane.destroyItem(item2);
expect(pane.getItems().includes(item2)).toBe(false);
expect(item2.isDestroyed()).toBe(true);
expect(pane.getActiveItem()).toBe(item1);
pane.destroyItem(item1);
expect(pane.getItems().includes(item1)).toBe(false);
expect(item1.isDestroyed()).toBe(true);
});
it('removes the item from the itemStack', () => {
pane.itemStack = [item2, item3, item1];
pane.activateItem(item1);
expect(pane.getActiveItem()).toBe(item1);
pane.destroyItem(item3);
expect(pane.itemStack).toEqual([item2, item1]);
expect(pane.getActiveItem()).toBe(item1);
pane.destroyItem(item1);
expect(pane.itemStack).toEqual([item2]);
expect(pane.getActiveItem()).toBe(item2);
pane.destroyItem(item2);
expect(pane.itemStack).toEqual([]);
expect(pane.getActiveItem()).toBeUndefined();
});
it('invokes ::onWillDestroyItem() and PaneContainer::onWillDestroyPaneItem observers before destroying the item', async () => {
jasmine.useRealClock();
pane.container = new PaneContainer({ config: atom.config, confirm });
const events = [];
pane.onWillDestroyItem(async event => {
expect(item2.isDestroyed()).toBe(false);
await timeoutPromise(50);
expect(item2.isDestroyed()).toBe(false);
events.push(['will-destroy-item', event]);
});
pane.container.onWillDestroyPaneItem(async event => {
expect(item2.isDestroyed()).toBe(false);
await timeoutPromise(50);
expect(item2.isDestroyed()).toBe(false);
events.push(['will-destroy-pane-item', event]);
});
await pane.destroyItem(item2);
expect(item2.isDestroyed()).toBe(true);
expect(events).toEqual([
['will-destroy-item', { item: item2, index: 1 }],
['will-destroy-pane-item', { item: item2, index: 1, pane }]
]);
});
it('invokes ::onWillRemoveItem() observers', () => {
const events = [];
pane.onWillRemoveItem(event => events.push(event));
pane.destroyItem(item2);
expect(events).toEqual([
{ item: item2, index: 1, moved: false, destroyed: true }
]);
});
it('invokes ::onDidRemoveItem() observers', () => {
const events = [];
pane.onDidRemoveItem(event => events.push(event));
pane.destroyItem(item2);
expect(events).toEqual([
{ item: item2, index: 1, moved: false, destroyed: true }
]);
});
describe('when the destroyed item is the active item and is the first item', () => {
it('activates the next item', () => {
expect(pane.getActiveItem()).toBe(item1);
pane.destroyItem(item1);
expect(pane.getActiveItem()).toBe(item2);
});
});
describe('when the destroyed item is the active item and is not the first item', () => {
beforeEach(() => pane.activateItem(item2));
it('activates the previous item', () => {
expect(pane.getActiveItem()).toBe(item2);
pane.destroyItem(item2);
expect(pane.getActiveItem()).toBe(item1);
});
});
describe('if the item is modified', () => {
let itemURI = null;
beforeEach(() => {
item1.shouldPromptToSave = () => true;
item1.save = jasmine.createSpy('save');
item1.saveAs = jasmine.createSpy('saveAs');
item1.getURI = () => itemURI;
});
describe('if the [Save] option is selected', () => {
describe('when the item has a uri', () => {
it('saves the item before destroying it', async () => {
itemURI = 'test';
confirm.andCallFake((options, callback) => callback(0));
const success = await pane.destroyItem(item1);
expect(item1.save).toHaveBeenCalled();
expect(pane.getItems().includes(item1)).toBe(false);
expect(item1.isDestroyed()).toBe(true);
expect(success).toBe(true);
});
});
describe('when the item has no uri', () => {
it('presents a save-as dialog, then saves the item with the given uri before removing and destroying it', async () => {
jasmine.useRealClock();
itemURI = null;
showSaveDialog.andCallFake((options, callback) =>
callback('/selected/path')
);
confirm.andCallFake((options, callback) => callback(0));
const success = await pane.destroyItem(item1);
expect(showSaveDialog.mostRecentCall.args[0]).toEqual({});
await conditionPromise(() => item1.saveAs.callCount === 1);
expect(item1.saveAs).toHaveBeenCalledWith('/selected/path');
expect(pane.getItems().includes(item1)).toBe(false);
expect(item1.isDestroyed()).toBe(true);
expect(success).toBe(true);
});
});
});
describe("if the [Don't Save] option is selected", () => {
it('removes and destroys the item without saving it', async () => {
confirm.andCallFake((options, callback) => callback(2));
const success = await pane.destroyItem(item1);
expect(item1.save).not.toHaveBeenCalled();
expect(pane.getItems().includes(item1)).toBe(false);
expect(item1.isDestroyed()).toBe(true);
expect(success).toBe(true);
});
});
describe('if the [Cancel] option is selected', () => {
it('does not save, remove, or destroy the item', async () => {
confirm.andCallFake((options, callback) => callback(1));
const success = await pane.destroyItem(item1);
expect(item1.save).not.toHaveBeenCalled();
expect(pane.getItems().includes(item1)).toBe(true);
expect(item1.isDestroyed()).toBe(false);
expect(success).toBe(false);
});
});
describe('when force=true', () => {
it('destroys the item immediately', async () => {
const success = await pane.destroyItem(item1, true);
expect(item1.save).not.toHaveBeenCalled();
expect(pane.getItems().includes(item1)).toBe(false);
expect(item1.isDestroyed()).toBe(true);
expect(success).toBe(true);
});
});
});
describe('when the last item is destroyed', () => {
describe("when the 'core.destroyEmptyPanes' config option is false (the default)", () => {
it('does not destroy the pane, but leaves it in place with empty items', () => {
expect(atom.config.get('core.destroyEmptyPanes')).toBe(false);
for (let item of pane.getItems()) {
pane.destroyItem(item);
}
expect(pane.isDestroyed()).toBe(false);
expect(pane.getActiveItem()).toBeUndefined();
expect(() => pane.saveActiveItem()).not.toThrow();
expect(() => pane.saveActiveItemAs()).not.toThrow();
});
});
describe("when the 'core.destroyEmptyPanes' config option is true", () => {
it('destroys the pane', () => {
atom.config.set('core.destroyEmptyPanes', true);
for (let item of pane.getItems()) {
pane.destroyItem(item);
}
expect(pane.isDestroyed()).toBe(true);
});
});
});
describe('when passed a permanent dock item', () => {
it("doesn't destroy the item", async () => {
spyOn(item1, 'isPermanentDockItem').andReturn(true);
const success = await pane.destroyItem(item1);
expect(pane.getItems().includes(item1)).toBe(true);
expect(item1.isDestroyed()).toBe(false);
expect(success).toBe(false);
});
it('destroy the item if force=true', async () => {
spyOn(item1, 'isPermanentDockItem').andReturn(true);
const success = await pane.destroyItem(item1, true);
expect(pane.getItems().includes(item1)).toBe(false);
expect(item1.isDestroyed()).toBe(true);
expect(success).toBe(true);
});
});
});
describe('::destroyActiveItem()', () => {
it('destroys the active item', () => {
const pane = new Pane(
paneParams({ items: [new Item('A'), new Item('B')] })
);
const activeItem = pane.getActiveItem();
pane.destroyActiveItem();
expect(activeItem.isDestroyed()).toBe(true);
expect(pane.getItems().includes(activeItem)).toBe(false);
});
it('does not throw an exception if there are no more items', () => {
const pane = new Pane(paneParams());
pane.destroyActiveItem();
});
});
describe('::destroyItems()', () => {
it('destroys all items', async () => {
const pane = new Pane(
paneParams({ items: [new Item('A'), new Item('B'), new Item('C')] })
);
const [item1, item2, item3] = pane.getItems();
await pane.destroyItems();
expect(item1.isDestroyed()).toBe(true);
expect(item2.isDestroyed()).toBe(true);
expect(item3.isDestroyed()).toBe(true);
expect(pane.getItems()).toEqual([]);
});
});
describe('::observeItems()', () => {
it('invokes the observer with all current and future items', () => {
const pane = new Pane(paneParams({ items: [new Item(), new Item()] }));
const [item1, item2] = pane.getItems();
const observed = [];
pane.observeItems(item => observed.push(item));
const item3 = new Item();
pane.addItem(item3);
expect(observed).toEqual([item1, item2, item3]);
});
});
describe('when an item emits a destroyed event', () => {
it('removes it from the list of items', () => {
const pane = new Pane(
paneParams({ items: [new Item('A'), new Item('B'), new Item('C')] })
);
const [item1, , item3] = pane.getItems();
pane.itemAtIndex(1).destroy();
expect(pane.getItems()).toEqual([item1, item3]);
});
});
describe('::destroyInactiveItems()', () => {
it('destroys all items but the active item', () => {
const pane = new Pane(
paneParams({ items: [new Item('A'), new Item('B'), new Item('C')] })
);
const [, item2] = pane.getItems();
pane.activateItem(item2);
pane.destroyInactiveItems();
expect(pane.getItems()).toEqual([item2]);
});
});
describe('::saveActiveItem()', () => {
let pane;
beforeEach(() => {
pane = new Pane(paneParams({ items: [new Item('A')] }));
showSaveDialog.andCallFake((options, callback) =>
callback('/selected/path')
);
});
describe('when the active item has a uri', () => {
beforeEach(() => {
pane.getActiveItem().uri = 'test';
});
describe('when the active item has a save method', () => {
it('saves the current item', () => {
pane.getActiveItem().save = jasmine.createSpy('save');
pane.saveActiveItem();
expect(pane.getActiveItem().save).toHaveBeenCalled();
});
});
describe('when the current item has no save method', () => {
it('does nothing', () => {
expect(pane.getActiveItem().save).toBeUndefined();
pane.saveActiveItem();
});
});
});
describe('when the current item has no uri', () => {
describe('when the current item has a saveAs method', () => {
it('opens a save dialog and saves the current item as the selected path', async () => {
pane.getActiveItem().saveAs = jasmine.createSpy('saveAs');
await pane.saveActiveItem();
expect(showSaveDialog.mostRecentCall.args[0]).toEqual({});
expect(pane.getActiveItem().saveAs).toHaveBeenCalledWith(
'/selected/path'
);
});
});
describe('when the current item has no saveAs method', () => {
it('does nothing', async () => {
expect(pane.getActiveItem().saveAs).toBeUndefined();
await pane.saveActiveItem();
expect(showSaveDialog).not.toHaveBeenCalled();
});
});
it('does nothing if the user cancels choosing a path', async () => {
pane.getActiveItem().saveAs = jasmine.createSpy('saveAs');
showSaveDialog.andCallFake((options, callback) => callback(undefined));
await pane.saveActiveItem();
expect(pane.getActiveItem().saveAs).not.toHaveBeenCalled();
});
});
describe("when the item's saveAs rejects with a well-known IO error", () => {
it('creates a notification', () => {
pane.getActiveItem().saveAs = () => {
const error = new Error("EACCES, permission denied '/foo'");
error.path = '/foo';
error.code = 'EACCES';
return Promise.reject(error);
};
waitsFor(done => {
const subscription = atom.notifications.onDidAddNotification(function(
notification
) {
expect(notification.getType()).toBe('warning');
expect(notification.getMessage()).toContain('Permission denied');
expect(notification.getMessage()).toContain('/foo');
subscription.dispose();
done();
});
pane.saveActiveItem();
});
});
});
describe("when the item's saveAs throws a well-known IO error", () => {
it('creates a notification', () => {
pane.getActiveItem().saveAs = () => {
const error = new Error("EACCES, permission denied '/foo'");
error.path = '/foo';
error.code = 'EACCES';
throw error;
};
waitsFor(done => {
const subscription = atom.notifications.onDidAddNotification(function(
notification
) {
expect(notification.getType()).toBe('warning');
expect(notification.getMessage()).toContain('Permission denied');
expect(notification.getMessage()).toContain('/foo');
subscription.dispose();
done();
});
pane.saveActiveItem();
});
});
});
});
describe('::saveActiveItemAs()', () => {
let pane = null;
beforeEach(() => {
pane = new Pane(paneParams({ items: [new Item('A')] }));
showSaveDialog.andCallFake((options, callback) =>
callback('/selected/path')
);
});
describe('when the current item has a saveAs method', () => {
it('opens the save dialog and calls saveAs on the item with the selected path', async () => {
jasmine.useRealClock();
pane.getActiveItem().path = __filename;
pane.getActiveItem().saveAs = jasmine.createSpy('saveAs');
pane.saveActiveItemAs();
expect(showSaveDialog.mostRecentCall.args[0]).toEqual({
defaultPath: __filename
});
await conditionPromise(
() => pane.getActiveItem().saveAs.callCount === 1
);
expect(pane.getActiveItem().saveAs).toHaveBeenCalledWith(
'/selected/path'
);
});
});
describe('when the current item does not have a saveAs method', () => {
it('does nothing', () => {
expect(pane.getActiveItem().saveAs).toBeUndefined();
pane.saveActiveItemAs();
expect(showSaveDialog).not.toHaveBeenCalled();
});
});
describe("when the item's saveAs method throws a well-known IO error", () => {
it('creates a notification', () => {
pane.getActiveItem().saveAs = () => {
const error = new Error("EACCES, permission denied '/foo'");
error.path = '/foo';
error.code = 'EACCES';
return Promise.reject(error);
};
waitsFor(done => {
const subscription = atom.notifications.onDidAddNotification(function(
notification
) {
expect(notification.getType()).toBe('warning');
expect(notification.getMessage()).toContain('Permission denied');
expect(notification.getMessage()).toContain('/foo');
subscription.dispose();
done();
});
pane.saveActiveItemAs();
});
});
});
});