Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix alignment of conditional content beneath radio buttons and checkboxes #50

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,20 @@ def initialize(builder, object_name, attribute_name, value, label:, hint_text:,
end

def html
@builder.content_tag('div', class: 'govuk-checkboxes__item') do
@builder.safe_join(
[
input,
label_element.html,
hint_element.html,
@conditional_content
]
)
end
@builder.safe_join(
[
@builder.content_tag('div', class: 'govuk-checkboxes__item') do
@builder.safe_join(
[
input,
label_element.html,
hint_element.html,
]
)
end,
@conditional_content
]
)
end

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,20 @@ def initialize(builder, object_name, attribute_name, value, label:, hint_text:,
end

def html
@builder.content_tag('div', class: 'govuk-radios__item') do
@builder.safe_join(
[
input,
label_element.html,
hint_element.html,
@conditional_content
]
)
end
@builder.safe_join(
[
@builder.content_tag('div', class: 'govuk-radios__item') do
@builder.safe_join(
[
input,
label_element.html,
hint_element.html,
]
)
end,
@conditional_content
]
)
end

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@
end
end

specify 'should place the conditional content at the same level as the checkbox container' do
expect(parsed_subject).to have_root_element_with_class('govuk-checkboxes__conditional')
end

specify 'should include content provided in the block in a conditional div' do
expect(subject).to have_tag('div', with: { class: 'govuk-checkboxes__conditional govuk-checkboxes__conditional--hidden' }) do |cd|
expect(cd).to have_tag('label', with: { class: 'govuk-label' }, text: 'Project_responsibilities')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
end
end

specify 'should place the conditional content at the same level as the radio button container' do
expect(parsed_subject).to have_root_element_with_class('govuk-radios__conditional')
end

specify 'should include content provided in the block in a conditional div' do
expect(subject).to have_tag('div', with: { class: 'govuk-radios__conditional govuk-radios__conditional--hidden' }) do |cd|
expect(cd).to have_tag('label', with: { class: 'govuk-label' }, text: 'Favourite_colour_reason')
Expand Down
4 changes: 2 additions & 2 deletions spec/support/html_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
def extract_classes(nokogiri_document, selector)
nokogiri_document
def extract_classes(nokogiri_fragment, selector)
nokogiri_fragment
.at_css(selector)
.attributes['class']
.value
Expand Down
18 changes: 18 additions & 0 deletions spec/support/matchers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'rspec/expectations'

RSpec::Matchers.define :have_root_element_with_class do |class_name|
# We're using xpath here because there's no sane way (ie without wrapping
# everything in a useless element just for testing) of checking that elements
# appear at the root of the fragment
#
# Note that the ./* matches ANY element `*` at the root `./` of the fragment
match do |nokogiri_fragment|
!nokogiri_fragment.xpath(%(./*[contains(concat(" ", @class, " "), " #{class_name} ")])).empty?
end

#:nocov:
failure_message do |nokogiri_fragment|
"expected that #{class_name} would be a class of a root element in fragment: #{nokogiri_fragment}"
end
#:nocov:
end
2 changes: 1 addition & 1 deletion spec/support/shared/setup_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
let(:object) { Person.new(name: 'Joey') }
let(:object_name) { :person }
let(:builder) { described_class.new(object_name, object, helper, {}) }
let(:parsed_subject) { Nokogiri.parse(subject) }
let(:parsed_subject) { Nokogiri::HTML::DocumentFragment.parse(subject) }
end