Skip to content

Commit

Permalink
types: add test for accessing a context default value (#2186)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcalexiei committed May 12, 2024
1 parent 9ce9150 commit bf45865
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
11 changes: 11 additions & 0 deletions test/typescript/custom-types/t.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ describe('t', () => {
assertType(t('foo', { context: 'cake' }));
});

it('should accept a default context key as a valid `t` function key', () => {
expectTypeOf(t('beverage')).toMatchTypeOf('cold water');
});

it('should throw error when no `context` is provided using and the context key has no default value ', () => {
// @ts-expect-error dessert has no default value, it needs a context
expectTypeOf(t('dessert')).toMatchTypeOf('error');
});

it('should work with enum as a context value', () => {
enum Dessert {
CAKE = 'cake',
Expand All @@ -163,7 +172,9 @@ describe('t', () => {
const ctx = Dessert.CAKE;

expectTypeOf(t('dessert', { context: ctx })).toMatchTypeOf<string>();
});

it('should trow error with string union with missing context value', () => {
enum DessertMissingValue {
COOKIE = 'cookie',
CAKE = 'cake',
Expand Down
4 changes: 4 additions & 0 deletions test/typescript/test.namespace.samples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export type TestNamespaceContext = {
dessert_cake: 'a nice cake';
dessert_muffin_one: 'a nice muffin';
dessert_muffin_other: '{{count}} nice muffins';

beverage: 'a classic beverage';
beverage_beer: 'fresh beer';
beverage_water: 'cold water';
};

export type TestNamespacePlurals = {
Expand Down

0 comments on commit bf45865

Please sign in to comment.