Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types: add tests for context values #2188

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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