Skip to content

Commit

Permalink
types: add tests for context values (i18next#2188)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcalexiei authored and heharm committed Aug 15, 2024
1 parent 3deb0d6 commit 246c9eb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
2 changes: 2 additions & 0 deletions test/typescript/custom-types/i18next.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
TestNamespaceFallback,
TestNamespaceNonPlurals,
TestNamespaceInterpolators,
TestNamespaceContextAlternate,
} from '../test.namespace.samples';

declare module 'i18next' {
Expand All @@ -23,6 +24,7 @@ declare module 'i18next' {
fallback: TestNamespaceFallback;

ctx: TestNamespaceContext;
ctxAlternate: TestNamespaceContextAlternate;

plurals: TestNamespacePlurals;

Expand Down
35 changes: 35 additions & 0 deletions test/typescript/custom-types/t.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>();

// @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<string>();

// @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<string>();
expectTypeOf(t('ctxAlternate:game', { context: 'monopoly' })).toMatchTypeOf<string>();
});
});

it('should work with false plural usage', () => {
const t = (() => '') as TFunction<'nonPlurals'>;

Expand Down
6 changes: 6 additions & 0 deletions test/typescript/test.namespace.samples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 246c9eb

Please sign in to comment.