Skip to content

Commit c56f325

Browse files
camertronsampart
andauthored
Action list description test selector (#2984)
Co-authored-by: Sam Partington <[email protected]>
1 parent 868b49e commit c56f325

File tree

5 files changed

+24
-9
lines changed

5 files changed

+24
-9
lines changed

.changeset/loud-shoes-enjoy.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/view-components': minor
3+
---
4+
5+
Add ability to attach a test selector to ActionList items

app/components/primer/alpha/action_list/item.html.erb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@
2828
<%= render(Primer::BaseComponent.new(tag: :span, **@label_arguments)) do %>
2929
<%= @label || content %>
3030
<% end %>
31-
<% if description? %>
32-
<span class="ActionListItem-description">
33-
<%= description %>
34-
</span>
35-
<% end %>
31+
<%= description if description? %>
3632
<% end %>
3733
<% if trailing_visual %>
3834
<span class="ActionListItem-visual ActionListItem-visual--trailing">

app/components/primer/alpha/action_list/item.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,13 @@ class Item < Primer::Component
3838
}
3939
TRUNCATION_BEHAVIOR_OPTIONS = TRUNCATION_BEHAVIOR_MAPPINGS.keys.freeze
4040

41-
# Description content that complements the item's label. See `ActionList`'s `description_scheme` argument
42-
# for layout options.
43-
renders_one :description
41+
# Description content that complements the item's label, with optional test_selector.
42+
# See `ActionList`'s `description_scheme` argument for layout options.
43+
#
44+
# @param test_selector [String] The value of this argument is set as the value of a `data-test-selector` HTML attribute on the description element.
45+
renders_one :description, -> (test_selector: nil) do
46+
Primer::BaseComponent.new(tag: "span", classes: "ActionListItem-description", test_selector: test_selector) { content }
47+
end
4448

4549
# An icon, avatar, SVG, or custom content that will render to the left of the label.
4650
#

previews/primer/alpha/action_list_preview.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ def item_with_description
380380
aria: { label: "List heading" }
381381
)) do |component|
382382
component.with_item(label: "Default item", href: "/") do |item|
383-
item.with_description.with_content("This is a description")
383+
item.with_description(test_selector: "some-selector").with_content("This is a description")
384384
end
385385
end
386386
end

test/components/alpha/action_list_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ def test_avatar_item
2929
assert_selector(".ActionListItem-description", text: "Bruce Banner")
3030
end
3131

32+
def test_item_description_test_selector
33+
render_inline(Primer::Alpha::ActionList.new(aria: { label: "List" })) do |component|
34+
component.with_item(label: "Item 1", href: "/item1") do |item|
35+
item.with_description(test_selector: "foo") { "My description" }
36+
end
37+
end
38+
39+
assert_selector(".ActionListItem-description[data-test-selector='foo']", text: "My description")
40+
end
41+
3242
def test_item_trailing_visual_text
3343
render_preview(:item, params: { trailing_visual_text: "trailing visual text" })
3444

0 commit comments

Comments
 (0)