import { themis } from '~/main'; import { expectResult, makeSut, getAnyArray } from './helpers'; describe('Integration - Array Tests', () => { const doTest = (value: unknown, valid: boolean) => { const { sut } = makeSut({ required: false }); expectResult(sut.validate(value)).toBe(valid); }; describe('should allow only array or null or undefined', () => { const arrayCases = getAnyArray(); arrayCases.forEach(testCase => { const name = testCase ? JSON.stringify(testCase) : String(testCase); test(`should allow [ ${name} ]`, () => { doTest(testCase, true); }); }); test('should not allow numbers', () => { const { sut } = makeSut({ required: false }); expect(sut.validate(1)).not.toBeValidResult(); }); test('should not allow strings', () => { const { sut } = makeSut({ required: false }); expect(sut.validate('batata')).not.toBeValidResult(); }); test('should not allow non-array objects', () => { const { sut } = makeSut({ required: false }); expect(sut.validate({})).not.toBeValidResult(); }); }); describe('should support nested schemas', () => { const { sut } = makeSut({ required: true, items: themis.number({ min: 5, max: 10 }), }); describe('should give error', () => { test('if input[n] is outside valid outside range', () => { expect(sut.validate([12])).not.toBeValidResult(); }); }); describe('should not give error', () => { test('if input[n] is inside valid range', () => { expect(sut.validate([7])).toBeValidResult(); }); }); }); });