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 120
/
Copy pathtabs-spec.coffee
1191 lines (940 loc) · 49 KB
/
tabs-spec.coffee
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
BrowserWindow = null
{$, View} = require 'atom-space-pen-views'
_ = require 'underscore-plus'
path = require 'path'
temp = require 'temp'
TabBarView = require '../lib/tab-bar-view'
TabView = require '../lib/tab-view'
{triggerMouseDownEvent, buildDragEvents, buildWheelEvent, buildWheelPlusShiftEvent} = require "./event-helpers"
describe "Tabs package main", ->
workspaceElement = null
beforeEach ->
workspaceElement = atom.views.getView(atom.workspace)
waitsForPromise ->
atom.workspace.open('sample.js')
waitsForPromise ->
atom.packages.activatePackage("tabs")
describe ".activate()", ->
it "appends a tab bar all existing and new panes", ->
expect(workspaceElement.querySelectorAll('.pane').length).toBe 1
expect(workspaceElement.querySelectorAll('.pane > .tab-bar').length).toBe 1
pane = atom.workspace.getActivePane()
pane.splitRight()
expect(workspaceElement.querySelectorAll('.pane').length).toBe 2
expect(workspaceElement.querySelectorAll('.pane > .tab-bar').length).toBe 2
describe ".deactivate()", ->
it "removes all tab bar views and stops adding them to new panes", ->
pane = atom.workspace.getActivePane()
pane.splitRight()
expect(workspaceElement.querySelectorAll('.pane').length).toBe 2
expect(workspaceElement.querySelectorAll('.pane > .tab-bar').length).toBe 2
atom.packages.deactivatePackage('tabs')
expect(workspaceElement.querySelectorAll('.pane').length).toBe 2
expect(workspaceElement.querySelectorAll('.pane > .tab-bar').length).toBe 0
pane.splitRight()
expect(workspaceElement.querySelectorAll('.pane').length).toBe 3
expect(workspaceElement.querySelectorAll('.pane > .tab-bar').length).toBe 0
it "serializes preview tab state", ->
atom.config.set('tabs.usePreviewTabs', true)
waitsForPromise ->
atom.workspace.open('sample.txt')
runs ->
expect(workspaceElement.querySelectorAll('.tab.preview-tab .title').length).toBe 1
expect(workspaceElement.querySelector('.tab.preview-tab .title')?.textContent).toBe 'sample.txt'
atom.packages.deactivatePackage('tabs')
expect(workspaceElement.querySelectorAll('.tab.preview-tab .title').length).toBe 0
waitsForPromise ->
atom.packages.activatePackage('tabs')
runs ->
expect(workspaceElement.querySelectorAll('.tab.preview-tab .title').length).toBe 1
expect(workspaceElement.querySelector('.tab.preview-tab .title')?.textContent).toBe 'sample.txt'
describe "TabBarView", ->
[deserializerDisposable, item1, item2, editor1, pane, tabBar] = []
class TestView extends View
@deserialize: ({title, longTitle, iconName}) -> new TestView(title, longTitle, iconName)
@content: (title) -> @div title
initialize: (@title, @longTitle, @iconName) ->
getTitle: -> @title
getLongTitle: -> @longTitle
getIconName: -> @iconName
serialize: -> {deserializer: 'TestView', @title, @longTitle, @iconName}
onDidChangeTitle: (callback) ->
@titleCallbacks ?= []
@titleCallbacks.push(callback)
dispose: => _.remove(@titleCallbacks, callback)
emitTitleChanged: ->
callback() for callback in @titleCallbacks ? []
onDidChangeIcon: (callback) ->
@iconCallbacks ?= []
@iconCallbacks.push(callback)
dispose: => _.remove(@iconCallbacks, callback)
emitIconChanged: ->
callback() for callback in @iconCallbacks ? []
onDidChangeModified: -> # to suppress deprecation warning
dispose: ->
beforeEach ->
deserializerDisposable = atom.deserializers.add(TestView)
item1 = new TestView('Item 1', undefined, "squirrel")
item2 = new TestView('Item 2')
waitsForPromise ->
atom.workspace.open('sample.js')
runs ->
editor1 = atom.workspace.getActiveTextEditor()
pane = atom.workspace.getActivePane()
pane.addItem(item1, 0)
pane.addItem(item2, 2)
pane.activateItem(item2)
tabBar = new TabBarView(pane)
afterEach ->
deserializerDisposable.dispose()
describe ".initialize(pane)", ->
it "creates a tab for each item on the tab bar's parent pane", ->
expect(pane.getItems().length).toBe 3
expect(tabBar.find('.tab').length).toBe 3
expect(tabBar.find('.tab:eq(0) .title').text()).toBe item1.getTitle()
expect(tabBar.find('.tab:eq(0) .title')).not.toHaveAttr('data-name')
expect(tabBar.find('.tab:eq(0) .title')).not.toHaveAttr('data-path')
expect(tabBar.find('.tab:eq(0)')).toHaveAttr('data-type', 'TestView')
expect(tabBar.find('.tab:eq(1) .title').text()).toBe editor1.getTitle()
expect(tabBar.find('.tab:eq(1) .title')).toHaveAttr('data-name', path.basename(editor1.getPath()))
expect(tabBar.find('.tab:eq(1) .title')).toHaveAttr('data-path', editor1.getPath())
expect(tabBar.find('.tab:eq(1)')).toHaveAttr('data-type', 'TextEditor')
expect(tabBar.find('.tab:eq(2) .title').text()).toBe item2.getTitle()
expect(tabBar.find('.tab:eq(2) .title')).not.toHaveAttr('data-name')
expect(tabBar.find('.tab:eq(2) .title')).not.toHaveAttr('data-path')
expect(tabBar.find('.tab:eq(2)')).toHaveAttr('data-type', 'TestView')
it "highlights the tab for the active pane item", ->
expect(tabBar.find('.tab:eq(2)')).toHaveClass 'active'
it "emits a warning when ::onDid... functions are not valid Disposables", ->
class BadView extends View
@content: (title) -> @div title
getTitle: -> "Anything"
onDidChangeTitle: ->
onDidChangeIcon: ->
onDidChangeModified: ->
onDidSave: ->
warnings = []
spyOn(console, "warn").andCallFake (message, object) ->
warnings.push({message, object})
badItem = new BadView('Item 3')
pane.addItem(badItem)
expect(warnings[0].message).toContain("onDidChangeTitle")
expect(warnings[0].object).toBe(badItem)
expect(warnings[1].message).toContain("onDidChangeIcon")
expect(warnings[1].object).toBe(badItem)
expect(warnings[2].message).toContain("onDidChangeModified")
expect(warnings[2].object).toBe(badItem)
expect(warnings[3].message).toContain("onDidSave")
expect(warnings[3].object).toBe(badItem)
describe "when the active pane item changes", ->
it "highlights the tab for the new active pane item", ->
pane.activateItem(item1)
expect(tabBar.find('.active').length).toBe 1
expect(tabBar.find('.tab:eq(0)')).toHaveClass 'active'
pane.activateItem(item2)
expect(tabBar.find('.active').length).toBe 1
expect(tabBar.find('.tab:eq(2)')).toHaveClass 'active'
describe "when a new item is added to the pane", ->
it "adds a tab for the new item at the same index as the item in the pane", ->
pane.activateItem(item1)
item3 = new TestView('Item 3')
pane.activateItem(item3)
expect(tabBar.find('.tab').length).toBe 4
expect($(tabBar.tabAtIndex(1)).find('.title')).toHaveText 'Item 3'
it "adds the 'modified' class to the new tab if the item is initially modified", ->
editor2 = null
waitsForPromise ->
atom.project.open('sample.txt').then (o) -> editor2 = o
runs ->
editor2.insertText('x')
pane.activateItem(editor2)
expect(tabBar.tabForItem(editor2)).toHaveClass 'modified'
describe "when an item is removed from the pane", ->
it "removes the item's tab from the tab bar", ->
pane.destroyItem(item2)
expect(tabBar.getTabs().length).toBe 2
expect(tabBar.find('.tab:contains(Item 2)')).not.toExist()
it "updates the titles of the remaining tabs", ->
expect(tabBar.tabForItem(item2)).toHaveText 'Item 2'
item2.longTitle = '2'
item2a = new TestView('Item 2')
item2a.longTitle = '2a'
pane.activateItem(item2a)
expect(tabBar.tabForItem(item2)).toHaveText '2'
expect(tabBar.tabForItem(item2a)).toHaveText '2a'
pane.destroyItem(item2a)
expect(tabBar.tabForItem(item2)).toHaveText 'Item 2'
describe "when a tab is clicked", ->
it "shows the associated item on the pane and focuses the pane", ->
spyOn(pane, 'activate')
event = triggerMouseDownEvent(tabBar.tabAtIndex(0), which: 1)
expect(pane.getActiveItem()).toBe pane.getItems()[0]
expect(event.preventDefault).not.toHaveBeenCalled() # allows dragging
event = triggerMouseDownEvent(tabBar.tabAtIndex(2), which: 1)
expect(pane.getActiveItem()).toBe pane.getItems()[2]
expect(event.preventDefault).not.toHaveBeenCalled() # allows dragging
# Pane activation is delayed because focus is stolen by the tab bar
# immediately afterward unless propagation of the mousedown event is
# stopped. But stopping propagation of the mousedown event prevents the
# dragstart event from occurring.
waits(1)
runs -> expect(pane.activate.callCount).toBe 2
it "closes the tab when middle clicked", ->
event = triggerMouseDownEvent(tabBar.tabForItem(editor1), which: 2)
expect(pane.getItems().length).toBe 2
expect(pane.getItems().indexOf(editor1)).toBe -1
expect(editor1.destroyed).toBeTruthy()
expect(tabBar.getTabs().length).toBe 2
expect(tabBar.find('.tab:contains(sample.js)')).not.toExist()
expect(event.preventDefault).toHaveBeenCalled()
it "doesn't switch tab when right (or ctrl-left) clicked", ->
spyOn(pane, 'activate')
event = triggerMouseDownEvent(tabBar.tabAtIndex(0), which: 3)
expect(pane.getActiveItem()).not.toBe pane.getItems()[0]
expect(event.preventDefault).toHaveBeenCalled()
event = triggerMouseDownEvent(tabBar.tabAtIndex(0), which: 1, ctrlKey: true)
expect(pane.getActiveItem()).not.toBe pane.getItems()[0]
expect(event.preventDefault).toHaveBeenCalled()
expect(pane.activate).not.toHaveBeenCalled()
describe "when a tab's close icon is clicked", ->
it "destroys the tab's item on the pane", ->
$(tabBar.tabForItem(editor1)).find('.close-icon').click()
expect(pane.getItems().length).toBe 2
expect(pane.getItems().indexOf(editor1)).toBe -1
expect(editor1.destroyed).toBeTruthy()
expect(tabBar.getTabs().length).toBe 2
expect(tabBar.find('.tab:contains(sample.js)')).not.toExist()
describe "when a tab item's title changes", ->
it "updates the title of the item's tab", ->
editor1.buffer.setPath('/this/is-a/test.txt')
expect(tabBar.tabForItem(editor1)).toHaveText 'test.txt'
describe "when two tabs have the same title", ->
it "displays the long title on the tab if it's available from the item", ->
item1.title = "Old Man"
item1.longTitle = "Grumpy Old Man"
item1.emitTitleChanged()
item2.title = "Old Man"
item2.longTitle = "Jolly Old Man"
item2.emitTitleChanged()
expect(tabBar.tabForItem(item1)).toHaveText "Grumpy Old Man"
expect(tabBar.tabForItem(item2)).toHaveText "Jolly Old Man"
item2.longTitle = undefined
item2.emitTitleChanged()
expect(tabBar.tabForItem(item1)).toHaveText "Grumpy Old Man"
expect(tabBar.tabForItem(item2)).toHaveText "Old Man"
describe "when an item has an icon defined", ->
it "displays the icon on the tab", ->
expect(tabBar.find('.tab:eq(0) .title')).toHaveClass "icon"
expect(tabBar.find('.tab:eq(0) .title')).toHaveClass "icon-squirrel"
it "hides the icon from the tab if the icon is removed", ->
item1.getIconName = null
item1.emitIconChanged()
expect(tabBar.find('.tab:eq(0) .title')).not.toHaveClass "icon"
expect(tabBar.find('.tab:eq(0) .title')).not.toHaveClass "icon-squirrel"
it "updates the icon on the tab if the icon is changed", ->
item1.getIconName = -> "zap"
item1.emitIconChanged()
expect(tabBar.find('.tab:eq(0) .title')).toHaveClass "icon"
expect(tabBar.find('.tab:eq(0) .title')).toHaveClass "icon-zap"
describe "when showIcon is set to true in package settings", ->
beforeEach ->
spyOn(tabBar.tabForItem(item1), 'updateIconVisibility').andCallThrough()
atom.config.set("tabs.showIcons", true)
waitsFor ->
tabBar.tabForItem(item1).updateIconVisibility.callCount > 0
runs ->
tabBar.tabForItem(item1).updateIconVisibility.reset()
it "doesn't hide the icon", ->
expect(tabBar.find('.tab:eq(0) .title')).not.toHaveClass "hide-icon"
it "hides the icon from the tab when showIcon is changed to false", ->
atom.config.set("tabs.showIcons", false)
waitsFor ->
tabBar.tabForItem(item1).updateIconVisibility.callCount > 0
runs ->
expect(tabBar.find('.tab:eq(0) .title')).toHaveClass "hide-icon"
describe "when showIcon is set to false in package settings", ->
beforeEach ->
spyOn(tabBar.tabForItem(item1), 'updateIconVisibility').andCallThrough()
atom.config.set("tabs.showIcons", false)
waitsFor ->
tabBar.tabForItem(item1).updateIconVisibility.callCount > 0
runs ->
tabBar.tabForItem(item1).updateIconVisibility.reset()
it "hides the icon", ->
expect(tabBar.find('.tab:eq(0) .title')).toHaveClass "hide-icon"
it "shows the icon on the tab when showIcon is changed to true", ->
atom.config.set("tabs.showIcons", true)
waitsFor ->
tabBar.tabForItem(item1).updateIconVisibility.callCount > 0
expect(tabBar.find('.tab:eq(0) .title')).not.toHaveClass "hide-icon"
describe "when the item doesn't have an icon defined", ->
it "doesn't display an icon on the tab", ->
expect(tabBar.find('.tab:eq(2) .title')).not.toHaveClass "icon"
expect(tabBar.find('.tab:eq(2) .title')).not.toHaveClass "icon-squirrel"
it "shows the icon on the tab if an icon is defined", ->
item2.getIconName = -> "squirrel"
item2.emitIconChanged()
expect(tabBar.find('.tab:eq(2) .title')).toHaveClass "icon"
expect(tabBar.find('.tab:eq(2) .title')).toHaveClass "icon-squirrel"
describe "when a tab item's modified status changes", ->
it "adds or removes the 'modified' class to the tab based on the status", ->
tab = tabBar.tabForItem(editor1)
expect(editor1.isModified()).toBeFalsy()
expect(tab).not.toHaveClass 'modified'
editor1.insertText('x')
advanceClock(editor1.buffer.stoppedChangingDelay)
expect(editor1.isModified()).toBeTruthy()
expect(tab).toHaveClass 'modified'
editor1.undo()
advanceClock(editor1.buffer.stoppedChangingDelay)
expect(editor1.isModified()).toBeFalsy()
expect(tab).not.toHaveClass 'modified'
describe "when a pane item moves to a new index", ->
it "updates the order of the tabs to match the new item order", ->
expect(tabBar.getTabs().map (tab) -> tab.textContent).toEqual ["Item 1", "sample.js", "Item 2"]
pane.moveItem(item2, 1)
expect(tabBar.getTabs().map (tab) -> tab.textContent).toEqual ["Item 1", "Item 2", "sample.js"]
pane.moveItem(editor1, 0)
expect(tabBar.getTabs().map (tab) -> tab.textContent).toEqual ["sample.js", "Item 1", "Item 2"]
pane.moveItem(item1, 2)
expect(tabBar.getTabs().map (tab) -> tab.textContent).toEqual ["sample.js", "Item 2", "Item 1"]
describe "context menu commands", ->
beforeEach ->
paneElement = atom.views.getView(pane)
paneElement.insertBefore(tabBar.element, paneElement.firstChild)
describe "when tabs:close-tab is fired", ->
it "closes the active tab", ->
triggerMouseDownEvent(tabBar.tabForItem(item2), which: 3)
atom.commands.dispatch(tabBar.element, 'tabs:close-tab')
expect(pane.getItems().length).toBe 2
expect(pane.getItems().indexOf(item2)).toBe -1
expect(tabBar.getTabs().length).toBe 2
expect(tabBar.find('.tab:contains(Item 2)')).not.toExist()
describe "when tabs:close-other-tabs is fired", ->
it "closes all other tabs except the active tab", ->
triggerMouseDownEvent(tabBar.tabForItem(item2), which: 3)
atom.commands.dispatch(tabBar.element, 'tabs:close-other-tabs')
expect(pane.getItems().length).toBe 1
expect(tabBar.getTabs().length).toBe 1
expect(tabBar.find('.tab:contains(sample.js)')).not.toExist()
expect(tabBar.find('.tab:contains(Item 2)')).toExist()
describe "when tabs:close-tabs-to-right is fired", ->
it "closes only the tabs to the right of the active tab", ->
pane.activateItem(editor1)
triggerMouseDownEvent(tabBar.tabForItem(editor1), which: 3)
atom.commands.dispatch(tabBar.element, 'tabs:close-tabs-to-right')
expect(pane.getItems().length).toBe 2
expect(tabBar.getTabs().length).toBe 2
expect(tabBar.find('.tab:contains(Item 2)')).not.toExist()
expect(tabBar.find('.tab:contains(Item 1)')).toExist()
describe "when tabs:close-all-tabs is fired", ->
it "closes all the tabs", ->
expect(pane.getItems().length).toBeGreaterThan 0
atom.commands.dispatch(tabBar.element, 'tabs:close-all-tabs')
expect(pane.getItems().length).toBe 0
describe "when tabs:close-saved-tabs is fired", ->
it "closes all the saved tabs", ->
item1.isModified = -> true
atom.commands.dispatch(tabBar.element, 'tabs:close-saved-tabs')
expect(pane.getItems().length).toBe 1
expect(pane.getItems()[0]).toBe item1
describe "when tabs:open-in-new-window is fired", ->
it "calls the open new window command with the selected tab", ->
spyOn(tabBar, "onOpenInNewWindow").andCallThrough()
spyOn(tabBar, "openTabInNewWindow").andCallThrough()
spyOn(atom.workspace, "open").andCallThrough()
triggerMouseDownEvent(tabBar.tabForItem(editor1), which: 3)
atom.commands.dispatch(tabBar.element, 'tabs:open-in-new-window')
waitsFor ->
atom.workspace.open()
runs ->
expect(tabBar.onOpenInNewWindow).toHaveBeenCalled()
expect(tabBar.openTabInNewWindow).toHaveBeenCalled()
expect(atom.workspace.open).toHaveBeenCalled()
describe "when tabs:split-up is fired", ->
it "splits the selected tab up", ->
triggerMouseDownEvent(tabBar.tabForItem(item2), which: 3)
expect(atom.workspace.getPanes().length).toBe 1
atom.commands.dispatch(tabBar.element, 'tabs:split-up')
expect(atom.workspace.getPanes().length).toBe 2
expect(atom.workspace.getPanes()[1]).toBe pane
expect(atom.workspace.getPanes()[0].getItems()[0].getTitle()).toBe item2.getTitle()
describe "when tabs:split-down is fired", ->
it "splits the selected tab down", ->
triggerMouseDownEvent(tabBar.tabForItem(item2), which: 3)
expect(atom.workspace.getPanes().length).toBe 1
atom.commands.dispatch(tabBar.element, 'tabs:split-down')
expect(atom.workspace.getPanes().length).toBe 2
expect(atom.workspace.getPanes()[0]).toBe pane
expect(atom.workspace.getPanes()[1].getItems()[0].getTitle()).toBe item2.getTitle()
describe "when tabs:split-left is fired", ->
it "splits the selected tab to the left", ->
triggerMouseDownEvent(tabBar.tabForItem(item2), which: 3)
expect(atom.workspace.getPanes().length).toBe 1
atom.commands.dispatch(tabBar.element, 'tabs:split-left')
expect(atom.workspace.getPanes().length).toBe 2
expect(atom.workspace.getPanes()[1]).toBe pane
expect(atom.workspace.getPanes()[0].getItems()[0].getTitle()).toBe item2.getTitle()
describe "when tabs:split-right is fired", ->
it "splits the selected tab to the right", ->
triggerMouseDownEvent(tabBar.tabForItem(item2), which: 3)
expect(atom.workspace.getPanes().length).toBe 1
atom.commands.dispatch(tabBar.element, 'tabs:split-right')
expect(atom.workspace.getPanes().length).toBe 2
expect(atom.workspace.getPanes()[0]).toBe pane
expect(atom.workspace.getPanes()[1].getItems()[0].getTitle()).toBe item2.getTitle()
describe "command palette commands", ->
paneElement = null
beforeEach ->
paneElement = atom.views.getView(pane)
describe "when tabs:close-tab is fired", ->
it "closes the active tab", ->
atom.commands.dispatch(paneElement, 'tabs:close-tab')
expect(pane.getItems().length).toBe 2
expect(pane.getItems().indexOf(item2)).toBe -1
expect(tabBar.getTabs().length).toBe 2
expect(tabBar.find('.tab:contains(Item 2)')).not.toExist()
it "does nothing if no tabs are open", ->
atom.commands.dispatch(paneElement, 'tabs:close-tab')
atom.commands.dispatch(paneElement, 'tabs:close-tab')
atom.commands.dispatch(paneElement, 'tabs:close-tab')
expect(pane.getItems().length).toBe 0
expect(tabBar.getTabs().length).toBe 0
describe "when tabs:close-other-tabs is fired", ->
it "closes all other tabs except the active tab", ->
atom.commands.dispatch(paneElement, 'tabs:close-other-tabs')
expect(pane.getItems().length).toBe 1
expect(tabBar.getTabs().length).toBe 1
expect(tabBar.find('.tab:contains(sample.js)')).not.toExist()
expect(tabBar.find('.tab:contains(Item 2)')).toExist()
describe "when tabs:close-tabs-to-right is fired", ->
it "closes only the tabs to the right of the active tab", ->
pane.activateItem(editor1)
atom.commands.dispatch(paneElement, 'tabs:close-tabs-to-right')
expect(pane.getItems().length).toBe 2
expect(tabBar.getTabs().length).toBe 2
expect(tabBar.find('.tab:contains(Item 2)')).not.toExist()
expect(tabBar.find('.tab:contains(Item 1)')).toExist()
describe "when tabs:close-all-tabs is fired", ->
it "closes all the tabs", ->
expect(pane.getItems().length).toBeGreaterThan 0
atom.commands.dispatch(paneElement, 'tabs:close-all-tabs')
expect(pane.getItems().length).toBe 0
describe "when tabs:close-saved-tabs is fired", ->
it "closes all the saved tabs", ->
item1.isModified = -> true
atom.commands.dispatch(paneElement, 'tabs:close-saved-tabs')
expect(pane.getItems().length).toBe 1
expect(pane.getItems()[0]).toBe item1
describe "dragging and dropping tabs", ->
describe "when getting dragged tab's URI", ->
it "getItemURI returns the tab location information", ->
itemWithNoURI = tabBar.getItemURI(item1)
expect(itemWithNoURI).not.toBeDefined()
itemWithURI = tabBar.getItemURI(editor1)
expect(itemWithURI).toBe editor1.getURI()
describe "when a tab is dragged within the same pane", ->
describe "when it is dropped on tab that's later in the list", ->
it "moves the tab and its item, shows the tab's item, and focuses the pane", ->
expect(tabBar.getTabs().map (tab) -> tab.textContent).toEqual ["Item 1", "sample.js", "Item 2"]
expect(pane.getItems()).toEqual [item1, editor1, item2]
expect(pane.getActiveItem()).toBe item2
spyOn(pane, 'activate')
tabToDrag = tabBar.tabAtIndex(0)
spyOn(tabToDrag, 'destroyTooltip')
spyOn(tabToDrag, 'updateTooltip')
[dragStartEvent, dropEvent] = buildDragEvents(tabToDrag, tabBar.tabAtIndex(1))
tabBar.onDragStart(dragStartEvent)
expect(tabToDrag.destroyTooltip).toHaveBeenCalled()
expect(tabToDrag.updateTooltip).not.toHaveBeenCalled()
tabBar.onDrop(dropEvent)
expect(tabToDrag.updateTooltip).toHaveBeenCalled()
expect(tabBar.getTabs().map (tab) -> tab.textContent).toEqual ["sample.js", "Item 1", "Item 2"]
expect(pane.getItems()).toEqual [editor1, item1, item2]
expect(pane.getActiveItem()).toBe item1
expect(pane.activate).toHaveBeenCalled()
describe "when it is dropped on a tab that's earlier in the list", ->
it "moves the tab and its item, shows the tab's item, and focuses the pane", ->
expect(tabBar.getTabs().map (tab) -> tab.textContent).toEqual ["Item 1", "sample.js", "Item 2"]
expect(pane.getItems()).toEqual [item1, editor1, item2]
expect(pane.getActiveItem()).toBe item2
spyOn(pane, 'activate')
[dragStartEvent, dropEvent] = buildDragEvents(tabBar.tabAtIndex(2), tabBar.tabAtIndex(0))
tabBar.onDragStart(dragStartEvent)
tabBar.onDrop(dropEvent)
expect(tabBar.getTabs().map (tab) -> tab.textContent).toEqual ["Item 1", "Item 2", "sample.js"]
expect(pane.getItems()).toEqual [item1, item2, editor1]
expect(pane.getActiveItem()).toBe item2
expect(pane.activate).toHaveBeenCalled()
describe "when it is dropped on itself", ->
it "doesn't move the tab or item, but does make it the active item and focuses the pane", ->
expect(tabBar.getTabs().map (tab) -> tab.textContent).toEqual ["Item 1", "sample.js", "Item 2"]
expect(pane.getItems()).toEqual [item1, editor1, item2]
expect(pane.getActiveItem()).toBe item2
spyOn(pane, 'activate')
[dragStartEvent, dropEvent] = buildDragEvents(tabBar.tabAtIndex(0), tabBar.tabAtIndex(0))
tabBar.onDragStart(dragStartEvent)
tabBar.onDrop(dropEvent)
expect(tabBar.getTabs().map (tab) -> tab.textContent).toEqual ["Item 1", "sample.js", "Item 2"]
expect(pane.getItems()).toEqual [item1, editor1, item2]
expect(pane.getActiveItem()).toBe item1
expect(pane.activate).toHaveBeenCalled()
describe "when it is dropped on the tab bar", ->
it "moves the tab and its item to the end", ->
expect(tabBar.getTabs().map (tab) -> tab.textContent).toEqual ["Item 1", "sample.js", "Item 2"]
expect(pane.getItems()).toEqual [item1, editor1, item2]
expect(pane.getActiveItem()).toBe item2
spyOn(pane, 'activate')
[dragStartEvent, dropEvent] = buildDragEvents(tabBar.tabAtIndex(0), tabBar)
tabBar.onDragStart(dragStartEvent)
tabBar.onDrop(dropEvent)
expect(tabBar.getTabs().map (tab) -> tab.textContent).toEqual ["sample.js", "Item 2", "Item 1"]
expect(pane.getItems()).toEqual [editor1, item2, item1]
describe "when a tab is dragged to a different pane", ->
[pane2, tabBar2, item2b] = []
beforeEach ->
pane2 = pane.splitRight(copyActiveItem: true)
[item2b] = pane2.getItems()
tabBar2 = new TabBarView(pane2)
it "removes the tab and item from their original pane and moves them to the target pane", ->
expect(tabBar.getTabs().map (tab) -> tab.textContent).toEqual ["Item 1", "sample.js", "Item 2"]
expect(pane.getItems()).toEqual [item1, editor1, item2]
expect(pane.getActiveItem()).toBe item2
expect(tabBar2.getTabs().map (tab) -> tab.textContent).toEqual ["Item 2"]
expect(pane2.getItems()).toEqual [item2b]
expect(pane2.activeItem).toBe item2b
spyOn(pane2, 'activate')
[dragStartEvent, dropEvent] = buildDragEvents(tabBar.tabAtIndex(0), tabBar2.tabAtIndex(0))
tabBar.onDragStart(dragStartEvent)
tabBar2.onDrop(dropEvent)
expect(tabBar.getTabs().map (tab) -> tab.textContent).toEqual ["sample.js", "Item 2"]
expect(pane.getItems()).toEqual [editor1, item2]
expect(pane.getActiveItem()).toBe item2
expect(tabBar2.getTabs().map (tab) -> tab.textContent).toEqual ["Item 2", "Item 1"]
expect(pane2.getItems()).toEqual [item2b, item1]
expect(pane2.activeItem).toBe item1
expect(pane2.activate).toHaveBeenCalled()
describe "when the tab is dragged to an empty pane", ->
it "removes the tab and item from their original pane and moves them to the target pane", ->
pane2.destroyItems()
expect(tabBar.getTabs().map (tab) -> tab.textContent).toEqual ["Item 1", "sample.js", "Item 2"]
expect(pane.getItems()).toEqual [item1, editor1, item2]
expect(pane.getActiveItem()).toBe item2
expect(tabBar2.getTabs().map (tab) -> tab.textContent).toEqual []
expect(pane2.getItems()).toEqual []
expect(pane2.activeItem).toBeUndefined()
spyOn(pane2, 'activate')
[dragStartEvent, dropEvent] = buildDragEvents(tabBar.tabAtIndex(0), tabBar2)
tabBar.onDragStart(dragStartEvent)
tabBar2.onDrop(dropEvent)
expect(tabBar.getTabs().map (tab) -> tab.textContent).toEqual ["sample.js", "Item 2"]
expect(pane.getItems()).toEqual [editor1, item2]
expect(pane.getActiveItem()).toBe item2
expect(tabBar2.getTabs().map (tab) -> tab.textContent).toEqual ["Item 1"]
expect(pane2.getItems()).toEqual [item1]
expect(pane2.activeItem).toBe item1
expect(pane2.activate).toHaveBeenCalled()
describe "when a non-tab is dragged to pane", ->
it "has no effect", ->
expect(tabBar.getTabs().map (tab) -> tab.textContent).toEqual ["Item 1", "sample.js", "Item 2"]
expect(pane.getItems()).toEqual [item1, editor1, item2]
expect(pane.getActiveItem()).toBe item2
spyOn(pane, 'activate')
[dragStartEvent, dropEvent] = buildDragEvents(tabBar.tabAtIndex(0), tabBar.tabAtIndex(0))
tabBar.onDrop(dropEvent)
expect(tabBar.getTabs().map (tab) -> tab.textContent).toEqual ["Item 1", "sample.js", "Item 2"]
expect(pane.getItems()).toEqual [item1, editor1, item2]
expect(pane.getActiveItem()).toBe item2
expect(pane.activate).not.toHaveBeenCalled()
describe "when a tab is dragged out of application", ->
it "should carry the file's information", ->
[dragStartEvent, dropEvent] = buildDragEvents(tabBar.tabAtIndex(1), tabBar.tabAtIndex(1))
tabBar.onDragStart(dragStartEvent)
expect(dragStartEvent.originalEvent.dataTransfer.getData("text/plain")).toEqual editor1.getPath()
if process.platform is 'darwin'
expect(dragStartEvent.originalEvent.dataTransfer.getData("text/uri-list")).toEqual "file://#{editor1.getPath()}"
it "should open a new window if the target doesn't handle the file information", ->
[dragStartEvent, dropEvent] = buildDragEvents(tabBar.tabAtIndex(1), tabBar.tabAtIndex(0))
spyOn(tabBar, "openTabInNewWindow").andCallThrough()
spyOn(atom.workspace, "open").andCallThrough()
tabBar.onDragStart(dragStartEvent)
dropEvent.originalEvent.dataTransfer.dropEffect = "none"
dropEvent.originalEvent.screenX = 10
dropEvent.originalEvent.screenY = 20
tabBar.onDragEnd(dropEvent)
waitsFor ->
atom.workspace.open()
runs ->
expect(tabBar.openTabInNewWindow).toHaveBeenCalledWith(dropEvent.target, 10, 20)
expect(atom.workspace.open).toHaveBeenCalled()
describe "when a tab is dragged to another Atom window", ->
it "closes the tab in the first window and opens the tab in the second window", ->
[dragStartEvent, dropEvent] = buildDragEvents(tabBar.tabAtIndex(1), tabBar.tabAtIndex(0))
tabBar.onDragStart(dragStartEvent)
tabBar.onDropOnOtherWindow(pane.id, 1)
expect(pane.getItems()).toEqual [item1, item2]
expect(pane.getActiveItem()).toBe item2
dropEvent.originalEvent.dataTransfer.setData('from-window-id', tabBar.getWindowId() + 1)
spyOn(tabBar, 'moveItemBetweenPanes').andCallThrough()
tabBar.onDrop(dropEvent)
waitsFor ->
tabBar.moveItemBetweenPanes.callCount > 0
runs ->
editor = atom.workspace.getActiveTextEditor()
expect(editor.getPath()).toBe editor1.getPath()
expect(pane.getItems()).toEqual [item1, editor, item2]
it "transfers the text of the editor when it is modified", ->
editor1.setText('I came from another window')
[dragStartEvent, dropEvent] = buildDragEvents(tabBar.tabAtIndex(1), tabBar.tabAtIndex(0))
tabBar.onDragStart(dragStartEvent)
tabBar.onDropOnOtherWindow(pane.id, 1)
dropEvent.originalEvent.dataTransfer.setData('from-window-id', tabBar.getWindowId() + 1)
spyOn(tabBar, 'moveItemBetweenPanes').andCallThrough()
tabBar.onDrop(dropEvent)
waitsFor ->
tabBar.moveItemBetweenPanes.callCount > 0
runs ->
expect(atom.workspace.getActiveTextEditor().getText()).toBe 'I came from another window'
it "allows untitled editors to be moved between windows", ->
editor1.getBuffer().setPath(null)
editor1.setText('I have no path')
[dragStartEvent, dropEvent] = buildDragEvents(tabBar.tabAtIndex(1), tabBar.tabAtIndex(0))
tabBar.onDragStart(dragStartEvent)
tabBar.onDropOnOtherWindow(pane.id, 1)
dropEvent.originalEvent.dataTransfer.setData('from-window-id', tabBar.getWindowId() + 1)
spyOn(tabBar, 'moveItemBetweenPanes').andCallThrough()
tabBar.onDrop(dropEvent)
waitsFor ->
tabBar.moveItemBetweenPanes.callCount > 0
runs ->
expect(atom.workspace.getActiveTextEditor().getText()).toBe 'I have no path'
expect(atom.workspace.getActiveTextEditor().getPath()).toBeUndefined()
describe "when the tab bar is double clicked", ->
it "opens a new empty editor", ->
newFileHandler = jasmine.createSpy('newFileHandler')
atom.commands.add(tabBar.element, 'application:new-file', newFileHandler)
$(tabBar.getTabs()[0]).dblclick()
expect(newFileHandler.callCount).toBe 0
tabBar.dblclick()
expect(newFileHandler.callCount).toBe 1
describe "when the mouse wheel is used on the tab bar", ->
describe "when tabScrolling is true in package settings", ->
beforeEach ->
atom.config.set("tabs.tabScrolling", true)
atom.config.set("tabs.tabScrollingThreshold", 120)
describe "when the mouse wheel scrolls up", ->
it "changes the active tab to the previous tab", ->
expect(pane.getActiveItem()).toBe item2
tabBar.trigger(buildWheelEvent(120))
expect(pane.getActiveItem()).toBe editor1
it "changes the active tab to the previous tab only after the wheelDelta crosses the threshold", ->
expect(pane.getActiveItem()).toBe item2
tabBar.trigger(buildWheelEvent(50))
expect(pane.getActiveItem()).toBe item2
tabBar.trigger(buildWheelEvent(50))
expect(pane.getActiveItem()).toBe item2
tabBar.trigger(buildWheelEvent(50))
expect(pane.getActiveItem()).toBe editor1
describe "when the mouse wheel scrolls down", ->
it "changes the active tab to the previous tab", ->
expect(pane.getActiveItem()).toBe item2
tabBar.trigger(buildWheelEvent(-120))
expect(pane.getActiveItem()).toBe item1
describe "when the mouse wheel scrolls up and shift key is pressed", ->
it "does not change the active tab", ->
expect(pane.getActiveItem()).toBe item2
tabBar.trigger(buildWheelPlusShiftEvent(120))
expect(pane.getActiveItem()).toBe item2
describe "when the mouse wheel scrolls down and shift key is pressed", ->
it "does not change the active tab", ->
expect(pane.getActiveItem()).toBe item2
tabBar.trigger(buildWheelPlusShiftEvent(-120))
expect(pane.getActiveItem()).toBe item2
describe "when tabScrolling is false in package settings", ->
beforeEach ->
atom.config.set("tabs.tabScrolling", false)
describe "when the mouse wheel scrolls up one unit", ->
it "does not change the active tab", ->
expect(pane.getActiveItem()).toBe item2
tabBar.trigger(buildWheelEvent(120))
expect(pane.getActiveItem()).toBe item2
describe "when the mouse wheel scrolls down one unit", ->
it "does not change the active tab", ->
expect(pane.getActiveItem()).toBe item2
tabBar.trigger(buildWheelEvent(-120))
expect(pane.getActiveItem()).toBe item2
describe "when alwaysShowTabBar is true in package settings", ->
beforeEach ->
atom.config.set("tabs.alwaysShowTabBar", true)
describe "when 2 tabs are open", ->
it "shows the tab bar", ->
expect(pane.getItems().length).toBe 3
expect(tabBar.element).not.toHaveClass 'hidden'
describe "when 1 tab is open", ->
it "shows the tab bar", ->
expect(pane.getItems().length).toBe 3
pane.destroyItem(item1)
pane.destroyItem(item2)
expect(pane.getItems().length).toBe 1
expect(tabBar.element).not.toHaveClass 'hidden'
describe "when alwaysShowTabBar is false in package settings", ->
beforeEach ->
atom.config.set("tabs.alwaysShowTabBar", false)
describe "when 2 tabs are open", ->
it "shows the tab bar", ->
expect(pane.getItems().length).toBe 3
expect(tabBar.element).not.toHaveClass 'hidden'
describe "when 1 tab is open", ->
it "hides the tab bar", ->
expect(pane.getItems().length).toBe 3
pane.destroyItem(item1)
pane.destroyItem(item2)
expect(pane.getItems().length).toBe 1
expect(tabBar.element).toHaveClass 'hidden'
describe "when usePreviewTabs is true in package settings", ->
beforeEach ->
atom.config.set("tabs.usePreviewTabs", true)
pane.destroyItems()
describe "when opening a new tab", ->
it "adds tab with class 'temp'", ->
editor1 = null
waitsForPromise ->
atom.project.open('sample.txt').then (o) -> editor1 = o
runs ->
pane.activateItem(editor1)
expect(tabBar.find('.tab .temp').length).toBe 1
expect(tabBar.find('.tab:eq(0) .title')).toHaveClass 'temp'
describe "when tabs:keep-preview-tab is trigger on the pane", ->
it "removes the 'temp' class", ->
editor1 = null
waitsForPromise ->
atom.project.open('sample.txt').then (o) -> editor1 = o
runs ->
pane.activateItem(editor1)
expect(tabBar.find('.tab .temp').length).toBe 1
atom.commands.dispatch(atom.views.getView(atom.workspace.getActivePane()), 'tabs:keep-preview-tab')
expect(tabBar.find('.tab .temp').length).toBe 0
describe "when there is a temp tab already", ->
it "it will replace an existing temporary tab", ->
editor1 = null
editor2 = null
waitsForPromise ->
atom.project.open('sample.txt').then (o) ->
editor1 = o
pane.activateItem(editor1)
atom.project.open('sample2.txt').then (o) ->
editor2 = o
pane.activateItem(editor2)
runs ->
expect(editor1.isDestroyed()).toBe true
expect(editor2.isDestroyed()).toBe false
expect(tabBar.tabForItem(editor1)).not.toExist()
expect($(tabBar.tabForItem(editor2)).find('.title')).toHaveClass 'temp'
it 'makes the tab permanent when double clicking the tab', ->
editor2 = null
waitsForPromise ->
atom.project.open('sample.txt').then (o) -> editor2 = o
runs ->
pane.activateItem(editor2)
dbclickEvt = document.createEvent 'MouseEvents'
dbclickEvt.initEvent 'dblclick'
tabBar.tabForItem(editor2).dispatchEvent dbclickEvt
expect($(tabBar.tabForItem(editor2)).find('.title')).not.toHaveClass 'temp'
describe 'when opening views that do not have file paths', ->
editor2 = null
settingsView = null
beforeEach ->
waitsForPromise ->
atom.project.open('sample.txt').then (o) ->
editor2 = o
pane.activateItem(editor2)
waitsForPromise ->
atom.packages.activatePackage('settings-view').then ->
atom.workspace.open('atom://config').then (o) ->
settingsView = o
pane.activateItem(settingsView)
it 'creates a permanent tab', ->
expect(tabBar.tabForItem(settingsView)).toExist()
expect($(tabBar.tabForItem(settingsView)).find('.title')).not.toHaveClass 'temp'
it 'keeps an existing temp tab', ->
expect(tabBar.tabForItem(editor2)).toExist()
expect($(tabBar.tabForItem(editor2)).find('.title')).toHaveClass 'temp'
describe 'when editing a file', ->
it 'makes the tab permanent', ->
editor1 = null
waitsForPromise ->
atom.workspace.open('sample.txt').then (o) ->
editor1 = o
pane.activateItem(editor1)
editor1.insertText('x')
advanceClock(editor1.buffer.stoppedChangingDelay)
runs ->
expect($(tabBar.tabForItem(editor1)).find('.title')).not.toHaveClass 'temp'
describe 'when saving a file', ->
it 'makes the tab permanent', ->
editor1 = null
waitsForPromise ->
atom.workspace.open(path.join(temp.mkdirSync('tabs-'), 'sample.txt')).then (o) ->
editor1 = o
pane.activateItem(editor1)
editor1.save()
runs ->
expect($(tabBar.tabForItem(editor1)).find('.title')).not.toHaveClass 'temp'
describe 'when switching from a preview tab to a permanent tab', ->
it "keeps the preview tab open", ->
atom.config.set("tabs.usePreviewTabs", false)
editor1 = null
editor2 = null
waitsForPromise ->
atom.workspace.open('sample.txt').then (o) ->
editor1 = o
pane.activateItem(editor1)
runs ->