From 246c9ebca6425199ad2ca4ce956712e758dbf951 Mon Sep 17 00:00:00 2001 From: Marco Pasqualetti <24919330+marcalexiei@users.noreply.github.com> Date: Tue, 14 May 2024 07:33:39 +0200 Subject: [PATCH] types: add tests for context values (#2188) --- test/typescript/custom-types/i18next.d.ts | 2 ++ test/typescript/custom-types/t.test.ts | 35 +++++++++++++++++++++++ test/typescript/test.namespace.samples.ts | 6 ++++ 3 files changed, 43 insertions(+) diff --git a/test/typescript/custom-types/i18next.d.ts b/test/typescript/custom-types/i18next.d.ts index 5fccf5a0..f70fa616 100644 --- a/test/typescript/custom-types/i18next.d.ts +++ b/test/typescript/custom-types/i18next.d.ts @@ -9,6 +9,7 @@ import { TestNamespaceFallback, TestNamespaceNonPlurals, TestNamespaceInterpolators, + TestNamespaceContextAlternate, } from '../test.namespace.samples'; declare module 'i18next' { @@ -23,6 +24,7 @@ declare module 'i18next' { fallback: TestNamespaceFallback; ctx: TestNamespaceContext; + ctxAlternate: TestNamespaceContextAlternate; plurals: TestNamespacePlurals; diff --git a/test/typescript/custom-types/t.test.ts b/test/typescript/custom-types/t.test.ts index e409a734..9a7ce725 100644 --- a/test/typescript/custom-types/t.test.ts +++ b/test/typescript/custom-types/t.test.ts @@ -203,6 +203,41 @@ describe('t', () => { // }); }); + describe('context + explicit namespace', () => { + const t = (() => '') as TFunction<['ctx']>; + + it('should work with basic usage', () => { + expectTypeOf(t('ctx:dessert', { context: 'cake' })).toEqualTypeOf<'a nice cake'>(); + + // context + plural + expectTypeOf(t('ctx:dessert', { context: 'muffin', count: 3 })).toMatchTypeOf(); + + // @ts-expect-error + // valid key with invalid context + assertType(t('ctx:foo', { context: 'cake' })); + }); + }); + + describe('context with `t` function with multiple namespaces', () => { + const t = (() => '') as TFunction<['ctx', 'ctxAlternate']>; + + it('should work with basic usage', () => { + expectTypeOf(t('ctx:dessert', { context: 'cake' })).toEqualTypeOf<'a nice cake'>(); + + // context + plural + expectTypeOf(t('ctx:dessert', { context: 'muffin', count: 3 })).toMatchTypeOf(); + + // @ts-expect-error + // valid key with invalid context + assertType(t('ctx:foo', { context: 'cake' })); + }); + + it('should work with text value from another namespace', () => { + expectTypeOf(t('ctxAlternate:game')).toMatchTypeOf(); + expectTypeOf(t('ctxAlternate:game', { context: 'monopoly' })).toMatchTypeOf(); + }); + }); + it('should work with false plural usage', () => { const t = (() => '') as TFunction<'nonPlurals'>; diff --git a/test/typescript/test.namespace.samples.ts b/test/typescript/test.namespace.samples.ts index 13ebe235..31e61d5e 100644 --- a/test/typescript/test.namespace.samples.ts +++ b/test/typescript/test.namespace.samples.ts @@ -43,6 +43,12 @@ export type TestNamespaceContext = { beverage_water: 'cold water'; }; +export type TestNamespaceContextAlternate = { + game: 'A fun game'; + game_chess: 'Chess'; + game_monopoly: 'Monopoly'; +}; + export type TestNamespacePlurals = { foo_zero: 'foo'; foo_one: 'foo';