chore: write initial i18n unit tests

pull/3206/head
Ryan Miller 8 months ago
parent b9326f6bc3
commit ef03b9cf7e

@ -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! <b>Welcome!</b>');
});
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! <b>Welcome, Bob!</b>');
});
});

@ -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}! <b>Welcome!</b>');
});
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}! <b>Welcome, {arg}!</b>');
});
});

@ -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');
});
});

@ -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!');
});
});

@ -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}! <b>Welcome!</b>',
argInTag: 'Hello, {name}! <b>Welcome, {arg}!</b>',
} as const;
export function initI18n(dictionary: Record<string, string> = en) {
return setupI18n({ initialLocale: 'en', initialDictionary: dictionary as LocalizerDictionary });
}
Loading…
Cancel
Save