forked from primer/view_components
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsub_header_test.rb
More file actions
423 lines (353 loc) · 15.3 KB
/
Copy pathsub_header_test.rb
File metadata and controls
423 lines (353 loc) · 15.3 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
# frozen_string_literal: true
require "components/test_helper"
class PrimerOpenProjectSubHeaderTest < Minitest::Test
include Primer::ComponentTestHelpers
def test_renders
render_inline(Primer::OpenProject::SubHeader.new) do |component|
component.with_filter_input(name: "filter", label: "Filter")
component.with_action_button(leading_icon: :plus, label: "Create", scheme: :primary) do |button|
"Create"
end
component.with_action_button(leading_icon: :trash, label: "Delete", scheme: :danger) { "Delete" }
end
assert_selector(".SubHeader")
assert_selector(".SubHeader-filterInput")
assert_selector(".SubHeader .Button--primary")
assert_selector(".SubHeader .Button--danger")
# Assert the correct order
assert_selector(".SubHeader-rightPane .Button--primary+.Button--danger")
end
def test_renders_an_icon_button_as_action
render_inline(Primer::OpenProject::SubHeader.new) do |component|
component.with_action_button(icon_only: true, leading_icon: :plus, label: "Create", aria: { label: "Create" })
end
assert_selector(".SubHeader")
assert_selector(".SubHeader .Button--iconOnly")
end
def test_renders_a_button_without_label
err = assert_raises ArgumentError do
render_inline(Primer::OpenProject::SubHeader.new) do |component|
component.with_action_button(icon_only: true, leading_icon: :plus, label: "", aria: { label: "Create" })
end
end
assert_equal "You need to provide a valid label.", err.message
end
def test_renders_a_button_group_as_action
render_inline(Primer::OpenProject::SubHeader.new) do |component|
component.with_action_button_group do |group|
group.with_button(icon: :note, "aria-label": "Button 1")
group.with_button(icon: :rows, "aria-label": "Button 2")
group.with_button(icon: "sort-desc", "aria-label": "Button 3")
end
end
assert_selector(".SubHeader")
assert_selector(".SubHeader .ButtonGroup")
assert_selector(".octicon.octicon-note")
assert_selector(".octicon.octicon-rows")
assert_selector(".octicon.octicon-sort-desc")
end
def test_renders_a_menu_as_action
render_inline(Primer::OpenProject::SubHeader.new) do |component|
component.with_action_menu(leading_icon: :plus, label: "Create", button_arguments: { scheme: :primary, "aria-label": "Menu"}) do |menu|
menu.with_item(label: "Foo") do |item|
item.with_leading_visual_icon(icon: :paste)
end
menu.with_item(label: "Bar") do |item|
item.with_leading_visual_icon(icon: :log)
end
end
end
assert_selector(".Button-leadingVisual .octicon-plus")
assert_selector("ul[role=menu]") do
assert_selector ".ActionListItem", text: "Foo"
assert_selector ".ActionListItem", text: "Bar"
end
end
def test_renders_a_menu_without_label
err = assert_raises ArgumentError do
render_inline(Primer::OpenProject::SubHeader.new) do |component|
component.with_action_menu(leading_icon: :plus, label: "", button_arguments: { scheme: :primary, "aria-label": "Menu"}) do |menu|
menu.with_item(label: "Foo") do |item|
item.with_leading_visual_icon(icon: :paste)
end
menu.with_item(label: "Bar") do |item|
item.with_leading_visual_icon(icon: :log)
end
end
end
end
assert_equal "You need to provide a valid label.", err.message
end
def test_renders_a_text
render_inline(Primer::OpenProject::SubHeader.new) do |component|
component.with_text { "Hello world!" }
end
assert_selector(".SubHeader")
assert_text("Hello world!")
end
def test_renders_a_filter_button
render_inline(Primer::OpenProject::SubHeader.new) do |component|
component.with_filter_button do |button|
button.with_trailing_visual_counter(count: "15")
"Filter"
end
end
assert_selector(".SubHeader")
assert_selector(".SubHeader-leftPane") do
assert_text("Filter")
end
end
def test_renders_an_icon_button_as_filter_button
render_inline(Primer::OpenProject::SubHeader.new) do |component|
component.with_filter_button(icon_only: true)
end
assert_selector(".SubHeader")
assert_selector(".SubHeader-leftPane") do
assert_selector(".SubHeader .Button--iconOnly")
assert_selector(".octicon.octicon-filter")
end
end
def test_renders_an_icon_filter_button
render_inline(Primer::OpenProject::SubHeader.new) do |component|
component.with_filter_button(icon: "filter", "aria-label": "Filter")
end
assert_selector(".SubHeader")
assert_selector(".SubHeader-leftPane .Button--iconOnly .octicon-filter")
end
def test_filter_button_uses_invisible_scheme
render_inline(Primer::OpenProject::SubHeader.new) do |component|
component.with_filter_button { "Filter" }
end
assert_selector(".SubHeader .Button--invisible")
end
def test_renders_a_custom_filter_button
render_inline(Primer::OpenProject::SubHeader.new) do |component|
component.with_filter_component do
"<button class='MyCustomButton'>Filter</button>".html_safe
end
component.with_bottom_pane_component do
"<div class='ABottomPane'>Whatever block you need</div>".html_safe
end
end
assert_selector(".SubHeader")
assert_selector(".SubHeader .MyCustomButton")
assert_selector(".SubHeader .SubHeader-bottomPane .ABottomPane")
end
def test_renders_a_clear_button_when_show_clear_button_is_set
render_inline(Primer::OpenProject::SubHeader.new) do |component|
component.with_filter_input(
name: "filter",
label: "Filter",
show_clear_button: true,
value: "value is set"
)
end
assert_selector(".SubHeader")
assert_selector(
".SubHeader-filterInput"\
"[data-action=\" input:sub-header#toggleFilterInputClearButton focus:sub-header#toggleFilterInputClearButton\"]"
)
assert_selector(".FormControl-input-trailingAction[data-action=\"click:primer-text-field#clearContents\"]")
end
def test_renders_a_filter_input_label
err = assert_raises ArgumentError do
render_inline(Primer::OpenProject::SubHeader.new) do |component|
component.with_filter_input(
name: "filter",
label: "",
show_clear_button: true,
value: "value is set"
)
end
end
assert_equal "You need to provide a valid label.", err.message
end
def test_renders_a_segmented_control
render_inline(Primer::OpenProject::SubHeader.new) do |component|
component.with_segmented_control("aria-label": "Segmented control") do |control|
control.with_item(tag: :a, href: "#", label: "Preview", icon: :eye, selected: true)
control.with_item(tag: :a, href: "#", label: "Raw", icon: :"file-code")
end
end
assert_selector(".SubHeader")
assert_selector(".SubHeader .SegmentedControl")
assert_text("Preview")
assert_text("Raw")
end
def test_renders_empty_left_pane
render_inline(Primer::OpenProject::SubHeader.new) do |component|
component.with_action_button_group do |group|
group.with_button(icon: :note, "aria-label": "Button 1")
group.with_button(icon: :rows, "aria-label": "Button 2")
end
end
assert_selector(".SubHeader.SubHeader--emptyLeftPane")
end
def test_does_not_render_input_events_when_show_clear_button_is_not_set
render_inline(Primer::OpenProject::SubHeader.new) do |component|
component.with_filter_input(
name: "filter",
label: "Filter",
show_clear_button: false,
value: "value is set"
)
end
assert_selector(".SubHeader")
assert_selector(".SubHeader-filterInput")
assert_no_selector(
".SubHeader-filterInput"\
"[data-action=\" input:sub-header#toggleFilterInputClearButton focus:sub-header#toggleFilterInputClearButton\"]"
)
assert_no_selector(".FormControl-input-trailingAction[data-action=\"click:primer-text-field#clearContents\"]")
end
def test_renders_collapsed_search_per_default
render_inline(Primer::OpenProject::SubHeader.new) do |component|
component.with_filter_input(name: "filter", label: "Filter")
end
assert_selector(".SubHeader")
# Trigger is rendered and wired to expand
assert_selector("[data-action='click:sub-header#expandFilterInput']")
# Filter container starts hidden on all screen sizes (d-none, no d-sm-flex)
assert_selector(".SubHeader-filterContainer.d-none")
assert_no_selector(".SubHeader-filterContainer.d-sm-flex")
end
def test_collapsed_search_trigger_visible_on_all_screen_sizes
render_inline(Primer::OpenProject::SubHeader.new) do |component|
component.with_filter_input(name: "filter", label: "Filter")
end
# Without collapsed_search, the trigger has d-md-none (hidden on desktop).
# With collapsed_search, it must not have d-md-none.
assert_no_selector("[data-action='click:sub-header#expandFilterInput'].d-md-none")
end
def test_expanded_search_trigger_hidden_on_desktop
render_inline(Primer::OpenProject::SubHeader.new(collapsed_search: false)) do |component|
component.with_filter_input(name: "filter", label: "Filter")
end
# Without collapsed_search the trigger is mobile-only (d-md-none)
assert_selector("[data-action='click:sub-header#expandFilterInput'].d-md-none")
end
def test_renders_quick_filters
render_inline(Primer::OpenProject::SubHeader.new) do |component|
component.with_filter_input(name: "filter", label: "Filter")
component.with_filter_button { "Filter" }
component.with_quick_filter { "<span class='MyQuickFilter'>Status</span>".html_safe }
component.with_quick_filter { "<span class='MyQuickFilter'>Assignee</span>".html_safe }
end
assert_selector(".SubHeader-leftPane .MyQuickFilter", count: 2)
end
def test_quick_filters_are_hidden_on_mobile_when_multiple
render_inline(Primer::OpenProject::SubHeader.new) do |component|
component.with_filter_button { "Filter" }
component.with_quick_filter { "<span class='MyQuickFilter'>Status</span>".html_safe }
component.with_quick_filter { "<span class='MyQuickFilter'>Assignee</span>".html_safe }
end
# Each quick_filter wrapper has d-none d-md-flex (hidden on mobile, visible on desktop)
assert_selector(".d-none.d-md-flex .MyQuickFilter", count: 2)
end
def test_single_quick_filter_is_visible_on_all_screen_sizes
render_inline(Primer::OpenProject::SubHeader.new) do |component|
component.with_quick_filter { "<span class='MyQuickFilter'>Status</span>".html_safe }
end
assert_selector(".MyQuickFilter")
assert_no_selector(".d-none .MyQuickFilter")
end
def test_quick_filters_require_filter_button_when_multiple_quick_filters
err = assert_raises ArgumentError do
render_inline(Primer::OpenProject::SubHeader.new) do |component|
component.with_filter_input(name: "filter", label: "Filter")
component.with_quick_filter { "Status" }
component.with_quick_filter { "Status" }
end
end
assert_equal "You must provide a filter_button when using quick_filters.", err.message
end
def test_quick_filters_require_no_filter_button_for_single_quick_filter
render_inline(Primer::OpenProject::SubHeader.new) do |component|
component.with_quick_filter { "<span class='MyQuickFilter'>Status</span>".html_safe }
end
assert_selector(".MyQuickFilter")
end
def test_quick_filters_maximum_is_five
err = assert_raises ArgumentError do
render_inline(Primer::OpenProject::SubHeader.new) do |component|
component.with_filter_button { "Filter" }
6.times { |i| component.with_quick_filter { "Filter #{i}" } }
end
end
assert_equal "SubHeader supports a maximum of 5 quick_filters, got 6.", err.message
end
def test_renders_sort_quick_filter
render_inline(Primer::OpenProject::SubHeader.new) do |component|
component.with_quick_sort { "<span class='SortFilter'>Sort</span>".html_safe }
end
assert_selector(".SubHeader-leftPane .SortFilter")
end
def test_renders_group_quick_filter
render_inline(Primer::OpenProject::SubHeader.new) do |component|
component.with_quick_group { "<span class='GroupFilter'>Group</span>".html_safe }
end
assert_selector(".SubHeader-leftPane .GroupFilter")
end
def test_sort_and_group_quick_filters_render_before_other_quick_filters
render_inline(Primer::OpenProject::SubHeader.new) do |component|
component.with_filter_button { "Filter" }
component.with_quick_filter(classes: "OtherFilter") { "Other" }
component.with_quick_group(classes: "GroupFilter") { "Group" }
component.with_quick_sort(classes: "SortFilter") { "Sort" }
end
assert_selector(".SortFilter ~ .GroupFilter")
assert_selector(".GroupFilter ~ .OtherFilter")
end
def test_sort_and_group_do_not_count_toward_filter_button_requirement
render_inline(Primer::OpenProject::SubHeader.new) do |component|
component.with_quick_sort { "Sort" }
component.with_quick_filter { "Status" }
end
assert_text "Sort"
assert_text "Status"
end
def test_sort_and_group_do_not_count_toward_maximum
render_inline(Primer::OpenProject::SubHeader.new) do |component|
component.with_filter_button { "Filter" }
component.with_quick_sort { "Sort" }
component.with_quick_group { "Group" }
4.times { |i| component.with_quick_filter { "Filter #{i}" } }
end
assert_text "Sort"
assert_text "Group"
assert_text "Filter 0"
assert_text "Filter 1"
assert_text "Filter 2"
assert_text "Filter 3"
end
def test_sort_and_group_are_not_hidden_on_mobile_when_multiple_quick_filters
render_inline(Primer::OpenProject::SubHeader.new) do |component|
component.with_filter_button { "Filter" }
component.with_quick_sort { "<span class='SortFilter'>Sort</span>".html_safe }
component.with_quick_group { "<span class='GroupFilter'>Group</span>".html_safe }
end
assert_no_selector(".d-none.d-md-flex .SortFilter")
assert_no_selector(".d-none.d-md-flex .GroupFilter")
end
def test_collapsed_search_auto_enabled_when_quick_filter_present
render_inline(Primer::OpenProject::SubHeader.new) do |component|
component.with_filter_input(name: "filter", label: "Filter")
component.with_quick_filter { "<span class='MyQuickFilter'>Status</span>".html_safe }
end
# Filter container is hidden (collapsed_search auto-detected as true)
assert_selector(".SubHeader-filterContainer.d-none")
assert_no_selector(".SubHeader-filterContainer.d-md-flex")
# Trigger is visible on all screen sizes (no d-sm-none)
assert_no_selector("[data-action='click:sub-header#expandFilterInput'].d-md-none")
end
def test_explicit_collapsed_search_false_overrides_auto_collapse
render_inline(Primer::OpenProject::SubHeader.new(collapsed_search: false)) do |component|
component.with_filter_input(name: "filter", label: "Filter")
component.with_quick_filter { "<span class='MyQuickFilter'>Status</span>".html_safe }
end
# Filter container is NOT hidden despite quick_filter being present
assert_selector(".SubHeader-filterContainer.d-md-flex")
# Trigger is mobile-only (d-sm-none) since collapsed_search is false
assert_selector("[data-action='click:sub-header#expandFilterInput'].d-md-none")
end
end