import { ValidationSchemas } from '~/domain'; import { JoiSchema } from '~/infra/joi/helper/joi-types'; import { RulesParser } from '~/infra/joi/protocols'; import { applyRules } from '~/infra/joi/validators/use-cases/joi-common-validator/get-joi-common-schema/apply-rules'; import { Jester } from '~/tests/helpers/jest-types'; const mockRulesParser = (): Jester.Mock.Stub> => { return new Proxy( {}, { get: (target, key) => { if (key === 'batata') return undefined; if (!target[key]) { target[key] = jest.fn(schema => schema); } return target[key]; }, } ); }; const makeSut = (type: T) => { const mocks = { rulesParser: mockRulesParser(), }; const sut = (rules: ValidationSchemas.Rules): JoiSchema => { return applyRules(type, 'joi-schema' as any, rules, mocks.rulesParser as any); }; return { sut, mocks }; }; describe('ApplyRules Test', () => { test('should throw on unknown rule', () => { const { sut } = makeSut('string'); expect(() => sut({ length: 2, format: 'uuid', batata: 2, } as any) ).toThrow(); }); test('should call rulesParser with proper parameters', () => { const { sut, mocks } = makeSut('string'); const rules = { length: 2, max: 5, format: 'uuid' as const, }; sut(rules); expect(mocks.rulesParser.length).toHaveBeenCalledWith('joi-schema', 2, rules); expect(mocks.rulesParser.max).toHaveBeenCalledWith('joi-schema', 5, rules); expect(mocks.rulesParser.format).toHaveBeenCalledWith('joi-schema', 'uuid', rules); }); test('should parse only the keys in rules', () => { const { sut, mocks } = makeSut('string'); sut({ length: 2, max: 2, format: 'uuid', }); expect(mocks.rulesParser.length).toHaveBeenCalled(); expect(mocks.rulesParser.max).toHaveBeenCalled(); expect(mocks.rulesParser.format).toHaveBeenCalled(); expect(mocks.rulesParser.match).not.toHaveBeenCalled(); expect(mocks.rulesParser.min).not.toHaveBeenCalled(); expect(mocks.rulesParser.required).not.toHaveBeenCalled(); }); });