velvetkit 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (175) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +80 -0
  3. data/README.md +5 -1
  4. data/lib/generators/velvetkit/add/add_generator.rb +58 -0
  5. data/lib/velvetkit/version.rb +4 -1
  6. data/lib/velvetkit.rb +7 -0
  7. data/registry/app/assets/tailwind/velvetkit.css +90 -155
  8. data/registry/app/components/ui/chart.html.erb +47 -0
  9. data/registry/app/components/ui/chart.rb +177 -0
  10. data/registry/app/components/ui/command/group.rb +27 -0
  11. data/registry/app/components/ui/command/item.rb +64 -0
  12. data/registry/app/components/ui/command.html.erb +41 -0
  13. data/registry/app/components/ui/command.rb +122 -0
  14. data/registry/app/components/ui/date_picker.html.erb +58 -64
  15. data/registry/app/components/ui/date_picker.rb +199 -75
  16. data/registry/app/components/ui/file_field.html.erb +14 -4
  17. data/registry/app/components/ui/file_field.rb +179 -28
  18. data/registry/app/components/ui/gallery/image.rb +20 -0
  19. data/registry/app/components/ui/gallery.html.erb +52 -0
  20. data/registry/app/components/ui/gallery.rb +164 -0
  21. data/registry/app/components/ui/kbd.html.erb +27 -0
  22. data/registry/app/components/ui/kbd.rb +134 -0
  23. data/registry/app/components/ui/number_input.html.erb +11 -0
  24. data/registry/app/components/ui/number_input.rb +103 -0
  25. data/registry/app/components/ui/progress/segment.rb +25 -0
  26. data/registry/app/components/ui/progress.html.erb +25 -0
  27. data/registry/app/components/ui/progress.rb +182 -0
  28. data/registry/app/components/ui/select.html.erb +24 -5
  29. data/registry/app/components/ui/select.rb +44 -5
  30. data/registry/app/components/ui/timeline/item.html.erb +21 -0
  31. data/registry/app/components/ui/timeline/item.rb +145 -0
  32. data/registry/app/components/ui/timeline.html.erb +1 -0
  33. data/registry/app/components/ui/timeline.rb +42 -0
  34. data/registry/app/javascript/chart_js.ts +37 -0
  35. data/registry/app/javascript/controllers/ui/_internal/date_format.ts +74 -0
  36. data/registry/app/javascript/controllers/ui/_internal/shortcut.ts +80 -0
  37. data/registry/app/javascript/controllers/ui/chart_controller.ts +585 -0
  38. data/registry/app/javascript/controllers/ui/command_controller.ts +454 -0
  39. data/registry/app/javascript/controllers/ui/date_picker_controller.ts +247 -82
  40. data/registry/app/javascript/controllers/ui/file_field_controller.ts +288 -16
  41. data/registry/app/javascript/controllers/ui/gallery_controller.ts +568 -0
  42. data/registry/app/javascript/controllers/ui/kbd_controller.ts +60 -0
  43. data/registry/app/javascript/controllers/ui/number_input_controller.ts +111 -0
  44. data/registry/app/javascript/controllers/ui/progress_controller.ts +87 -0
  45. data/registry/app/javascript/controllers/ui/select_controller.ts +198 -8
  46. data/registry/app/javascript/vanilla_calendar.ts +15 -0
  47. data/registry/components.yml +19 -3
  48. data/registry/config/locales/velvetkit.en.yml +73 -0
  49. data/registry/icons/lucide/download.svg +17 -0
  50. data/registry/icons/lucide/file-plus.svg +18 -0
  51. data/registry/icons/lucide/file.svg +16 -0
  52. data/registry/icons/lucide/folder.svg +15 -0
  53. data/registry/icons/lucide/keyboard.svg +23 -0
  54. data/registry/icons/lucide/minus.svg +15 -0
  55. data/registry/icons/lucide/package.svg +18 -0
  56. data/registry/icons/lucide/pause.svg +16 -0
  57. data/registry/icons/lucide/percent.svg +17 -0
  58. data/registry/icons/lucide/printer.svg +17 -0
  59. data/registry/icons/lucide/receipt.svg +17 -0
  60. data/registry/icons/lucide/sun-moon.svg +19 -0
  61. data/registry/icons/lucide/truck.svg +19 -0
  62. data/registry/icons/lucide/user-plus.svg +18 -0
  63. data/registry/icons/lucide/zoom-in.svg +18 -0
  64. data/registry/icons/lucide/zoom-out.svg +17 -0
  65. data/registry/icons/phosphor/circle-half.svg +2 -0
  66. data/registry/icons/phosphor/download-simple.svg +2 -0
  67. data/registry/icons/phosphor/file-plus.svg +2 -0
  68. data/registry/icons/phosphor/file.svg +2 -0
  69. data/registry/icons/phosphor/folder.svg +2 -0
  70. data/registry/icons/phosphor/keyboard.svg +2 -0
  71. data/registry/icons/phosphor/magnifying-glass-minus.svg +2 -0
  72. data/registry/icons/phosphor/magnifying-glass-plus.svg +2 -0
  73. data/registry/icons/phosphor/minus.svg +2 -0
  74. data/registry/icons/phosphor/package.svg +2 -0
  75. data/registry/icons/phosphor/pause.svg +2 -0
  76. data/registry/icons/phosphor/percent.svg +2 -0
  77. data/registry/icons/phosphor/printer.svg +2 -0
  78. data/registry/icons/phosphor/receipt.svg +2 -0
  79. data/registry/icons/phosphor/truck.svg +2 -0
  80. data/registry/icons/phosphor/user-plus.svg +2 -0
  81. data/registry/icons/tabler/download.svg +18 -0
  82. data/registry/icons/tabler/file-plus.svg +19 -0
  83. data/registry/icons/tabler/file.svg +17 -0
  84. data/registry/icons/tabler/folder.svg +16 -0
  85. data/registry/icons/tabler/keyboard.svg +23 -0
  86. data/registry/icons/tabler/minus.svg +16 -0
  87. data/registry/icons/tabler/package.svg +20 -0
  88. data/registry/icons/tabler/percentage.svg +18 -0
  89. data/registry/icons/tabler/player-pause.svg +17 -0
  90. data/registry/icons/tabler/printer.svg +18 -0
  91. data/registry/icons/tabler/receipt.svg +16 -0
  92. data/registry/icons/tabler/sun-moon.svg +21 -0
  93. data/registry/icons/tabler/truck.svg +18 -0
  94. data/registry/icons/tabler/user-plus.svg +19 -0
  95. data/registry/icons/tabler/zoom-in.svg +19 -0
  96. data/registry/icons/tabler/zoom-out.svg +18 -0
  97. data/registry/icons.yml +16 -0
  98. data/registry/test/components/previews/ui/chart_preview/cash_flow.html.erb +6 -0
  99. data/registry/test/components/previews/ui/chart_preview/donut.html.erb +6 -0
  100. data/registry/test/components/previews/ui/chart_preview/margin.html.erb +6 -0
  101. data/registry/test/components/previews/ui/chart_preview/pie.html.erb +6 -0
  102. data/registry/test/components/previews/ui/chart_preview/sales.html.erb +6 -0
  103. data/registry/test/components/previews/ui/chart_preview/stock.html.erb +6 -0
  104. data/registry/test/components/previews/ui/chart_preview/takings.html.erb +8 -0
  105. data/registry/test/components/previews/ui/chart_preview.rb +46 -0
  106. data/registry/test/components/previews/ui/command_preview/_picked.html.erb +14 -0
  107. data/registry/test/components/previews/ui/command_preview/dialog.html.erb +24 -0
  108. data/registry/test/components/previews/ui/command_preview/inline.html.erb +16 -0
  109. data/registry/test/components/previews/ui/command_preview/playground.html.erb +15 -0
  110. data/registry/test/components/previews/ui/command_preview/server_search.html.erb +24 -0
  111. data/registry/test/components/previews/ui/command_preview/shortcuts.html.erb +12 -0
  112. data/registry/test/components/previews/ui/command_preview/till.html.erb +17 -0
  113. data/registry/test/components/previews/ui/command_preview.rb +25 -0
  114. data/registry/test/components/previews/ui/date_picker_preview/custom_presets.html.erb +6 -0
  115. data/registry/test/components/previews/ui/date_picker_preview/in_form.html.erb +11 -0
  116. data/registry/test/components/previews/ui/date_picker_preview/range.html.erb +5 -0
  117. data/registry/test/components/previews/ui/date_picker_preview/states.html.erb +4 -3
  118. data/registry/test/components/previews/ui/date_picker_preview.rb +26 -6
  119. data/registry/test/components/previews/ui/file_field_preview/multiple_files.html.erb +1 -1
  120. data/registry/test/components/previews/ui/file_field_preview/product_photos.html.erb +12 -0
  121. data/registry/test/components/previews/ui/file_field_preview/single_image.html.erb +4 -0
  122. data/registry/test/components/previews/ui/file_field_preview.rb +32 -5
  123. data/registry/test/components/previews/ui/gallery_preview/hover_zoom.html.erb +13 -0
  124. data/registry/test/components/previews/ui/gallery_preview/landscape.html.erb +5 -0
  125. data/registry/test/components/previews/ui/gallery_preview/product_page.html.erb +13 -0
  126. data/registry/test/components/previews/ui/gallery_preview/single.html.erb +3 -0
  127. data/registry/test/components/previews/ui/gallery_preview.rb +45 -0
  128. data/registry/test/components/previews/ui/kbd_preview/bound.html.erb +20 -0
  129. data/registry/test/components/previews/ui/kbd_preview/keys.html.erb +13 -0
  130. data/registry/test/components/previews/ui/kbd_preview/menu.html.erb +13 -0
  131. data/registry/test/components/previews/ui/kbd_preview/search_field.html.erb +8 -0
  132. data/registry/test/components/previews/ui/kbd_preview/shortcuts_list.html.erb +17 -0
  133. data/registry/test/components/previews/ui/kbd_preview/till.html.erb +23 -0
  134. data/registry/test/components/previews/ui/kbd_preview/tooltip_and_buttons.html.erb +14 -0
  135. data/registry/test/components/previews/ui/kbd_preview.rb +32 -0
  136. data/registry/test/components/previews/ui/number_input_preview/cart.html.erb +11 -0
  137. data/registry/test/components/previews/ui/number_input_preview/decimals.html.erb +4 -0
  138. data/registry/test/components/previews/ui/number_input_preview/sizes.html.erb +5 -0
  139. data/registry/test/components/previews/ui/number_input_preview/states.html.erb +7 -0
  140. data/registry/test/components/previews/ui/number_input_preview/stock_adjustment.html.erb +3 -0
  141. data/registry/test/components/previews/ui/number_input_preview.rb +36 -0
  142. data/registry/test/components/previews/ui/progress_preview/file_upload.html.erb +36 -0
  143. data/registry/test/components/previews/ui/progress_preview/free_shipping.html.erb +4 -0
  144. data/registry/test/components/previews/ui/progress_preview/indeterminate.html.erb +4 -0
  145. data/registry/test/components/previews/ui/progress_preview/sizes_and_tones.html.erb +8 -0
  146. data/registry/test/components/previews/ui/progress_preview/steps.html.erb +9 -0
  147. data/registry/test/components/previews/ui/progress_preview/storage.html.erb +8 -0
  148. data/registry/test/components/previews/ui/progress_preview/storage_by_category.html.erb +7 -0
  149. data/registry/test/components/previews/ui/progress_preview/tasks_by_status.html.erb +14 -0
  150. data/registry/test/components/previews/ui/progress_preview/traffic_by_source.html.erb +15 -0
  151. data/registry/test/components/previews/ui/progress_preview.rb +46 -0
  152. data/registry/test/components/previews/ui/select_preview/server_search.html.erb +22 -0
  153. data/registry/test/components/previews/ui/select_preview.rb +5 -0
  154. data/registry/test/components/previews/ui/timeline_preview/activity_log.html.erb +7 -0
  155. data/registry/test/components/previews/ui/timeline_preview/colored.html.erb +7 -0
  156. data/registry/test/components/previews/ui/timeline_preview/colored_by_meaning.html.erb +7 -0
  157. data/registry/test/components/previews/ui/timeline_preview/failed_delivery.html.erb +6 -0
  158. data/registry/test/components/previews/ui/timeline_preview/order_status.html.erb +9 -0
  159. data/registry/test/components/previews/ui/timeline_preview/real_dates.html.erb +5 -0
  160. data/registry/test/components/previews/ui/timeline_preview/stock_history.html.erb +6 -0
  161. data/registry/test/components/previews/ui/timeline_preview.rb +40 -0
  162. data/registry/test/components/ui/chart_test.rb +187 -0
  163. data/registry/test/components/ui/command/group_test.rb +19 -0
  164. data/registry/test/components/ui/command/item_test.rb +47 -0
  165. data/registry/test/components/ui/command_test.rb +110 -0
  166. data/registry/test/components/ui/date_picker_test.rb +129 -111
  167. data/registry/test/components/ui/file_field_test.rb +105 -9
  168. data/registry/test/components/ui/gallery_test.rb +192 -0
  169. data/registry/test/components/ui/kbd_test.rb +150 -0
  170. data/registry/test/components/ui/number_input_test.rb +155 -0
  171. data/registry/test/components/ui/progress_test.rb +188 -0
  172. data/registry/test/components/ui/select_test.rb +62 -0
  173. data/registry/test/components/ui/timeline_test.rb +175 -0
  174. metadata +147 -2
  175. data/registry/app/javascript/cally.ts +0 -8
