diff --git a/tests/domains.test.js b/tests/domains.test.js index f1d123695..a55b9b04f 100644 --- a/tests/domains.test.js +++ b/tests/domains.test.js @@ -11,29 +11,41 @@ describe('Domains', () => { }); describe('validateDomainData', () => { - it('should return true if the name is invalid', () => { - const names = ['hello world', 'good12312++123', 'ajsdjasdaSD_123yuqehq', 'khsda%', '', undefined, '12112**dsd', Array(101).fill('a').join('')]; - - names.forEach(name => { - const { valid, errors } = validateDomainData({ + const invalidCases = [ + {}, + { forceHttps: false }, + { forceHttps: 1 }, + { name: 'helo' }, + { record: { CNAME: ['sd'] } }, + { name: 'wwow', record: { A: ['12312'] } }, + ...['', ' ', undefined, 'hello world', 'good12312++123', 'ajsdjasdaSD_123yuqehq', 'khsda%', '12112**dsd', Array(101).fill('a').join('')] + .map(name => ({ name, forceHttps: true, record: { CNAME: ['hello.com'] }, - }); + })), + ]; + + const validCases = [ + { name: 'asas', forceHttps: false, record: { A: ['111.111.111.111'] } }, + ...['hello', 'hello-world', '11111111111', '--wow--', 'wow--', '--wow'].map(name => ({ + name, + forceHttps: true, + record: { CNAME: ['hello.com'] }, + })) + ]; + + it('should return false for invalid data', () => { + invalidCases.forEach(data => { + const { valid, errors } = validateDomainData(data); expect(valid).toBe(false); - expect(errors.length).toBe(1); - expect(errors[0][0]).toBe('name'); + expect(errors.length).toBeGreaterThan(0); }); }); - it('should return true for a valid object', () => { - const names = ['hello', 'hello-world', '11111111111', '--wow--', 'wow--', '--wow']; - names.forEach(name => { - const { valid, errors } = validateDomainData({ - name, - forceHttps: true, - record: { CNAME: ['hello.com'] }, - }); + it('should return true if the name is valid', () => { + validCases.forEach(data => { + const { valid, errors } = validateDomainData(data); expect(valid).toBe(true); expect(errors).toEqual([]); }); @@ -44,7 +56,6 @@ describe('Domains', () => { it('should have a the correct keys', async () => { const list = await getDomains(); list.forEach(data => { - console.log(data); const { errors } = validateDomainData(data); if (errors.length) { console.log(errors); diff --git a/utils/domain.js b/utils/domain.js index d3b5e0a78..85c2de5a3 100644 --- a/utils/domain.js +++ b/utils/domain.js @@ -32,25 +32,25 @@ const validateDomainData = validate({ ]), }, description: { - reason: 'Description has to be shorter than 100 characters', + reason: '`description` has to be shorter than 100 characters', fn: R.anyPass([ R.empty, hasLengthLessThan(100), ]), }, forceHttps: { - reason: 'forceHttp is required to be true or false', + reason: '`forceHttp` is required to be true or false', fn: R.is(Boolean), }, record: { reason: 'Invalid record', fn: R.allPass([ R.is(Object), + R.compose(hasLengthLessThan(1), R.keys), R.anyPass([ R.propSatisfies(R.is(Array), 'CNAME'), R.propSatisfies(R.is(Array), 'A'), ]), - R.compose(hasLengthLessThan(1), R.keys), ]), }, });