Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes i18n Jest specs #2120

Merged
merged 1 commit into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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