| 1 | import Ajv, { Ajv as AjvType } from 'ajv'; |
| 2 | |
| 3 | import createAjvInstance, { AJV_CONFIG, COLOR_FORMAT_REGEX, DATA_URL_FORMAT_REGEX } from '../src/createAjvInstance'; |
| 4 | import { CustomValidatorOptionsType } from '../src'; |
| 5 | |
| 6 | jest.mock('ajv'); |
| 7 | |
| 8 | export const CUSTOM_OPTIONS: CustomValidatorOptionsType = { |
| 9 | additionalMetaSchemas: [require('ajv/lib/refs/json-schema-draft-04.json')], |
| 10 | customFormats: { |
| 11 | 'phone-us': /\(?\d{3}\)?[\s-]?\d{3}[\s-]?\d{4}$/, |
| 12 | 'area-code': /\d{3}/, |
| 13 | }, |
| 14 | ajvOptionsOverrides: { |
| 15 | $data: true, |
| 16 | verbose: true, |
| 17 | }, |
| 18 | }; |
| 19 | |
| 20 | describe('createAjvInstance()', () => { |
| 21 | describe('no additional meta schemas, custom formats or ajv options overrides', () => { |
| 22 | let ajv: AjvType; |
| 23 | beforeAll(() => { |
| 24 | ajv = createAjvInstance(); |
| 25 | }); |
| 26 | afterAll(() => { |
| 27 | (Ajv as unknown as jest.Mock).mockClear(); |
| 28 | }); |
| 29 | it('expect a new Ajv to be constructed with the AJV_CONFIG', () => { |
| 30 | expect(Ajv).toHaveBeenCalledWith(AJV_CONFIG); |
| 31 | }); |
| 32 | it('addFormat() was called twice', () => { |
| 33 | expect(ajv.addFormat).toHaveBeenCalledTimes(2); |
| 34 | }); |
| 35 | it('the first addFormat() was for data-url', () => { |
| 36 | expect(ajv.addFormat).toHaveBeenNthCalledWith(1, 'data-url', DATA_URL_FORMAT_REGEX); |
| 37 | }); |
| 38 | it('the second addFormat() was for color', () => { |
| 39 | expect(ajv.addFormat).toHaveBeenNthCalledWith(2, 'color', COLOR_FORMAT_REGEX); |
| 40 | }); |
| 41 | it('addMetaSchema was not called', () => { |
| 42 | expect(ajv.addMetaSchema).not.toHaveBeenCalled(); |
| 43 | }); |
| 44 | }); |
| 45 | describe('has additional meta schemas, custom formats and ajv options overrides', () => { |
| 46 | let ajv: AjvType; |
| 47 | beforeAll(() => { |
| 48 | ajv = createAjvInstance( |
| 49 | CUSTOM_OPTIONS.additionalMetaSchemas, |
| 50 | CUSTOM_OPTIONS.customFormats, |
| 51 | CUSTOM_OPTIONS.ajvOptionsOverrides |
| 52 | ); |
| 53 | }); |
| 54 | afterAll(() => { |
| 55 | (Ajv as unknown as jest.Mock).mockClear(); |
| 56 | }); |
| 57 | it('expect a new Ajv to be constructed with the AJV_CONFIG', () => { |
| 58 | expect(Ajv).toHaveBeenCalledWith({ |
| 59 | ...AJV_CONFIG, |
| 60 | ...CUSTOM_OPTIONS.ajvOptionsOverrides, |
| 61 | }); |
| 62 | }); |
| 63 | it('addFormat() was called twice', () => { |
| 64 | expect(ajv.addFormat).toHaveBeenCalledTimes(4); |
| 65 | }); |
| 66 | it('the first addFormat() was for data-url', () => { |
| 67 | expect(ajv.addFormat).toHaveBeenNthCalledWith(1, 'data-url', DATA_URL_FORMAT_REGEX); |
| 68 | }); |
| 69 | it('the second addFormat() was for color', () => { |
| 70 | expect(ajv.addFormat).toHaveBeenNthCalledWith(2, 'color', COLOR_FORMAT_REGEX); |
| 71 | }); |
| 72 | it('the remaining addFormat() calls were for custom formats', () => { |
| 73 | Object.keys(CUSTOM_OPTIONS.customFormats!).forEach((key: string, i: number) => { |
| 74 | expect(ajv.addFormat).toHaveBeenNthCalledWith(3 + i, key, CUSTOM_OPTIONS.customFormats![key]); |
| 75 | }); |
| 76 | }); |
| 77 | it('addMetaSchema was not called', () => { |
| 78 | expect(ajv.addMetaSchema).toHaveBeenCalledWith(CUSTOM_OPTIONS.additionalMetaSchemas); |
| 79 | }); |
| 80 | }); |
| 81 | }); |