This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 365
/
tree-view-package-spec.coffee
4626 lines (3589 loc) · 205 KB
/
tree-view-package-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
_ = require 'underscore-plus'
fs = require 'fs-plus'
path = require 'path'
temp = require('@atom/temp').track()
os = require 'os'
{remote, shell} = require 'electron'
Directory = require '../lib/directory'
eventHelpers = require "./event-helpers"
waitForPackageActivation = ->
waitsForPromise ->
atom.packages.activatePackage('tree-view')
waitsForPromise ->
atom.packages.getActivePackage('tree-view').mainModule.treeViewOpenPromise
waitForWorkspaceOpenEvent = (causeFileToOpen) ->
waitsFor (done) ->
disposable = atom.workspace.onDidOpen ({item}) ->
disposable.dispose()
done()
causeFileToOpen()
setupPaneFiles = ->
rootDirPath = fs.absolute(temp.mkdirSync('tree-view'))
dirPath = path.join(rootDirPath, "test-dir")
fs.makeTreeSync(dirPath)
[1..9].forEach (index) ->
filePath = path.join(dirPath, "test-file-#{index}.txt")
fs.writeFileSync(filePath, "#{index}. Some text.")
return dirPath
getPaneFileName = (index) -> "test-file-#{index}.txt"
describe "TreeView", ->
[treeView, path1, path2, root1, root2, sampleJs, sampleTxt, workspaceElement] = []
selectEntry = (pathToSelect) ->
treeView.selectEntryForPath atom.project.getDirectories()[0].resolve pathToSelect
beforeEach ->
expect(atom.workspace.getLeftDock().getActivePaneItem()).toBeUndefined()
expect(atom.config.get('core.allowPendingPaneItems')).toBeTruthy()
fixturesPath = atom.project.getPaths()[0]
path1 = path.join(fixturesPath, "root-dir1")
path2 = path.join(fixturesPath, "root-dir2")
atom.project.setPaths([path1, path2])
workspaceElement = atom.views.getView(atom.workspace)
waitForPackageActivation()
runs ->
moduleInstance = atom.packages.getActivePackage('tree-view').mainModule.getTreeViewInstance()
treeView = atom.workspace.getLeftDock().getActivePaneItem()
files = treeView.element.querySelectorAll('.file')
root1 = treeView.roots[0]
root2 = treeView.roots[1]
sampleJs = files[0]
sampleTxt = files[1]
expect(root1.directory.watchSubscription).toBeTruthy()
afterEach ->
if treeViewOpenPromise = atom.packages.getActivePackage('tree-view')?.mainModule.treeViewOpenPromise
waitsForPromise -> treeViewOpenPromise
describe "on package activation", ->
it "renders the root directories of the project and their contents alphabetically with subdirectories first, in a collapsed state", ->
expect(root1.querySelector('.header .disclosure-arrow')).not.toHaveClass('expanded')
expect(root1.querySelector('.header .name')).toHaveText('root-dir1')
rootEntries = root1.querySelectorAll('.entries li')
subdir0 = rootEntries[0]
expect(subdir0).not.toHaveClass('expanded')
expect(subdir0.querySelector('.name')).toHaveText('dir1')
subdir2 = rootEntries[1]
expect(subdir2).not.toHaveClass('expanded')
expect(subdir2.querySelector('.name')).toHaveText('dir2')
expect(subdir0.querySelector('[data-name="dir1"]')).toExist()
expect(subdir2.querySelector('[data-name="dir2"]')).toExist()
file1 = root1.querySelector('.file [data-name="tree-view.js"]')
expect(file1).toExist()
expect(file1).toHaveText('tree-view.js')
file2 = root1.querySelector('.file [data-name="tree-view.txt"]')
expect(file2).toExist()
expect(file2).toHaveText('tree-view.txt')
it "selects the root folder", ->
expect(treeView.selectedEntry()).toEqual(treeView.roots[0])
it "makes the root folder non-draggable", ->
expect(treeView.roots[0].hasAttribute('draggable')).toBe(false)
describe "when the project has no path", ->
beforeEach ->
atom.project.setPaths([])
waitsForPromise ->
Promise.resolve(atom.packages.deactivatePackage("tree-view")) # Wrapped for both async and non-async versions of Atom
runs ->
expect(atom.workspace.getLeftDock().getActivePaneItem()).toBeUndefined()
waitsForPromise -> atom.packages.activatePackage("tree-view")
runs ->
treeView = atom.packages.getActivePackage("tree-view").mainModule.getTreeViewInstance()
it "does not attach to the workspace or create a root node when initialized", ->
expect(treeView.element.parentElement).toBeFalsy()
expect(treeView.roots).toHaveLength(0)
it "does not attach to the workspace or create a root node when attach() is called", ->
expect(atom.workspace.getLeftDock().getActivePaneItem()).toBeUndefined()
it "does not throw an exception when files are opened", ->
filePath = path.join(os.tmpdir(), 'non-project-file.txt')
fs.writeFileSync(filePath, 'test')
waitsForPromise ->
atom.workspace.open(filePath)
it "does not reveal the active file", ->
filePath = path.join(os.tmpdir(), 'non-project-file.txt')
fs.writeFileSync(filePath, 'test')
waitsForPromise ->
atom.workspace.open(filePath)
waitsForPromise ->
treeView.revealActiveFile()
runs ->
expect(treeView.element.parentElement).toBeFalsy()
expect(treeView.roots).toHaveLength(0)
describe "when the project is assigned a path because a new buffer is saved", ->
it "creates a root directory view and attaches to the workspace", ->
projectPath = temp.mkdirSync('atom-project')
waitsForPromise ->
atom.workspace.open()
waitsFor (done) ->
atom.workspace.getCenter().getActivePaneItem().saveAs(path.join(projectPath, 'test.txt'))
atom.workspace.onDidOpen(done)
runs ->
treeView = atom.workspace.getLeftDock().getActivePaneItem()
expect(treeView.roots).toHaveLength(1)
expect(fs.absolute(treeView.roots[0].getPath())).toBe fs.absolute(projectPath)
describe "when the root view is opened to a file path", ->
it "does not show the dock on activation", ->
waitsForPromise ->
Promise.resolve(atom.packages.deactivatePackage("tree-view")) # Wrapped for both async and non-async versions of Atom
runs ->
atom.packages.packageStates = {}
atom.workspace.getLeftDock().hide()
expect(atom.workspace.getLeftDock().isVisible()).toBe(false)
waitsForPromise ->
atom.workspace.open('tree-view.js')
runs ->
expect(atom.workspace.getLeftDock().isVisible()).toBe(false)
waitForPackageActivation()
runs ->
expect(atom.workspace.getLeftDock().isVisible()).toBe(false)
atom.project.addPath(path.join(__dirname, 'fixtures'))
waitsFor -> atom.workspace.getLeftDock().isVisible()
describe "when the root view is opened to a directory", ->
it "attaches to the workspace", ->
waitsForPromise -> atom.packages.activatePackage('tree-view')
runs ->
treeView = atom.packages.getActivePackage("tree-view").mainModule.getTreeViewInstance()
expect(treeView.element.parentElement).toBeTruthy()
expect(treeView.roots).toHaveLength(2)
describe "when the project is a .git folder", ->
it "does not create the tree view", ->
dotGit = path.join(temp.mkdirSync('repo'), '.git')
fs.makeTreeSync(dotGit)
atom.project.setPaths([dotGit])
waitsForPromise ->
Promise.resolve(atom.packages.deactivatePackage("tree-view")) # Wrapped for both async and non-async versions of Atom
runs ->
atom.packages.packageStates = {}
waitsForPromise ->
atom.packages.activatePackage('tree-view')
runs ->
{treeView} = atom.packages.getActivePackage("tree-view").mainModule
expect(treeView).toBeFalsy()
describe "on package deactivation", ->
it "destroys the Tree View", ->
spyOn(treeView, 'destroy').andCallThrough()
waitsForPromise ->
atom.packages.deactivatePackage('tree-view')
runs ->
expect(treeView.destroy).toHaveBeenCalled()
it "waits for the Tree View to open before destroying it", ->
jasmine.useRealClock()
resolveOpenPromise = null
opened = false
waitsForPromise ->
# First deactivate the package so that we can start from scratch
atom.packages.deactivatePackage('tree-view')
runs ->
spyOn(atom.workspace, 'open').andReturn(new Promise (resolve) -> resolveOpenPromise = resolve)
waitsForPromise ->
atom.packages.activatePackage('tree-view')
runs ->
atom.packages.deactivatePackage('tree-view').then -> expect(opened).toBe(true)
# Wait what should be a sufficient amount of time for Tree View
# to deactivate if it wasn't waiting for the open promise
window.setTimeout ->
opened = true
resolveOpenPromise()
, 1000
describe "when tree-view:toggle is triggered on the root view", ->
beforeEach ->
jasmine.attachToDOM(workspaceElement)
describe "when the tree view is visible", ->
beforeEach ->
expect(atom.workspace.getLeftDock().isVisible()).toBe(true)
it "hides the tree view", ->
workspaceElement.focus()
waitsForPromise -> treeView.toggle()
runs ->
expect(atom.workspace.getLeftDock().isVisible()).toBe(false)
describe "when the tree view is hidden", ->
it "shows and focuses the tree view", ->
atom.workspace.getLeftDock().hide()
expect(atom.workspace.getLeftDock().isVisible()).toBe(false)
waitsForPromise -> treeView.toggle()
runs ->
expect(atom.workspace.getLeftDock().isVisible()).toBe(true)
expect(treeView.element).toHaveFocus()
describe "when tree-view:toggle-focus is triggered on the root view", ->
beforeEach ->
jasmine.attachToDOM(workspaceElement)
describe "when the tree view is hidden", ->
it "shows and focuses the tree view", ->
atom.workspace.getLeftDock().hide()
waitsForPromise -> treeView.toggleFocus()
runs ->
expect(atom.workspace.getLeftDock().isVisible()).toBe(true)
expect(treeView.element).toHaveFocus()
describe "when the tree view is shown", ->
it "focuses the tree view", ->
waitsForPromise ->
atom.workspace.open() # When we call focus below, we want an editor to become focused
runs ->
workspaceElement.focus()
expect(atom.workspace.getLeftDock().isVisible()).toBe(true)
expect(atom.workspace.getLeftDock().isVisible()).toBe(true)
waitsForPromise -> treeView.toggleFocus()
runs ->
expect(atom.workspace.getLeftDock().isVisible()).toBe(true)
expect(treeView.element).toHaveFocus()
describe "when the tree view is focused", ->
it "unfocuses the tree view", ->
waitsForPromise ->
atom.workspace.open() # When we call focus below, we want an editor to become focused
runs ->
treeView.focus()
expect(atom.workspace.getLeftDock().isVisible()).toBe(true)
treeView.toggleFocus()
expect(atom.workspace.getLeftDock().isVisible()).toBe(true)
expect(treeView.element).not.toHaveFocus()
describe "when the tree-view is destroyed", ->
it "can correctly re-create the tree-view", ->
treeView.useSyncFS = true
treeView = atom.workspace.getLeftDock().getActivePaneItem()
treeViewHTML = treeView.element.outerHTML
treeView.roots[0].collapse()
treeView.destroy()
waitForWorkspaceOpenEvent ->
atom.commands.dispatch(atom.views.getView(atom.workspace), 'tree-view:toggle')
runs ->
treeView2 = atom.workspace.getLeftDock().getActivePaneItem()
treeView2.roots[0].expand()
expect(treeView2.element.outerHTML).toBe(treeViewHTML)
describe "when tree-view:reveal-active-file is triggered", ->
beforeEach ->
atom.workspace.getLeftDock().hide()
spyOn(treeView, 'focus')
describe "if the current file has a path", ->
describe "if the tree-view.focusOnReveal config option is true", ->
it "shows and focuses the tree view and selects the file", ->
atom.config.set "tree-view.focusOnReveal", true
waitsForPromise ->
atom.workspace.open(path.join(path1, 'dir1', 'file1'))
waitsForPromise ->
treeView.revealActiveFile()
runs ->
expect(treeView.element.parentElement).toBeTruthy()
expect(treeView.focus).toHaveBeenCalled()
waitsForPromise ->
treeView.focus.reset()
atom.workspace.open(path.join(path2, 'dir3', 'file3'))
waitsForPromise ->
treeView.revealActiveFile()
runs ->
expect(treeView.element.parentElement).toBeTruthy()
expect(treeView.focus).toHaveBeenCalled()
describe "if the tree-view.focusOnReveal config option is false", ->
it "shows the tree view and selects the file, but does not change the focus", ->
atom.config.set "tree-view.focusOnReveal", false
waitsForPromise ->
atom.workspace.open(path.join(path1, 'dir1', 'file1'))
waitsForPromise ->
treeView.revealActiveFile()
runs ->
expect(treeView.element.parentElement).toBeTruthy()
expect(treeView.focus).not.toHaveBeenCalled()
waitsForPromise ->
treeView.focus.reset()
atom.workspace.open(path.join(path2, 'dir3', 'file3'))
waitsForPromise ->
treeView.revealActiveFile()
runs ->
expect(treeView.element.parentElement).toBeTruthy()
expect(treeView.focus).not.toHaveBeenCalled()
describe "if the file is located under collapsed folders", ->
it "expands all the folders and selects the file", ->
waitsForPromise ->
atom.workspace.open(path.join(path1, 'dir1', 'file1'))
runs ->
treeView.selectEntry(root1)
treeView.collapseDirectory(true) # Recursively collapse all directories
waitsForPromise ->
treeView.revealActiveFile()
runs ->
expect(treeView.entryForPath(path1).classList.contains('expanded')).toBe true
expect(treeView.entryForPath(path.join(path1, 'dir1')).classList.contains('expanded')).toBe true
expect(treeView.selectedEntry()).toBeTruthy()
expect(treeView.selectedEntry().getPath()).toBe path.join(path1, 'dir1', 'file1')
describe "if the current file has no path", ->
it "shows and focuses the tree view, but does not attempt to select a specific file", ->
waitsForPromise ->
atom.workspace.open()
runs ->
expect(atom.workspace.getCenter().getActivePaneItem().getPath()).toBeUndefined()
expect(atom.workspace.getLeftDock().isVisible()).toBe(false)
waitsForPromise ->
treeView.revealActiveFile()
runs ->
expect(atom.workspace.getLeftDock().isVisible()).toBe(true)
expect(treeView.focus).toHaveBeenCalled()
describe "if there is no editor open", ->
it "shows and focuses the tree view, but does not attempt to select a specific file", ->
expect(atom.workspace.getCenter().getActivePaneItem()).toBeUndefined()
expect(atom.workspace.getLeftDock().isVisible()).toBe(false)
waitsForPromise ->
treeView.revealActiveFile()
runs ->
expect(atom.workspace.getLeftDock().isVisible()).toBe(true)
expect(treeView.focus).toHaveBeenCalled()
describe 'if there are more items than can be visible in the viewport', ->
[rootDirPath] = []
beforeEach ->
rootDirPath = fs.absolute(temp.mkdirSync('tree-view-root1'))
for i in [1..20]
filepath = path.join(rootDirPath, "file-#{i}.txt")
fs.writeFileSync(filepath, "doesn't matter")
atom.project.setPaths([rootDirPath])
treeView.element.style.height = '100px'
jasmine.attachToDOM(workspaceElement)
it 'scrolls the selected file into the visible view', ->
# Open file at bottom
waitsForPromise -> atom.workspace.open(path.join(rootDirPath, 'file-20.txt'))
waitsForPromise ->
treeView.revealActiveFile()
runs ->
expect(treeView.scrollTop()).toBeGreaterThan 400
entries = treeView.element.querySelectorAll('.entry')
scrollTop = treeView.element.scrollTop
for i in [0...entries.length]
atom.commands.dispatch(treeView.element, 'core:move-up')
expect(treeView.element.scrollTop - scrollTop).toBeLessThan entries[i].clientHeight
scrollTop = treeView.element.scrollTop
# Open file in the middle, should be centered in scroll
waitsForPromise -> atom.workspace.open(path.join(rootDirPath, 'file-10.txt'))
waitsForPromise ->
treeView.revealActiveFile()
runs ->
expect(treeView.scrollTop()).toBeLessThan 400
expect(treeView.scrollTop()).toBeGreaterThan 0
# Open file at top
waitsForPromise -> atom.workspace.open(path.join(rootDirPath, 'file-1.txt'))
waitsForPromise ->
treeView.revealActiveFile()
runs ->
expect(treeView.scrollTop()).toEqual 0
describe "when tree-view:unfocus is triggered on the tree view", ->
it "surrenders focus to the workspace but remains open", ->
waitsForPromise ->
atom.workspace.open() # When we trigger 'tree-view:unfocus' below, we want an editor to become focused
runs ->
jasmine.attachToDOM(workspaceElement)
treeView.focus()
expect(treeView.element).toHaveFocus()
atom.commands.dispatch(treeView.element, 'tree-view:unfocus')
expect(atom.workspace.getLeftDock().isVisible()).toBe(true)
expect(treeView.element).not.toHaveFocus()
expect(atom.workspace.getCenter().getActivePane().isActive()).toBe(true)
describe "copy path commands", ->
[pathToSelect, relativizedPath] = []
beforeEach ->
pathToSelect = path.join(treeView.roots[0].directory.path, 'dir1', 'file1')
relativizedPath = atom.project.relativize(pathToSelect)
spyOn(atom.clipboard, 'write')
describe "when tree-view:copy-full-path is triggered on the tree view", ->
it "copies the selected path to the clipboard", ->
treeView.selectedPath = pathToSelect
atom.commands.dispatch(treeView.element, 'tree-view:copy-full-path')
expect(atom.clipboard.write).toHaveBeenCalledWith(pathToSelect)
describe "when there is no selected path", ->
beforeEach ->
treeView.selectedPath = null
it "does nothing", ->
atom.commands.dispatch(treeView.element, 'tree-view:copy-full-path')
expect(atom.clipboard.write).not.toHaveBeenCalled()
describe "when tree-view:copy-project-path is triggered on the tree view", ->
it "copies the relativized selected path to the clipboard", ->
treeView.selectedPath = pathToSelect
atom.commands.dispatch(treeView.element, 'tree-view:copy-project-path')
expect(atom.clipboard.write).toHaveBeenCalledWith(relativizedPath)
describe "when there is no selected path", ->
beforeEach ->
treeView.selectedPath = null
it "does nothing", ->
atom.commands.dispatch(treeView.element, 'tree-view:copy-project-path')
expect(atom.clipboard.write).not.toHaveBeenCalled()
describe "when a directory's disclosure arrow is clicked", ->
it "expands / collapses the associated directory", ->
subdir = root1.querySelector('.entries > li')
expect(subdir).not.toHaveClass('expanded')
subdir.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
expect(subdir).toHaveClass('expanded')
subdir.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
expect(subdir).not.toHaveClass('expanded')
it "restores the expansion state of descendant directories", ->
child = root1.querySelector('.entries > li')
child.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
grandchild = child.querySelector('.entries > li')
grandchild.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
root1.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
expect(treeView.roots[0]).not.toHaveClass('expanded')
root1.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
# previously expanded descendants remain expanded
expect(root1.querySelectorAll('.entries > li > .entries > li > .entries').length).toBe 1
# collapsed descendants remain collapsed
expect(root1.querySelectorAll('.entries > li')[1].querySelector('.entries')).not.toHaveClass('expanded')
it "when collapsing a directory, removes change subscriptions from the collapsed directory and its descendants", ->
child = root1.querySelector('li')
child.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
grandchild = child.querySelector('li')
grandchild.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
expect(root1.directory.watchSubscription).toBeTruthy()
expect(child.directory.watchSubscription).toBeTruthy()
expect(grandchild.directory.watchSubscription).toBeTruthy()
root1.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
expect(root1.directory.watchSubscription).toBeFalsy()
expect(child.directory.watchSubscription).toBeFalsy()
expect(grandchild.directory.watchSubscription).toBeFalsy()
describe "when mouse down fires on a file or directory", ->
it "selects the entry", ->
dir = root1.querySelector('li')
expect(dir).not.toHaveClass 'selected'
dir.dispatchEvent(new MouseEvent('mousedown', {bubbles: true, detail: 1}))
expect(dir).toHaveClass 'selected'
expect(sampleJs).not.toHaveClass 'selected'
sampleJs.dispatchEvent(new MouseEvent('mousedown', {bubbles: true, detail: 1}))
expect(sampleJs).toHaveClass 'selected'
describe "when the package first activates and there is a file open (regression)", ->
# Note: it is important that this test is not nested inside any other tests
# that generate click events in their `beforeEach` hooks, as this test
# tests incorrect behavior that only manifested itself on the first
# UI interaction after the package was activated.
describe "when the file is permanent", ->
beforeEach ->
waitForWorkspaceOpenEvent ->
atom.workspace.open('tree-view.js')
it "does not throw when the file is double clicked", ->
expect ->
sampleJs.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
sampleJs.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 2}))
.not.toThrow()
waitsFor ->
# Ensure we don't move on to the next test until the promise spawned click event resolves.
# (If it resolves in the middle of the next test we'll pollute that test).
not treeView.currentlyOpening.has(atom.workspace.getCenter().getActivePaneItem().getPath())
describe "when the file is pending", ->
editor = null
beforeEach ->
waitsForPromise ->
atom.workspace.open('tree-view.js', pending: true).then (o) ->
editor = o
it "marks the pending file as permanent", ->
runs ->
expect(atom.workspace.getCenter().getActivePane().getActiveItem()).toBe editor
expect(atom.workspace.getCenter().getActivePane().getPendingItem()).toBe editor
sampleJs.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
sampleJs.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 2}))
waitsFor ->
atom.workspace.getCenter().getActivePane().getPendingItem() is null
describe "when files are clicked", ->
beforeEach ->
jasmine.attachToDOM(workspaceElement)
describe "when a file is single-clicked", ->
describe "when core.allowPendingPaneItems is set to true (default)", ->
activePaneItem = null
beforeEach ->
treeView.focus()
waitForWorkspaceOpenEvent ->
r = sampleJs.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
runs ->
activePaneItem = atom.workspace.getCenter().getActivePaneItem()
it "selects the file and retains focus on tree-view", ->
expect(sampleJs).toHaveClass 'selected'
expect(treeView.element).toHaveFocus()
it "opens the file in a pending state", ->
expect(activePaneItem.getPath()).toBe atom.project.getDirectories()[0].resolve('tree-view.js')
expect(atom.workspace.getCenter().getActivePane().getPendingItem()).toEqual activePaneItem
describe "when core.allowPendingPaneItems is set to false", ->
beforeEach ->
atom.config.set('core.allowPendingPaneItems', false)
spyOn(atom.workspace, 'open')
treeView.focus()
sampleJs.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
it "selects the file and retains focus on tree-view", ->
expect(sampleJs).toHaveClass 'selected'
expect(treeView.element).toHaveFocus()
it "does not open the file", ->
expect(atom.workspace.open).not.toHaveBeenCalled()
describe "when it is immediately opened with `::openSelectedEntry` afterward", ->
it "does not open a duplicate file", ->
# Fixes https://github.com/atom/atom/issues/11391
openedCount = 0
originalOpen = atom.workspace.open.bind(atom.workspace)
spyOn(atom.workspace, 'open').andCallFake (uri, options) ->
originalOpen(uri, options).then -> openedCount++
sampleJs.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
treeView.openSelectedEntry()
waitsFor 'open to be called twice', ->
openedCount is 2
runs ->
expect(atom.workspace.getCenter().getActivePane().getItems().length).toBe 1
describe "when a file is double-clicked", ->
activePaneItem = null
beforeEach ->
treeView.focus()
it "opens the file and focuses it", ->
waitForWorkspaceOpenEvent ->
sampleJs.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
sampleJs.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 2}))
waitsFor "next tick to avoid race condition", (done) ->
setImmediate(done)
runs ->
activePaneItem = atom.workspace.getCenter().getActivePaneItem()
expect(activePaneItem.getPath()).toBe atom.project.getDirectories()[0].resolve('tree-view.js')
expect(atom.views.getView(activePaneItem)).toHaveFocus()
it "does not open a duplicate file", ->
# Fixes https://github.com/atom/atom/issues/11391
openedCount = 0
originalOpen = atom.workspace.open.bind(atom.workspace)
spyOn(atom.workspace, 'open').andCallFake (uri, options) ->
originalOpen(uri, options).then -> openedCount++
sampleJs.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
sampleJs.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 2}))
waitsFor 'open to be called twice', ->
openedCount is 2
runs ->
expect(atom.workspace.getCenter().getActivePane().getItems().length).toBe 1
describe "when a directory is single-clicked", ->
it "is selected", ->
subdir = root1.querySelector('.directory')
subdir.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
expect(subdir).toHaveClass 'selected'
describe "when a directory is double-clicked", ->
it "toggles the directory expansion state and does not change the focus to the editor", ->
jasmine.attachToDOM(workspaceElement)
subdir = null
waitForWorkspaceOpenEvent ->
sampleJs.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
runs ->
treeView.focus()
subdir = root1.querySelector('.directory')
subdir.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
expect(subdir).toHaveClass 'selected'
expect(subdir).toHaveClass 'expanded'
subdir.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 2}))
expect(subdir).toHaveClass 'selected'
expect(subdir).not.toHaveClass 'expanded'
expect(treeView.element).toHaveFocus()
describe "when an directory is alt-clicked", ->
describe "when the directory is collapsed", ->
it "recursively expands the directory", ->
root1.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
treeView.roots[0].collapse()
expect(treeView.roots[0]).not.toHaveClass 'expanded'
root1.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1, altKey: true}))
expect(treeView.roots[0]).toHaveClass 'expanded'
children = root1.querySelectorAll('.directory')
expect(children.length).toBeGreaterThan 0
for child in children
expect(child).toHaveClass 'expanded'
describe "when the directory is expanded", ->
parent = null
children = null
beforeEach ->
parent = root1.querySelectorAll('.entries > .directory')[2]
parent.expand()
children = parent.querySelectorAll('.expanded.directory')
for child in children
child.expand()
it "recursively collapses the directory", ->
parent.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
parent.expand()
expect(parent).toHaveClass 'expanded'
for child in children
child.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
child.expand()
expect(child).toHaveClass 'expanded'
parent.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1, altKey: true}))
expect(parent).not.toHaveClass 'expanded'
for child in children
expect(child).not.toHaveClass 'expanded'
expect(treeView.roots[0]).toHaveClass 'expanded'
describe "when the active item changes on the active pane", ->
describe "when the item has a path", ->
it "selects the entry with that path in the tree view if it is visible", ->
waitForWorkspaceOpenEvent ->
sampleJs.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
waitsForPromise ->
atom.workspace.open(atom.project.getDirectories()[0].resolve('tree-view.txt'))
runs ->
expect(sampleTxt).toHaveClass 'selected'
expect(treeView.element.querySelectorAll('.selected').length).toBe 1
it "selects the path's parent dir if its entry is not visible", ->
waitsForPromise ->
atom.workspace.open(path.join('dir1', 'sub-dir1', 'sub-file1'))
runs ->
dirView = root1.querySelector('.directory')
expect(dirView).toHaveClass 'selected'
describe "when the tree-view.autoReveal config setting is true", ->
beforeEach ->
jasmine.attachToDOM(atom.workspace.getElement())
atom.config.set "tree-view.autoReveal", true
it "selects the active item's entry in the tree view, expanding parent directories if needed", ->
waitsForPromise ->
atom.workspace.open(path.join('dir1', 'sub-dir1', 'sub-file1'))
waitsFor ->
treeView.getSelectedEntries()[0].textContent is 'sub-file1'
runs ->
expect(atom.workspace.getActiveTextEditor().getElement()).toHaveFocus()
describe "when a different editor becomes active", ->
beforeEach ->
jasmine.attachToDOM(workspaceElement)
it "selects the file in that is open in that editor", ->
leftEditorPane = null
waitForWorkspaceOpenEvent ->
sampleJs.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
runs ->
leftEditorPane = atom.workspace.getCenter().getActivePane()
leftEditorPane.splitRight()
waitForWorkspaceOpenEvent ->
sampleTxt.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
runs ->
expect(sampleTxt).toHaveClass('selected')
leftEditorPane.activate()
expect(sampleJs).toHaveClass('selected')
describe "keyboard navigation", ->
afterEach ->
expect(treeView.element.querySelectorAll('.selected').length).toBeLessThan 2
describe "core:move-down", ->
describe "when a collapsed directory is selected", ->
it "skips to the next directory", ->
root1.querySelector('.directory').dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
atom.commands.dispatch(treeView.element, 'core:move-down')
expect(root1.querySelectorAll('.directory')[1]).toHaveClass 'selected'
describe "when an expanded directory is selected", ->
it "selects the first entry of the directory", ->
subdir = root1.querySelectorAll('.directory')[1]
subdir.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
atom.commands.dispatch(treeView.element, 'core:move-down')
expect(subdir.querySelector('.entry')).toHaveClass 'selected'
describe "when the last entry of an expanded directory is selected", ->
it "selects the entry after its parent directory", ->
subdir1 = root1.querySelectorAll('.directory')[1]
subdir1.expand()
waitForWorkspaceOpenEvent ->
entries = subdir1.querySelectorAll('.entry')
entries[entries.length - 1].dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
runs ->
atom.commands.dispatch(treeView.element, 'core:move-down')
expect(root1.querySelectorAll('.directory')[2]).toHaveClass 'selected'
describe "when the last directory of another last directory is selected", ->
[nested, nested2] = []
beforeEach ->
nested = root1.querySelectorAll('.directory')[2]
expect(nested.querySelector('.header').textContent).toContain 'nested'
nested.expand()
entries = nested.querySelectorAll('.entry')
nested2 = entries[entries.length - 1]
nested2.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
nested2.collapse()
describe "when the directory is collapsed", ->
it "selects the entry after its grandparent directory", ->
atom.commands.dispatch(treeView.element, 'core:move-down')
expect(nested.nextSibling).toHaveClass 'selected'
describe "when the directory is expanded", ->
it "selects the entry after its grandparent directory", ->
nested2.expand()
nested2.querySelector('.file').remove() # kill the .gitkeep file, which has to be there but screws the test
atom.commands.dispatch(treeView.element, 'core:move-down')
expect(nested.nextSibling).toHaveClass 'selected'
describe "when the last entry of the last directory is selected", ->
it "does not change the selection", ->
entries = root2.querySelectorAll('.entries .entry')
lastEntry = entries[entries.length - 1]
waitForWorkspaceOpenEvent ->
lastEntry.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
runs ->
atom.commands.dispatch(treeView.element, 'core:move-down')
expect(lastEntry).toHaveClass 'selected'
describe "core:move-up", ->
describe "when there is an expanded directory before the currently selected entry", ->
[directories, lastDir, fileAfterDir] = []
beforeEach ->
directories = root1.querySelectorAll('.directory')
lastDir = directories[directories.length - 1]
fileAfterDir = lastDir.nextSibling
lastDir.expand()
waitForWorkspaceOpenEvent ->
fileAfterDir.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
it "selects the last entry in the expanded directory", ->
atom.commands.dispatch(treeView.element, 'core:move-up')
entries = lastDir.querySelectorAll('.entry')
expect(entries[entries.length - 1]).toHaveClass 'selected'
describe "when the last child of the expanded directory is another expanded directory", ->
it "selects the last entry in the expanded directory", ->
subDir = lastDir.querySelectorAll('.directory')[0]
subDir.expand()
atom.commands.dispatch(treeView.element, 'core:move-up')
entries = subDir.querySelectorAll('.entry')
expect(entries[entries.length - 1]).toHaveClass 'selected'
describe "when the expanded directory has no children", ->
it "selects the expanded directory itself", ->
lastDir.querySelector('.entry').remove() # pretend it's empty
atom.commands.dispatch(treeView.element, 'core:move-up')
expect(lastDir).toHaveClass 'selected'
describe "when there is an entry before the currently selected entry", ->
it "selects the previous entry", ->
entries = root1.querySelectorAll('.entry')
lastEntry = entries[entries.length - 1]
waitForWorkspaceOpenEvent ->
lastEntry.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
runs ->
atom.commands.dispatch(treeView.element, 'core:move-up')
expect(lastEntry.previousSibling).toHaveClass 'selected'
describe "when there is no entry before the currently selected entry, but there is a parent directory", ->
it "selects the parent directory", ->
subdir = root1.querySelector('.directory')
subdir.expand()
subdir.querySelector('.entries .entry').dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
atom.commands.dispatch(treeView.element, 'core:move-up')
expect(subdir).toHaveClass 'selected'
describe "when there is no parent directory or previous entry", ->
it "does not change the selection", ->
root1.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
atom.commands.dispatch(treeView.element, 'core:move-up')
expect(treeView.roots[0]).toHaveClass 'selected'
describe "when the tree view is empty", ->
it "does nothing", ->
atom.commands.dispatch(treeView.roots[0].querySelector(".header"), "tree-view:remove-project-folder")
atom.commands.dispatch(treeView.roots[0].querySelector(".header"), "tree-view:remove-project-folder")
expect(atom.project.getPaths()).toHaveLength(0)
expect(treeView.element.querySelectorAll('.selected').length).toBe 0
atom.commands.dispatch(treeView.element, 'core:move-up')
expect(treeView.element.querySelectorAll('.selected').length).toBe 0
describe "core:move-to-top", ->
it "scrolls to the top", ->
treeView.element.style.height = '100px'
jasmine.attachToDOM(treeView.element)
element.expand() for element in treeView.element.querySelectorAll('.directory')
expect(treeView.element.scrollTop).toBe(0)
entryCount = treeView.element.querySelectorAll(".entry").length
_.times entryCount, -> atom.commands.dispatch(treeView.element, 'core:move-down')
atom.commands.dispatch(treeView.element, 'core:move-to-top')
expect(treeView.element.scrollTop).toBe(0)
it "selects the root entry", ->
entryCount = treeView.element.querySelectorAll(".entry").length
_.times entryCount, -> atom.commands.dispatch(treeView.element, 'core:move-down')
expect(treeView.roots[0]).not.toHaveClass 'selected'
atom.commands.dispatch(treeView.element, 'core:move-to-top')
expect(treeView.roots[0]).toHaveClass 'selected'
describe "core:move-to-bottom", ->
it "scrolls to the bottom", ->
treeView.element.style.height = '100px'
jasmine.attachToDOM(treeView.element)
element.expand() for element in treeView.element.querySelectorAll('.directory')
expect(treeView.element.scrollTop).toBe(0)
atom.commands.dispatch(treeView.element, 'core:move-to-bottom')
expect(treeView.element.scrollTop).toBeGreaterThan(0)
treeView.roots[0].collapse()