-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add shared examples for localisation functionality
The with_localisations method allows us to temporarily set the locale backend and always cleans up after itself. Refs #68
- Loading branch information
1 parent
5b41c9c
commit 8c88349
Showing
3 changed files
with
115 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,12 @@ | ||
helpers: | ||
label: | ||
person: | ||
name: Who goes there? | ||
fieldset: | ||
person: | ||
favourite_colour: To which colours are you most partial? | ||
hint: | ||
person: | ||
name: Check your birth certificate or passport | ||
favourite_colour: |- | ||
Yes, there are plenty of options but please pick the one you enjoy most |
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,7 @@ | ||
def with_localisations(localisations, locale: :en) | ||
I18n.backend.store_translations(locale, localisations[locale]) | ||
|
||
yield | ||
ensure | ||
I18n.reload! | ||
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,96 @@ | ||
LOCALISATIONS = { | ||
en: YAML.load_file('spec/support/locales/sample.en.yaml') | ||
}.freeze | ||
|
||
shared_examples 'a field that supports setting the label via localisation' do | ||
let(:localisations) { LOCALISATIONS } | ||
|
||
context 'localising when no text is supplied' do | ||
let(:expected_label) { I18n.translate("helpers.label.person.#{attribute}") } | ||
subject { builder.send(*args) } | ||
|
||
specify 'should set the label from the locales' do | ||
with_localisations(localisations) do | ||
expect(subject).to have_tag('label', text: expected_label) | ||
end | ||
end | ||
end | ||
|
||
context 'allowing localised text to be overridden' do | ||
let(:expected_label) { "Yeah but, who are you really?" } | ||
|
||
subject { builder.send(*args.push(label: { text: expected_label })) } | ||
|
||
specify 'should use the supplied label text' do | ||
with_localisations(localisations) do | ||
expect(subject).to have_tag('label', text: expected_label) | ||
end | ||
end | ||
end | ||
end | ||
|
||
shared_examples 'a field that supports setting the hint via localisation' do | ||
let(:arbitrary_html_content) { builder.tag.p("a wild paragraph has appeared") } | ||
let(:localisations) { LOCALISATIONS } | ||
|
||
context 'localising when no text is supplied' do | ||
let(:expected_hint) { I18n.translate("helpers.hint.person.#{attribute}") } | ||
|
||
subject { builder.send(*args) { arbitrary_html_content } } | ||
|
||
specify 'should set the hint from the locales' do | ||
with_localisations(localisations) do | ||
expect(subject).to have_tag('span', text: expected_hint, with: { class: 'govuk-hint' }) | ||
end | ||
end | ||
end | ||
|
||
context 'allowing localised text to be overridden' do | ||
let(:expected_hint) { "It's quite a straightforward question!" } | ||
|
||
subject do | ||
builder.send(*args.push(hint_text: expected_hint)) { arbitrary_html_content } | ||
end | ||
|
||
specify 'should use the supplied hint text' do | ||
with_localisations(localisations) do | ||
expect(subject).to have_tag('span', text: expected_hint, with: { class: 'govuk-hint' }) | ||
end | ||
end | ||
end | ||
end | ||
|
||
shared_examples 'a field that supports setting the legend via localisation' do | ||
let(:arbitrary_html_content) { builder.tag.p("a wild paragraph has appeared") } | ||
let(:localisations) { LOCALISATIONS } | ||
|
||
context 'localising when no text is supplied' do | ||
let(:expected_legend) { I18n.translate("helpers.fieldset.person.#{attribute}") } | ||
subject { builder.send(*args) { arbitrary_html_content } } | ||
|
||
specify 'should set the legend from the locales' do | ||
with_localisations(localisations) do | ||
expect(subject).to have_tag('fieldset') do | ||
with_tag('legend', with: { class: 'govuk-fieldset__legend' }) do | ||
with_tag('h1', text: expected_legend, with: { class: 'govuk-fieldset__heading' }) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
context 'allowing localised text to be overridden' do | ||
let(:expected_legend) { "It is quite a straightforward question!" } | ||
subject { builder.send(*args.push(legend: { text: expected_legend })) { arbitrary_html_content } } | ||
|
||
specify 'should set the legend from the locales' do | ||
with_localisations(localisations) do | ||
expect(subject).to have_tag('fieldset') do | ||
with_tag('legend', with: { class: 'govuk-fieldset__legend' }) do | ||
with_tag('h1', text: expected_legend, with: { class: 'govuk-fieldset__heading' }) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |