0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

test(core): add ut for util methods (#264)

add ut for util methods
This commit is contained in:
simeng-li 2022-02-21 16:42:17 +08:00 committed by GitHub
parent 4571af65ea
commit 7488bc7e17
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 100 additions and 0 deletions

View 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' }));
});
});

View 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();
}
});
});

View 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')
);
});
});