From 7488bc7e17edc416d3ffea03ed6413c282d2d20b Mon Sep 17 00:00:00 2001 From: simeng-li Date: Mon, 21 Feb 2022 16:42:17 +0800 Subject: [PATCH] test(core): add ut for util methods (#264) add ut for util methods --- packages/core/src/utils/assert-that.test.ts | 24 ++++++++++ packages/core/src/utils/id.test.ts | 23 +++++++++ packages/core/src/utils/zod.test.ts | 53 +++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 packages/core/src/utils/assert-that.test.ts create mode 100644 packages/core/src/utils/id.test.ts create mode 100644 packages/core/src/utils/zod.test.ts diff --git a/packages/core/src/utils/assert-that.test.ts b/packages/core/src/utils/assert-that.test.ts new file mode 100644 index 000000000..8c0a86782 --- /dev/null +++ b/packages/core/src/utils/assert-that.test.ts @@ -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' })); + }); +}); diff --git a/packages/core/src/utils/id.test.ts b/packages/core/src/utils/id.test.ts new file mode 100644 index 000000000..189b6dd3f --- /dev/null +++ b/packages/core/src/utils/id.test.ts @@ -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(); + } + }); +}); diff --git a/packages/core/src/utils/zod.test.ts b/packages/core/src/utils/zod.test.ts new file mode 100644 index 000000000..39ed3b758 --- /dev/null +++ b/packages/core/src/utils/zod.test.ts @@ -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') + ); + }); +});