velvetkit 0.5.1 → 0.6.1

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 (54) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +44 -0
  3. data/lib/velvetkit/version.rb +1 -1
  4. data/registry/app/components/ui/carousel.html.erb +9 -4
  5. data/registry/app/components/ui/carousel.rb +42 -1
  6. data/registry/app/components/ui/file_tree/file_item.html.erb +9 -0
  7. data/registry/app/components/ui/file_tree/file_item.rb +50 -0
  8. data/registry/app/components/ui/file_tree/folder.html.erb +9 -0
  9. data/registry/app/components/ui/file_tree/folder.rb +76 -0
  10. data/registry/app/components/ui/file_tree.html.erb +1 -0
  11. data/registry/app/components/ui/file_tree.rb +93 -0
  12. data/registry/app/components/ui/rating.html.erb +16 -0
  13. data/registry/app/components/ui/rating.rb +200 -0
  14. data/registry/app/components/ui/sidebar/menu_button.html.erb +10 -1
  15. data/registry/app/components/ui/sidebar/menu_button.rb +44 -6
  16. data/registry/app/components/ui/sidebar/menu_item.rb +4 -3
  17. data/registry/app/components/ui/sidebar/wrapper.html.erb +1 -1
  18. data/registry/app/components/ui/sidebar/wrapper.rb +45 -7
  19. data/registry/app/components/ui/sidebar.html.erb +10 -1
  20. data/registry/app/components/ui/sidebar.rb +60 -4
  21. data/registry/app/javascript/controllers/ui/carousel_controller.ts +59 -1
  22. data/registry/app/javascript/controllers/ui/file_tree_controller.ts +189 -0
  23. data/registry/app/javascript/controllers/ui/rating_controller.ts +117 -0
  24. data/registry/app/javascript/controllers/ui/sidebar_controller.ts +17 -0
  25. data/registry/app/javascript/controllers/ui/tooltip_controller.ts +3 -0
  26. data/registry/components.yml +4 -0
  27. data/registry/config/locales/velvetkit.en.yml +13 -0
  28. data/registry/test/components/previews/ui/carousel_preview/with_indicators.html.erb +10 -0
  29. data/registry/test/components/previews/ui/carousel_preview.rb +7 -3
  30. data/registry/test/components/previews/ui/file_tree_preview/changes.html.erb +13 -0
  31. data/registry/test/components/previews/ui/file_tree_preview/from_data.html.erb +14 -0
  32. data/registry/test/components/previews/ui/file_tree_preview/picking_a_file.html.erb +25 -0
  33. data/registry/test/components/previews/ui/file_tree_preview/without_guides.html.erb +10 -0
  34. data/registry/test/components/previews/ui/file_tree_preview.rb +41 -0
  35. data/registry/test/components/previews/ui/image_preview/radii.html.erb +3 -3
  36. data/registry/test/components/previews/ui/image_preview/sizing.html.erb +9 -4
  37. data/registry/test/components/previews/ui/image_preview.rb +14 -1
  38. data/registry/test/components/previews/ui/rating_preview/in_form.html.erb +6 -0
  39. data/registry/test/components/previews/ui/rating_preview/model_errors.html.erb +20 -0
  40. data/registry/test/components/previews/ui/rating_preview/playground.html.erb +5 -0
  41. data/registry/test/components/previews/ui/rating_preview/scores.html.erb +18 -0
  42. data/registry/test/components/previews/ui/rating_preview/sizes.html.erb +6 -0
  43. data/registry/test/components/previews/ui/rating_preview.rb +28 -0
  44. data/registry/test/components/previews/ui/sidebar_preview/_top_bar.html.erb +19 -0
  45. data/registry/test/components/previews/ui/sidebar_preview/icon_rail.html.erb +81 -0
  46. data/registry/test/components/previews/ui/sidebar_preview/site_header.html.erb +52 -0
  47. data/registry/test/components/previews/ui/sidebar_preview.rb +23 -0
  48. data/registry/test/components/ui/carousel_test.rb +48 -0
  49. data/registry/test/components/ui/file_tree_test.rb +187 -0
  50. data/registry/test/components/ui/rating_test.rb +271 -0
  51. data/registry/test/components/ui/sidebar/menu_button_test.rb +49 -0
  52. data/registry/test/components/ui/sidebar/wrapper_test.rb +30 -0
  53. data/registry/test/components/ui/sidebar_test.rb +50 -0
  54. metadata +28 -1
