import { isThemisSchema } from '~/data/helpers/is-themis-schema'; import { MakeSchemaMakeSut } from '~/data/use-cases/make-schemas-adapter/tests/helpers/make-sut'; import { ValidationSchemas } from '~/domain'; export const makeSchemasAdapterCommonTests = (makeSut: MakeSchemaMakeSut) => { test(`should define the schema's type`, () => { const { sut, type } = makeSut(); const schema = sut.make({}); expect(schema.type).toBe(type); }); test(`should define the schema's rules with default applied`, () => { const { sut, stubs } = makeSut(); const prevRules: any = { batata: 'potato' }; const rules = { ...prevRules, required: true }; jest.spyOn(stubs.applyDefaultRules, 'apply').mockReturnValueOnce(rules); const schema = sut.make({}); expect(schema.rules).toStrictEqual(rules); }); test('isThemisSchema should return true', () => { const { sut } = makeSut(); const rules = { required: true }; const schema = sut.make(rules); expect(isThemisSchema(schema)).toBe(true); }); test('should call applyDefaultRules.apply with correct parameters', () => { const { sut, stubs } = makeSut(); const rules = {}; const applyDefaultSpy = jest.spyOn(stubs.applyDefaultRules, 'apply'); sut.make(rules); expect(applyDefaultSpy).toHaveBeenCalledWith(rules); }); test('schema.validate should call validation.validate with correct parameters', () => { const { sut, stubs, type } = makeSut(); const input: unknown = 'any_input'; const rules = { required: true }; const validateSpy = jest.spyOn(stubs.schemaValidate, 'validate'); sut.make(rules).validate(input); expect(validateSpy).toHaveBeenCalledWith( expect.objectContaining({ type, rules: rules, }), input ); }); test('schema.validate should call parseValidationError.parse with correctly', () => { const { sut, stubs } = makeSut(); const input: unknown = 'any_input'; jest.spyOn(stubs.schemaValidate, 'validate').mockReturnValueOnce('any_validation_result' as any); const parseErrorSpy = jest.spyOn(stubs.parseValidationError, 'parse'); parseErrorSpy.mockReturnValueOnce('valid_error' as any); const schema = sut.make({}); expect(schema.validate(input)).toBe('valid_error'); expect(parseErrorSpy).toHaveBeenCalledWith('any_validation_result'); }); };