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,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
4
|
+
|
|
5
|
+
class UI::TableOfContents::ItemTest < ViewComponent::TestCase
|
|
6
|
+
def setup_fixtures(*); end
|
|
7
|
+
def teardown_fixtures(*); end
|
|
8
|
+
|
|
9
|
+
def test_renders_list_item_with_link
|
|
10
|
+
render_inline(UI::TableOfContents::Item.new(href: "#usage", text: "Usage"))
|
|
11
|
+
assert_selector "li > a[href='#usage']", text: "Usage"
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def test_user_class_merge
|
|
15
|
+
render_inline(UI::TableOfContents::Item.new(href: "#usage", text: "Usage", class: "italic"))
|
|
16
|
+
classes = page.find("a")[:class].split
|
|
17
|
+
assert_includes classes, "italic"
|
|
18
|
+
assert_includes classes, "rounded-md"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def test_html_attrs_and_data_merge_onto_link
|
|
22
|
+
render_inline(UI::TableOfContents::Item.new(href: "#usage", text: "Usage", title: "Usage", data: { turbo: false }))
|
|
23
|
+
link = page.find("a")
|
|
24
|
+
assert_equal "Usage", link[:title]
|
|
25
|
+
assert_equal "false", link["data-turbo"]
|
|
26
|
+
assert_equal "link", link["data-ui--table-of-contents-target"]
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def test_children_nest_deeper
|
|
30
|
+
render_inline(UI::TableOfContents::Item.new(href: "#a", text: "A")) do |item|
|
|
31
|
+
item.with_item(href: "#b", text: "B") do |child|
|
|
32
|
+
child.with_item(href: "#c", text: "C")
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
assert_selector "li > ul > li > ul > li > a[href='#c']"
|
|
36
|
+
assert_includes page.find("a[href='#c']")[:class].split, "py-1"
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
4
|
+
|
|
5
|
+
class UI::TableOfContentsTest < ViewComponent::TestCase
|
|
6
|
+
def setup_fixtures(*); end
|
|
7
|
+
def teardown_fixtures(*); end
|
|
8
|
+
|
|
9
|
+
def render_toc(**opts)
|
|
10
|
+
render_inline(UI::TableOfContents.new(**opts)) do |toc|
|
|
11
|
+
toc.with_item(href: "#process", text: "Contribution process") do |item|
|
|
12
|
+
item.with_item(href: "#issue", text: "Create an issue")
|
|
13
|
+
item.with_item(href: "#merge", text: "Merge your changes")
|
|
14
|
+
end
|
|
15
|
+
toc.with_item(href: "#resources", text: "Resources")
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Structure
|
|
20
|
+
|
|
21
|
+
def test_renders_nav_landmark_with_default_label
|
|
22
|
+
render_toc
|
|
23
|
+
assert_selector "nav[aria-label='On this page']"
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def test_custom_label
|
|
27
|
+
render_toc(label: "Sections")
|
|
28
|
+
assert_selector "nav[aria-label='Sections']"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def test_title_labels_the_nav
|
|
32
|
+
render_toc(title: "On this page", id: "toc")
|
|
33
|
+
assert_selector "nav#toc[aria-labelledby='toc-title']:not([aria-label])"
|
|
34
|
+
assert_selector "p#toc-title", text: "On this page"
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def test_renders_top_level_links
|
|
38
|
+
render_toc
|
|
39
|
+
assert_selector "nav > div > ul > li > a[href='#process']", text: "Contribution process"
|
|
40
|
+
assert_selector "nav > div > ul > li > a[href='#resources']", text: "Resources"
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def test_nested_items_render_in_a_nested_list
|
|
44
|
+
render_toc
|
|
45
|
+
assert_selector "li > ul > li > a[href='#issue']", text: "Create an issue"
|
|
46
|
+
assert_selector "li > ul > li > a[href='#merge']"
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def test_nested_links_are_tighter_than_top_level
|
|
50
|
+
render_toc
|
|
51
|
+
assert_includes page.find("a[href='#process']")[:class].split, "py-1.5"
|
|
52
|
+
assert_includes page.find("a[href='#issue']")[:class].split, "py-1"
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def test_leaf_items_have_no_nested_list
|
|
56
|
+
render_toc
|
|
57
|
+
assert_no_selector "a[href='#resources'] + ul"
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Controller wiring
|
|
61
|
+
|
|
62
|
+
def test_root_carries_controller_and_values
|
|
63
|
+
render_toc(offset: 120, container: "#main")
|
|
64
|
+
nav = page.find("nav[data-controller~='ui--table-of-contents']")
|
|
65
|
+
assert_equal "120", nav["data-ui--table-of-contents-offset-value"]
|
|
66
|
+
assert_equal "#main", nav["data-ui--table-of-contents-container-value"]
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def test_container_value_omitted_by_default
|
|
70
|
+
render_toc
|
|
71
|
+
nav = page.find("nav")
|
|
72
|
+
assert_nil nav["data-ui--table-of-contents-container-value"]
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def test_links_are_targets_with_click_action
|
|
76
|
+
render_toc
|
|
77
|
+
links = page.all("a[data-ui--table-of-contents-target='link']")
|
|
78
|
+
assert_equal 4, links.size
|
|
79
|
+
links.each do |link|
|
|
80
|
+
assert_equal "click->ui--table-of-contents#select", link["data-action"]
|
|
81
|
+
assert_equal "inactive", link["data-state"]
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def test_rail_and_indicator_targets
|
|
86
|
+
render_toc
|
|
87
|
+
assert_selector "[data-ui--table-of-contents-target='rail'] > [data-ui--table-of-contents-target='indicator'][aria-hidden='true']", visible: :all
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def test_no_link_is_current_before_the_controller_runs
|
|
91
|
+
render_toc
|
|
92
|
+
assert_no_selector "a[aria-current]"
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Attributes
|
|
96
|
+
|
|
97
|
+
def test_root_has_auto_generated_id
|
|
98
|
+
render_toc
|
|
99
|
+
assert page.find("nav")[:id].start_with?("ui-toc-")
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def test_user_class_merge
|
|
103
|
+
render_toc(class: "sticky top-20")
|
|
104
|
+
classes = page.find("nav")[:class].split
|
|
105
|
+
assert_includes classes, "sticky"
|
|
106
|
+
assert_includes classes, "text-sm"
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def test_html_attrs_passthrough_keeps_controller
|
|
110
|
+
render_toc(data: { test_id: "toc" })
|
|
111
|
+
assert_selector "nav[data-test-id='toc'][data-controller~='ui--table-of-contents']"
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def test_starts_settling_so_the_first_state_does_not_animate
|
|
115
|
+
render_toc
|
|
116
|
+
nav = page.find("nav")
|
|
117
|
+
assert nav.matches_css?("[data-settling]")
|
|
118
|
+
assert_includes nav[:class].split, "data-[settling]:[&_*]:transition-none"
|
|
119
|
+
end
|
|
120
|
+
end
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
4
|
+
|
|
5
|
+
class UI::TextareaTest < ViewComponent::TestCase
|
|
6
|
+
def setup_fixtures(*); end
|
|
7
|
+
def teardown_fixtures(*); end
|
|
8
|
+
|
|
9
|
+
# ── Default render ───────────────────────────────────────────────────────────
|
|
10
|
+
|
|
11
|
+
def test_renders_textarea_tag
|
|
12
|
+
render_inline(UI::Textarea.new)
|
|
13
|
+
assert_selector "textarea"
|
|
14
|
+
refute_selector "div"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def test_default_rows
|
|
18
|
+
render_inline(UI::Textarea.new)
|
|
19
|
+
assert_selector "textarea[rows='3']"
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def test_custom_rows
|
|
23
|
+
render_inline(UI::Textarea.new(rows: 6))
|
|
24
|
+
assert_selector "textarea[rows='6']"
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def test_default_classes
|
|
28
|
+
render_inline(UI::Textarea.new)
|
|
29
|
+
classes = page.find("textarea")[:class].split
|
|
30
|
+
assert_includes classes, "border-input"
|
|
31
|
+
assert_includes classes, "text-sm"
|
|
32
|
+
assert_includes classes, "resize-y"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# ── Sizes and resizing ───────────────────────────────────────────────────────
|
|
36
|
+
|
|
37
|
+
def test_size_sm
|
|
38
|
+
render_inline(UI::Textarea.new(size: :sm))
|
|
39
|
+
assert_includes page.find("textarea")[:class].split, "text-xs"
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def test_size_lg
|
|
43
|
+
render_inline(UI::Textarea.new(size: :lg))
|
|
44
|
+
assert_includes page.find("textarea")[:class].split, "text-base"
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def test_resize_none
|
|
48
|
+
render_inline(UI::Textarea.new(resize: :none))
|
|
49
|
+
classes = page.find("textarea")[:class].split
|
|
50
|
+
assert_includes classes, "resize-none"
|
|
51
|
+
refute_includes classes, "resize-y"
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def test_autogrow_sizes_to_content_and_cannot_be_dragged
|
|
55
|
+
render_inline(UI::Textarea.new(autogrow: true))
|
|
56
|
+
classes = page.find("textarea")[:class].split
|
|
57
|
+
assert_includes classes, "field-sizing-content"
|
|
58
|
+
assert_includes classes, "resize-none"
|
|
59
|
+
refute_includes classes, "resize-y"
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# ── Value ────────────────────────────────────────────────────────────────────
|
|
63
|
+
|
|
64
|
+
def test_value_renders_as_content
|
|
65
|
+
render_inline(UI::Textarea.new(value: "Hello\nworld"))
|
|
66
|
+
assert_equal "Hello\nworld", page.find("textarea").value
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def test_value_is_escaped
|
|
70
|
+
render_inline(UI::Textarea.new(value: "</textarea><script>x</script>"))
|
|
71
|
+
assert_selector "textarea", count: 1
|
|
72
|
+
refute_selector "script"
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# ── States ───────────────────────────────────────────────────────────────────
|
|
76
|
+
|
|
77
|
+
def test_error_sets_aria_invalid_and_destructive_border
|
|
78
|
+
render_inline(UI::Textarea.new(error: true))
|
|
79
|
+
textarea = page.find("textarea[aria-invalid='true']")
|
|
80
|
+
assert_includes textarea[:class].split, "border-destructive"
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def test_no_error_no_aria_invalid
|
|
84
|
+
render_inline(UI::Textarea.new)
|
|
85
|
+
refute_selector "textarea[aria-invalid]"
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def test_disabled
|
|
89
|
+
render_inline(UI::Textarea.new(disabled: true))
|
|
90
|
+
assert_selector "textarea[disabled]"
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def test_required
|
|
94
|
+
render_inline(UI::Textarea.new(required: true))
|
|
95
|
+
assert_selector "textarea[required]"
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# ── Attributes ───────────────────────────────────────────────────────────────
|
|
99
|
+
|
|
100
|
+
def test_html_attrs_pass_through
|
|
101
|
+
render_inline(UI::Textarea.new(name: "post[body]", id: "body", placeholder: "Write…", maxlength: 500))
|
|
102
|
+
assert_selector "textarea#body[name='post[body]'][placeholder='Write…'][maxlength='500']"
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def test_user_class_merges_via_tailwind_merge
|
|
106
|
+
render_inline(UI::Textarea.new(class: "bg-muted max-h-60"))
|
|
107
|
+
classes = page.find("textarea")[:class].split
|
|
108
|
+
refute_includes classes, "bg-background"
|
|
109
|
+
assert_includes classes, "bg-muted"
|
|
110
|
+
assert_includes classes, "max-h-60"
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# ── Label ────────────────────────────────────────────────────────────────────
|
|
114
|
+
|
|
115
|
+
def test_label_is_a_linked_sibling
|
|
116
|
+
render_inline(UI::Textarea.new(label: "Message"))
|
|
117
|
+
assert_selector "div > label + textarea"
|
|
118
|
+
assert_equal page.find("textarea")[:id], page.find("label")[:for]
|
|
119
|
+
assert page.find("textarea")[:id].start_with?("ui-textarea-")
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def test_explicit_id_is_linked
|
|
123
|
+
render_inline(UI::Textarea.new(label: "Message", id: "msg"))
|
|
124
|
+
assert_selector "label[for='msg'] + textarea#msg"
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def test_required_label_shows_asterisk
|
|
128
|
+
render_inline(UI::Textarea.new(label: "Message", required: true))
|
|
129
|
+
assert_selector "label span.text-destructive", text: "*"
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# ── Form builder ─────────────────────────────────────────────────────────────
|
|
133
|
+
|
|
134
|
+
class Post
|
|
135
|
+
include ActiveModel::Model
|
|
136
|
+
attr_accessor :body
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def form_builder_for(record)
|
|
140
|
+
ActionView::Helpers::FormBuilder.new(:post, record, vc_test_controller.view_context, {})
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def test_form_binding_scopes_name_and_fills_value
|
|
144
|
+
render_inline(UI::Textarea.new(form: form_builder_for(Post.new(body: "Draft")), field: :body))
|
|
145
|
+
textarea = page.find("textarea[name='post[body]']")
|
|
146
|
+
assert_equal "Draft", textarea.value
|
|
147
|
+
assert_includes textarea[:class].split, "border-input"
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def test_form_binding_derives_linked_label
|
|
151
|
+
render_inline(UI::Textarea.new(form: form_builder_for(Post.new), field: :body))
|
|
152
|
+
assert_selector "label", text: "Body"
|
|
153
|
+
assert_equal page.find("label")[:for], page.find("textarea")[:id]
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def test_label_false_opts_out
|
|
157
|
+
render_inline(UI::Textarea.new(form: form_builder_for(Post.new), field: :body, label: false))
|
|
158
|
+
assert_no_selector "label"
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
# ── Hint and error message ───────────────────────────────────────────────────
|
|
162
|
+
|
|
163
|
+
def test_hint_and_error_describe_the_textarea
|
|
164
|
+
render_inline(UI::Textarea.new(label: "Bio", id: "bio", hint: "Markdown works", error: "Too long"))
|
|
165
|
+
textarea = page.find("textarea#bio[aria-invalid='true']")
|
|
166
|
+
assert_equal "bio-hint bio-error", textarea["aria-describedby"]
|
|
167
|
+
assert_includes textarea[:class].split, "border-destructive"
|
|
168
|
+
assert_selector "textarea + p#bio-hint + p#bio-error"
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
def test_model_errors_show_under_a_bound_textarea
|
|
172
|
+
post = Post.new
|
|
173
|
+
post.errors.add(:body, :blank)
|
|
174
|
+
render_inline(UI::Textarea.new(form: form_builder_for(post), field: :body))
|
|
175
|
+
assert_selector "textarea[aria-invalid='true']"
|
|
176
|
+
assert_selector "p.text-destructive", text: "Body can't be blank"
|
|
177
|
+
end
|
|
178
|
+
end
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
4
|
+
|
|
5
|
+
class UI::Toast::ViewportTest < ViewComponent::TestCase
|
|
6
|
+
def setup_fixtures(*); end
|
|
7
|
+
def teardown_fixtures(*); end
|
|
8
|
+
|
|
9
|
+
def viewport_el
|
|
10
|
+
page.find("##{UI::Toast::Viewport::ID}", visible: :all)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def test_renders_with_default_id
|
|
14
|
+
render_inline(UI::Toast::Viewport.new)
|
|
15
|
+
assert_equal "ui-toast-viewport", viewport_el["id"]
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def test_renders_with_custom_id
|
|
19
|
+
render_inline(UI::Toast::Viewport.new(id: "my-toasts"))
|
|
20
|
+
assert page.has_css?("#my-toasts", visible: :all)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def test_default_position_is_bottom_right
|
|
24
|
+
render_inline(UI::Toast::Viewport.new)
|
|
25
|
+
classes = viewport_el[:class].split
|
|
26
|
+
assert_includes classes, "bottom-0"
|
|
27
|
+
assert_includes classes, "right-0"
|
|
28
|
+
assert_includes classes, "items-end"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def test_top_position_reverses_stack_order
|
|
32
|
+
render_inline(UI::Toast::Viewport.new(position: :top_right))
|
|
33
|
+
classes = viewport_el[:class].split
|
|
34
|
+
assert_includes classes, "top-0"
|
|
35
|
+
assert_includes classes, "flex-col-reverse"
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def test_data_controller
|
|
39
|
+
render_inline(UI::Toast::Viewport.new)
|
|
40
|
+
assert_equal "ui--toast-viewport", viewport_el["data-controller"]
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def test_renders_one_template_per_variant
|
|
44
|
+
render_inline(UI::Toast::Viewport.new)
|
|
45
|
+
templates = page.all("template[data-ui--toast-viewport-target='template']", visible: :all)
|
|
46
|
+
assert_equal UI::Toast::Viewport::VARIANTS.map(&:to_s).sort,
|
|
47
|
+
templates.map { |t| t["data-variant"] }.sort
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def test_templates_carry_title_and_description_targets
|
|
51
|
+
# Capybara's rack_test driver parses <template> as an ordinary element
|
|
52
|
+
# with no HTML5 content fragment, so its children never show up through
|
|
53
|
+
# Capybara/Nokogiri node traversal; assert on the raw markup instead.
|
|
54
|
+
html = render_inline(UI::Toast::Viewport.new).to_html
|
|
55
|
+
UI::Toast::Viewport::VARIANTS.each do |variant|
|
|
56
|
+
template_html = html[/<template[^>]*data-variant="#{variant}"[^>]*>(.*?)<\/template>/m, 1]
|
|
57
|
+
assert template_html, "no template found for #{variant}"
|
|
58
|
+
assert_includes template_html, "data-ui--toast-target=\"title\""
|
|
59
|
+
assert_includes template_html, "data-ui--toast-target=\"description\""
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def test_user_class_merge
|
|
64
|
+
render_inline(UI::Toast::Viewport.new(class: "my-viewport"))
|
|
65
|
+
assert_includes viewport_el[:class].split, "my-viewport"
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def test_html_attrs_passthrough
|
|
69
|
+
render_inline(UI::Toast::Viewport.new(data: { test_id: "v" }))
|
|
70
|
+
assert_equal "v", viewport_el["data-test-id"]
|
|
71
|
+
end
|
|
72
|
+
end
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
4
|
+
|
|
5
|
+
class UI::ToastTest < ViewComponent::TestCase
|
|
6
|
+
def setup_fixtures(*); end
|
|
7
|
+
def teardown_fixtures(*); end
|
|
8
|
+
|
|
9
|
+
def toast_el
|
|
10
|
+
page.find("[data-slot='toast']")
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def test_renders_container
|
|
14
|
+
render_inline(UI::Toast.new(title: "Saved"))
|
|
15
|
+
assert toast_el.present?
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def test_fill_mode_forwards_prevents_a_flash_before_removal
|
|
19
|
+
# tw-animate-css's animate-in/animate-out default to fill-mode: none, so
|
|
20
|
+
# without this the element snaps back to fully visible right as the exit
|
|
21
|
+
# animation ends, a flicker just before the controller removes it.
|
|
22
|
+
render_inline(UI::Toast.new(title: "x"))
|
|
23
|
+
assert_includes toast_el[:class].split, "fill-mode-forwards"
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def test_surface_is_neutral_regardless_of_variant
|
|
27
|
+
# Color lives on the icon only, not the surface — matching
|
|
28
|
+
# Dialog/Sheet/Popover/Menu rather than Alert's tinted banner.
|
|
29
|
+
%i[default success destructive warning info].each do |variant|
|
|
30
|
+
render_inline(UI::Toast.new(variant: variant, title: "x"))
|
|
31
|
+
classes = toast_el[:class].split
|
|
32
|
+
assert_includes classes, "bg-popover"
|
|
33
|
+
assert_includes classes, "text-popover-foreground"
|
|
34
|
+
refute classes.any? { |c| c.match?(/^(bg|border)-(emerald|amber|sky|destructive)/) },
|
|
35
|
+
"variant #{variant} colored something other than the icon"
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def test_default_variant_classes
|
|
40
|
+
render_inline(UI::Toast.new(title: "x"))
|
|
41
|
+
classes = toast_el[:class].split
|
|
42
|
+
assert_includes classes, "[&>i]:text-muted-foreground"
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def test_success_variant_classes
|
|
46
|
+
render_inline(UI::Toast.new(variant: :success, title: "x"))
|
|
47
|
+
classes = toast_el[:class].split
|
|
48
|
+
assert_includes classes, "[&>i]:text-emerald-600"
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def test_destructive_variant_classes
|
|
52
|
+
render_inline(UI::Toast.new(variant: :destructive, title: "x"))
|
|
53
|
+
classes = toast_el[:class].split
|
|
54
|
+
assert_includes classes, "[&>i]:text-destructive"
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def test_warning_variant_classes
|
|
58
|
+
render_inline(UI::Toast.new(variant: :warning, title: "x"))
|
|
59
|
+
classes = toast_el[:class].split
|
|
60
|
+
assert_includes classes, "[&>i]:text-amber-600"
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def test_info_variant_classes
|
|
64
|
+
render_inline(UI::Toast.new(variant: :info, title: "x"))
|
|
65
|
+
classes = toast_el[:class].split
|
|
66
|
+
assert_includes classes, "[&>i]:text-sky-600"
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def test_default_icon_per_variant
|
|
70
|
+
{ default: "info", success: "circle-check", destructive: "circle-alert", warning: "circle-alert", info: "info" }
|
|
71
|
+
.each do |variant, icon|
|
|
72
|
+
render_inline(UI::Toast.new(variant: variant, title: "x"))
|
|
73
|
+
assert_selector "i[data-lucide='#{icon}']"
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def test_icon_false_suppresses_icon
|
|
78
|
+
render_inline(UI::Toast.new(title: "x", icon: false))
|
|
79
|
+
refute_selector "[data-slot='toast'] > i"
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def test_icon_override
|
|
83
|
+
render_inline(UI::Toast.new(title: "x", icon: "rocket"))
|
|
84
|
+
assert_selector "[data-slot='toast'] > i[data-lucide='rocket']"
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def test_title_renders
|
|
88
|
+
render_inline(UI::Toast.new(title: "Saved"))
|
|
89
|
+
assert_selector "[data-ui--toast-target='title']", text: "Saved"
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def test_description_renders
|
|
93
|
+
render_inline(UI::Toast.new(title: "Saved", description: "All set."))
|
|
94
|
+
assert_selector "[data-ui--toast-target='description']", text: "All set."
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def test_no_description_renders_no_description_node
|
|
98
|
+
render_inline(UI::Toast.new(title: "Saved"))
|
|
99
|
+
refute_selector "[data-ui--toast-target='description']"
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def test_default_role_and_live_region
|
|
103
|
+
render_inline(UI::Toast.new(title: "x"))
|
|
104
|
+
assert_equal "status", toast_el["role"]
|
|
105
|
+
assert_equal "polite", toast_el["aria-live"]
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def test_destructive_role_and_live_region
|
|
109
|
+
render_inline(UI::Toast.new(variant: :destructive, title: "x"))
|
|
110
|
+
assert_equal "alert", toast_el["role"]
|
|
111
|
+
assert_equal "assertive", toast_el["aria-live"]
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def test_dismissible_by_default
|
|
115
|
+
render_inline(UI::Toast.new(title: "x"))
|
|
116
|
+
assert_selector "button[aria-label='Dismiss']"
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def test_dismissible_false_hides_close_button
|
|
120
|
+
render_inline(UI::Toast.new(title: "x", dismissible: false))
|
|
121
|
+
refute_selector "button[aria-label='Dismiss']"
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def test_close_button_wired_to_controller
|
|
125
|
+
render_inline(UI::Toast.new(title: "x"))
|
|
126
|
+
button = page.find("button[aria-label='Dismiss']")
|
|
127
|
+
assert_includes button["data-action"], "click->ui--toast#dismiss"
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def test_data_controller
|
|
131
|
+
render_inline(UI::Toast.new(title: "x"))
|
|
132
|
+
assert_equal "ui--toast", toast_el["data-controller"]
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def test_default_duration_value_is_5_seconds_in_ms
|
|
136
|
+
render_inline(UI::Toast.new(title: "x"))
|
|
137
|
+
assert_equal "5000", toast_el["data-ui--toast-duration-value"]
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def test_custom_duration_value_is_given_in_seconds
|
|
141
|
+
render_inline(UI::Toast.new(title: "x", duration: 10))
|
|
142
|
+
assert_equal "10000", toast_el["data-ui--toast-duration-value"]
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def test_duration_accepts_an_active_support_duration
|
|
146
|
+
render_inline(UI::Toast.new(title: "x", duration: 10.seconds))
|
|
147
|
+
assert_equal "10000", toast_el["data-ui--toast-duration-value"]
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def test_persistent_duration_value
|
|
151
|
+
render_inline(UI::Toast.new(title: "x", duration: 0))
|
|
152
|
+
assert_equal "0", toast_el["data-ui--toast-duration-value"]
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def test_pause_and_resume_wired_on_hover
|
|
156
|
+
render_inline(UI::Toast.new(title: "x"))
|
|
157
|
+
action = toast_el["data-action"]
|
|
158
|
+
assert_includes action, "mouseenter->ui--toast#pause"
|
|
159
|
+
assert_includes action, "mouseleave->ui--toast#resume"
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def test_initial_state_is_open
|
|
163
|
+
render_inline(UI::Toast.new(title: "x"))
|
|
164
|
+
assert_equal "open", toast_el["data-state"]
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def test_renders_action_slot
|
|
168
|
+
render_inline(UI::Toast.new(title: "x")) do |toast|
|
|
169
|
+
toast.with_action { "Undo" }
|
|
170
|
+
end
|
|
171
|
+
assert_selector "[data-slot='toast'] div", text: "Undo"
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def test_user_class_merge
|
|
175
|
+
render_inline(UI::Toast.new(title: "x", class: "my-toast"))
|
|
176
|
+
assert_includes toast_el[:class].split, "my-toast"
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
def test_html_attrs_passthrough
|
|
180
|
+
render_inline(UI::Toast.new(title: "x", id: "my-id", data: { test_id: "t" }))
|
|
181
|
+
assert_selector "[data-slot='toast']#my-id[data-test-id='t']"
|
|
182
|
+
end
|
|
183
|
+
end
|