Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jarda-svoboda committed Apr 6, 2022
1 parent ed07c91 commit 7f3456a
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 19 deletions.
4 changes: 2 additions & 2 deletions tests/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export const CONFIG:Config<
},
{
key: 'common',
locale: 'cs',
loader: async () => (import('../data/translations/cs/common.json')),
locale: 'zh-Hans',
loader: async () => (import('../data/translations/zh-Hans/common.json')),
},
],
parserOptions: {
Expand Down
13 changes: 9 additions & 4 deletions tests/data/translations/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { toDotNotation } from '../../utils';

import * as common from './en/common.json';
import * as route from './en/route.json';
import * as common_cs from './cs/common.json';
import { toDotNotation } from '../../utils';
import * as common_ku from './ku/common.json';
import * as common_zhHans from './zh-Hans/common.json';

export default ({
en: toDotNotation({
common,
route1: route,
route2: route,
}),
cs: toDotNotation({
common: common_cs,
'zh-Hans': toDotNotation({
common: common_zhHans,
}),
ku: toDotNotation({
common: common_ku,
}),
}) as Record<string, any>;
File renamed without changes.
9 changes: 9 additions & 0 deletions tests/data/translations/zh-Hans/common.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"no_placeholder": "NO_PLACEHOLDER",
"placeholder": "VALUES: {{value}}, {{value;}}, {{ value }}, {{ value; }}",
"modifier_test": "VALUES: {{value; option1:VALUE1; option2:VALUE2; default:DEFAULT VALUE;}}, {{ value ; option1 : VALUE1 ; option2 : VALUE2 ; default : DEFAULT VALUE ;}}",
"custom_modifier": "{{date:date;}}",
"modifier_number": "{{value:number;}}",
"modifier_date": "{{value:date;}}",
"modifier_ago": "{{value:ago;}}"
}
51 changes: 38 additions & 13 deletions tests/specs/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import i18n from '../../src';
import { CONFIG, TRANSLATIONS } from '../data';
import { filterTranslationKeys } from '../utils';

const { initLocale = '', loaders } = CONFIG;
const { initLocale = '', loaders = [] } = CONFIG;

describe('i18n instance', () => {
it('exports all properties and methods', () => {
Expand All @@ -27,7 +27,7 @@ describe('i18n instance', () => {

setRoute('/');
const $initialized = get(initialized);
const $loading = get(loading);
const $loading = loading.get();
const $locale = locale.get();

expect($locale).toBe(undefined);
Expand All @@ -39,7 +39,7 @@ describe('i18n instance', () => {

await setLocale(initLocale);
setRoute('/');
const $loading = get(loading);
const $loading = loading.get();
expect($loading).toBe(true);

const $initialized = get(initialized);
Expand All @@ -50,7 +50,7 @@ describe('i18n instance', () => {

setLocale(initLocale);

const $loading = get(loading);
const $loading = loading.get();
expect($loading).toBe(false);
});
it('`setLocale` method triggers loading when route is set', async () => {
Expand All @@ -59,15 +59,15 @@ describe('i18n instance', () => {
await setRoute('');
setLocale(initLocale);

const $loading = get(loading);
const $loading = loading.get();
expect($loading).toBe(true);
});
it('`setLocale` does not set `unknown` locale', async () => {
const { setLocale, loading, locale } = new i18n({ loaders });

setLocale('unknown');

const $loading = get(loading);
const $loading = loading.get();
const $locale = locale.get();

expect($loading).toBe(false);
Expand All @@ -78,7 +78,7 @@ describe('i18n instance', () => {

locale.set(initLocale);

const $loading = get(loading);
const $loading = loading.get();
expect($loading).toBe(false);

await loading.toPromise();
Expand All @@ -92,7 +92,7 @@ describe('i18n instance', () => {
await setRoute('');
locale.set(initLocale);

const $loading = get(loading);
const $loading = loading.get();
expect($loading).toBe(true);

await loading.toPromise();
Expand All @@ -106,7 +106,7 @@ describe('i18n instance', () => {
await setRoute('');
locale.set(initLocale.toUpperCase());

const $loading = get(loading);
const $loading = loading.get();
expect($loading).toBe(true);

await loading.toPromise();
Expand All @@ -117,13 +117,38 @@ describe('i18n instance', () => {
const $locale = locale.get();
expect($locale).toBe(initLocale.toLocaleLowerCase());
});
it('`locale` can be non-standard', async () => {
const nonStandardLocale = 'ku';
const { loading, locale, locales, setRoute, initialized, translations } = new i18n({ loaders: [{ key: 'common', locale: `${nonStandardLocale}`.toUpperCase(), loader: () => import(`../data/translations/${nonStandardLocale}/common.json`) }] });
await setRoute('');
locale.set(nonStandardLocale);

const $loading = loading.get();
expect($loading).toBe(true);

await loading.toPromise();

const $initialized = get(initialized);
expect($initialized).toBe(true);

const $locale = locale.get();
expect($locale).toBe(nonStandardLocale);

const $locales = locales.get();
expect($locales).toContainEqual(nonStandardLocale);

const $translations = translations.get();
expect($translations[nonStandardLocale]).toEqual(
expect.objectContaining(filterTranslationKeys(TRANSLATIONS[nonStandardLocale], ['common'])),
);
});
it('`getTranslationProps` method works', async () => {
const { initialized, getTranslationProps } = new i18n({ loaders });

const [translations = {}] = await getTranslationProps(initLocale);
const $initialized = get(initialized);

const keys = (loaders || []).filter(({ routes }) => !routes).map(({ key }) => key);
const keys = loaders.filter(({ routes }) => !routes).map(({ key }) => key);

expect(translations[initLocale]).toEqual(
expect.objectContaining(filterTranslationKeys(TRANSLATIONS[initLocale], keys)),
Expand All @@ -147,7 +172,7 @@ describe('i18n instance', () => {
addTranslations(TRANSLATIONS);
loadTranslations(initLocale);

expect(get(loading)).toBe(false);
expect(loading.get()).toBe(false);
});
it('initializes properly with `initLocale`', async () => {
const { initialized, loadConfig } = new i18n();
Expand Down Expand Up @@ -175,7 +200,7 @@ describe('i18n instance', () => {
outputArray.push($loading);
});

await loadConfig(CONFIG).then(() => expect(get(loading)).toBe(false));
await loadConfig(CONFIG).then(() => expect(loading.get()).toBe(false));

testArray.forEach((value, index) => {
expect(value).toBe(testArray[index]);
Expand Down Expand Up @@ -214,7 +239,7 @@ describe('i18n instance', () => {
});
it('includes both `translations` when using `fallbackLocale`', async () => {
const { translations, locales, loadConfig } = new i18n();
const fallbackLocale = CONFIG.loaders?.find(({ locale }) => locale.toLowerCase() !== CONFIG.initLocale?.toLowerCase())?.locale;
const fallbackLocale = loaders.find(({ locale }) => locale.toLowerCase() !== initLocale?.toLowerCase())?.locale;

await loadConfig({ ...CONFIG, fallbackLocale });
const $translations = translations.get();
Expand Down

0 comments on commit 7f3456a

Please sign in to comment.