velvetkit 0.1.0.beta → 0.2.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.
- checksums.yaml +4 -4
- data/README.md +143 -75
- data/lib/velvetkit/version.rb +1 -1
- data/registry/app/components/ui/checkbox.html.erb +2 -0
- data/registry/app/components/ui/checkbox.rb +11 -5
- data/registry/app/components/ui/collapsible.html.erb +7 -0
- data/registry/app/components/ui/collapsible.rb +67 -0
- data/registry/app/components/ui/date_picker.html.erb +4 -1
- data/registry/app/components/ui/date_picker.rb +10 -2
- data/registry/app/components/ui/file_field.html.erb +4 -0
- data/registry/app/components/ui/file_field.rb +73 -0
- data/registry/app/components/ui/form_field/messages.html.erb +6 -0
- data/registry/app/components/ui/form_field/messages.rb +11 -0
- data/registry/app/components/ui/form_field.html.erb +9 -0
- data/registry/app/components/ui/form_field.rb +105 -0
- data/registry/app/components/ui/input.html.erb +1 -8
- data/registry/app/components/ui/input.rb +12 -36
- data/registry/app/components/ui/input_group/addon.rb +4 -1
- data/registry/app/components/ui/input_group/field.rb +4 -1
- data/registry/app/components/ui/input_group.rb +12 -10
- data/registry/app/components/ui/native_select.html.erb +2 -9
- data/registry/app/components/ui/native_select.rb +17 -31
- data/registry/app/components/ui/password_input.html.erb +1 -8
- data/registry/app/components/ui/password_input.rb +9 -27
- data/registry/app/components/ui/select.html.erb +4 -0
- data/registry/app/components/ui/select.rb +12 -29
- data/registry/app/components/ui/session_tabs/add.html.erb +1 -0
- data/registry/app/components/ui/session_tabs/add.rb +52 -0
- data/registry/app/components/ui/session_tabs/tab.html.erb +37 -0
- data/registry/app/components/ui/session_tabs/tab.rb +243 -0
- data/registry/app/components/ui/session_tabs.html.erb +15 -0
- data/registry/app/components/ui/session_tabs.rb +101 -0
- data/registry/app/components/ui/skeleton.html.erb +1 -0
- data/registry/app/components/ui/skeleton.rb +30 -0
- data/registry/app/components/ui/table_of_contents/item.html.erb +8 -0
- data/registry/app/components/ui/table_of_contents/item.rb +41 -0
- data/registry/app/components/ui/table_of_contents.html.erb +13 -0
- data/registry/app/components/ui/table_of_contents.rb +58 -0
- data/registry/app/components/ui/textarea.html.erb +2 -0
- data/registry/app/components/ui/textarea.rb +101 -0
- data/registry/app/components/ui/toast/viewport.html.erb +4 -0
- data/registry/app/components/ui/toast/viewport.rb +41 -0
- data/registry/app/components/ui/toast.html.erb +11 -0
- data/registry/app/components/ui/toast.rb +89 -0
- data/registry/app/javascript/controllers/ui/file_field_controller.ts +49 -0
- data/registry/app/javascript/controllers/ui/menu_controller.ts +3 -0
- data/registry/app/javascript/controllers/ui/session_tabs_controller.ts +732 -0
- data/registry/app/javascript/controllers/ui/table_of_contents_controller.ts +179 -0
- data/registry/app/javascript/controllers/ui/toast_controller.ts +62 -0
- data/registry/app/javascript/controllers/ui/toast_viewport_controller.ts +82 -0
- data/registry/app/javascript/velvetkit_icons.ts +21 -9
- data/registry/components.yml +17 -1
- data/registry/config/locales/velvetkit.en.yml +21 -0
- data/registry/test/components/previews/ui/collapsible_preview/default.html.erb +18 -0
- data/registry/test/components/previews/ui/collapsible_preview/show_more.html.erb +7 -0
- data/registry/test/components/previews/ui/collapsible_preview.rb +19 -0
- data/registry/test/components/previews/ui/file_field_preview/multiple_files.html.erb +3 -0
- data/registry/test/components/previews/ui/file_field_preview/with_error.html.erb +3 -0
- data/registry/test/components/previews/ui/file_field_preview.rb +18 -0
- data/registry/test/components/previews/ui/form_field_preview/custom_control.html.erb +6 -0
- data/registry/test/components/previews/ui/form_field_preview/default.html.erb +7 -0
- data/registry/test/components/previews/ui/form_field_preview/model_errors.html.erb +11 -0
- data/registry/test/components/previews/ui/form_field_preview.rb +12 -0
- data/registry/test/components/previews/ui/input_preview/hint_and_error.html.erb +4 -0
- data/registry/test/components/previews/ui/input_preview.rb +3 -0
- data/registry/test/components/previews/ui/session_tabs_preview/handling_events.html.erb +46 -0
- data/registry/test/components/previews/ui/session_tabs_preview/saved_views.html.erb +16 -0
- data/registry/test/components/previews/ui/session_tabs_preview/touch_with_confirmation.html.erb +20 -0
- data/registry/test/components/previews/ui/session_tabs_preview.rb +35 -0
- data/registry/test/components/previews/ui/sidebar_preview/_brand.html.erb +4 -4
- data/registry/test/components/previews/ui/sidebar_preview/with_dropdown_header.html.erb +1 -1
- data/registry/test/components/previews/ui/skeleton_preview/card_placeholder.html.erb +10 -0
- data/registry/test/components/previews/ui/skeleton_preview/shapes.html.erb +9 -0
- data/registry/test/components/previews/ui/skeleton_preview.rb +15 -0
- data/registry/test/components/previews/ui/table_of_contents_preview/default.html.erb +41 -0
- data/registry/test/components/previews/ui/table_of_contents_preview/flat.html.erb +17 -0
- data/registry/test/components/previews/ui/table_of_contents_preview.rb +9 -0
- data/registry/test/components/previews/ui/textarea_preview/autogrow.html.erb +4 -0
- data/registry/test/components/previews/ui/textarea_preview/default.html.erb +3 -0
- data/registry/test/components/previews/ui/textarea_preview/sizes.html.erb +5 -0
- data/registry/test/components/previews/ui/textarea_preview/states.html.erb +7 -0
- data/registry/test/components/previews/ui/textarea_preview.rb +33 -0
- data/registry/test/components/previews/ui/toast_preview/live_viewport.html.erb +21 -0
- data/registry/test/components/previews/ui/toast_preview/variants.html.erb +7 -0
- data/registry/test/components/previews/ui/toast_preview/with_action.html.erb +7 -0
- data/registry/test/components/previews/ui/toast_preview.rb +29 -0
- data/registry/test/components/ui/checkbox_test.rb +24 -0
- data/registry/test/components/ui/collapsible_test.rb +71 -0
- data/registry/test/components/ui/date_picker_test.rb +14 -0
- data/registry/test/components/ui/file_field_test.rb +101 -0
- data/registry/test/components/ui/form_field/messages_test.rb +19 -0
- data/registry/test/components/ui/form_field_test.rb +122 -0
- data/registry/test/components/ui/input_group_test.rb +30 -2
- data/registry/test/components/ui/input_test.rb +37 -0
- data/registry/test/components/ui/native_select_test.rb +25 -0
- data/registry/test/components/ui/password_input_test.rb +9 -0
- data/registry/test/components/ui/select_test.rb +14 -0
- data/registry/test/components/ui/session_tabs/add_test.rb +52 -0
- data/registry/test/components/ui/session_tabs_test.rb +281 -0
- data/registry/test/components/ui/skeleton_test.rb +65 -0
- data/registry/test/components/ui/table_of_contents/item_test.rb +38 -0
- data/registry/test/components/ui/table_of_contents_test.rb +120 -0
- data/registry/test/components/ui/textarea_test.rb +178 -0
- data/registry/test/components/ui/toast/viewport_test.rb +72 -0
- data/registry/test/components/ui/toast_test.rb +183 -0
- metadata +77 -4
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
4
|
+
|
|
5
|
+
class UI::FormFieldTest < ViewComponent::TestCase
|
|
6
|
+
def setup_fixtures(*); end
|
|
7
|
+
def teardown_fixtures(*); end
|
|
8
|
+
|
|
9
|
+
class Account
|
|
10
|
+
include ActiveModel::Model
|
|
11
|
+
attr_accessor :email, :name
|
|
12
|
+
validates :email, presence: true
|
|
13
|
+
validates :email, format: { with: /@/ }, allow_blank: true
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def form_builder_for(record)
|
|
17
|
+
ActionView::Helpers::FormBuilder.new(:account, record, vc_test_controller.view_context, {})
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def invalid_account
|
|
21
|
+
Account.new.tap(&:validate)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# ── Wrapping ─────────────────────────────────────────────────────────────────
|
|
25
|
+
|
|
26
|
+
def test_nothing_to_show_renders_the_control_alone
|
|
27
|
+
render_inline(UI::FormField.new) { "<input id='x'>".html_safe }
|
|
28
|
+
assert_selector "input#x"
|
|
29
|
+
refute_selector "div"
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def test_label_hint_and_error_wrap_the_control
|
|
33
|
+
render_inline(UI::FormField.new(label: "Colour", hint: "For your avatar", error: "Pick a colour", for: "colour")) do
|
|
34
|
+
"<input id='colour'>".html_safe
|
|
35
|
+
end
|
|
36
|
+
assert_selector "div > label[for='colour'] + input#colour + p#colour-hint + p#colour-error"
|
|
37
|
+
assert_selector "p#colour-hint.text-muted-foreground", text: "For your avatar"
|
|
38
|
+
assert_selector "p#colour-error.text-destructive", text: "Pick a colour"
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def test_block_receives_the_field_for_custom_controls
|
|
42
|
+
render_inline(UI::FormField.new(label: "Colour", hint: "For your avatar", error: "Pick a colour")) do |field|
|
|
43
|
+
ActionController::Base.helpers.tag.input(type: "color", **field.control_attributes)
|
|
44
|
+
end
|
|
45
|
+
input = page.find("input[type='color']")
|
|
46
|
+
assert_equal page.find("label")[:for], input[:id]
|
|
47
|
+
assert input[:id].start_with?("ui-field-")
|
|
48
|
+
assert_equal "#{input[:id]}-hint #{input[:id]}-error", input["aria-describedby"]
|
|
49
|
+
assert_equal "true", input["aria-invalid"]
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def test_user_class_and_attrs_on_the_wrapper
|
|
53
|
+
render_inline(UI::FormField.new(label: "Name", class: "max-w-sm", data: { test_id: "f" })) { "x" }
|
|
54
|
+
wrapper = page.find("div[data-test-id='f']")
|
|
55
|
+
assert_includes wrapper[:class].split, "max-w-sm"
|
|
56
|
+
assert_includes wrapper[:class].split, "gap-2"
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# ── Error states ─────────────────────────────────────────────────────────────
|
|
60
|
+
|
|
61
|
+
def test_error_true_is_invalid_without_a_message
|
|
62
|
+
field = UI::FormField.new(error: true)
|
|
63
|
+
assert field.error?
|
|
64
|
+
assert_nil field.error_message
|
|
65
|
+
refute field.wrap?
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def test_no_hint_or_error_no_describedby
|
|
69
|
+
field = UI::FormField.new(label: "Name")
|
|
70
|
+
assert_nil field.describedby
|
|
71
|
+
assert_equal({ id: field.control_id }, field.control_attributes)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# ── Form binding ─────────────────────────────────────────────────────────────
|
|
75
|
+
|
|
76
|
+
def test_model_errors_become_the_message
|
|
77
|
+
field = UI::FormField.new(form: form_builder_for(invalid_account), field: :email)
|
|
78
|
+
assert field.error?
|
|
79
|
+
assert_equal "Email can't be blank", field.error_message
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def test_several_model_errors_join_into_one_sentence
|
|
83
|
+
account = Account.new(email: "nope")
|
|
84
|
+
account.errors.add(:email, :taken)
|
|
85
|
+
account.validate
|
|
86
|
+
account.errors.add(:email, "is reserved")
|
|
87
|
+
field = UI::FormField.new(form: form_builder_for(account), field: :email)
|
|
88
|
+
assert_equal "Email is invalid and Email is reserved", field.error_message
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def test_a_foreign_key_field_shows_its_association_errors
|
|
92
|
+
account = Account.new
|
|
93
|
+
account.errors.add(:team, "must exist")
|
|
94
|
+
field = UI::FormField.new(form: form_builder_for(account), field: :team_id)
|
|
95
|
+
assert_equal "Team must exist", field.error_message
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def test_no_model_errors_no_message
|
|
99
|
+
field = UI::FormField.new(form: form_builder_for(Account.new(email: "a@b.c")), field: :email)
|
|
100
|
+
refute field.error?
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def test_explicit_error_wins_over_model_errors
|
|
104
|
+
field = UI::FormField.new(form: form_builder_for(invalid_account), field: :email, error: "Use your work email")
|
|
105
|
+
assert_equal "Use your work email", field.error_message
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def test_error_false_ignores_model_errors
|
|
109
|
+
field = UI::FormField.new(form: form_builder_for(invalid_account), field: :email, error: false)
|
|
110
|
+
refute field.error?
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def test_form_without_a_model_has_no_errors
|
|
114
|
+
builder = ActionView::Helpers::FormBuilder.new(:search, nil, vc_test_controller.view_context, {})
|
|
115
|
+
refute UI::FormField.new(form: builder, field: :q).error?
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def test_label_comes_from_the_attribute
|
|
119
|
+
render_inline(UI::FormField.new(form: form_builder_for(Account.new), field: :name)) { "x" }
|
|
120
|
+
assert_selector "label", text: "Name"
|
|
121
|
+
end
|
|
122
|
+
end
|
|
@@ -74,8 +74,17 @@ class UI::InputGroupTest < ViewComponent::TestCase
|
|
|
74
74
|
render_inline(UI::InputGroup.new) do |g|
|
|
75
75
|
g.with_field(error: true, placeholder: "x")
|
|
76
76
|
end
|
|
77
|
-
|
|
78
|
-
|
|
77
|
+
classes = page.find("div[data-slot='input-group']")[:class].split
|
|
78
|
+
# Driven by the field's aria-invalid, with a selector browsers accept:
|
|
79
|
+
# a :has() nested in another :has() is invalid and the rule is dropped.
|
|
80
|
+
assert_includes classes, "has-[[data-slot][aria-invalid=true]:enabled]:border-destructive"
|
|
81
|
+
assert classes.none? { |c| c.scan(":has(").size > 0 && c.start_with?("has-[") }, "nested :has() in #{classes.grep(/has\(/)}"
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def test_field_never_renders_messages_inside_the_group
|
|
85
|
+
render_inline(UI::InputGroup.new) { |g| g.with_field(error: "Taken", hint: "Pick another") }
|
|
86
|
+
assert_selector "input[aria-invalid='true']"
|
|
87
|
+
refute_selector "p"
|
|
79
88
|
end
|
|
80
89
|
|
|
81
90
|
def test_field_disabled_sets_disabled_on_input
|
|
@@ -140,4 +149,23 @@ class UI::InputGroupTest < ViewComponent::TestCase
|
|
|
140
149
|
end
|
|
141
150
|
assert_selector "div#my-group[data-slot='input-group']"
|
|
142
151
|
end
|
|
152
|
+
|
|
153
|
+
def test_ring_follows_the_control_not_any_focused_addon
|
|
154
|
+
render_inline(UI::InputGroup.new) { |g| g.with_field(placeholder: "x") }
|
|
155
|
+
classes = page.find("div[data-slot='input-group']")[:class].split
|
|
156
|
+
assert_includes classes, "has-[[data-slot]:focus]:ring-2"
|
|
157
|
+
refute classes.any? { |c| c.start_with?("focus-within:") }
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def test_addon_button_rings_on_keyboard_focus_only
|
|
161
|
+
render_inline(UI::InputGroup.new) do |g|
|
|
162
|
+
g.with_field(placeholder: "x")
|
|
163
|
+
g.with_addon(align: "inline-end") { |a| a.with_icon_button(aria: { label: "Show" }) { "o" } }
|
|
164
|
+
end
|
|
165
|
+
classes = page.find("button[aria-label='Show']")[:class].split
|
|
166
|
+
assert_includes classes, "focus:ring-0"
|
|
167
|
+
assert_includes classes, "focus:ring-offset-0"
|
|
168
|
+
assert_includes classes, "focus-visible:ring-2"
|
|
169
|
+
refute_includes classes, "focus:ring-2"
|
|
170
|
+
end
|
|
143
171
|
end
|
|
@@ -248,4 +248,41 @@ class UI::InputTest < ViewComponent::TestCase
|
|
|
248
248
|
render_inline(UI::Input.new(name: "user[email]"))
|
|
249
249
|
assert_no_selector "label"
|
|
250
250
|
end
|
|
251
|
+
|
|
252
|
+
# ── Hint and error message ───────────────────────────────────────────────────
|
|
253
|
+
|
|
254
|
+
def test_hint_describes_the_input
|
|
255
|
+
render_inline(UI::Input.new(hint: "We never share it"))
|
|
256
|
+
input = page.find("input")
|
|
257
|
+
assert input[:id].start_with?("ui-input-")
|
|
258
|
+
assert_selector "div > input + p##{input[:id]}-hint", text: "We never share it"
|
|
259
|
+
assert_equal "#{input[:id]}-hint", input["aria-describedby"]
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
def test_error_message_marks_invalid_and_describes
|
|
263
|
+
render_inline(UI::Input.new(label: "Email", id: "email", error: "Enter an email"))
|
|
264
|
+
input = page.find("input#email[aria-invalid='true']")
|
|
265
|
+
assert_includes input[:class].split, "border-destructive"
|
|
266
|
+
assert_selector "p#email-error.text-destructive", text: "Enter an email"
|
|
267
|
+
assert_equal "email-error", input["aria-describedby"]
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
def test_error_true_styles_without_variant
|
|
271
|
+
render_inline(UI::Input.new(error: true))
|
|
272
|
+
assert_includes page.find("input[aria-invalid='true']")[:class].split, "border-destructive"
|
|
273
|
+
refute_selector "p"
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
class Signup
|
|
277
|
+
include ActiveModel::Model
|
|
278
|
+
attr_accessor :email
|
|
279
|
+
validates :email, presence: true
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
def test_model_errors_show_under_a_bound_input
|
|
283
|
+
signup = Signup.new.tap(&:validate)
|
|
284
|
+
render_inline(UI::Input.new(form: ActionView::Helpers::FormBuilder.new(:signup, signup, vc_test_controller.view_context, {}), field: :email))
|
|
285
|
+
input = page.find("input[name='signup[email]'][aria-invalid='true']")
|
|
286
|
+
assert_selector "p##{input[:id]}-error", text: "Email can't be blank"
|
|
287
|
+
end
|
|
251
288
|
end
|
|
@@ -17,6 +17,20 @@ class UI::NativeSelectTest < ViewComponent::TestCase
|
|
|
17
17
|
|
|
18
18
|
# Native rather than a custom listbox: it gets the platform picker on a
|
|
19
19
|
# phone, keyboard support and screen-reader behaviour for free.
|
|
20
|
+
def test_hint_and_error_describe_the_select
|
|
21
|
+
render_inline(UI::NativeSelect.new(options: %w[a b], label: "Team", id: "team", hint: "You can change it later", error: "Choose a team"))
|
|
22
|
+
select = page.find("select#team[aria-invalid='true']")
|
|
23
|
+
assert_equal "team-hint team-error", select["aria-describedby"]
|
|
24
|
+
assert_selector "p#team-hint", text: "You can change it later"
|
|
25
|
+
assert_selector "p#team-error.text-destructive", text: "Choose a team"
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def test_no_label_or_messages_renders_no_wrapper
|
|
29
|
+
render_inline(UI::NativeSelect.new(options: %w[a b]))
|
|
30
|
+
refute_selector "div.flex-col"
|
|
31
|
+
refute_selector "select[aria-describedby]"
|
|
32
|
+
end
|
|
33
|
+
|
|
20
34
|
def test_accepts_a_plain_array_of_strings
|
|
21
35
|
render_inline(UI::NativeSelect.new(name: "board", options: %w[cbse icse]))
|
|
22
36
|
assert_selector "option[value='cbse']", text: "cbse"
|
|
@@ -115,4 +129,15 @@ class UI::NativeSelectTest < ViewComponent::TestCase
|
|
|
115
129
|
assert_equal "", page.all("option").first[:value]
|
|
116
130
|
assert_equal "selected", page.find("option[value='2']")[:selected]
|
|
117
131
|
end
|
|
132
|
+
|
|
133
|
+
def test_a_bound_select_keeps_the_models_value
|
|
134
|
+
record = Struct.new(:team_id) do
|
|
135
|
+
def to_model = self
|
|
136
|
+
def model_name = ActiveModel::Name.new(self.class, nil, "Member")
|
|
137
|
+
def persisted? = false
|
|
138
|
+
end.new(2)
|
|
139
|
+
form = ActionView::Helpers::FormBuilder.new(:member, record, vc_test_controller.view_context, {})
|
|
140
|
+
render_inline(UI::NativeSelect.new(form: form, field: :team_id, options: [ [ "Design", 1 ], [ "Engineering", 2 ] ], prompt: "Choose"))
|
|
141
|
+
assert_selector "option[value='2'][selected]", text: "Engineering"
|
|
142
|
+
end
|
|
118
143
|
end
|
|
@@ -95,4 +95,13 @@ class UI::PasswordInputTest < ViewComponent::TestCase
|
|
|
95
95
|
render_inline(UI::PasswordInput.new(form: form_builder_for(User.new), field: :password, label: false))
|
|
96
96
|
assert_no_selector "label"
|
|
97
97
|
end
|
|
98
|
+
|
|
99
|
+
def test_hint_and_error_sit_outside_the_group_and_describe_the_input
|
|
100
|
+
render_inline(UI::PasswordInput.new(label: "Password", id: "pw", hint: "At least 12 characters", error: "Too short"))
|
|
101
|
+
input = page.find("input#pw[aria-invalid='true']")
|
|
102
|
+
assert_equal "pw-hint pw-error", input["aria-describedby"]
|
|
103
|
+
assert_selector "p#pw-hint", text: "At least 12 characters"
|
|
104
|
+
assert_selector "p#pw-error", text: "Too short"
|
|
105
|
+
assert_selector "p", count: 2
|
|
106
|
+
end
|
|
98
107
|
end
|
|
@@ -178,4 +178,18 @@ class UI::SelectTest < ViewComponent::TestCase
|
|
|
178
178
|
assert_equal "true", root["data-ui--select-multiple-value"]
|
|
179
179
|
assert_equal "%{count} selected", root["data-ui--select-summary-value"]
|
|
180
180
|
end
|
|
181
|
+
|
|
182
|
+
def test_hint_and_error_describe_the_trigger
|
|
183
|
+
render_inline(UI::Select.new(options: %w[Free Pro], label: "Plan", hint: "Billed monthly", error: "Choose a plan"))
|
|
184
|
+
trigger = page.find("button[role='combobox'][aria-invalid='true']")
|
|
185
|
+
assert_equal "#{trigger[:id]}-hint #{trigger[:id]}-error", trigger["aria-describedby"]
|
|
186
|
+
assert_selector "p##{trigger[:id]}-hint", text: "Billed monthly"
|
|
187
|
+
assert_selector "p##{trigger[:id]}-error.text-destructive", text: "Choose a plan"
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def test_no_messages_no_describedby
|
|
191
|
+
render_inline(UI::Select.new(options: %w[Free Pro]))
|
|
192
|
+
refute_selector "button[aria-describedby]"
|
|
193
|
+
refute_selector "p.text-destructive"
|
|
194
|
+
end
|
|
181
195
|
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
4
|
+
|
|
5
|
+
class UI::SessionTabs::AddTest < ViewComponent::TestCase
|
|
6
|
+
def setup_fixtures(*); end
|
|
7
|
+
def teardown_fixtures(*); end
|
|
8
|
+
|
|
9
|
+
def render_with_add(size: :default, **opts)
|
|
10
|
+
render_inline(UI::SessionTabs.new(size: size)) do |tabs|
|
|
11
|
+
tabs.with_tab(value: "a", active: true) { "Alpha" }
|
|
12
|
+
tabs.with_add(**opts)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def test_without_url_is_an_event_button
|
|
17
|
+
render_with_add
|
|
18
|
+
button = page.find("button[aria-label='New tab']")
|
|
19
|
+
assert_equal "button", button[:type]
|
|
20
|
+
assert_includes button["data-action"], "click->ui--session-tabs#add"
|
|
21
|
+
assert_no_selector "form"
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def test_with_url_posts_a_form
|
|
25
|
+
render_with_add(url: "/tabs")
|
|
26
|
+
form = page.find("form[action='/tabs']")
|
|
27
|
+
assert_equal "post", form[:method]
|
|
28
|
+
assert form.has_selector?("button[aria-label='New tab'][data-action~='click->ui--session-tabs#add']")
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def test_custom_label
|
|
32
|
+
render_with_add(label: "New order")
|
|
33
|
+
assert_selector "button[aria-label='New order'][title='New order']"
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def test_touch_size
|
|
37
|
+
render_with_add(size: :touch)
|
|
38
|
+
assert_includes page.find("button[aria-label='New tab']")[:class], "size-8"
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def test_default_size_is_compact
|
|
42
|
+
render_with_add
|
|
43
|
+
assert_includes page.find("button[aria-label='New tab']")[:class], "size-7"
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def test_keeps_user_action
|
|
47
|
+
render_with_add(data: { action: "click->analytics#track" })
|
|
48
|
+
action = page.find("button[aria-label='New tab']")["data-action"]
|
|
49
|
+
assert_includes action, "click->analytics#track"
|
|
50
|
+
assert_includes action, "click->ui--session-tabs#add"
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
4
|
+
|
|
5
|
+
class UI::SessionTabsTest < ViewComponent::TestCase
|
|
6
|
+
def setup_fixtures(*); end
|
|
7
|
+
def teardown_fixtures(*); end
|
|
8
|
+
|
|
9
|
+
def render_tabs(**root_opts, &block)
|
|
10
|
+
render_inline(UI::SessionTabs.new(**root_opts)) do |tabs|
|
|
11
|
+
if block
|
|
12
|
+
block.call(tabs)
|
|
13
|
+
else
|
|
14
|
+
tabs.with_tab(value: "a", active: true, rename: true, close: true) { "Alpha" }
|
|
15
|
+
tabs.with_tab(value: "b", rename: true, close: true) { "Beta" }
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def test_root_carries_controller_and_values
|
|
21
|
+
render_tabs(min_tabs: 2, return_focus: "#search", rename_param: "view[name]")
|
|
22
|
+
root = page.find("[data-controller~='ui--session-tabs']")
|
|
23
|
+
assert_equal "2", root["data-ui--session-tabs-min-tabs-value"]
|
|
24
|
+
assert_equal "#search", root["data-ui--session-tabs-return-focus-value"]
|
|
25
|
+
assert_equal "view[name]", root["data-ui--session-tabs-rename-param-value"]
|
|
26
|
+
assert_equal "Tab name", root["data-ui--session-tabs-rename-label-value"]
|
|
27
|
+
assert root[:id].start_with?("ui-session-tabs-")
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def test_tablist_has_accessible_name
|
|
31
|
+
render_tabs(label: "Documents")
|
|
32
|
+
assert_selector "[role='tablist'][aria-label='Documents']"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def test_tablist_label_defaults_to_translation
|
|
36
|
+
render_tabs
|
|
37
|
+
assert_selector "[role='tablist'][aria-label='Open tabs']"
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def test_tab_without_href_is_a_button
|
|
41
|
+
render_tabs
|
|
42
|
+
assert_selector "button[role='tab'][type='button']", text: "Alpha"
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def test_tab_with_href_is_a_link
|
|
46
|
+
render_tabs do |t|
|
|
47
|
+
t.with_tab(value: "a", href: "/docs/a", active: true) { "Alpha" }
|
|
48
|
+
end
|
|
49
|
+
link = page.find("a[role='tab']")
|
|
50
|
+
assert_equal "/docs/a", link[:href]
|
|
51
|
+
assert_equal "page", link["aria-current"]
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def test_active_state_and_roving_tabindex
|
|
55
|
+
render_tabs
|
|
56
|
+
active = page.find("[role='tab']", text: "Alpha")
|
|
57
|
+
inactive = page.find("[role='tab']", text: "Beta")
|
|
58
|
+
assert_equal "true", active["aria-selected"]
|
|
59
|
+
assert_equal "0", active[:tabindex]
|
|
60
|
+
assert_equal "false", inactive["aria-selected"]
|
|
61
|
+
assert_equal "-1", inactive[:tabindex]
|
|
62
|
+
assert_selector "[data-ui--session-tabs-target='tab'][data-value='a'][data-state='active']"
|
|
63
|
+
assert_selector "[data-ui--session-tabs-target='tab'][data-value='b'][data-state='inactive']"
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def test_wrapper_is_presentation
|
|
67
|
+
render_tabs
|
|
68
|
+
assert_selector "[role='presentation'][data-ui--session-tabs-target='tab']", count: 2
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def test_tab_html_attrs_go_on_the_tab_element
|
|
72
|
+
render_tabs do |t|
|
|
73
|
+
t.with_tab(value: "a", href: "/a", data: { turbo_frame: "pane" }) { "Alpha" }
|
|
74
|
+
end
|
|
75
|
+
tab = page.find("a[role='tab']")
|
|
76
|
+
assert_equal "pane", tab["data-turbo-frame"]
|
|
77
|
+
assert_includes tab["data-action"], "click->ui--session-tabs#select"
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def test_meta_slot
|
|
81
|
+
render_tabs do |t|
|
|
82
|
+
t.with_tab(value: "a") do |tab|
|
|
83
|
+
tab.with_meta { "3 items" }
|
|
84
|
+
"Alpha"
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
assert_selector "[role='tab'] span", text: "3 items"
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def test_no_menu_without_actions
|
|
91
|
+
render_tabs do |t|
|
|
92
|
+
t.with_tab(value: "a") { "Alpha" }
|
|
93
|
+
end
|
|
94
|
+
assert_no_selector "[data-controller~='ui--menu']"
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def test_menu_items_follow_enabled_actions
|
|
98
|
+
render_tabs do |t|
|
|
99
|
+
t.with_tab(value: "a", rename: true) { "Alpha" }
|
|
100
|
+
t.with_tab(value: "b") { "Beta" }
|
|
101
|
+
end
|
|
102
|
+
assert_selector "[role='menuitem']", text: "Rename", visible: :all
|
|
103
|
+
assert_no_selector "[role='menuitem']", text: "Duplicate", visible: :all
|
|
104
|
+
assert_no_selector "[role='menuitem']", text: "Close", visible: :all
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def test_menu_trigger_is_labelled_by_tab
|
|
108
|
+
render_tabs
|
|
109
|
+
trigger = page.find("[data-ui--session-tabs-target='menuTrigger']", match: :first)
|
|
110
|
+
assert_equal "Tab options", trigger["aria-label"]
|
|
111
|
+
label_id = page.find("[data-ui--session-tabs-target='label']", text: "Alpha")[:id]
|
|
112
|
+
assert_equal label_id, trigger["aria-describedby"]
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def test_action_urls_and_flags
|
|
116
|
+
render_tabs do |t|
|
|
117
|
+
t.with_tab(value: "a", rename: "/a/rename", duplicate: "/a/dup", move: true, close: "/a",
|
|
118
|
+
close_confirm: "Discard?") { "Alpha" }
|
|
119
|
+
t.with_tab(value: "b") { "Beta" }
|
|
120
|
+
end
|
|
121
|
+
tab = page.find("[data-value='a']")
|
|
122
|
+
assert_equal "/a/rename", tab["data-rename-url"]
|
|
123
|
+
assert_equal "/a/dup", tab["data-duplicate-url"]
|
|
124
|
+
assert_equal "/a", tab["data-close-url"]
|
|
125
|
+
assert_nil tab["data-move-url"]
|
|
126
|
+
assert_equal "true", tab["data-move-enabled"]
|
|
127
|
+
assert_equal "Discard?", tab["data-close-confirm"]
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def test_menu_actions_are_wired
|
|
131
|
+
render_tabs do |t|
|
|
132
|
+
t.with_tab(value: "a", rename: true, duplicate: true, move: true, close: true) { "Alpha" }
|
|
133
|
+
t.with_tab(value: "b") { "Beta" }
|
|
134
|
+
end
|
|
135
|
+
%w[rename duplicate moveLeft moveRight close].each do |action|
|
|
136
|
+
assert_selector "[role='menuitem'][data-action='click->ui--session-tabs##{action}']", visible: :all
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def test_move_left_hidden_on_first_and_move_right_on_last
|
|
141
|
+
render_tabs do |t|
|
|
142
|
+
t.with_tab(value: "a", move: true) { "Alpha" }
|
|
143
|
+
t.with_tab(value: "b", move: true) { "Beta" }
|
|
144
|
+
end
|
|
145
|
+
first = page.find("[data-value='a']")
|
|
146
|
+
last = page.find("[data-value='b']")
|
|
147
|
+
assert first.has_selector?("[data-move='left'][hidden][data-disabled]", visible: :all)
|
|
148
|
+
assert first.has_selector?("[data-move='right']:not([hidden])", visible: :all)
|
|
149
|
+
assert last.has_selector?("[data-move='right'][hidden][data-disabled]", visible: :all)
|
|
150
|
+
assert last.has_selector?("[data-move='left']:not([hidden])", visible: :all)
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def test_fixed_tab_is_never_passed
|
|
154
|
+
render_tabs do |t|
|
|
155
|
+
t.with_tab(value: "default") { "Default" }
|
|
156
|
+
t.with_tab(value: "a", move: true) { "Alpha" }
|
|
157
|
+
t.with_tab(value: "b", move: true) { "Beta" }
|
|
158
|
+
end
|
|
159
|
+
alpha = page.find("[data-value='a']")
|
|
160
|
+
assert alpha.has_selector?("[data-move='left'][hidden]", visible: :all)
|
|
161
|
+
assert_nil page.find("[data-value='default']")["data-move-enabled"]
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def test_movable_tab_gets_a_drag_handle
|
|
165
|
+
render_tabs do |t|
|
|
166
|
+
t.with_tab(value: "a", move: true) { "Alpha" }
|
|
167
|
+
end
|
|
168
|
+
handle = page.find("[data-ui--session-tabs-target='handle']", visible: :all)
|
|
169
|
+
assert_equal "pointerdown->ui--session-tabs#dragStart", handle["data-action"]
|
|
170
|
+
assert_equal "true", handle["aria-hidden"]
|
|
171
|
+
assert_includes handle[:class], "touch-none"
|
|
172
|
+
assert handle.has_selector?("[data-lucide='grip-vertical']", visible: :all)
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
def test_controls_reveal_on_hover_and_slide_in
|
|
176
|
+
render_tabs do |t|
|
|
177
|
+
t.with_tab(value: "a", move: true, rename: true) { "Alpha" }
|
|
178
|
+
end
|
|
179
|
+
grip = page.find("[data-ui--session-tabs-target='handle'] [data-lucide]", visible: :all)[:class]
|
|
180
|
+
menu = page.find("[data-ui--session-tabs-target='menuTrigger']")[:class]
|
|
181
|
+
dots = page.find("[data-ui--session-tabs-target='menuTrigger'] [data-lucide]", visible: :all)[:class]
|
|
182
|
+
[ grip, menu ].each do |classes|
|
|
183
|
+
assert_includes classes, "opacity-0"
|
|
184
|
+
assert_includes classes, "group-hover/tab:opacity-100"
|
|
185
|
+
assert_includes classes, "group-focus-within/tab:opacity-100"
|
|
186
|
+
assert_includes classes, "[@media(hover:none)]:opacity-100"
|
|
187
|
+
end
|
|
188
|
+
assert_includes grip, "-translate-x-2"
|
|
189
|
+
assert_includes dots, "translate-x-2"
|
|
190
|
+
assert_includes dots, "group-hover/tab:translate-x-0"
|
|
191
|
+
assert_includes page.find("[data-value='a']")[:class], "hover:bg-primary/5"
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
def test_fixed_tab_has_no_handle
|
|
195
|
+
render_tabs do |t|
|
|
196
|
+
t.with_tab(value: "a", rename: true) { "Alpha" }
|
|
197
|
+
end
|
|
198
|
+
assert_no_selector "[data-ui--session-tabs-target='handle']", visible: :all
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
def test_tab_body_is_draggable_but_not_natively
|
|
202
|
+
render_tabs do |t|
|
|
203
|
+
t.with_tab(value: "a", href: "/a", move: true) { "Alpha" }
|
|
204
|
+
end
|
|
205
|
+
tab = page.find("a[role='tab']")
|
|
206
|
+
assert_includes tab["data-action"], "pointerdown->ui--session-tabs#pressStart"
|
|
207
|
+
assert_equal "false", tab[:draggable]
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
def test_menu_trigger_is_compact
|
|
211
|
+
render_tabs
|
|
212
|
+
assert_includes page.find("[data-ui--session-tabs-target='menuTrigger']", match: :first)[:class], "size-6"
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
def test_close_hidden_at_min_tabs
|
|
216
|
+
render_tabs do |t|
|
|
217
|
+
t.with_tab(value: "a", close: true) { "Alpha" }
|
|
218
|
+
end
|
|
219
|
+
assert_selector "[role='menuitem'][data-close-part][hidden][data-disabled]", visible: :all
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
def test_close_available_above_min_tabs
|
|
223
|
+
render_tabs
|
|
224
|
+
assert_selector "[role='menuitem'][data-close-part]:not([hidden])", visible: :all, count: 2
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
def test_close_is_destructive_and_divided
|
|
228
|
+
render_tabs
|
|
229
|
+
item = page.find("[role='menuitem'][data-close-part]", match: :first, visible: :all)
|
|
230
|
+
assert_includes item[:class], "text-destructive"
|
|
231
|
+
assert_selector "[role='separator'][data-close-part]", visible: :all, count: 2
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
def test_extra_menu_items
|
|
235
|
+
render_tabs do |t|
|
|
236
|
+
t.with_tab(value: "a", close: true) do |tab|
|
|
237
|
+
tab.with_menu_item("Pin", data: { action: "click->pins#toggle" })
|
|
238
|
+
"Alpha"
|
|
239
|
+
end
|
|
240
|
+
t.with_tab(value: "b") { "Beta" }
|
|
241
|
+
end
|
|
242
|
+
assert_selector "[role='menuitem'][data-action='click->pins#toggle']", text: "Pin", visible: :all
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
def test_confirm_dialog_is_rendered_once
|
|
246
|
+
render_tabs
|
|
247
|
+
assert_selector "[data-ui--session-tabs-target='dialog'] dialog", count: 1, visible: :all
|
|
248
|
+
assert_selector "[data-ui--session-tabs-target='confirmMessage']", visible: :all
|
|
249
|
+
assert_selector "button[data-action='click->ui--session-tabs#confirmClose']", text: "Close tab", visible: :all
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
def test_touch_size
|
|
253
|
+
render_tabs(size: :touch)
|
|
254
|
+
assert_includes page.find("[data-value='a']")[:class], "h-11"
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
def test_default_size
|
|
258
|
+
render_tabs
|
|
259
|
+
assert_includes page.find("[data-value='a']")[:class], "h-9"
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
def test_unknown_size_falls_back
|
|
263
|
+
render_tabs(size: :huge)
|
|
264
|
+
assert_includes page.find("[data-value='a']")[:class], "h-9"
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
def test_user_class_and_attrs_on_root
|
|
268
|
+
render_tabs(class: "my-tabs", id: "doc-tabs", data: { turbo_permanent: true })
|
|
269
|
+
root = page.find("#doc-tabs")
|
|
270
|
+
assert_includes root[:class], "my-tabs"
|
|
271
|
+
assert_equal "ui--session-tabs", root["data-controller"]
|
|
272
|
+
assert_equal "true", root["data-turbo-permanent"]
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
def test_values_with_spaces_get_safe_ids
|
|
276
|
+
render_tabs do |t|
|
|
277
|
+
t.with_tab(value: "Saved view 1") { "One" }
|
|
278
|
+
end
|
|
279
|
+
assert page.find("[role='tab']")[:id].end_with?("-tab-saved-view-1")
|
|
280
|
+
end
|
|
281
|
+
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
4
|
+
|
|
5
|
+
class UI::SkeletonTest < ViewComponent::TestCase
|
|
6
|
+
def setup_fixtures(*); end
|
|
7
|
+
def teardown_fixtures(*); end
|
|
8
|
+
|
|
9
|
+
def skeleton_el
|
|
10
|
+
page.find("div", visible: :all)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def test_renders_a_div
|
|
14
|
+
render_inline(UI::Skeleton.new)
|
|
15
|
+
assert skeleton_el.present?
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def test_pulses_regardless_of_shape
|
|
19
|
+
render_inline(UI::Skeleton.new)
|
|
20
|
+
assert_includes skeleton_el[:class].split, "animate-pulse"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def test_hidden_from_screen_readers
|
|
24
|
+
render_inline(UI::Skeleton.new)
|
|
25
|
+
assert_equal "true", skeleton_el["aria-hidden"]
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def test_default_shape_is_text
|
|
29
|
+
render_inline(UI::Skeleton.new)
|
|
30
|
+
classes = skeleton_el[:class].split
|
|
31
|
+
assert_includes classes, "h-4"
|
|
32
|
+
assert_includes classes, "w-full"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def test_circle_shape
|
|
36
|
+
render_inline(UI::Skeleton.new(shape: :circle))
|
|
37
|
+
classes = skeleton_el[:class].split
|
|
38
|
+
assert_includes classes, "size-10"
|
|
39
|
+
assert_includes classes, "rounded-full"
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def test_rectangle_shape
|
|
43
|
+
render_inline(UI::Skeleton.new(shape: :rectangle))
|
|
44
|
+
classes = skeleton_el[:class].split
|
|
45
|
+
assert_includes classes, "h-24"
|
|
46
|
+
assert_includes classes, "w-full"
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def test_user_class_merge
|
|
50
|
+
render_inline(UI::Skeleton.new(class: "w-32"))
|
|
51
|
+
assert_includes skeleton_el[:class].split, "w-32"
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def test_user_class_overrides_shape_sizing_via_tailwind_merge
|
|
55
|
+
render_inline(UI::Skeleton.new(shape: :text, class: "h-10"))
|
|
56
|
+
classes = skeleton_el[:class].split
|
|
57
|
+
assert_includes classes, "h-10"
|
|
58
|
+
refute_includes classes, "h-4"
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def test_html_attrs_passthrough
|
|
62
|
+
render_inline(UI::Skeleton.new(id: "my-id", data: { test_id: "s" }))
|
|
63
|
+
assert_selector "div#my-id[data-test-id='s']"
|
|
64
|
+
end
|
|
65
|
+
end
|