@@ -0,0 +1,52 @@
1
+ <%# The header stretched across the top, the sidebar and page below it. The
2
+ wrapper's `header` slot is all it takes: the sidebar sticks just under
3
+ the header, as tall as what's left, with no height to work out by hand.
4
+ The phone's menu button goes in the header, since there's no sidebar
5
+ to put it in until it opens. The current page is filled in:
6
+ `shape: :block`. %>
7
+ <% sections = UI::SidebarPreview::SECTIONS %>
8
+ <% projects = ["Website redesign", "Spring campaign", "Mobile onboarding", "Pricing page", "Support handbook", "Q3 planning", "Partner portal", "Brand refresh"] %>
9
+
10
+ <%# Fills the preview pane, so the sidebar's footer is in sight. %>
11
+ <div class="relative h-[calc(100svh-3rem)] min-h-96 overflow-hidden rounded-lg border transform-gpu">
12
+ <%= render(UI::Sidebar::Wrapper.new(min_height: :none, class: "h-full")) do |wrapper| %>
13
+ <% wrapper.with_header(class: "gap-2 px-3") do %>
14
+ <%= render(UI::Sidebar::Trigger.new(sidebar_id: "preview-sidebar-site-header", class: "md:hidden")) %>
15
+ <%= render "ui/sidebar_preview/top_bar" %>
16
+ <% end %>
17
+
18
+ <%= render(UI::Sidebar.new(id: "preview-sidebar-site-header", collapsible: :icon)) do |sidebar| %>
19
+ <% sidebar.with_sidebar_content do |content| %>
20
+ <% content.with_sidebar_group(class: "px-3 py-4 group-data-[collapsible=icon]:px-2") do |group| %>
21
+ <% group.with_menu do |menu| %>
22
+ <% sections.each do |key, nav| %>
23
+ <% menu.with_menu_item do |item| %>
24
+ <% item.with_menu_button("#", active: key == "projects", shape: :block, tooltip: nav[:text]) do %>
25
+ <%= render(UI::Icon.new(name: nav[:icon], size: :sm)) %>
26
+ <span><%= nav[:text] %></span>
27
+ <% end %>
28
+ <% end %>
29
+ <% end %>
30
+ <% end %>
31
+ <% end %>
32
+ <% end %>
33
+ <% sidebar.with_sidebar_footer(class: "border-t border-sidebar-border px-3 group-data-[collapsible=icon]:px-2 max-md:hidden") do %>
34
+ <%= render(UI::Sidebar::Trigger.new(sidebar_id: "preview-sidebar-site-header")) %>
35
+ <% end %>
36
+ <% end %>
37
+
38
+ <%= render(UI::Sidebar::Inset.new(class: "@container overflow-y-auto")) do %>
39
+ <div class="mx-auto w-full max-w-4xl space-y-6 p-6 @xl:p-8">
40
+ <div class="space-y-1">
41
+ <h1 class="text-2xl font-semibold">Projects</h1>
42
+ <p class="text-sm text-muted-foreground">Scroll this page: the header and the sidebar stay put.</p>
43
+ </div>
44
+ <div class="space-y-3">
45
+ <% projects.each do |name| %>
46
+ <div class="rounded-lg border px-4 py-5 text-sm"><%= name %></div>
47
+ <% end %>
48
+ </div>
49
+ </div>
50
+ <% end %>
51
+ <% end %>
52
+ </div>
@@ -15,4 +15,27 @@ class UI::SidebarPreview < ViewComponent::Preview
15
15
 
16
16
  # @label Pill Links, Sub-menus and a Storage Meter
17
17
  def pill_links; render_with_template; end
18
+
19
+ # The sections the full-width-header previews show, each with the pages
20
+ # listed beside the rail when it's open.
21
+ SECTIONS = {
22
+ "home" => { icon: "home", text: "Home",
23
+ pages: [ [ "layout-dashboard", "Dashboard" ], [ "list", "Activity" ], [ "calendar", "Calendar" ] ] },
24
+ "projects" => { icon: "folder-kanban", text: "Projects",
25
+ pages: [ [ "folder", "All projects" ], [ "star", "Starred" ], [ "package", "Archived" ] ] },
26
+ "inbox" => { icon: "inbox", text: "Inbox",
27
+ pages: [ [ "mail", "Unread" ], [ "message-circle", "Mentions" ], [ "check", "Done" ] ] },
28
+ "reports" => { icon: "bar-chart-3", text: "Reports",
29
+ pages: [ [ "chart-column", "Revenue" ], [ "globe", "Traffic" ], [ "download", "Exports" ] ] },
30
+ "team" => { icon: "users", text: "Team",
31
+ pages: [ [ "users", "Members" ], [ "user-plus", "Invites" ], [ "shield", "Roles" ] ] },
32
+ "settings" => { icon: "settings", text: "Settings",
33
+ pages: [ [ "settings", "General" ], [ "credit-card", "Billing" ], [ "lock", "Security" ] ] }
34
+ }.freeze
35
+
36
+ # @label Header Across the Top
37
+ def site_header; render_with_template; end
38
+
39
+ # @label Icon Rail and Nested Sidebar
40
+ def icon_rail; render_with_template; end
18
41
  end
@@ -30,6 +30,54 @@ class UI::CarouselTest < ViewComponent::TestCase
30
30
  assert_selector "button[aria-label=Next][hidden][aria-controls='#{track}'] svg[data-icon='chevron-right']", visible: :all
31
31
  end
32
32
 
