Skip to content

Commit

Permalink
Improve the tests around merging HTML attributes
Browse files Browse the repository at this point in the history
This adds a few more tests that try to ensure we're properly dealing
with custom HTML attributes.

There are a couple of small improvements to area labelledby and
describedby argument building where we wrap the id in an array to allow
the values to be cleanly merged if required.

Also add a test example with some JSON for completeness.
  • Loading branch information
peteryates committed Oct 4, 2021
1 parent 28de299 commit 90c3397
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def options
module: %(#{brand}-error-summary)
},
aria: {
labelledby: summary_title_id
labelledby: [summary_title_id.presence]
}
}
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def options
id: field_id(link_errors: @link_errors),
class: classes,
multiple: @multiple,
aria: { describedby: [hint_id] },
aria: { describedby: [hint_id.presence] },
data: { 'aria-controls' => @conditional_id }
}
end
Expand Down
48 changes: 42 additions & 6 deletions spec/support/shared/shared_html_attribute_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,55 @@
let(:example_block) { proc { builder.tag.span('block text') } }

context 'with args in the expected formats' do
sample_json = { e: 'f', g: %w(h i j) }.to_json
custom_attributes = {
required: { provided: true, expected: 'required' },
autocomplete: { provided: false, expected: 'false' },
placeholder: { provided: 'Seymour Skinner', expected: 'Seymour Skinner' },
data: { provided: { a: 'b' }, expected: { 'data-a' => 'b' } },
class: { provided: %w(red spots), expected: %w(red spots) },
value: { provided: 'Montgomery Montgomery', expected: 'Montgomery Montgomery' },

# aria-label is a special case, along with value it should not be treated
# like a list when deep merging
data: {
provided: {
a: 'b',
c: 'd',
json: sample_json,
},
expected: {
'data-a' => 'b',
'data-c' => 'd',
'data-json' => sample_json,
}
},
aria: {
provided: { c: 'd', label: 'Burns Burns' },
expected: { 'aria-c' => 'd', 'aria-label' => 'Burns Burns' }
provided: {
# these mergeable items all accept lists of values that are space
# separated strings (e.g., aria-controls: "item-a item-b") so should
# be deeply-merged with attributes generated by the form builder.
#
# Any duplicate values will be repeated in this proces.
#
# see: GOVUKDesignSystemFormbuilder::Traits::HTMLAttributes::Attributes::MERGEABLE)
controls: 'Risotto Risotto',
describedby: 'Wolfcastle Wolfcastle',
flowto: 'McClure McClure',
labelledby: 'Spuckler Spuckler',
owns: 'Muntz Muntz',

# aria-label is a special case, along with value it should not be
# treated like a list when deep merging
label: 'Burns Burns',

},
expected: {
'aria-label' => 'Burns Burns',

'aria-controls' => 'Risotto',
'aria-describedby' => 'Wolfcastle',
'aria-flowto' => 'McClure',
'aria-labelledby' => 'Spuckler',
'aria-owns' => 'Muntz',
}
},
}

Expand Down Expand Up @@ -53,7 +89,7 @@
when key.in?(special_keys)
specify "#{val} is set" do
val.each do |data_attribute_name, data_attribute_value|
expect(subject).to have_tag(described_element, with: { data_attribute_name => data_attribute_value })
expect(parsed_subject.at_css(described_element)[data_attribute_name]).to match(data_attribute_value)
end
end

Expand Down

0 comments on commit 90c3397

Please sign in to comment.