0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-27 21:39:16 -05:00
logto/packages/toolkit/language-kit/src/utility.test.ts

39 lines
1.2 KiB
TypeScript

import { number, ZodError } from 'zod';
import { fallback, isLanguageTag, languageTagGuard } from './utility.js';
describe('isLanguageTag', () => {
it('should pass when input is a valid language key', () => {
expect(isLanguageTag('en-GB')).toBeTruthy();
expect(isLanguageTag('zh-CN')).toBeTruthy();
});
it('should fail when input is not a valid language key', () => {
for (const invalidLanguageKey of [undefined, '', 'xx-XX']) {
expect(isLanguageTag(invalidLanguageKey)).toBeFalsy();
}
});
});
describe('languageTagGuard', () => {
it('should pass when input is a valid language key', () => {
expect(languageTagGuard.safeParse('en-GB').success).toBeTruthy();
expect(languageTagGuard.safeParse('zh-CN').success).toBeTruthy();
});
it('should fail when input is not a valid language key', () => {
for (const invalidLanguageKey of [undefined, '', 'xx-XX']) {
expect(languageTagGuard.safeParse(invalidLanguageKey).success).toBeFalsy();
}
});
});
describe('fallback', () => {
it('should fallback to default value', () => {
const schema = number();
const tolerant = schema.or(fallback(-1));
expect(() => schema.parse('foo')).toThrow(ZodError);
expect(tolerant.parse('foo')).toBe(-1);
});
});