@@ -0,0 +1,187 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ class UI::ChartTest < ViewComponent::TestCase
6
+ def setup_fixtures(*); end
7
+ def teardown_fixtures(*); end
8
+
9
+ LABELS = %w[Jan Feb Mar].freeze
10
+
11
+ def render_chart(series: [ { name: "Sales", data: [ 1200, 1500.5, 900 ] } ], **opts)
12
+ render_inline(UI::Chart.new(labels: LABELS, series: series, **opts))
13
+ end
14
+
15
+ def two_series = [ { name: "This year", data: [ 1, 2, 3 ] }, { name: "Last year", data: [ 3, 2, 1 ] } ]
16
+
17
+ def config = JSON.parse(page.find("[data-slot='chart']")["data-ui--chart-config-value"])
18
+
19
+ def test_renders_the_controller_with_its_module_and_a_named_canvas
20
+ render_chart(label: "Monthly sales")
21
+ root = page.find("[data-slot='chart'][data-controller='ui--chart'][data-type='line']")
22
+ assert_match(/chart_js.*\.js\z/, root["data-ui--chart-module-url-value"])
23
+ assert_selector "canvas[role='img'][tabindex='0'][data-ui--chart-target='canvas']"
24
+ assert_includes page.find("canvas")["aria-label"], "Monthly sales."
25
+ assert_includes page.find("canvas")["aria-label"], "arrow keys"
26
+ end
27
+
28
+ def test_config_carries_the_type_labels_and_series
29
+ render_chart(type: :bar, stacked: true, series: two_series)
30
+ assert_equal "bar", config["type"]
31
+ assert config["stacked"]
32
+ assert_equal LABELS, config["labels"]
33
+ assert_equal [ "This year", "Last year" ], config["series"].map { |s| s["name"] }
34
+ assert_equal [ 1, 2, 3 ], config["series"].first["data"]
35
+ end
36
+
37
+ def test_stacked_only_applies_to_bars
38
+ render_chart(type: :line, stacked: true)
39
+ assert_equal false, config["stacked"]
40
+ end
41
+
42
+ def test_unknown_type_and_format_fall_back
43
+ render_chart(type: :radar, format: :weird)
44
+ assert_equal "line", config["type"]
45
+ assert_equal "number", config["axis"]["style"]
46
+ end
47
+
48
+ def test_series_take_the_colours_in_order_unless_given_one
49
+ render_chart(series: [ { name: "A", data: [ 1, 2, 3 ] }, { name: "B", data: [ 1, 2, 3 ], color: 5 }, { name: "C", data: [ 1, 2, 3 ], color: 9 } ])
50
+ assert_equal [ 1, 5, 3 ], config["series"].map { |s| s["color"] }
51
+ end
52
+
53
+ def test_more_than_five_series_is_refused
54
+ six = Array.new(6) { |i| { name: "S#{i}", data: [ 1, 2, 3 ] } }
55
+ error = assert_raises(ArgumentError) { render_chart(series: six) }
56
+ assert_includes error.message, "Other"
57
+ end
58
+
59
+ def test_a_series_must_have_a_value_for_each_label
60
+ error = assert_raises(ArgumentError) { render_chart(series: [ { name: "Short", data: [ 1, 2 ] } ]) }
61
+ assert_includes error.message, "Short"
62
+ end
63
+
64
+ def test_one_series_has_no_legend
65
+ render_chart
66
+ assert_no_selector "ul li"
67
+ end
68
+
69
+ def test_two_series_get_a_legend_with_swatches_shaped_like_the_marks
70
+ render_chart(series: two_series)
71
+ assert_selector "ul li", count: 2
72
+ assert_selector "ul li span.bg-chart-1.h-0\\.5"
73
+ assert_selector "ul li", text: "Last year"
74
+ render_chart(type: :bar, series: two_series)
75
+ assert_selector "ul li span.bg-chart-2.size-2\\.5"
76
+ end
77
+
78
+ def test_table_has_every_value_written_out
79
+ render_chart(label: "Monthly sales")
80
+ assert_selector "details summary", text: "Show the data"
81
+ assert_selector "table caption", text: "Monthly sales", visible: :all
82
+ assert_selector "th[scope='col']", text: "Sales", visible: :all
83
+ assert_selector "th[scope='row']", text: "Feb", visible: :all
84
+ assert_selector "td", text: "1,500.5", visible: :all
85
+ end
86
+
87
+ def test_currency_and_percent_formats
88
+ render_chart(format: :currency)
89
+ assert_selector "td", text: "$1,200.00", visible: :all
90
+ assert_equal [ "$1,200.00", "$1,500.50", "$900.00" ], config["series"].first["text"]
91
+ render_chart(format: :currency, unit: "₹")
92
+ assert_selector "td", text: "₹1,200.00", visible: :all
93
+ assert_equal "₹", config["axis"]["unit"]
94
+ render_chart(format: :percent, series: [ { name: "Margin", data: [ 38.25, 40, 41.5 ] } ])
95
+ assert_selector "td", text: "40%", visible: :all
96
+ assert_selector "td", text: "41.5%", visible: :all
97
+ end
98
+
99
+ def test_missing_values_leave_a_gap
100
+ render_chart(series: [ { name: "Sales", data: [ 1, nil, 3 ] } ])
101
+ assert_nil config["series"].first["data"][1]
102
+ assert_selector "td", text: "—", visible: :all
103
+ end
104
+
105
+ def render_pie(type: :pie, labels: %w[Card Cash UPI], data: [ 600, 300, 100 ], **opts)
106
+ render_inline(UI::Chart.new(type: type, labels: labels, series: [ { name: "Takings", data: data } ], **opts))
107
+ end
108
+
109
+ def test_pie_and_donut_are_types
110
+ render_pie
111
+ assert_equal "pie", config["type"]
112
+ render_pie(type: :donut)
113
+ assert_equal "donut", config["type"]
114
+ assert_selector "[data-slot='chart'][data-type='donut']"
115
+ end
116
+
117
+ def test_pie_slices_take_the_colours_in_order_and_carry_their_shares
118
+ render_pie
119
+ slice = config["series"].first
120
+ assert_equal [ 1, 2, 3 ], slice["colors"]
121
+ assert_equal %w[60% 30% 10%], slice["shares"]
122
+ end
123
+
124
+ def test_pie_legend_lists_each_slice_with_its_value_and_share
125
+ render_pie(format: :currency)
126
+ assert_selector "ul li", count: 3
127
+ assert_selector "ul li span.bg-chart-2.size-2\\.5"
128
+ assert_selector "ul li", text: /Cash\s*\$300\.00\s*30%/
129
+ end
130
+
131
+ def test_a_single_slice_still_gets_a_legend
132
+ render_pie(labels: %w[Card], data: [ 5 ])
133
+ assert_selector "ul li", count: 1
134
+ end
135
+
136
+ def test_pie_table_adds_a_share_column
137
+ render_pie
138
+ assert_selector "th[scope='col']", text: "Share", visible: :all
139
+ assert_selector "td", text: "60%", visible: :all
140
+ end
141
+
142
+ def test_donut_carries_its_total
143
+ render_pie(type: :donut, format: :currency)
144
+ assert_equal "$1,000.00", config["total"]
145
+ assert_equal "Total", config["total_label"]
146
+ end
147
+
148
+ def test_line_and_bar_carry_no_slices
149
+ render_chart
150
+ assert_nil config["total"]
151
+ assert_nil config["series"].first["shares"]
152
+ end
153
+
154
+ def test_pie_takes_one_series
155
+ error = assert_raises(ArgumentError) do
156
+ render_inline(UI::Chart.new(type: :pie, labels: LABELS, series: [ { name: "A", data: [ 1, 2, 3 ] }, { name: "B", data: [ 1, 2, 3 ] } ]))
157
+ end
158
+ assert_includes error.message, "one series"
159
+ end
160
+
161
+ def test_pie_draws_up_to_five_slices
162
+ error = assert_raises(ArgumentError) { render_pie(labels: %w[a b c d e f], data: [ 1, 1, 1, 1, 1, 1 ]) }
163
+ assert_includes error.message, "Other"
164
+ end
165
+
166
+ def test_pie_refuses_values_below_zero
167
+ error = assert_raises(ArgumentError) { render_pie(data: [ 5, -1, 3 ]) }
168
+ assert_includes error.message, "below zero"
169
+ end
170
+
171
+ def test_pie_with_no_total_shows_no_shares
172
+ render_pie(data: [ 0, 0, nil ])
173
+ assert_equal %w[— — —], config["series"].first["shares"]
174
+ end
175
+
176
+ def test_names_are_escaped
177
+ render_chart(series: [ { name: "<b>Sales</b>", data: [ 1, 2, 3 ] }, { name: "Other", data: [ 1, 2, 3 ] } ])
178
+ assert_no_selector "b"
179
+ assert_selector "li", text: "<b>Sales</b>"
180
+ end
181
+
182
+ def test_height_class_and_attributes
183
+ render_chart(height: 200, class: "max-w-xl", id: "sales", data: { foo: "bar" })
184
+ assert_selector "#sales.max-w-xl[data-foo='bar'][data-controller='ui--chart']"
185
+ assert_selector "div.relative[style='height: 200px'] canvas"
186
+ end
187
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ class UI::Command::GroupTest < ViewComponent::TestCase
6
+ def setup_fixtures(*); end
7
+ def teardown_fixtures(*); end
8
+
9
+ def test_a_group_named_by_its_heading
10
+ render_inline(UI::Command::Group.new(heading: "Pages")) { |g| g.with_item(label: "Home", href: "/") }
11
+ heading = page.find("[role='group'] > div", text: "Pages")
12
+ assert_selector "[role='group'][data-ui--command-target='group'][aria-labelledby='#{heading[:id]}'] [role='option']", text: "Home"
13
+ end
14
+
15
+ def test_a_group_without_a_heading
16
+ render_inline(UI::Command::Group.new(class: "pt-0")) { |g| g.with_item(label: "Home") }
17
+ assert_selector "[role='group'].pt-0:not([aria-labelledby])"
18
+ end
19
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ class UI::Command::ItemTest < ViewComponent::TestCase
6
+ def setup_fixtures(*); end
7
+ def teardown_fixtures(*); end
8
+
9
+ def test_an_option_carrying_what_the_controller_needs
10
+ render_inline(UI::Command::Item.new(label: "Settings", href: "/settings", id: "s"))
11
+ assert_selector "div#s[role='option'][aria-selected='false'][data-ui--command-target='item'][data-label='Settings'][data-href='/settings'][data-value='/settings']", text: "Settings"
12
+ end
13
+
14
+ def test_value_defaults_to_href_then_label
15
+ render_inline(UI::Command::Item.new(label: "New invoice", value: "new-invoice"))
16
+ assert_selector "[data-value='new-invoice']"
17
+ assert_no_selector "[data-href]"
18
+ render_inline(UI::Command::Item.new(label: "Log out"))
19
+ assert_selector "[data-value='Log out']"
20
+ end
21
+
22
+ def test_icon_description_and_keywords
23
+ render_inline(UI::Command::Item.new(label: "Invoices", icon: "receipt", description: "All bills, paid and unpaid", keywords: %w[bills money]))
24
+ assert_selector "span[aria-hidden='true'] svg[data-icon='receipt']"
25
+ assert_selector "span.text-xs.text-muted-foreground", text: "All bills, paid and unpaid"
26
+ assert_selector "[data-keywords='bills money']"
27
+ end
28
+
29
+ def test_a_shortcut_is_shown_and_bound_only_when_asked
30
+ render_inline(UI::Command::Item.new(label: "Settings", shortcut: "mod+,", id: "s"))
31
+ assert_selector "[role='option'] [data-slot='kbd'].ms-auto"
32
+ assert_no_selector "[data-ui--kbd-keys-value]"
33
+ render_inline(UI::Command::Item.new(label: "Settings", shortcut: "mod+,", bind: true, id: "s"))
34
+ assert_selector "[data-ui--kbd-keys-value='mod+,'][data-ui--kbd-target-value='#s']"
35
+ end
36
+
37
+ def test_disabled_items_say_so_and_do_not_bind
38
+ render_inline(UI::Command::Item.new(label: "Export", shortcut: "mod+e", bind: true, disabled: true))
39
+ assert_selector "[role='option'][aria-disabled='true']"
40
+ assert_no_selector "[data-ui--kbd-keys-value]"
41
+ end
42
+
43
+ def test_a_block_replaces_how_it_looks_but_not_its_label
44
+ render_inline(UI::Command::Item.new(label: "Ada Lovelace")) { "<strong>Ada</strong> Lovelace".html_safe }
45
+ assert_selector "[role='option'][data-label='Ada Lovelace'] strong", text: "Ada"
46
+ end
47
+ end
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ class UI::CommandTest < ViewComponent::TestCase
6
+ def setup_fixtures(*); end
7
+ def teardown_fixtures(*); end
8
+
9
+ def render_command(**opts)
10
+ render_inline(UI::Command.new(id: "cmd", **opts)) do |command|
11
+ command.with_trigger if opts[:dialog]
12
+ command.with_group(heading: "Pages") do |group|
13
+ group.with_item(label: "Dashboard", href: "/dashboard", icon: "house")
14
+ group.with_item(label: "Settings", href: "/settings", shortcut: "mod+,")
15
+ end
16
+ command.with_group(heading: "Actions") do |group|
17
+ group.with_item(label: "New invoice", value: "new-invoice", keywords: %w[create bill])
18
+ end
19
+ end
20
+ end
21
+
22
+ def test_a_combobox_over_a_listbox
23
+ render_command
24
+ assert_selector "[data-controller='ui--command'] input#cmd-input[role='combobox'][aria-controls='cmd-list'][aria-expanded='true'][aria-autocomplete='list']"
25
+ assert_selector "#cmd-list[role='listbox'][aria-label='Command menu']"
26
+ assert_selector "input[placeholder='Type a command or search…'][aria-label='Command menu']"
27
+ end
28
+
29
+ def test_groups_and_items_are_listed_in_order
30
+ render_command
31
+ assert_equal %w[Pages Actions], page.all("[role='group'] > div:first-child").map(&:text)
32
+ assert_equal [ "Dashboard", "Settings", "New invoice" ], page.all("[role='option']").map { |o| o["data-label"] }
33
+ assert_selector "[role='group'][aria-labelledby]", count: 2
34
+ end
35
+
36
+ def test_on_a_page_it_is_the_panel_with_no_dialog_or_footer
37
+ render_command
38
+ assert_no_selector "dialog"
39
+ assert_no_selector "button[data-action='ui--command#open']"
40
+ assert_selector "[data-ui--command-dialog-value='false'][data-ui--command-shortcut-value='']"
41
+ assert_no_text "to move"
42
+ end
43
+
44
+ def test_as_a_dialog_it_has_a_trigger_a_shortcut_and_a_footer
45
+ render_command(dialog: true)
46
+ assert_selector "dialog[aria-label='Command menu'][data-ui--command-target='dialog'] [data-ui--command-target='panel']"
47
+ assert_selector "button[data-action='ui--command#open'][aria-haspopup='dialog']", text: "Search…"
48
+ assert_selector "button[data-action='ui--command#open'] [data-slot='kbd']"
49
+ assert_selector "[data-ui--command-dialog-value='true'][data-ui--command-shortcut-value='mod+k']"
50
+ assert_text "to move"
51
+ assert_text "to close"
52
+ end
53
+
54
+ def test_the_shortcut_can_change_or_go
55
+ render_command(dialog: true, shortcut: "/")
56
+ assert_selector "[data-ui--command-shortcut-value='/']"
57
+ render_command(dialog: true, shortcut: nil)
58
+ assert_selector "[data-ui--command-shortcut-value='']"
59
+ assert_no_selector "button[data-action='ui--command#open'] [data-slot='kbd']"
60
+ end
61
+
62
+ def test_more_than_one_shortcut_the_trigger_showing_the_first
63
+ render_inline(UI::Command.new(dialog: true, shortcut: [ "mod+k", "/" ])) { |c| c.with_trigger }
64
+ assert_selector "[data-ui--command-shortcut-value='mod+k /']"
65
+ assert_selector "button[data-action='ui--command#open'] [data-slot='kbd']", count: 1
66
+ assert_selector "button [data-slot='kbd'] kbd", text: "K"
67
+ end
68
+
69
+ def test_the_trigger_keeps_its_own_aria_and_data
70
+ render_inline(UI::Command.new(dialog: true)) do |c|
71
+ c.with_trigger(aria: { label: "Search the docs" }, data: { testid: "open", action: "click->other#thing" })
72
+ end
73
+ assert_selector "button[aria-label='Search the docs'][aria-haspopup='dialog'][data-testid='open']"
74
+ assert_selector "button[data-action='ui--command#open click->other#thing']"
75
+ end
76
+
77
+ def test_sorting_groups_and_saying_what_empty_means
78
+ render_command(sort_groups: true, empty: "Nothing like that here.")
79
+ assert_selector "[data-ui--command-sort-groups-value='true'][data-ui--command-empty-value='Nothing like that here.']"
80
+ render_command
81
+ assert_selector "[data-ui--command-sort-groups-value='false'][data-ui--command-empty-value='No results found.']"
82
+ end
83
+
84
+ def test_a_trigger_with_its_own_label_or_content
85
+ render_inline(UI::Command.new(dialog: true)) { |c| c.with_trigger(label: "Jump to…", class: "w-64") }
86
+ assert_selector "button.w-64", text: "Jump to…"
87
+ render_inline(UI::Command.new(dialog: true)) { |c| c.with_trigger(variant: :ghost) { "Open" } }
88
+ assert_selector "button[data-action='ui--command#open']", exact_text: "Open"
89
+ end
90
+
91
+ def test_searching_the_server_is_set_up_only_with_a_url
92
+ render_command
93
+ assert_no_selector "[data-ui--command-url-value]"
94
+ render_command(url: "/search", param: "term", results_heading: "Customers")
95
+ assert_selector "[data-ui--command-url-value='/search'][data-ui--command-param-value='term'][data-ui--command-results-heading-value='Customers']"
96
+ assert_selector "template[data-ui--command-target='template']", visible: :all
97
+ end
98
+
99
+ def test_messages_for_the_controller
100
+ render_command
101
+ assert_selector "[data-ui--command-empty-value='No results found.'][data-ui--command-searching-value='Searching…']"
102
+ assert_selector "[data-ui--command-target='empty'][hidden]", visible: :all
103
+ assert_selector "[data-ui--command-target='status'][role='status'][hidden]", visible: :all
104
+ end
105
+
106
+ def test_custom_placeholder_label_and_attributes
107
+ render_inline(UI::Command.new(placeholder: "Find a page", label: "Site search", class: "max-w-md", data: { testid: "c" }))
108
+ assert_selector "[data-controller='ui--command'].max-w-md[data-testid='c'] input[placeholder='Find a page'][aria-label='Site search']"
109
+ end
110
+ end