Skip to content

Commit

Permalink
Add shared examples for localisation functionality
Browse files Browse the repository at this point in the history
The with_localisations method allows us to temporarily set the locale
backend and always cleans up after itself.

Refs #68
  • Loading branch information
peteryates committed Nov 16, 2019
1 parent 5b41c9c commit 8c88349
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
12 changes: 12 additions & 0 deletions spec/support/locales/sample.en.yaml
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
7 changes: 7 additions & 0 deletions spec/support/localisation.rb
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
96 changes: 96 additions & 0 deletions spec/support/shared/shared_localisation_examples.rb
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

0 comments on commit 8c88349

Please sign in to comment.