Skip to content

Commit

Permalink
Add tests for translation helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
injms committed Nov 23, 2020
1 parent 1b62fd8 commit c7ca70c
Showing 1 changed file with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
RSpec.describe GovukPublishingComponents::Presenters::SharedHelper do
describe "Shared component helper" do
after(:each) do
I18n.locale = :en
end

it "returns a default margin class" do
shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new({})
margin_class = shared_helper.get_margin_bottom
Expand Down Expand Up @@ -52,5 +56,55 @@
GovukPublishingComponents::Presenters::SharedHelper.new(classes: "js-okay not-cool-man")
}.to raise_error(ArgumentError, "Passed classes must be prefixed with `js-`")
end

it "returns nil if given locale is same as page locale" do
default_locale = I18n.locale
shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new({})

expect(shared_helper.t_locale_check(default_locale)).to be nil
end

it "returns a locale if different to the page locale" do
locale = "ar"
shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new({})

expect(shared_helper.t_locale_check(locale)).to eq locale
end

it "returns the language attribute if translation is not present" do
I18n.locale = :de

translation_key = "this.is.a.key.that.should.not.be.found.to.test.the.translation"
shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new({})

expect(shared_helper.t_lang(translation_key)).to eq "lang=en"
end

it "returns no language attribute if translation is present" do
I18n.locale = :fr

translation_key = "components.contents_list.contents"
shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new({})

expect(shared_helper.t_lang(translation_key)).to be nil
end

it "returns the locale if translation is not present" do
I18n.locale = :de

translation_key = "this.is.a.key.that.should.not.be.found.to.test.the.translation"
shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new({})

expect(shared_helper.t_locale(translation_key)).to eq :en
end

it "returns no locale if translation is present and using default locale" do
I18n.locale = :fr

translation_key = "components.contents_list.contents"
shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new({})

expect(shared_helper.t_locale(translation_key)).to be :fr
end
end
end

0 comments on commit c7ca70c

Please sign in to comment.