Files
openchamber/packages/ui/src/lib/i18n/store.test.ts
T
Isaac Sanchez-HawkinsandIsaac Sanchez edd984c08f fix(i18n): retry failed locale loads (#1206)
* fix(i18n): retry failed locale loads

* test(i18n): reset locale cache in retry test

---------

Co-authored-by: Isaac Sanchez <isanchez-hawkins@arize.com>
2026-05-12 10:56:17 +03:00

46 lines
1.2 KiB
TypeScript

import { beforeEach, describe, expect, test } from 'bun:test';
import { DEFAULT_LOCALE, type Locale } from './runtime';
import { resetI18nDictionaryCacheForTests, useI18nStore } from './store';
const defaultDictionary = useI18nStore.getState().dictionary;
const resetStore = () => {
resetI18nDictionaryCacheForTests();
useI18nStore.setState({
locale: DEFAULT_LOCALE,
dictionary: defaultDictionary,
loadingLocale: null,
});
};
const waitForLocaleLoadToSettle = async (locale: Locale) => {
for (let attempt = 0; attempt < 20; attempt += 1) {
if (useI18nStore.getState().loadingLocale !== locale) {
return;
}
await new Promise((resolve) => setTimeout(resolve, 0));
}
throw new Error(`Timed out waiting for ${locale} dictionary load`);
};
describe('i18n store', () => {
beforeEach(resetStore);
test('retries loading the active locale when it is not cached', async () => {
useI18nStore.setState({
locale: 'es',
dictionary: defaultDictionary,
loadingLocale: null,
});
try {
useI18nStore.getState().setLocale('es');
expect(useI18nStore.getState().loadingLocale).toBe('es');
await waitForLocaleLoadToSettle('es');
} finally {
resetStore();
}
});
});