From ef03b9cf7ed4f756eb57d266edffb55f31a4c815 Mon Sep 17 00:00:00 2001 From: Ryan Miller Date: Thu, 22 Aug 2024 16:55:12 +1000 Subject: [PATCH] chore: write initial i18n unit tests --- .../unit/utils/i18n/getMessage_test.ts | 39 +++++++++++++++++++ .../unit/utils/i18n/getRawMessage_test.ts | 39 +++++++++++++++++++ .../session/unit/utils/i18n/setupI18n_test.ts | 13 +++++++ .../session/unit/utils/i18n/stripped_test.ts | 39 +++++++++++++++++++ ts/test/session/unit/utils/i18n/util.ts | 16 ++++++++ 5 files changed, 146 insertions(+) create mode 100644 ts/test/session/unit/utils/i18n/getMessage_test.ts create mode 100644 ts/test/session/unit/utils/i18n/getRawMessage_test.ts create mode 100644 ts/test/session/unit/utils/i18n/setupI18n_test.ts create mode 100644 ts/test/session/unit/utils/i18n/stripped_test.ts create mode 100644 ts/test/session/unit/utils/i18n/util.ts diff --git a/ts/test/session/unit/utils/i18n/getMessage_test.ts b/ts/test/session/unit/utils/i18n/getMessage_test.ts new file mode 100644 index 000000000..9eca8d16d --- /dev/null +++ b/ts/test/session/unit/utils/i18n/getMessage_test.ts @@ -0,0 +1,39 @@ +// eslint-disable-next-line @typescript-eslint/ban-ts-comment +// @ts-nocheck - TODO: add generic type to setupI18n to fix this + +import { expect } from 'chai'; +import { initI18n, testDictionary } from './util'; + +describe('getMessage', () => { + const i18n = initI18n(testDictionary); + + it('returns the raw message for a token', () => { + const message = i18n('greeting', { name: 'Alice' }); + expect(message).to.equal('Hello, Alice!'); + }); + + it('returns the raw message for a plural token', () => { + const message = i18n('search', { count: 1, found_count: 2 }); + expect(message).to.equal('2 of 1 match'); + }); + + it('returns the raw message for a token with no args', () => { + const message = i18n('noArgs'); + expect(message).to.equal('No args'); + }); + + it('returns the raw message for a token with args', () => { + const message = i18n('args', { name: 'Alice' }); + expect(message).to.equal('Hello, Alice!'); + }); + + it('returns the raw message for a token with a tag', () => { + const message = i18n('tag', { name: 'Alice' }); + expect(message).to.equal('Hello, Alice! Welcome!'); + }); + + it('returns the raw message for a token with a tag and args', () => { + const message = i18n('argInTag', { name: 'Alice', arg: 'Bob' }); + expect(message).to.equal('Hello, Alice! Welcome, Bob!'); + }); +}); diff --git a/ts/test/session/unit/utils/i18n/getRawMessage_test.ts b/ts/test/session/unit/utils/i18n/getRawMessage_test.ts new file mode 100644 index 000000000..ffcb9148b --- /dev/null +++ b/ts/test/session/unit/utils/i18n/getRawMessage_test.ts @@ -0,0 +1,39 @@ +// eslint-disable-next-line @typescript-eslint/ban-ts-comment +// @ts-nocheck - TODO: add generic type to setupI18n to fix this + +import { expect } from 'chai'; +import { initI18n, testDictionary } from './util'; + +describe('getRawMessage', () => { + const i18n = initI18n(testDictionary); + + it('returns the raw message for a token', () => { + const rawMessage = i18n.getRawMessage('greeting', { name: 'Alice' }); + expect(rawMessage).to.equal('Hello, {name}!'); + }); + + it('returns the raw message for a plural token', () => { + const rawMessage = i18n.getRawMessage('search', { count: 1, found_count: 2 }); + expect(rawMessage).to.equal('{found_count} of {count} match'); + }); + + it('returns the raw message for a token with no args', () => { + const rawMessage = i18n.getRawMessage('noArgs'); + expect(rawMessage).to.equal('No args'); + }); + + it('returns the raw message for a token with args', () => { + const rawMessage = i18n.getRawMessage('args', { name: 'Alice' }); + expect(rawMessage).to.equal('Hello, {name}!'); + }); + + it('returns the raw message for a token with a tag', () => { + const rawMessage = i18n.getRawMessage('tag', { name: 'Alice' }); + expect(rawMessage).to.equal('Hello, {name}! Welcome!'); + }); + + it('returns the raw message for a token with a tag and args', () => { + const rawMessage = i18n.getRawMessage('argInTag', { name: 'Alice', arg: 'Bob' }); + expect(rawMessage).to.equal('Hello, {name}! Welcome, {arg}!'); + }); +}); diff --git a/ts/test/session/unit/utils/i18n/setupI18n_test.ts b/ts/test/session/unit/utils/i18n/setupI18n_test.ts new file mode 100644 index 000000000..1e749fe95 --- /dev/null +++ b/ts/test/session/unit/utils/i18n/setupI18n_test.ts @@ -0,0 +1,13 @@ +import { expect } from 'chai'; +import { initI18n } from './util'; + +describe('setupI18n', () => { + it('returns setupI18n with all methods defined', () => { + const setupI18nReturn = initI18n(); + expect(setupI18nReturn).to.be.a('function'); + expect(setupI18nReturn.getRawMessage).to.be.a('function'); + expect(setupI18nReturn.formatMessageWithArgs).to.be.a('function'); + expect(setupI18nReturn.stripped).to.be.a('function'); + expect(setupI18nReturn.inEnglish).to.be.a('function'); + }); +}); diff --git a/ts/test/session/unit/utils/i18n/stripped_test.ts b/ts/test/session/unit/utils/i18n/stripped_test.ts new file mode 100644 index 000000000..655a1cd63 --- /dev/null +++ b/ts/test/session/unit/utils/i18n/stripped_test.ts @@ -0,0 +1,39 @@ +// eslint-disable-next-line @typescript-eslint/ban-ts-comment +// @ts-nocheck - TODO: add generic type to setupI18n to fix this + +import { expect } from 'chai'; +import { initI18n, testDictionary } from './util'; + +describe('stripped', () => { + const i18n = initI18n(testDictionary); + + it('returns the raw message for a token', () => { + const message = i18n.stripped('greeting', { name: 'Alice' }); + expect(message).to.equal('Hello, Alice!'); + }); + + it('returns the raw message for a plural token', () => { + const message = i18n.stripped('search', { count: 1, found_count: 2 }); + expect(message).to.equal('2 of 1 match'); + }); + + it('returns the raw message for a token with no args', () => { + const message = i18n.stripped('noArgs'); + expect(message).to.equal('No args'); + }); + + it('returns the raw message for a token with args', () => { + const message = i18n.stripped('args', { name: 'Alice' }); + expect(message).to.equal('Hello, Alice!'); + }); + + it('returns the raw message for a token with the tags stripped', () => { + const message = i18n.stripped('tag', { name: 'Alice' }); + expect(message).to.equal('Hello, Alice! Welcome!'); + }); + + it('returns the raw message for a token with the tags stripped', () => { + const message = i18n.stripped('argInTag', { name: 'Alice', arg: 'Bob' }); + expect(message).to.equal('Hello, Alice! Welcome, Bob!'); + }); +}); diff --git a/ts/test/session/unit/utils/i18n/util.ts b/ts/test/session/unit/utils/i18n/util.ts new file mode 100644 index 000000000..9e566ff16 --- /dev/null +++ b/ts/test/session/unit/utils/i18n/util.ts @@ -0,0 +1,16 @@ +import { setupI18n } from '../../../../../util/i18n'; +import { en } from '../../../../../localization/locales'; +import type { LocalizerDictionary } from '../../../../../types/Localizer'; + +export const testDictionary = { + greeting: 'Hello, {name}!', + search: '{found_count} of {count} match', + noArgs: 'No args', + args: 'Hello, {name}!', + tag: 'Hello, {name}! Welcome!', + argInTag: 'Hello, {name}! Welcome, {arg}!', +} as const; + +export function initI18n(dictionary: Record = en) { + return setupI18n({ initialLocale: 'en', initialDictionary: dictionary as LocalizerDictionary }); +}