-
-
Notifications
You must be signed in to change notification settings - Fork 627
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Alina Saalfeld <alina.saalfeld@binary-butterfly.de>
- Loading branch information
Showing
4 changed files
with
105 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,105 @@ | ||
import { describe, it, expectTypeOf, assertType } from 'vitest'; | ||
import { TFunction } from 'i18next'; | ||
import { KeyPrefix, Namespace, TFunction } from 'i18next'; | ||
|
||
describe('t', () => { | ||
describe('main', () => { | ||
const t = (() => '') as TFunction<['main']>; | ||
describe('main', () => { | ||
const t = (() => '') as TFunction<['main']>; | ||
|
||
it('works with simple usage', () => { | ||
expectTypeOf(t('arrayOfStrings.0')).toEqualTypeOf<'zero'>(); | ||
expectTypeOf(t('arrayOfStrings.1')).toEqualTypeOf<'one'>(); | ||
it('works with simple usage', () => { | ||
expectTypeOf(t('arrayOfStrings.0')).toEqualTypeOf<'zero'>(); | ||
expectTypeOf(t('arrayOfStrings.1')).toEqualTypeOf<'one'>(); | ||
|
||
expectTypeOf(t('readonlyArrayOfStrings.0')).toEqualTypeOf<'readonly zero'>(); | ||
expectTypeOf(t('readonlyArrayOfStrings.1')).toEqualTypeOf<'readonly one'>(); | ||
expectTypeOf(t('readonlyArrayOfStrings.0')).toEqualTypeOf<'readonly zero'>(); | ||
expectTypeOf(t('readonlyArrayOfStrings.1')).toEqualTypeOf<'readonly one'>(); | ||
|
||
expectTypeOf(t('arrayOfObjects.0.foo')).toEqualTypeOf<'bar'>(); | ||
expectTypeOf(t('arrayOfObjects.1.fizz')).toEqualTypeOf<'buzz'>(); | ||
expectTypeOf(t('arrayOfObjects.0.foo')).toEqualTypeOf<'bar'>(); | ||
expectTypeOf(t('arrayOfObjects.1.fizz')).toEqualTypeOf<'buzz'>(); | ||
|
||
expectTypeOf(t('arrayOfObjects.2.0.test')).toEqualTypeOf<'success'>(); | ||
expectTypeOf(t('arrayOfObjects.2.0.sub.deep')).toEqualTypeOf<'still success'>(); | ||
}); | ||
expectTypeOf(t('arrayOfObjects.2.0.test')).toEqualTypeOf<'success'>(); | ||
expectTypeOf(t('arrayOfObjects.2.0.sub.deep')).toEqualTypeOf<'still success'>(); | ||
}); | ||
|
||
it('should throw an error when key is not present', () => { | ||
// @ts-expect-error expected error | ||
assertType(t('arrayOfStrings.2')); | ||
it('should throw an error when key is not present', () => { | ||
// @ts-expect-error expected error | ||
assertType(t('arrayOfStrings.2')); | ||
|
||
// @ts-expect-error expected error | ||
assertType(t('arrayOfObjects.0.food')); | ||
// @ts-expect-error expected error | ||
assertType(t('arrayOfObjects.0.fizz')); | ||
// @ts-expect-error expected error | ||
assertType(t('arrayOfObjects.0.food')); | ||
// @ts-expect-error expected error | ||
assertType(t('arrayOfObjects.0.fizz')); | ||
|
||
// @ts-expect-error expected error | ||
assertType(t('arrayOfObjects.2')); | ||
// @ts-expect-error expected error | ||
assertType(t('arrayOfObjects.2')); | ||
|
||
// @ts-expect-error expected error | ||
assertType(t('arrayOfObjects.2.bar')); | ||
// @ts-expect-error expected error | ||
assertType(t('arrayOfObjects.2.sub.deep')); | ||
// @ts-expect-error expected error | ||
assertType(t('arrayOfObjects.2.test')); | ||
}); | ||
// @ts-expect-error expected error | ||
assertType(t('arrayOfObjects.2.bar')); | ||
// @ts-expect-error expected error | ||
assertType(t('arrayOfObjects.2.sub.deep')); | ||
// @ts-expect-error expected error | ||
assertType(t('arrayOfObjects.2.test')); | ||
}); | ||
|
||
it('should work with `returnObjects`', () => { | ||
expectTypeOf(t('arrayOfStrings', { returnObjects: true })).toBeArray(); | ||
expectTypeOf(t('arrayOfObjects', { returnObjects: true })).toEqualTypeOf< | ||
[{ foo: 'bar' }, { fizz: 'buzz' }, [{ test: 'success'; sub: { deep: 'still success' } }]] | ||
>(); | ||
expectTypeOf(t('arrayOfObjects.0', { returnObjects: true })).toEqualTypeOf<{ foo: 'bar' }>(); | ||
}); | ||
it('should work with `returnObjects`', () => { | ||
expectTypeOf(t('arrayOfStrings', { returnObjects: true })).toBeArray(); | ||
expectTypeOf(t('arrayOfObjects', { returnObjects: true })).toEqualTypeOf< | ||
[{ foo: 'bar' }, { fizz: 'buzz' }, [{ test: 'success'; sub: { deep: 'still success' } }]] | ||
>(); | ||
expectTypeOf(t('arrayOfObjects.0', { returnObjects: true })).toEqualTypeOf<{ foo: 'bar' }>(); | ||
}); | ||
|
||
it('should work with const keys', () => { | ||
const alternateTranslationKeys = ['arrayOfStrings.0', 'arrayOfObjects.0.foo'] as const; | ||
it('should work with const keys', () => { | ||
const alternateTranslationKeys = ['arrayOfStrings.0', 'arrayOfObjects.0.foo'] as const; | ||
|
||
const result = alternateTranslationKeys.map((value) => t(value)); | ||
const result = alternateTranslationKeys.map((value) => t(value)); | ||
|
||
assertType<string[]>(result); | ||
}); | ||
assertType<string[]>(result); | ||
}); | ||
}); | ||
|
||
it('should work with context', () => { | ||
const t = (() => '') as TFunction<'ctx'>; | ||
it('should work with context', () => { | ||
const t = (() => '') as TFunction<'ctx'>; | ||
|
||
expectTypeOf(t('dessert.0.dessert', { context: 'cake' })).toEqualTypeOf<'a nice cake'>(); | ||
expectTypeOf(t('dessert.0.dessert', { context: 'cake' })).toEqualTypeOf<'a nice cake'>(); | ||
|
||
// context + plural | ||
expectTypeOf(t('dessert.0.dessert', { context: 'muffin', count: 3 })).toMatchTypeOf<string>(); | ||
}); | ||
// context + plural | ||
expectTypeOf(t('dessert.0.dessert', { context: 'muffin', count: 3 })).toMatchTypeOf<string>(); | ||
}); | ||
|
||
it('should process ordinal plurals', () => { | ||
const t = (() => '') as TFunction<'ord'>; | ||
it('should process ordinal plurals', () => { | ||
const t = (() => '') as TFunction<'ord'>; | ||
|
||
expectTypeOf(t('ord.0.place', { ordinal: true, count: 1 })).toBeString(); | ||
expectTypeOf(t('ord.0.place', { ordinal: true, count: 2 })).toBeString(); | ||
expectTypeOf(t('ord.0.place', { ordinal: true, count: 3 })).toBeString(); | ||
expectTypeOf(t('ord.0.place', { ordinal: true, count: 4 })).toBeString(); | ||
}); | ||
|
||
describe("don't break prefixes", () => { | ||
it('does not allow access to morning', () => { | ||
const t = (() => '') as TFunction<'prefix', 'deep'>; | ||
expectTypeOf(t('deep.deep')).toEqualTypeOf<string>(); | ||
|
||
// @ts-expect-error expected error | ||
assertType(t('morning')); | ||
}); | ||
|
||
expectTypeOf(t('ord.0.place', { ordinal: true, count: 1 })).toBeString(); | ||
expectTypeOf(t('ord.0.place', { ordinal: true, count: 2 })).toBeString(); | ||
expectTypeOf(t('ord.0.place', { ordinal: true, count: 3 })).toBeString(); | ||
expectTypeOf(t('ord.0.place', { ordinal: true, count: 4 })).toBeString(); | ||
it('should work with useTranslation', () => { | ||
const useTranslation = <Ns extends Namespace, KPrefix extends KeyPrefix<Ns> = undefined>( | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
_: Ns, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
__: { | ||
keyPrefix?: KPrefix; | ||
}, | ||
): { | ||
t: TFunction<Ns, KPrefix>; | ||
// @ts-expect-error we only care about typing here, not about actual returns | ||
} => undefined; | ||
|
||
const use = useTranslation('prefix', { keyPrefix: 'deep' }); | ||
|
||
expectTypeOf(use).toEqualTypeOf<{ t: TFunction<'prefix', 'deep'> }>(); | ||
|
||
// @ts-expect-error expected error | ||
assertType(use.t('morning')); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters