|
| 1 | +import type { ExceptionJSON } from '@packages/exceptions' |
| 2 | +import type { ValidationExceptionDTO } from '@packages/exceptions/dtos' |
| 3 | +import { ExceptionCode } from '@packages/exceptions/enums' |
| 4 | +import ERRORS from '@packages/exceptions/tests/fixtures/validation-errors.fixture' |
| 5 | +import type { Testcase } from '@tests/utils/types' |
| 6 | +import TestSubject from '../validation.exception' |
| 7 | + |
| 8 | +/** |
| 9 | + * @file Unit Tests - ValidationException |
| 10 | + * @module exceptions/exceptions/tests/unit/ValidationException |
| 11 | + */ |
| 12 | + |
| 13 | +describe('unit:exceptions/ValidationException', () => { |
| 14 | + type Case = Testcase<Pick<ExceptionJSON, 'code' | 'data' | 'message'>> & { |
| 15 | + do: string |
| 16 | + dto: Partial<ValidationExceptionDTO> |
| 17 | + } |
| 18 | + |
| 19 | + const MESSAGE = `Model validation failure: [title,text]` |
| 20 | + |
| 21 | + const cases: Case[] = [ |
| 22 | + { |
| 23 | + do: 'create ValidationException', |
| 24 | + dto: { options: {} }, |
| 25 | + expected: { |
| 26 | + code: ExceptionCode.UNPROCESSABLE_ENTITY, |
| 27 | + data: { isExceptionJSON: true, options: {} }, |
| 28 | + message: MESSAGE |
| 29 | + } |
| 30 | + }, |
| 31 | + { |
| 32 | + do: 'override default code', |
| 33 | + dto: { code: ExceptionCode.BAD_REQUEST }, |
| 34 | + expected: { |
| 35 | + code: ExceptionCode.BAD_REQUEST, |
| 36 | + data: { isExceptionJSON: true }, |
| 37 | + message: MESSAGE |
| 38 | + } |
| 39 | + }, |
| 40 | + { |
| 41 | + do: 'override default message', |
| 42 | + dto: { id: 42, message: 'user id must be belong to existing user' }, |
| 43 | + expected: { |
| 44 | + code: ExceptionCode.UNPROCESSABLE_ENTITY, |
| 45 | + data: { id: 42, isExceptionJSON: true }, |
| 46 | + message: 'user id must be belong to existing user' |
| 47 | + } |
| 48 | + } |
| 49 | + ] |
| 50 | + |
| 51 | + it.each<Case>(cases)('should $do', testcase => { |
| 52 | + // Arrange |
| 53 | + const { dto, expected } = testcase |
| 54 | + |
| 55 | + // Act |
| 56 | + const result = new TestSubject('Model', { ...dto, errors: ERRORS }).toJSON() |
| 57 | + |
| 58 | + // Expect |
| 59 | + expect(result.code).toBe(expected.code) |
| 60 | + expect(result.data).toStrictEqual(expected.data) |
| 61 | + expect(result.message).toBe(expected.message) |
| 62 | + }) |
| 63 | +}) |
0 commit comments