33
+ def test_no_indicators_unless_asked
34
+ render_carousel
35
+ assert_no_selector "[data-ui--carousel-target=indicator]", visible: :all
36
+ assert_no_selector "[role=group]", visible: :all
37
+ end
38
+
39
+ def test_indicators_a_hidden_one_for_each_card_until_the_controller_shows_them
40
+ render_carousel(indicators: true)
41
+ track = page.find("ul")[:id]
42
+ group = page.find("[role=group][aria-label='Choose a slide']")
43
+ indicators = group.all("button[type=button][hidden][data-ui--carousel-target=indicator]", visible: :all)
44
+ assert_equal [ "Go to slide 1", "Go to slide 2", "Go to slide 3" ], indicators.map { _1[:"aria-label"] }
45
+ assert_equal %w[0 1 2], indicators.map { _1[:"data-ui--carousel-index-param"] }
46
+ assert indicators.all? { _1[:"aria-controls"] == track && _1[:"data-action"] == "ui--carousel#go" }
47
+ end
48
+
49
+ # The indicators draw no focus ring: the lit one shows which card is in view.
50
+ def test_an_indicator_draws_no_focus_ring
51
+ render_carousel(indicators: true)
52
+ classes = page.first("[data-ui--carousel-target=indicator]", visible: :all)[:class].split
53
+ refute classes.any? { _1.include?("focus") || _1.include?("ring") }
54
+ assert_includes classes, "outline-none"
55
+ end
56
+
57
+ # The controller sets --fill as the strip scrolls; the indicator widens and fills
58
+ # with it, with no transition to trail behind the scroll.
59
+ def test_an_indicator_widens_and_fills_with_the_scroll
60
+ render_carousel(indicators: true)
61
+ indicator = page.first("[data-ui--carousel-target=indicator]", visible: :all)
62
+ classes = indicator[:class].split
63
+ assert_includes classes, "w-[calc(0.5rem+1rem*var(--fill,0))]"
64
+ refute classes.any? { _1.start_with?("transition") }
65
+ assert_includes indicator.find("span[aria-hidden=true]", visible: :all)[:class].split, "opacity-[var(--fill,0)]"
66
+ end
67
+
68
+ # The indicators sit under the strip, so the buttons centre on the strip alone.
69
+ def test_the_buttons_centre_on_the_strip_not_the_indicators
70
+ render_carousel(indicators: true)
71
+ assert_selector "[role=region] > div.relative > ul + button + button", visible: :all
72
+ assert_selector "[role=region] > div.relative + [role=group]", visible: :all
73
+ end
74
+
75
+ # 0.6.0 called them dots.
76
+ def test_dots_still_turns_on_the_indicators
77
+ render_carousel(dots: true)
78
+ assert_selector "[data-ui--carousel-target=indicator]", count: 3, visible: :all
79
+ end
80
+
33
81
  def test_gap_class_and_attributes
34
82
  render_carousel(gap: 6, class: "mt-4", id: "highlights")
35
83
  assert_selector "#highlights.mt-4"
