-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c02d6d6
commit 57b0b34
Showing
5 changed files
with
441 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
module GovukRSpecHelpers | ||
class ChooseGovukRadio | ||
|
||
attr_reader :page, :label_text | ||
|
||
def initialize(page:, label_text:) | ||
@label_text = label_text | ||
@page = page | ||
end | ||
|
||
def choose | ||
labels = page.all('label', text: label_text, exact_text: true, normalize_ws: true) | ||
|
||
if labels.size == 0 | ||
check_for_input_id_match | ||
|
||
raise "Unable to find label with the text \"#{label_text}\"" | ||
end | ||
|
||
@label = labels.first | ||
|
||
check_that_fieldset_legend_was_specified | ||
|
||
@label.click | ||
end | ||
|
||
private | ||
|
||
def check_for_input_id_match | ||
inputs = page.all('input', id: label_text) | ||
|
||
if inputs.size > 0 | ||
|
||
matching_labels = page.all("label[for=#{label_text}]") | ||
|
||
if matching_labels.size > 0 | ||
raise "Use the full label text \"#{matching_labels.first.text}\" instead of the input ID" | ||
end | ||
end | ||
end | ||
|
||
def check_that_fieldset_legend_was_specified | ||
if page.current_scope.is_a?(Capybara::Node::Document) | ||
|
||
fieldset = @label.ancestor('fieldset') | ||
legend = fieldset.find('legend') | ||
|
||
if legend | ||
raise "Specify the legend using: within_govuk_fieldset \"#{legend.text}\" do" | ||
end | ||
end | ||
end | ||
|
||
end | ||
|
||
def choose_govuk_radio(label_text) | ||
ChooseGovukRadio.new(page: page, label_text: label_text).choose | ||
end | ||
|
||
RSpec.configure do |rspec| | ||
rspec.include self | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
module GovukRSpecHelpers | ||
class WithinGovukFieldset | ||
|
||
attr_reader :page, :legend_text, :hint, :block | ||
|
||
def initialize(page:, legend_text:, hint:, block:) | ||
@legend_text = legend_text | ||
@hint = hint | ||
@page = page | ||
@block = block | ||
end | ||
|
||
def within | ||
|
||
legends = page.all('legend', text: legend_text, exact_text: true, normalize_ws: true) | ||
|
||
if legends.size == 0 | ||
check_for_fieldset_id_match | ||
raise "No fieldset found with matching legend" | ||
end | ||
|
||
legend = legends.first | ||
@fieldset = legend.ancestor('fieldset') | ||
|
||
check_hint | ||
|
||
@page.within(@fieldset) do | ||
block.call | ||
end | ||
end | ||
|
||
private | ||
|
||
def check_hint | ||
if hint | ||
hint_elements = @fieldset.all('.govuk-hint', text: hint, exact_text: true, normalize_ws: true) | ||
|
||
if hint_elements.size == 0 | ||
raise "Count not find hint with that text" | ||
end | ||
|
||
hint_id = hint_elements.first[:id] | ||
|
||
if !@fieldset["aria-describedby"].to_s.split(/\s+/).include?(hint_id) | ||
raise "Found hint but it is not associated with the fieldset using aria-describedby" | ||
end | ||
|
||
end | ||
end | ||
|
||
def check_for_fieldset_id_match | ||
matching_fieldsets = page.all("fieldset[id=#{Capybara::Selector::CSS.escape(legend_text)}]") | ||
|
||
if matching_fieldsets.size > 0 | ||
|
||
legends = matching_fieldsets.first.all('legend') | ||
|
||
if legends.size > 0 | ||
raise "Use the full legend text \"#{legends.first.text}\" instead of the fieldset ID" | ||
end | ||
end | ||
end | ||
|
||
end | ||
|
||
def within_govuk_fieldset(legend_text, hint: nil, &block) | ||
WithinGovukFieldset.new(page: page, legend_text: legend_text, hint: hint, block: block).within | ||
end | ||
|
||
RSpec.configure do |rspec| | ||
rspec.include self | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# frozen_string_literal: true | ||
require "spec_helper" | ||
|
||
RSpec.describe "choosing a radio button", type: :feature do | ||
|
||
context "where the options are in a fieldset and associated with a label" do | ||
|
||
before do | ||
TestApp.body = '<form action="/success" method="post"> | ||
<div class="govuk-form-group"> | ||
<fieldset class="govuk-fieldset"> | ||
<legend class="govuk-fieldset__legend govuk-fieldset__legend--l"> | ||
<h1 class="govuk-fieldset__heading"> | ||
Where do you live? | ||
</h1> | ||
</legend> | ||
<div class="govuk-radios" data-module="govuk-radios"> | ||
<div class="govuk-radios__item"> | ||
<input class="govuk-radios__input" id="whereDoYouLive" name="whereDoYouLive" type="radio" value="england"> | ||
<label class="govuk-label govuk-radios__label" for="whereDoYouLive"> | ||
England | ||
</label> | ||
</div> | ||
<div class="govuk-radios__item"> | ||
<input class="govuk-radios__input" id="whereDoYouLive-2" name="whereDoYouLive" type="radio" value="scotland"> | ||
<label class="govuk-label govuk-radios__label" for="whereDoYouLive-2"> | ||
Scotland | ||
</label> | ||
</div> | ||
<div class="govuk-radios__item"> | ||
<input class="govuk-radios__input" id="whereDoYouLive-3" name="whereDoYouLive" type="radio" value="wales"> | ||
<label class="govuk-label govuk-radios__label" for="whereDoYouLive-3"> | ||
Wales | ||
</label> | ||
</div> | ||
<div class="govuk-radios__item"> | ||
<input class="govuk-radios__input" id="whereDoYouLive-4" name="whereDoYouLive" type="radio" value="northern-ireland"> | ||
<label class="govuk-label govuk-radios__label" for="whereDoYouLive-4"> | ||
Northern Ireland | ||
</label> | ||
</div> | ||
</div> | ||
</fieldset> | ||
</div> | ||
</form>' | ||
visit('/') | ||
end | ||
|
||
context "and the full fieldset and label is specified" do | ||
it 'should be successful' do | ||
within_govuk_fieldset "Where do you live?" do | ||
choose_govuk_radio "Wales" | ||
end | ||
|
||
expect(page).to have_checked_field("Wales") | ||
end | ||
end | ||
|
||
context "and the input ID is specified" do | ||
it 'should raise an error' do | ||
expect { | ||
within_govuk_fieldset "Where do you live?" do | ||
choose_govuk_radio "whereDoYouLive-3" | ||
end | ||
}.to raise_error('Use the full label text "Wales" instead of the input ID') | ||
end | ||
end | ||
|
||
context "and no matching label can be found" do | ||
it 'should raise an error' do | ||
expect { | ||
within_govuk_fieldset "Where do you live?" do | ||
choose_govuk_radio "France" | ||
end | ||
}.to raise_error('Unable to find label with the text "France"') | ||
end | ||
end | ||
|
||
end | ||
end |
Oops, something went wrong.