mirror of
https://github.com/logto-io/logto.git
synced 2024-12-16 20:26:19 -05:00
parent
4571af65ea
commit
7488bc7e17
3 changed files with 100 additions and 0 deletions
24
packages/core/src/utils/assert-that.test.ts
Normal file
24
packages/core/src/utils/assert-that.test.ts
Normal file
|
@ -0,0 +1,24 @@
|
|||
import RequestError from '@/errors/RequestError';
|
||||
|
||||
import assertThat from './assert-that';
|
||||
|
||||
describe('assertThat util', () => {
|
||||
it('assert to be truthy', () => {
|
||||
expect(() => {
|
||||
assertThat(true, new Error(' '));
|
||||
}).not.toThrow();
|
||||
});
|
||||
|
||||
it('should throw exception if type is Error', () => {
|
||||
const error = new Error('exception');
|
||||
expect(() => {
|
||||
assertThat(false, error);
|
||||
}).toMatchError(error);
|
||||
});
|
||||
|
||||
it('should throw RequestError if logto errorcode is provided', () => {
|
||||
expect(() => {
|
||||
assertThat(false, 'auth.unauthorized');
|
||||
}).toMatchError(new RequestError({ code: 'auth.unauthorized' }));
|
||||
});
|
||||
});
|
23
packages/core/src/utils/id.test.ts
Normal file
23
packages/core/src/utils/id.test.ts
Normal file
|
@ -0,0 +1,23 @@
|
|||
import { buildIdGenerator, alphabet } from './id';
|
||||
|
||||
describe('id generator', () => {
|
||||
it('should match the input length', () => {
|
||||
const id = buildIdGenerator(10)();
|
||||
expect(id.length).toEqual(10);
|
||||
});
|
||||
|
||||
it('to random id should not equal', () => {
|
||||
const id_1 = buildIdGenerator(10)();
|
||||
const id_2 = buildIdGenerator(10)();
|
||||
|
||||
expect(id_1).not.toEqual(id_2);
|
||||
});
|
||||
|
||||
it('should only contains provided alphabets', () => {
|
||||
const id = buildIdGenerator(20)();
|
||||
|
||||
for (const char of id) {
|
||||
expect(alphabet.includes(char)).toBeTruthy();
|
||||
}
|
||||
});
|
||||
});
|
53
packages/core/src/utils/zod.test.ts
Normal file
53
packages/core/src/utils/zod.test.ts
Normal file
|
@ -0,0 +1,53 @@
|
|||
import { string, boolean, number, object } from 'zod';
|
||||
|
||||
import RequestError from '@/errors/RequestError';
|
||||
|
||||
import { zodTypeToSwagger } from './zod';
|
||||
|
||||
describe('zodTypeToSwagger', () => {
|
||||
it('string type', () => {
|
||||
expect(zodTypeToSwagger(string())).toEqual({ type: 'string' });
|
||||
});
|
||||
|
||||
it('boolean type', () => {
|
||||
expect(zodTypeToSwagger(boolean())).toEqual({ type: 'boolean' });
|
||||
});
|
||||
|
||||
it('number type', () => {
|
||||
expect(zodTypeToSwagger(number())).toEqual({ type: 'number' });
|
||||
});
|
||||
|
||||
it('array type', () => {
|
||||
expect(zodTypeToSwagger(string().array())).toEqual({
|
||||
type: 'array',
|
||||
items: {
|
||||
type: 'string',
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('object type', () => {
|
||||
expect(zodTypeToSwagger(object({ x: string(), y: number().optional() }))).toEqual({
|
||||
type: 'object',
|
||||
properties: {
|
||||
x: {
|
||||
type: 'string',
|
||||
},
|
||||
y: {
|
||||
type: 'number',
|
||||
},
|
||||
},
|
||||
required: ['x'],
|
||||
});
|
||||
});
|
||||
|
||||
it('optional type', () => {
|
||||
expect(zodTypeToSwagger(string().optional())).toEqual({ type: 'string' });
|
||||
});
|
||||
|
||||
it('unknow type', () => {
|
||||
expect(() => zodTypeToSwagger('test')).toMatchError(
|
||||
new RequestError('swagger.invalid_zod_type', 'test')
|
||||
);
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue