Skip to content

Commit

Permalink
Add specs covering localisation via procs
Browse files Browse the repository at this point in the history
The :value_method, :text_method and :hint_method params passed to radio
collections and the :hint_method params passed to checkbox collections
now accept procs which can be used to customise the value retrieved from
the item.

This could be used for simple transformations like upcasing the text or
more-complex operations like localising the value, as per the provided
specs

Refs #68
  • Loading branch information
peteryates committed Dec 7, 2019
1 parent de79ebe commit 3c76246
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@
end
end
end

it_behaves_like 'a field that allows the hint to be localised via a proc' do
let(:attribute) { :favourite_colour }
let(:i18n_proc) { ->(item) { I18n.t("colours.#{item.name.downcase}") } }
let(:args) { [method, attribute, colours, :id, :name].push(i18n_proc) }
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@
end
end

it_behaves_like 'a field that allows the label to be localised via a proc' do
let(:i18n_proc) { ->(item) { I18n.t("colours.#{item.name.downcase}") } }
let(:args) { [method, attribute, colours, :id, i18n_proc] }
end

context 'radio button hints' do
let(:colours_with_descriptions) { colours.select { |c| c.description.present? } }
let(:colours_without_descriptions) { colours.reject { |c| c.description.present? } }
Expand Down Expand Up @@ -117,6 +122,11 @@
specify 'all labels should be bold when hints are enabled' do
expect(subject).to have_tag('label', with: { class: 'govuk-label--s' }, count: colours.size)
end

it_behaves_like 'a field that allows the hint to be localised via a proc' do
let(:i18n_proc) { ->(item) { I18n.t("colours.#{item.name.downcase}") } }
let(:args) { [method, attribute, colours, :id, :name, i18n_proc] }
end
end

context 'layout direction' do
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'rspec-html-matchers'
require 'action_view'
require 'active_model'
require 'active_support'
require 'pry'
require 'simplecov'

Expand Down
5 changes: 5 additions & 0 deletions spec/support/locales/colours.de.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
colours:
red: Rot
green: Grün
yellow: Gelb
blue: Blau
11 changes: 10 additions & 1 deletion spec/support/localisation.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
def with_localisations(localisations, locale: :en)
DEFAULT_LOCALE = :en

def with_localisations(localisations, locale: DEFAULT_LOCALE)
unless locale.eql?(DEFAULT_LOCALE)
I18n.available_locales = [DEFAULT_LOCALE, locale]
I18n.locale = locale
end
I18n.backend.store_translations(locale, localisations[locale])

yield
ensure
I18n.available_locales = DEFAULT_LOCALE
I18n.locale = DEFAULT_LOCALE

I18n.reload!
end
46 changes: 46 additions & 0 deletions spec/support/shared/shared_localisation_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,49 @@
end
end
end

shared_examples 'a field that allows the label to be localised via a proc' do
let(:locale) { :de }
let(:localisations) do
YAML.load_file('spec/support/locales/colours.de.yaml')
end

context 'localising collection items using procs' do
subject { builder.send(*args) }

specify 'labels should be present and correctly-localised' do
with_localisations({ locale => localisations }, locale: locale) do
colours.each do |c|
expect(subject).to have_tag(
'label',
text: localisations.dig("colours", c.name.downcase),
with: { class: 'govuk-label' }
)
end
end
end
end
end

shared_examples 'a field that allows the hint to be localised via a proc' do
let(:locale) { :de }
let(:localisations) do
YAML.load_file('spec/support/locales/colours.de.yaml')
end

context 'localising collection items using procs' do
subject { builder.send(*args) }

specify 'hints should be present and correctly-localised' do
with_localisations({ locale => localisations }, locale: locale) do
colours.each do |c|
expect(subject).to have_tag(
'span',
text: localisations.dig("colours", c.name.downcase),
with: { class: 'govuk-hint' }
)
end
end
end
end
end

0 comments on commit 3c76246

Please sign in to comment.