import { ValidationSchemas, schemaTypes } from '~/domain'; import { JoiInitNestedSchema } from '~/infra/joi/helper/parse-nested-structure/init-nested-schema'; import { IGetSchema } from '~/infra/joi/protocols'; const makeSchemaGetterStub = () => ({ getSchema: jest.fn(), }); type GetSchemaMock = { getSchema: jest.Mock, Parameters> }; const makeSut = (dontRunSetup?: boolean) => { const stubs: Record = { number: makeSchemaGetterStub(), string: makeSchemaGetterStub(), object: makeSchemaGetterStub(), array: makeSchemaGetterStub(), }; const sut = new JoiInitNestedSchema(); if (!dontRunSetup) { sut.setup(stubs); } return { sut, stubs }; }; describe('JoiInitNestedSchema Tests', () => { schemaTypes.forEach(type => { describe(type, () => { test(`should call get${type}Schema`, () => { const { sut, stubs } = makeSut(); const getSchemaSpy = jest.spyOn(stubs[type], 'getSchema'); const rules: any = { any_key: 'any_value' }; sut.getJoiSchema(type, rules); expect(getSchemaSpy).toHaveBeenCalled(); }); test(`should return the output of get${type}Schema`, () => { const { sut, stubs } = makeSut(); const getSchemaSpy = jest.spyOn(stubs[type], 'getSchema'); getSchemaSpy.mockReturnValueOnce('valid-schema' as any); const schema = sut.getJoiSchema(type, {}); expect(schema).toBe('valid-schema'); }); }); }); });