@@ -0,0 +1,187 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ class UI::FileTreeTest < ViewComponent::TestCase
6
+ def setup_fixtures(*); end
7
+ def teardown_fixtures(*); end
8
+
9
+ def render_project(active: false, open: nil, **opts)
10
+ render_inline(UI::FileTree.new(**opts)) do |tree|
11
+ tree.with_folder(name: "app", open: open) do |app|
12
+ app.with_folder(name: "models") do |models|
13
+ models.with_file(name: "user.rb", href: "/blob/app/models/user.rb", active: active)
14
+ end
15
+ app.with_file(name: "application.rb", href: "/blob/app/application.rb")
16
+ end
17
+ tree.with_file(name: "README.md", href: "/blob/README.md")
18
+ end
19
+ end
20
+
21
+ def test_renders_a_labelled_tree_wired_to_its_controller
22
+ render_project
23
+ assert_selector "ul[role='tree'][aria-label='Files'][data-slot='file-tree'][data-controller='ui--file-tree']"
24
+ assert_selector "ul[role='tree'][data-action='click->ui--file-tree#click keydown->ui--file-tree#keydown']"
25
+ end
26
+
27
+ def test_label_names_the_tree
28
+ render_project(label: "Project files")
29
+ assert_selector "ul[role='tree'][aria-label='Project files']"
30
+ end
31
+
32
+ def test_rows_are_treeitems_in_the_order_given
33
+ render_project
34
+ assert_equal %w[app models user.rb application.rb README.md],
35
+ page.all("[role='treeitem']", visible: :all).map { |row| row["data-name"] }
36
+ assert_selector "li[role='none'] > [role='treeitem']", count: 5, visible: :all
37
+ assert_selector "[role='treeitem'][data-ui--file-tree-target='item']", count: 5, visible: :all
38
+ end
39
+
40
+ def test_rows_carry_their_level
41
+ render_project
42
+ { "app" => 1, "models" => 2, "user.rb" => 3, "application.rb" => 2, "README.md" => 1 }.each do |name, level|
43
+ assert_selector "[role='treeitem'][data-name='#{name}'][aria-level='#{level}']", visible: :all
44
+ end
45
+ end
46
+
47
+ def test_only_the_first_row_is_in_the_tab_order
48
+ render_project
49
+ assert_selector "[role='treeitem'][tabindex='0']", count: 1
50
+ assert_selector "[role='treeitem'][data-name='app'][tabindex='0']"
51
+ assert_selector "[role='treeitem'][tabindex='-1']", count: 4, visible: :all
52
+ end
53
+
54
+ def test_a_folder_owns_its_group
55
+ render_project
56
+ folder = page.find("[data-slot='file-tree-folder'][data-name='app']")
57
+ assert_selector "ul##{folder['aria-owns']}[role='group'][data-slot='file-tree-group']", visible: :all
58
+ assert_selector "ul##{folder['aria-owns']} > li > [data-name='models']", visible: :all
59
+ end
60
+
61
+ def test_folders_start_closed
62
+ render_project
63
+ assert_selector "[data-name='app'][aria-expanded='false']"
64
+ assert_selector "ul[role='group'][hidden][data-state='closed']", count: 2, visible: :all
65
+ assert_no_selector "[data-name='README.md'][hidden]"
66
+ end
67
+
68
+ def test_an_open_folders_group_is_marked_open
69
+ render_project(open: true)
70
+ assert_selector "ul[role='group'][data-state='open']:not([hidden])", count: 1
71
+ end
72
+
73
+ def test_folders_holding_the_active_file_start_open
74
+ render_project(active: true)
75
+ assert_selector "[data-name='app'][aria-expanded='true']"
76
+ assert_selector "[data-name='models'][aria-expanded='true']"
77
+ assert_no_selector "ul[role='group'][hidden]", visible: :all
78
+ end
79
+
80
+ def test_open_decides_over_the_active_file
81
+ render_project(active: true, open: false)
82
+ assert_selector "[data-name='app'][aria-expanded='false']"
83
+ render_project(open: true)
84
+ assert_selector "[data-name='app'][aria-expanded='true']"
85
+ assert_selector "[data-name='models'][aria-expanded='false']"
86
+ end
87
+
88
+ def test_a_file_with_an_href_is_a_link
89
+ render_project
90
+ assert_selector "a[role='treeitem'][href='/blob/README.md'][data-slot='file-tree-file']"
91
+ end
92
+
93
+ def test_the_active_link_is_the_current_page_and_selected
94
+ render_project(active: true)
95
+ assert_selector "a[data-name='user.rb'][aria-current='page'][aria-selected='true']"
96
+ assert_selector "a[data-name='README.md'][aria-selected='false']"
97
+ assert_no_selector "a[data-name='README.md'][aria-current]"
98
+ end
99
+
100
+ def test_a_file_without_an_href_is_a_row_to_pick
101
+ render_inline(UI::FileTree.new) do |tree|
102
+ tree.with_file(name: "notes.md", value: "docs/notes.md", active: true)
103
+ tree.with_file(name: "todo.md")
104
+ end
105
+ assert_selector "span[role='treeitem'][data-name='notes.md'][data-value='docs/notes.md'][aria-selected='true']"
106
+ assert_selector "span[role='treeitem'][data-name='todo.md'][data-value='todo.md'][aria-selected='false']"
107
+ assert_no_selector "[aria-current]"
108
+ assert_no_selector "a"
109
+ end
110
+
111
+ def test_folders_are_not_selectable
112
+ render_project
113
+ assert_no_selector "[data-slot='file-tree-folder'][aria-selected]", visible: :all
114
+ end
115
+
116
+ def test_icons
117
+ render_project(active: true)
118
+ assert_selector "[data-name='app'] svg[data-icon='chevron-right']"
119
+ assert_selector "[data-name='app'] svg[data-icon='folder']"
120
+ assert_selector "[data-name='README.md'] svg[data-icon='file']"
121
+ assert_no_selector "[data-name='README.md'] svg[data-icon='chevron-right']"
122
+ end
123
+
124
+ def test_icon_swaps_the_icon
125
+ render_inline(UI::FileTree.new) do |tree|
126
+ tree.with_folder(name: "docs", icon: "book-open")
127
+ tree.with_file(name: "notes.md", icon: "file-text")
128
+ end
129
+ assert_selector "[data-name='docs'] svg[data-icon='book-open']"
130
+ assert_selector "[data-name='notes.md'] svg[data-icon='file-text']"
131
+ end
132
+
133
+ def test_a_files_block_shows_at_the_end_of_its_row
134
+ render_inline(UI::FileTree.new) { |tree| tree.with_file(name: "user.rb") { "M" } }
135
+ assert_selector "[data-name='user.rb'] > span:last-child", text: "M"
136
+ end
137
+
138
+ def test_guides_draw_a_line_down_open_folders
139
+ render_project
140
+ assert_selector "ul[role='group'].before\\:bg-border", count: 2, visible: :all
141
+ render_project(guides: false)
142
+ assert_no_selector "ul[role='group'].before\\:bg-border", visible: :all
143
+ assert_selector "ul[role='group'].ps-5\\.5", count: 2, visible: :all
144
+ end
145
+
146
+ def test_a_guide_starts_below_its_folders_focus_ring
147
+ render_project
148
+ assert_selector "[data-slot='file-tree-folder'].peer\\/row + ul.peer-focus\\/row\\:before\\:top-1\\.5",
149
+ count: 2, visible: :all
150
+ end
151
+
152
+ def test_builds_from_data
153
+ render_inline(UI::FileTree.new(items: [
154
+ { name: "app", children: [
155
+ { name: "models", children: [ { name: "user.rb", value: "app/models/user.rb", active: true } ] }
156
+ ] },
157
+ { "name" => "README.md", "href" => "/blob/README.md" }
158
+ ]))
159
+ assert_equal %w[app models user.rb README.md], page.all("[role='treeitem']").map { |row| row["data-name"] }
160
+ assert_selector "[data-name='app'][aria-expanded='true']"
161
+ assert_selector "[data-name='user.rb'][aria-level='3'][aria-selected='true'][data-value='app/models/user.rb']"
162
+ assert_selector "a[data-name='README.md'][href='/blob/README.md']"
163
+ end
164
+
165
+ def test_an_empty_children_list_is_an_empty_folder
166
+ render_inline(UI::FileTree.new(items: [ { name: "tmp", children: [] } ]))
167
+ assert_selector "[data-slot='file-tree-folder'][data-name='tmp'][aria-expanded='false']"
168
+ end
169
+
170
+ def test_data_and_slots_together
171
+ render_inline(UI::FileTree.new(items: [ { name: "Gemfile" } ])) { |tree| tree.with_file(name: "Rakefile") }
172
+ assert_equal %w[Gemfile Rakefile], page.all("[role='treeitem']").map { |row| row["data-name"] }
173
+ end
174
+
175
+ def test_class_and_attributes_pass_through
176
+ render_inline(UI::FileTree.new(class: "max-w-xs", id: "files", data: { turbo: false }, aria: { describedby: "hint" })) do |tree|
177
+ tree.with_folder(name: "app", class: "font-semibold", id: "app-folder")
178
+ tree.with_file(name: "README.md", class: "italic", data: { turbo_frame: "blob" })
179
+ end
180
+ assert_selector "ul#files.max-w-xs[role='tree'][data-turbo='false'][aria-describedby='hint'][aria-label='Files']"
181
+ assert_selector "ul#files[data-controller='ui--file-tree']"
182
+ assert_selector "span.font-semibold[data-name='app']"
183
+ assert_selector "ul#app-folder-group[role='group']", visible: :all
184
+ assert_selector "[data-name='app'][aria-owns='app-folder-group']"
185
+ assert_selector "span.italic[data-name='README.md'][data-turbo-frame='blob'][data-ui--file-tree-target='item']"
186
+ end
187
+ end
@@ -0,0 +1,271 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ class UI::RatingTest < ViewComponent::TestCase
6
+ def setup_fixtures(*); end
7
+ def teardown_fixtures(*); end
8
+
9
+ Review = Struct.new(:rating) do
10
+ include ActiveModel::Validations
11
+ def self.model_name = ActiveModel::Name.new(self, nil, "Review")
12
+ validates :rating, presence: true
13
+ end
14
+
15
+ def form_builder_for(record)
16
+ ActionView::Helpers::FormBuilder.new(:review, record, vc_test_controller.view_context, {})
17
+ end
18
+
19
+ # --- Shown ------------------------------------------------------------------
20
+
21
+ def test_shows_a_score_as_one_named_image
22
+ render_inline(UI::Rating.new(value: 4.5))
23
+ assert_selector "[role='img'][aria-label='Rated 4.5 out of 5']"
24
+ assert_no_selector "input"
25
+ assert_no_selector "[role='radiogroup']"
26
+ end
27
+
28
+ def test_fills_to_the_nearest_half_star
29
+ render_inline(UI::Rating.new(value: 3.7))
30
+ assert_selector "[role='img'] > svg.text-amber-500", count: 3
31
+ assert_selector "[role='img'] > span.relative", count: 1
32
+ assert_selector "[role='img'] > span.relative > span.w-1\\/2.overflow-hidden svg"
33
+ assert_selector "[role='img'] > svg.text-border.dark\\:text-muted", count: 1
34
+ end
35
+
36
+ def test_rounds_to_whole_stars_when_closer
37
+ render_inline(UI::Rating.new(value: 4.2))
38
+ assert_selector "[role='img'] > svg.text-amber-500", count: 4
39
+ assert_no_selector "[role='img'] > span.relative"
40
+ assert_selector "[role='img'][aria-label='Rated 4.2 out of 5']"
41
+ end
42
+
43
+ def test_no_score_is_five_empty_stars
44
+ render_inline(UI::Rating.new)
45
+ assert_selector "[role='img'][aria-label='Rated 0 out of 5'] > svg.text-border.dark\\:text-muted", count: 5
46
+ end
47
+
48
+ def test_a_score_out_of_range_is_held_to_it
49
+ render_inline(UI::Rating.new(value: 7))
50
+ assert_selector "[role='img'][aria-label='Rated 5 out of 5'] > svg.text-amber-500", count: 5
51
+ render_inline(UI::Rating.new(value: -2))
52
+ assert_selector "[role='img'][aria-label='Rated 0 out of 5']"
53
+ end
54
+
55
+ def test_a_score_given_as_a_string_or_decimal
56
+ render_inline(UI::Rating.new(value: "3.5"))
57
+ assert_selector "[role='img'][aria-label='Rated 3.5 out of 5']"
58
+ render_inline(UI::Rating.new(value: BigDecimal("4.0")))
59
+ assert_selector "[role='img'][aria-label='Rated 4 out of 5']"
60
+ end
61
+
62
+ def test_max_sets_how_many_stars
63
+ render_inline(UI::Rating.new(value: 7, max: 10))
64
+ assert_selector "[role='img'][aria-label='Rated 7 out of 10'] > svg", count: 10
65
+ end
66
+
67
+ def test_the_value_and_count_beside_the_stars
68
+ render_inline(UI::Rating.new(value: 4.5, count: 1234, show_value: true))
69
+ assert_selector "span[aria-hidden='true'].tabular-nums", text: "4.5"
70
+ assert_selector "span[aria-hidden='true']", text: "(1,234)"
71
+ assert_selector "span.sr-only", text: "1234 ratings"
72
+ end
73
+
74
+ def test_one_rating_is_singular
75
+ render_inline(UI::Rating.new(value: 5, count: 1))
76
+ assert_selector "span.sr-only", text: "1 rating"
77
+ end
78
+
79
+ def test_neither_value_nor_count_by_default
80
+ render_inline(UI::Rating.new(value: 4))
81
+ assert_no_selector ".tabular-nums"
82
+ assert_no_selector ".sr-only"
83
+ end
84
+
85
+ def test_another_icon
86
+ render_inline(UI::Rating.new(value: 2, icon: "circle-check"))
87
+ assert_selector "svg[data-icon='circle-check']", count: 5
88
+ end
89
+
90
+ def test_sizes
91
+ render_inline(UI::Rating.new(value: 2, size: :sm))
92
+ assert_includes page.find("[role='img']")[:class].split, "[&_svg]:size-4"
93
+ render_inline(UI::Rating.new(value: 2, size: :lg))
94
+ assert_includes page.find("[role='img']")[:class].split, "[&_svg]:size-6"
95
+ end
96
+
97
+ def test_class_and_attributes_on_the_shown_score
98
+ render_inline(UI::Rating.new(value: 4, class: "text-base", id: "score", data: { testid: "score" }))
99
+ score = page.find("span#score[data-testid='score']")
100
+ assert_includes score[:class].split, "text-base"
101
+ refute_includes score[:class].split, "text-sm"
102
+ end
103
+
104
+ # --- Chosen -----------------------------------------------------------------
105
+
106
+ def test_a_name_makes_it_a_radio_for_each_half_star
107
+ render_inline(UI::Rating.new(name: "review[rating]", id: "rate"))
108
+ assert_selector "div#rate[role='radiogroup'][aria-label='Rating'][data-controller='ui--rating']"
109
+ expected = %w[0.5 1 1.5 2 2.5 3 3.5 4 4.5 5]
110
+ actual = page.all("input[type='radio'][name='review[rating]']", visible: :all).map(&:value)
111
+ assert_equal expected, actual
112
+ assert_selector "label[for='rate-3'][data-ui--rating-target='star'] span.sr-only", text: "3 out of 5"
113
+ assert_no_selector "[role='img']"
114
+ end
115
+
116
+ def test_a_half_star_value_checks_its_option
117
+ render_inline(UI::Rating.new(name: "rating", value: 0.5))
118
+ assert_equal [ "0.5" ], page.all("input[checked]", visible: :all).map(&:value)
119
+ end
120
+
121
+ def test_a_fractional_value_checks_the_nearest_half_star
122
+ render_inline(UI::Rating.new(name: "rating", value: 3.6))
123
+ assert_equal [ "3.5" ], page.all("input[checked]", visible: :all).map(&:value)
124
+ end
125
+
126
+ def test_each_half_star_is_lit_by_css_up_to_the_checked_one
127
+ render_inline(UI::Rating.new(name: "rating"))
128
+ classes = page.find("label", match: :first)[:class].split
129
+ assert_includes classes, "[input:checked+&]:text-amber-500"
130
+ assert_includes classes, "[&:has(~input:checked)]:text-amber-500"
131
+ assert_includes classes, "data-[lit=true]:!text-amber-500"
132
+ end
133
+
134
+ # The stars draw no focus ring: the chosen score shows where the field is.
135
+ def test_a_half_star_draws_no_focus_ring
136
+ render_inline(UI::Rating.new(name: "rating"))
137
+ classes = page.find("label", match: :first)[:class].split
138
+ refute classes.any? { _1.include?("focus") || _1.include?("ring") || _1.include?("drop-shadow") }
139
+ end
140
+
141
+ # Both halves of a hovered star grow from its middle, so it grows as one.
142
+ def test_a_hovered_star_grows_from_its_middle
143
+ render_inline(UI::Rating.new(name: "rating"))
144
+ start_half, end_half = page.all("label", minimum: 2).first(2).map { _1[:class].split }
145
+ assert_includes start_half, "motion-safe:data-[hovered=true]:scale-125"
146
+ assert_includes start_half, "origin-right"
147
+ assert_includes start_half, "rtl:origin-left"
148
+ assert_includes end_half, "origin-left"
149
+ assert_includes end_half, "rtl:origin-right"
150
+ end
151
+
152
+ def test_each_radio_sits_before_its_half_star
153
+ render_inline(UI::Rating.new(name: "rating", max: 3))
154
+ expected = Array.new(6) { %w[input label] }.flatten
155
+ actual = page.all("[role='radiogroup'] > *", visible: :all).map(&:tag_name)
156
+ assert_equal expected, actual
157
+ end
158
+
159
+ def test_hover_reset_and_change_wiring
160
+ render_inline(UI::Rating.new(name: "rating"))
161
+ assert_selector "[data-action='mouseleave->ui--rating#reset reset@document->ui--rating#formReset']"
162
+ assert_selector "label[data-action='mouseenter->ui--rating#preview']", count: 10
163
+ assert_selector "input[data-action='change->ui--rating#choose']", count: 10, visible: :all
164
+ end
165
+
166
+ def test_label_names_the_field
167
+ render_inline(UI::Rating.new(name: "rating", label: "Delivery"))
168
+ assert_selector "[role='radiogroup'][aria-label='Delivery']"
169
+ end
170
+
171
+ def test_required_and_disabled
172
+ render_inline(UI::Rating.new(name: "rating", required: true, disabled: true))
173
+ assert_selector "input[required][disabled]", count: 10, visible: :all
174
+ assert_selector "label.cursor-not-allowed.opacity-50", count: 10
175
+ end
176
+
177
+ # Choosing clears the error with JavaScript, so it needs the error's id.
178
+ def test_the_error_to_clear_once_chosen
179
+ render_inline(UI::Rating.new(name: "rating", legend: "Delivery", error: "Pick a score"))
180
+ error_id = page.find("fieldset p", text: "Pick a score")[:id]
181
+ assert_selector "[data-controller='ui--rating'][data-ui--rating-error-value='#{error_id}']"
182
+ end
183
+
184
+ def test_no_error_to_clear
185
+ render_inline(UI::Rating.new(name: "rating", legend: "Delivery"))
186
+ assert_no_selector "[data-ui--rating-error-value]"
187
+ end
188
+
189
+ def test_a_field_shows_its_score_beside_the_stars
190
+ render_inline(UI::Rating.new(name: "rating", value: 3.5, show_value: true))
191
+ score = page.find("output[aria-hidden='true']", text: "3.5/5")
192
+ assert_selector "[data-ui--rating-score-value='#{score[:id]}']"
193
+ assert_selector "label[data-value='4'][data-score='4/5']"
194
+ assert_selector "label[data-value='4.5'][data-score='4.5/5']"
195
+ end
196
+
197
+ def test_a_field_with_no_score_shows_zero
198
+ render_inline(UI::Rating.new(name: "rating", show_value: true))
199
+ assert_selector "output", text: "0/5"
200
+ end
201
+
202
+ def test_a_field_shows_no_score_by_default
203
+ render_inline(UI::Rating.new(name: "rating"))
204
+ assert_no_selector "output"
205
+ assert_no_selector "[data-ui--rating-score-value]"
206
+ assert_no_selector "label[data-score]"
207
+ end
208
+
209
+ def test_thanks_show_once_chosen
210
+ render_inline(UI::Rating.new(name: "rating", legend: "Delivery", thanks: true))
211
+ thanks = page.find("p[role='status'][hidden].text-emerald-600", text: "Thank you for your rating", visible: :all)
212
+ assert_selector "[data-ui--rating-thanks-value='#{thanks[:id]}']"
213
+ end
214
+
215
+ def test_thanks_in_your_words
216
+ render_inline(UI::Rating.new(name: "rating", thanks: "Thanks, noted!"))
217
+ assert_selector "p[role='status']", text: "Thanks, noted!", visible: :all
218
+ end
219
+
220
+ def test_no_thanks_by_default
221
+ render_inline(UI::Rating.new(name: "rating"))
222
+ assert_no_selector "p[role='status']", visible: :all
223
+ assert_no_selector "[data-ui--rating-thanks-value]"
224
+ end
225
+
226
+ def test_a_legend_renders_through_fieldset
227
+ render_inline(UI::Rating.new(name: "rating", legend: "Your rating", hint: "Out of five.", required: true, id: "r"))
228
+ fieldset = page.find("fieldset[role='radiogroup'][aria-required='true']")
229
+ assert fieldset[:"aria-labelledby"].present?
230
+ assert_nil fieldset[:"aria-label"]
231
+ assert_selector "legend", text: "Your rating"
232
+ assert_selector "fieldset > p", text: "Out of five."
233
+ assert_selector "fieldset > div#r:not([role])"
234
+ assert_equal 1, page.all("[role='radiogroup']").size
235
+ end
236
+
237
+ def test_legend_true_without_a_form_still_names_it
238
+ render_inline(UI::Rating.new(name: "rating", legend: true, hint: "Out of five."))
239
+ assert_no_selector "legend"
240
+ assert_selector "fieldset[role='radiogroup'][aria-label='Rating']"
241
+ end
242
+
243
+ def test_bound_to_a_form
244
+ render_inline(UI::Rating.new(form: form_builder_for(Review.new(4)), field: :rating))
245
+ assert_equal [ "4" ], page.all("input[name='review[rating]'][checked]", visible: :all).map(&:value)
246
+ assert_no_selector "fieldset"
247
+ end
248
+
249
+ def test_bound_shows_the_models_errors_and_its_name
250
+ render_inline(UI::Rating.new(form: form_builder_for(Review.new(nil).tap(&:validate)), field: :rating, legend: true))
251
+ assert_selector "legend", text: "Rating"
252
+ assert_selector "fieldset[role='radiogroup'][aria-invalid='true']"
253
+ assert_selector "p.text-destructive", text: "Rating can't be blank"
254
+ assert_no_selector "input[checked]", visible: :all
255
+ end
256
+
257
+ def test_class_and_attributes_on_the_stars
258
+ render_inline(UI::Rating.new(name: "rating", class: "gap-2", data: { testid: "stars" }))
259
+ stars = page.find("[role='radiogroup'][data-testid='stars'][data-controller='ui--rating']")
260
+ assert_includes stars[:class].split, "gap-2"
261
+ end
262
+
263
+ def test_renders_no_whitespace_around_itself
264
+ render_inline(UI::Rating.new(name: "rating"))
265
+ assert rendered_content.start_with?("<div"), rendered_content[0, 20].inspect
266
+ render_inline(UI::Rating.new(name: "rating", legend: "Rating"))
267
+ assert rendered_content.start_with?("<fieldset"), rendered_content[0, 20].inspect
268
+ render_inline(UI::Rating.new(value: 3))
269
+ assert rendered_content.start_with?("<span"), rendered_content[0, 20].inspect
270
+ end
271
+ end
@@ -49,4 +49,53 @@ class UI::Sidebar::MenuButtonTest < ViewComponent::TestCase
49
49
  link = page.find("a", text: "Dashboard")
