import { schemaFactory } from '~/data/tests/factories/schema'; import { expectResult, getMakeSut, getAnyObject } from './helpers'; describe('Integration - Object Tests', () => { const makeSut = getMakeSut('object'); const doTest = (value: unknown, valid: boolean) => { const { sut } = makeSut(); expectResult(sut.validate(value)).toBe(valid); }; describe('should allow only object or null or undefined', () => { const objectCases = getAnyObject(); objectCases.forEach(testCase => { const name = testCase ? JSON.stringify(testCase) : String(testCase); test(`should allow [ ${name} ]`, () => { doTest(testCase, true); }); }); const nonObjectCases = [[0], [1], ['array', []]]; nonObjectCases.forEach(testCase => { const [name, value] = testCase.length === 1 ? [String(testCase), testCase] : testCase; test(`should not allow [ ${name} ]`, () => { doTest(value, false); }); }); }); describe('should support nested schemas', () => { const { sut } = getMakeSut('object')({ required: true, shape: { value: schemaFactory.spread('number', { min: 5, max: 10, }), id: schemaFactory.spread('string', { required: true, }), }, }); describe('should give error', () => { test('if input does not contain id', () => { expectResult(sut.validate({})).toBeInvalid(); }); test('if input.value < 5', () => { expectResult(sut.validate({ value: 2, id: '' })).toBeInvalid(); }); test('if input.value > 10', () => { expectResult(sut.validate({ value: 12, id: '' })).toBeInvalid(); }); test('if input.value is a string', () => { expectResult(sut.validate({ value: 'batata', id: '' })).toBeInvalid(); }); test('if input.id is a number', () => { expectResult(sut.validate({ id: 24 })).toBeInvalid(); }); }); describe('should not give error', () => { test('if input.id is valid', () => { expectResult(sut.validate({ value: 7, id: 'valid_id' })).toBeValid(); }); test('if input.value and input.id are both valid', () => { expectResult(sut.validate({ value: 7, id: 'valid_id' })).toBeValid(); }); }); }); });