forked from primer/view_components
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtree_view_test.rb
More file actions
1016 lines (722 loc) · 29.8 KB
/
Copy pathtree_view_test.rb
File metadata and controls
1016 lines (722 loc) · 29.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# frozen_string_literal: true
require "system/test_case"
require "test_helpers/tree_view_helpers"
module Alpha
class IntegrationTreeViewTest < System::TestCase
include Primer::DriverTestHelpers
include Primer::KeyboardTestHelpers
include Primer::JsTestHelpers
include Primer::TreeViewHelpers
##### TEST HELPERS #####
def active_element
page.evaluate_script("document.activeElement")
end
def remove_fail_param_from_fragment_src_for(*path)
evaluate_multiline_script(<<~JS)
const selector = CSS.escape(JSON.stringify(#{path.inspect}))
const includeFragment = document.querySelector(`[data-path="${selector}"]`).closest('li').querySelector('tree-view-include-fragment')
const relativeUrl = includeFragment.getAttribute('src')
const url = new URL(relativeUrl, 'http://dummy')
url.searchParams.delete('fail')
const newUrl = `${url.pathname}?${url.searchParams.toString()}`
includeFragment.setAttribute('src', newUrl)
JS
end
def capture_event(event_name, cancel: false)
evaluate_multiline_script(<<~JS)
window.treeViewEventDetails = null
const treeViewShouldCancelEvent = #{cancel}
document.querySelector('tree-view').addEventListener('#{event_name}', (event) => {
window.treeViewEventDetails = event.detail
if (treeViewShouldCancelEvent) {
event.preventDefault()
}
})
JS
yield
return page.evaluate_script("window.treeViewEventDetails")
end
def assert_window_opened(to: nil, &block)
window = window_opened_by(&block)
return unless to
within_window(window) do
assert_current_path to
end
end
def refute_window_opened(&block)
error_classes = chrome? ?
[Capybara::Cuprite::MouseEventFailed] :
[Selenium::WebDriver::Error::ElementClickInterceptedError]
assert_raises(Capybara::WindowError, *error_classes) do
window_opened_by(&block)
end
end
def refute_alert(&block)
error_classes = chrome? ?
[Capybara::Cuprite::MouseEventFailed] :
[Selenium::WebDriver::Error::ElementClickInterceptedError]
assert_raises(Capybara::ModalNotFound, *error_classes) do
accept_alert(&block)
end
end
def swallow_click_failure
yield
rescue *(chrome? ?
[Capybara::Cuprite::MouseEventFailed] :
[Selenium::WebDriver::Error::ElementClickInterceptedError])
end
##### TESTS #####
def test_expands
visit_preview(:default)
activate_at_path("src")
assert_path "src", "button.rb"
assert_path "src", "icon_button.rb"
node_at_path("src").tap do |node|
assert_equal "true", node["aria-expanded"]
end
end
def test_disabled_nodes_still_expandable
visit_preview(:links, disabled: true)
refute_path("Cloud Services", "OpenProject")
expand_at_path("Cloud Services")
assert_path("Cloud Services", "OpenProject")
collapse_at_path("Cloud Services")
refute_path("Cloud Services", "OpenProject")
end
def test_automatically_expands_all_ancestors
visit_preview(:auto_expansion)
assert node_at_path("Level 1", "Level 2", "Level 3", "Level 4")
end
# This explicitly tests the MutationObserver in tree_view.ts that listens for expanded nodes
def test_automatically_expands_all_ancestors_when_async_items_have_expanded_child
visit_preview(:async_alpha, action_menu_expanded: true)
activate_at_path("primer")
assert node_at_path("primer", "alpha", "action_menu", "heading.rb")
end
def test_collapses
visit_preview(:default)
activate_at_path("src")
node_at_path("src").tap do |node|
assert_equal "true", node["aria-expanded"]
end
# should collapse
activate_at_path("src")
refute_path "src", "button.rb"
refute_path "src", "icon_button.rb"
node_at_path("src").tap do |node|
assert_equal "false", node["aria-expanded"]
end
end
def test_expanded_and_collapsed_icons
visit_preview(:default)
assert_selector "#{selector_for("src")} .TreeViewItemVisual svg.octicon-file-directory-fill"
activate_at_path("src")
assert_selector "#{selector_for("src")} .TreeViewItemVisual svg.octicon-file-directory-open-fill"
end
def test_current
visit_preview(:default)
refute current_node
activate_at_path("src")
assert current_node
assert_equal label_of(current_node), "icon_button.rb"
end
def test_first_item_tabbable_when_no_current
visit_preview(:default)
assert_path_tabbable("src")
end
def test_disabled_first_item_tabbable_when_no_current
visit_preview(:default, disabled: true)
assert_path_tabbable("src")
end
def test_current_item_tabbable
visit_preview(:default, expanded: true)
assert_path_tabbable("src", "icon_button.rb")
end
def test_tab_selects_current_item
visit_preview(:default, expanded: true)
refute label_of(active_element)
keyboard.type(:tab)
assert_equal label_of(active_element), "icon_button.rb"
assert_path_selected("src", "icon_button.rb")
end
def test_tab_selects_disabled_current_item
visit_preview(:default, expanded: true, disabled: true)
refute label_of(active_element)
keyboard.type(:tab)
assert_equal label_of(active_element), "icon_button.rb"
assert_path_selected("src", "icon_button.rb")
end
def test_tree_tabbable_after_parent_of_focused_item_is_collapsed
visit_preview(:default, expanded: true)
keyboard.type(:tab)
assert_path_selected("src", "icon_button.rb")
activate_at_path("src")
# collapsed node now tabbable
assert_path_tabbable("src")
end
def test_sub_tree_node_links_navigate
visit_preview(:links)
assert_window_opened(to: "https://en.wikipedia.org/wiki/Cloud_computing") do
activate_at_path("Cloud Services")
end
# check that clicking did not expand
refute_path "Cloud Services", "OpenProject"
refute_path "Cloud Services", "Hetzner"
end
def test_disabled_sub_tree_node_links_do_not_navigate
visit_preview(:links, disabled: true)
refute_window_opened do
activate_at_path("Cloud Services")
end
end
def test_leaf_node_links_navigate
visit_preview(:links, expanded: true)
assert_window_opened(to: "https://www.openproject.org/") do
activate_at_path("Cloud Services", "OpenProject")
end
end
def test_disabled_leaf_node_links_do_not_navigate
visit_preview(:links, expanded: true, disabled: true)
refute_window_opened do
activate_at_path("Cloud Services", "OpenProject")
end
end
def test_sub_tree_node_buttons_alert
visit_preview(:buttons)
accept_alert("Shhhh") do
activate_at_path("Secrets")
end
# check that clicking did not expand
refute_path "Secrets", "Life and the universe"
refute_path "Secrets", "Secret ingredient"
end
def test_disabled_sub_tree_node_buttons_do_not_alert
visit_preview(:buttons, disabled: true)
refute_alert do
activate_at_path("Secrets")
end
end
def test_leaf_node_buttons_alert
visit_preview(:buttons, expanded: true)
accept_alert("42") do
activate_at_path("Secrets", "Life and the universe")
end
end
def test_disabled_leaf_node_buttons_do_not_alert
visit_preview(:buttons, expanded: true, disabled: true)
refute_alert do
activate_at_path("Secrets", "Life and the universe")
end
end
##### KEYBOARD NAVIGATION #####
def test_expands_on_enter
visit_preview(:default)
keyboard.type(:tab, :enter)
assert_path("src", "button.rb")
end
def test_collapses_on_enter
visit_preview(:default)
keyboard.type(:tab, :enter)
assert_path("src", "button.rb")
keyboard.type(:enter)
refute_path("src", "button.rb")
end
def test_expands_on_right_arrow
visit_preview(:default)
keyboard.type(:tab, :right)
assert_path("src", "button.rb")
end
def test_disabled_node_expands_on_right_arrow
visit_preview(:default, disabled: true)
keyboard.type(:tab, :right)
assert_path("src", "button.rb")
end
def test_collapses_on_left_arrow
visit_preview(:default)
keyboard.type(:tab, :right)
assert_path("src", "button.rb")
keyboard.type(:left)
refute_path("src", "button.rb")
end
def test_disabled_node_collapses_on_left_arrow
visit_preview(:default, disabled: true)
keyboard.type(:tab, :right)
assert_path("src", "button.rb")
keyboard.type(:left)
refute_path("src", "button.rb")
end
def test_arrow_down_selects_next_visible_node
visit_preview(:default)
keyboard.type(:tab, :down)
assert_equal label_of(active_element), "action_menu.rb"
assert_path_selected("action_menu.rb")
end
def test_arrow_up_selects_previous_visible_node
visit_preview(:default)
keyboard.type(:tab, :down)
assert_equal label_of(active_element), "action_menu.rb"
assert_path_selected("action_menu.rb")
keyboard.type(:up)
assert_equal label_of(active_element), "src"
assert_path_selected("src")
end
def test_arrow_down_selects_expanded_item
visit_preview(:default)
keyboard.type(:tab, :enter, :down)
assert_equal label_of(active_element), "button.rb"
assert_path_selected("src", "button.rb")
end
def test_sub_tree_node_links_navigate_on_enter
visit_preview(:links)
assert_window_opened(to: "https://en.wikipedia.org/wiki/Cloud_computing") do
keyboard.type(:tab, :enter)
end
# check that clicking did not expand
refute_path "Cloud Services", "OpenProject"
refute_path "Cloud Services", "Hetzner"
end
def test_sub_tree_node_links_navigate_on_space
visit_preview(:links)
assert_window_opened(to: "https://en.wikipedia.org/wiki/Cloud_computing") do
keyboard.type(:tab, :space)
end
# check that clicking did not expand
refute_path "Cloud Services", "OpenProject"
refute_path "Cloud Services", "Hetzner"
end
def test_sub_tree_node_buttons_alert_on_enter
visit_preview(:buttons)
accept_alert("Shhhh") do
keyboard.type(:tab, :enter)
end
# check that clicking did not expand
refute_path "Secrets", "Life and the universe"
refute_path "Secrets", "Secret ingredient"
end
def test_sub_tree_node_buttons_alert_on_space
visit_preview(:buttons)
accept_alert("Shhhh") do
keyboard.type(:tab, :space)
end
# check that clicking did not expand
refute_path "Secrets", "Life and the universe"
refute_path "Secrets", "Secret ingredient"
end
def test_leaf_node_links_navigate_on_enter
visit_preview(:links, expanded: true)
assert_window_opened(to: "https://www.openproject.org/") do
keyboard.type(:tab, :down, :enter)
end
end
def test_leaf_node_links_navigate_on_space
visit_preview(:links, expanded: true)
assert_window_opened(to: "https://www.openproject.org/") do
keyboard.type(:tab, :down, :space)
end
end
def test_leaf_node_buttons_alert_on_enter
visit_preview(:buttons, expanded: true)
accept_alert("42") do
keyboard.type(:tab, :down, :enter)
end
end
def test_leaf_node_buttons_alert_on_space
visit_preview(:buttons, expanded: true)
accept_alert("42") do
keyboard.type(:tab, :down, :space)
end
end
##### LOADERS #####
def test_loading_spinner
visit_preview(:loading_spinner)
activate_at_path("primer")
# assert loader appears and is subsequently replaced
assert_selector "#{selector_for("primer", "loader")} svg"
assert_selector "#{selector_for("primer", "alpha")}"
# assert loader is gone
refute_selector "#{selector_for("primer", "loader")}"
end
def test_selecting_spinner_causes_selection_to_move_to_first_loaded_node
visit_preview(:loading_spinner)
keyboard.type(:tab, :enter)
assert_path("primer", "loader")
keyboard.type(:down)
assert_path_selected("primer", "alpha")
end
def test_loading_spinner_failure
visit_preview(:loading_spinner, simulate_failure: true)
activate_at_path("primer")
assert_selector "#{selector_for("primer", "failure_msg")} .TreeViewFailureMessage", text: "Something went wrong"
end
def test_loading_spinner_retry_after_failure
visit_preview(:loading_spinner, simulate_failure: true)
activate_at_path("primer")
assert_selector "#{selector_for("primer", "failure_msg")} .TreeViewFailureMessage"
remove_fail_param_from_fragment_src_for("primer")
click_on("Retry")
assert_path("primer", "alpha")
refute_selector "#{selector_for("primer", "failure_msg")} .TreeViewFailureMessage"
end
def test_empty_after_loading_spinner
visit_preview(:loading_spinner, simulate_empty: true)
activate_at_path("primer")
refute_selector("tree-view-include-fragment") # wait for fragment to load
assert_selector "#{selector_for("primer")} .TreeViewItemContentText", text: "No items"
end
def test_loading_skeleton
visit_preview(:loading_skeleton)
activate_at_path("primer")
# assert loader appears and is subsequently replaced
assert_selector "#{selector_for("primer", "loader")} .SkeletonBox"
assert_selector "#{selector_for("primer", "alpha")}"
# assert loader is gone
refute_selector "#{selector_for("primer", "loader")}"
end
def test_selecting_skeleton_causes_selection_to_move_to_first_loaded_node
visit_preview(:loading_skeleton)
keyboard.type(:tab, :enter)
assert_path("primer", "loader")
keyboard.type(:down)
assert_path_selected("primer", "alpha")
end
def test_loading_skeleton_failure
visit_preview(:loading_skeleton, simulate_failure: true)
activate_at_path("primer")
assert_selector "#{selector_for("primer", "loader")} .TreeViewFailureMessage", text: "Something went wrong"
end
def test_loading_skeleton_retry_after_failure
visit_preview(:loading_skeleton, simulate_failure: true)
activate_at_path("primer")
assert_selector "#{selector_for("primer", "loader")} .TreeViewFailureMessage"
remove_fail_param_from_fragment_src_for("primer")
click_on("Retry")
assert_path("primer", "alpha")
refute_selector "#{selector_for("primer", "loader")} .TreeViewFailureMessage"
end
def test_empty_after_loading_skeleton
visit_preview(:loading_skeleton, simulate_empty: true)
activate_at_path("primer")
assert_selector "#{selector_for("primer")} .TreeViewItemContentText", text: "No items"
end
def test_empty
visit_preview(:empty)
activate_at_path("src")
assert_selector "#{selector_for("src")} .TreeViewItemContentText", text: "No items"
end
##### JAVASCRIPT EVENTS #####
def test_fires_event_before_activation
visit_preview(:default)
details = capture_event("treeViewBeforeNodeActivated") do
activate_at_path("src")
end
assert details["node"]
assert_equal details["type"], "sub-tree"
assert_equal details["path"], ["src"]
assert_equal details["checkedValue"], "false"
assert_equal details["previousCheckedValue"], "false"
end
def test_canceling_activation_event_prevents_expansion
visit_preview(:default)
refute_path "src", "button.rb"
capture_event("treeViewBeforeNodeActivated", cancel: true) do
activate_at_path("src")
end
# src should still be collapsed
refute_path "src", "button.rb"
end
def test_fires_activation_event
visit_preview(:default)
details = capture_event("treeViewNodeActivated") do
activate_at_path("src")
end
assert details["node"]
assert_equal details["type"], "sub-tree"
assert_equal details["path"], ["src"]
assert_equal details["checkedValue"], "false"
assert_equal details["previousCheckedValue"], "false"
end
def test_fires_event_before_checking_single_variant
visit_preview(:default, select_variant: :single)
details = capture_event("treeViewBeforeNodeChecked") do
activate_at_path("src")
end
assert_equal details.size, 1
assert details[0]["node"]
assert_equal details[0]["type"], "sub-tree"
assert_equal details[0]["path"], ["src"]
assert_equal details[0]["checkedValue"], "true"
assert_equal details[0]["previousCheckedValue"], "false"
end
def test_canceling_check_event_prevents_checking_for_single_variant
visit_preview(:default, select_variant: :single)
refute_path_checked "src"
capture_event("treeViewBeforeNodeChecked", cancel: true) do
activate_at_path("src")
end
# src should still not be checked
refute_path_checked "src"
end
def test_fires_check_event_after_single_variant
visit_preview(:default, select_variant: :single)
details = capture_event("treeViewNodeChecked") do
activate_at_path("src")
end
assert_equal details.size, 1
assert details[0]["node"]
assert_equal details[0]["type"], "sub-tree"
assert_equal details[0]["path"], ["src"]
assert_equal details[0]["checkedValue"], "true"
assert_equal details[0]["previousCheckedValue"], "false"
end
def test_single_select_with_duplicate_paths_selects_clicked_node
visit_preview(:doubled_path)
nodes = all(selector_for("action_menu.rb"))
assert_equal 3, nodes.size
nodes[1].click
nodes[1].assert_matches_selector("[aria-checked='true']")
nodes[0].assert_matches_selector("[aria-checked='false']")
nodes[0].click
nodes[0].assert_matches_selector("[aria-checked='true']")
nodes[1].assert_matches_selector("[aria-checked='false']")
end
def test_single_select_with_duplicate_paths_selects_focused_node_on_keyboard
visit_preview(:doubled_path)
nodes = all(selector_for("action_menu.rb"))
assert_equal 3, nodes.size
# icon_button.rb has current: true so it owns tabindex=0. Sending keys directly
# to a tabindex=-1 node is unreliable: focusZone redirects focus to the aria-current
# item on focusin, so Space would land on the wrong node.
# Start from icon_button.rb and navigate down with arrow keys instead.
find('[aria-current]').send_keys(:down)
keyboard.type(:down)
keyboard.type(:space)
nodes[1].assert_matches_selector("[aria-checked='true']")
nodes[0].assert_matches_selector("[aria-checked='false']")
keyboard.type(:up)
keyboard.type(:space)
nodes[0].assert_matches_selector("[aria-checked='true']")
nodes[1].assert_matches_selector("[aria-checked='false']")
end
def test_fires_check_event_after_single_variant_toggle_off
visit_preview(:default, select_variant: :single)
activate_at_path("src")
assert_path_checked "src"
details = capture_event("treeViewNodeChecked") do
activate_at_path("src")
end
assert_equal 1, details.size
assert details[0]["node"]
assert_equal details[0]["path"], ["src"]
assert_equal details[0]["checkedValue"], "false"
assert_equal details[0]["previousCheckedValue"], "true"
end
def test_keyboard_toggles_off_already_selected_single_variant
visit_preview(:doubled_path)
nodes = all(selector_for("action_menu.rb"))
# Navigate from icon_button.rb (aria-current, tabindex=0) to nodes[0]
find('[aria-current]').send_keys(:down)
keyboard.type(:space)
nodes[0].assert_matches_selector("[aria-checked='true']")
keyboard.type(:space)
nodes[0].assert_matches_selector("[aria-checked='false']")
end
def test_fires_event_before_checking
visit_preview(:default, select_variant: :multiple)
details = capture_event("treeViewBeforeNodeChecked") do
check_at_path("src")
end
assert_equal details.size, 3
assert details[0]["node"]
assert_equal details[0]["type"], "sub-tree"
assert_equal details[0]["path"], ["src"]
assert_equal details[0]["checkedValue"], "true"
assert_equal details[0]["previousCheckedValue"], "false"
end
def test_canceling_check_event_prevents_checking
visit_preview(:default, select_variant: :multiple)
refute_path_checked "src"
capture_event("treeViewBeforeNodeChecked", cancel: true) do
check_at_path("src")
end
# src should still not be checked
refute_path_checked "src"
end
def test_fires_check_event
visit_preview(:default, select_variant: :multiple)
details = capture_event("treeViewNodeChecked") do
check_at_path("src")
end
assert_equal details.size, 3
assert details[0]["node"]
assert_equal details[0]["type"], "sub-tree"
assert_equal details[0]["path"], ["src"]
assert_equal details[0]["checkedValue"], "true"
assert_equal details[0]["previousCheckedValue"], "false"
end
def test_js_api_expand_at_path
visit_preview(:playground)
evaluate_multiline_script(<<~JS)
document.querySelector('tree-view').expandAtPath(["primer", "alpha", "action_menu"])
JS
assert node_at_path("primer", "alpha", "action_menu", "heading.rb")
end
##### CHECKBOX BEHAVIOR #####
def test_activation_checks_sub_tree_node
visit_preview(:playground, select_variant: :multiple)
refute_path_checked "primer"
activate_at_path("primer")
assert_path_checked "primer"
end
def test_activation_checks_leaf_node
visit_preview(:playground, select_variant: :multiple)
expand_at_path("primer")
expand_at_path("primer", "alpha")
refute_path_checked "primer", "alpha", "action_bar.pcss"
activate_at_path("primer", "alpha", "action_bar.pcss")
assert_path_checked "primer", "alpha", "action_bar.pcss"
end
def test_space_checks_sub_tree_node
visit_preview(:playground, select_variant: :multiple)
refute_path_checked "primer"
keyboard.type(:tab, :space)
assert_path_checked "primer"
end
def test_space_checks_leaf_node
visit_preview(:playground, select_variant: :multiple)
expand_at_path("primer")
expand_at_path("primer", "alpha")
refute_path_checked "primer", "alpha", "action_bar.pcss"
keyboard.type(:tab, :down, :down, :down, :space)
assert_path_checked "primer", "alpha", "action_bar.pcss"
end
def test_enter_checks_sub_tree_node
visit_preview(:playground, select_variant: :multiple)
refute_path_checked "primer"
keyboard.type(:tab, :enter)
assert_path_checked "primer"
end
def test_enter_checks_leaf_node
visit_preview(:playground, select_variant: :multiple)
expand_at_path("primer")
expand_at_path("primer", "alpha")
refute_path_checked "primer", "alpha", "action_bar.pcss"
keyboard.type(:tab, :down, :down, :down, :enter)
assert_path_checked "primer", "alpha", "action_bar.pcss"
end
def test_checking_sub_tree_node_checks_all_children
visit_preview(:default, expanded: true, select_variant: :multiple)
assert_path "src", "button.rb"
refute_path_checked "src", "button.rb"
assert_path "src", "button.rb"
refute_path_checked "src", "icon_button.rb"
check_at_path("src")
assert_path_checked "src"
assert_path_checked "src", "button.rb"
assert_path_checked "src", "icon_button.rb"
end
def test_unchecking_sub_tree_child_results_in_mixed_parent
visit_preview(:default, expanded: true, select_variant: :multiple)
check_at_path("src")
assert_path_checked "src"
check_at_path("src", "button.rb") # uncheck
assert_path_checked "src", value: :mixed
refute_path_checked "src", "button.rb"
assert_path_checked "src", "icon_button.rb"
end
def test_checking_deeply_nested_child_results_in_all_mixed_ancestors
visit_preview(:playground, expanded: true, select_variant: :multiple)
check_at_path("primer", "alpha", "action_bar", "item.rb")
assert_path_checked "primer", value: :mixed
assert_path_checked "primer", "alpha", value: :mixed
assert_path_checked "primer", "alpha", "action_bar", value: :mixed
assert_path_checked "primer", "alpha", "action_bar", "item.rb"
end
def test_self_select_strategy_does_not_check_parent
visit_preview(:playground, expanded: true, select_variant: :multiple, select_strategy: :self)
check_at_path("primer", "alpha", "action_bar", "item.rb")
refute_path_checked "primer", value: :true_or_mixed
refute_path_checked "primer", "alpha", value: :true_or_mixed
refute_path_checked "primer", "alpha", "action_bar", value: :true_or_mixed
assert_path_checked "primer", "alpha", "action_bar", "item.rb"
end
def test_self_select_strategy_checking_sub_tree_does_not_check_children
visit_preview(:playground, expanded: true, select_variant: :multiple, select_strategy: :self)
check_at_path("primer", "alpha", "action_bar")
refute_path_checked "primer", "alpha", "action_bar", "divider.rb", value: :true_or_mixed
refute_path_checked "primer", "alpha", "action_bar", "item.rb", value: :true_or_mixed
assert_path_checked "primer", "alpha", "action_bar"
end
def test_form_submission
visit_preview(:form_input, expanded: true, route_format: :json)
assert_path_checked "src", "button.rb"
check_at_path("action_menu.rb")
find("button[type=submit]").click
# for some reason the JSON response is wrapped in HTML, I have no idea why
response = JSON.parse(find("pre").text)
assert_equal "{\"path\":[\"src\",\"button.rb\"],\"nodeId\":\"src-button-rb\",\"value\":\"1\"}", response.dig("form_params", "folder_structure", 0)
assert_equal "{\"path\":[\"action_menu.rb\"],\"nodeId\":\"action-menu-rb\",\"value\":\"3\"}", response.dig("form_params", "folder_structure", 1)
end
def test_initial_form_state
visit_preview(:form_input, expanded: true, route_format: :json)
assert_path_checked "src", "button.rb"
find("button[type=submit]").click
# for some reason the JSON response is wrapped in HTML, I have no idea why
response = JSON.parse(find("pre").text)
assert_equal "{\"path\":[\"src\",\"button.rb\"],\"nodeId\":\"src-button-rb\",\"value\":\"1\"}", response.dig("form_params", "folder_structure", 0)
end
def test_form_submission_with_single_select_variant
visit_preview(:form_input, expanded: true, select_variant: :single, route_format: :json)
assert_path_checked "src", "button.rb"
activate_at_path("action_menu.rb")
find("button[type=submit]").click
# for some reason the JSON response is wrapped in HTML, I have no idea why
response = JSON.parse(find("pre").text)
assert_equal "{\"path\":[\"action_menu.rb\"],\"nodeId\":\"action-menu-rb\",\"value\":\"3\"}", response.dig("form_params", "folder_structure", 0)
end
def test_initial_form_state_for_single_select
visit_preview(:form_input, expanded: true, route_format: :json)
assert_path_checked "src", "button.rb"
find("button[type=submit]").click
# for some reason the JSON response is wrapped in HTML, I have no idea why
response = JSON.parse(find("pre").text)
assert_equal "{\"path\":[\"src\",\"button.rb\"],\"nodeId\":\"src-button-rb\",\"value\":\"1\"}", response.dig("form_params", "folder_structure", 0)
end
def test_form_payload_includes_node_id_when_present
visit_preview(:form_input, expanded: true, select_variant: :single, route_format: :json)
activate_at_path("action_menu.rb")
find("button[type=submit]").click
response = JSON.parse(find("pre").text)
assert_equal "{\"path\":[\"action_menu.rb\"],\"nodeId\":\"action-menu-rb\",\"value\":\"3\"}", response.dig("form_params", "folder_structure", 0)
end
def test_single_select_with_special_character
visit_preview(:form_input, expanded: true, select_variant: :single, route_format: :json)
assert_path_checked "src", "button.rb"
activate_at_path("Docs & legal requirements")
assert_path_checked "Docs & legal requirements"
find("button[type=submit]").click
# for some reason the JSON response is wrapped in HTML, I have no idea why
response = JSON.parse(find("pre").text)
assert_equal "{\"path\":[\"Docs & legal requirements\"],\"nodeId\":\"docs\",\"value\":\"4\"}", response.dig("form_params", "folder_structure", 0)
end
def test_keyboard_focus_moves_to_parent_when_include_fragment_with_role_treeitem_is_collapsed
visit_preview(:loading_spinner)
# Tab to focus the tree view
keyboard.type(:tab)
# Enter to expand the spinner node (which includes an include-fragment with role="treeitem")
keyboard.type(:enter)
# Verify that the include-fragment has role="treeitem" when expanded
assert_selector "tree-view-include-fragment[role=treeitem]"
# Wait for the loader to be replaced