50
50
  assert_equal "step", link[:"aria-current"]
51
51
  end
52
+
53
+ def test_icon_shape_fills_the_current_section_and_names_it_in_a_tooltip
54
+ render_inline(UI::Sidebar::MenuButton.new("/projects", shape: :icon, active: true)) do
55
+ '<svg></svg><span>Projects</span>'.html_safe
56
+ end
57
+ link = page.find("a[href='/projects']")
58
+ assert_equal "icon", link[:"data-shape"]
59
+ assert_includes link[:class], "size-8"
60
+ assert_includes link[:class], "data-[active=true]:bg-sidebar-primary/15"
61
+ # The text stays for screen readers.
62
+ assert_includes link[:class], "[&>span]:sr-only"
63
+ assert_selector "[data-controller=ui--tooltip] [role=tooltip]", text: "Projects", visible: :all
64
+ end
65
+
66
+ def test_a_tooltip_of_its_own
67
+ render_inline(UI::Sidebar::MenuButton.new("#", tooltip: "Go home")) { "Home" }
68
+ assert_selector "[role=tooltip]", text: "Go home", visible: :all
69
+ end
70
+
71
+ def test_no_tooltip_unless_asked_for
72
+ render_inline(UI::Sidebar::MenuButton.new("#")) { "Home" }
73
+ assert_no_selector "[data-controller=ui--tooltip]"
74
+ end
75
+
76
+ def test_block_shape_fills_the_current_page_without_a_bar
77
+ render_inline(UI::Sidebar::MenuItem.new) do |item|
78
+ item.with_menu_button("#", active: true, shape: :block) { "Inbox" }
79
+ end
80
+ link = page.find("a", text: "Inbox")
81
+ assert_equal "block", link[:"data-shape"]
82
+ assert_includes link[:class], "rounded-lg"
83
+ assert_includes link[:class], "data-[active=true]:bg-sidebar-primary/15"
84
+ # The edge bar is for the default shape only.
85
+ assert_includes page.find("li")[:class], "has-[[data-active=true][data-shape=default]]:before:opacity-100"
86
+ end
87
+
88
+ def test_a_section_button_shows_its_panel_without_loading_a_page
89
+ render_inline(UI::Sidebar::MenuButton.new(section: "inbox", shape: :icon, active: true)) do
90
+ '<svg></svg><span>Inbox</span>'.html_safe
91
+ end
92
+ button = page.find("button[type=button][data-section=inbox]")
93
+ assert_equal "ui--sidebar#showSection", button[:"data-action"]
94
+ assert_equal "inbox", button[:"data-ui--sidebar-section-param"]
95
+ assert_equal UI::Sidebar.section_id("inbox"), button[:"aria-controls"]
96
+ assert_equal "true", button[:"aria-expanded"]
97
+ # It shows a panel rather than being the current page.
98
+ assert_nil button[:"aria-current"]
99
+ assert_no_selector "a"
100
+ end
52
101
  end