Skip to content

Commit

Permalink
Merge pull request #2120 from tvdeyen/fix-translations-spec
Browse files Browse the repository at this point in the history
Fixes i18n Jest specs
  • Loading branch information
tvdeyen authored Jun 7, 2021
2 parents c03a531 + a34c5d3 commit d000c63
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
23 changes: 23 additions & 0 deletions package/src/__tests__/i18n.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,20 @@ describe("translate", () => {
})

describe("if translation is present", () => {
beforeEach(() => {
Alchemy.translations = { en: { help: "Help" } }
})

it("Returns translated string", () => {
expect(translate("help")).toEqual("Help")
})

describe("if key includes a period", () => {
describe("that is translated", () => {
beforeEach(() => {
Alchemy.translations = { en: { formats: { date: "Y-m-d" } } }
})

it("splits into group", () => {
expect(translate("formats.date")).toEqual("Y-m-d")
})
Expand All @@ -40,6 +48,10 @@ describe("translate", () => {
})

describe("if replacement is given", () => {
beforeEach(() => {
Alchemy.translations = { en: { allowed_chars: "of %{number} chars" } }
})

it("replaces it", () => {
expect(translate("allowed_chars", 5)).toEqual("of 5 chars")
})
Expand Down Expand Up @@ -67,4 +79,15 @@ describe("translate", () => {
spy.mockRestore()
})
})

describe("if Alchemy.translations is not set", () => {
it("Returns passed string and logs a warning", () => {
const spy = jest.spyOn(console, "warn").mockImplementation(() => {})
expect(translate("help")).toEqual("help")
expect(spy.mock.calls).toEqual([
["Translations for locale kl not found!"]
])
spy.mockRestore()
})
})
})
2 changes: 1 addition & 1 deletion package/src/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function currentLocale() {

function getTranslations() {
const locale = currentLocale()
const translations = Alchemy.translations[locale]
const translations = Alchemy.translations && Alchemy.translations[locale]

if (translations) {
return translations
Expand Down

0 comments on commit d000c63

Please sign in to comment.