diff options
Diffstat (limited to 'spec/frontend/lib/utils/forms_spec.js')
-rw-r--r-- | spec/frontend/lib/utils/forms_spec.js | 111 |
1 files changed, 110 insertions, 1 deletions
diff --git a/spec/frontend/lib/utils/forms_spec.js b/spec/frontend/lib/utils/forms_spec.js index 2f71b26b29a..b97f5bf3c51 100644 --- a/spec/frontend/lib/utils/forms_spec.js +++ b/spec/frontend/lib/utils/forms_spec.js @@ -1,7 +1,12 @@ import { serializeForm, serializeFormObject, + safeTrim, isEmptyValue, + hasMinimumLength, + isParseableAsInteger, + isIntegerGreaterThan, + isEmail, parseRailsFormFields, } from '~/lib/utils/forms'; @@ -99,6 +104,22 @@ describe('lib/utils/forms', () => { }); }); + describe('safeTrim', () => { + it.each` + input | returnValue + ${''} | ${''} + ${[]} | ${[]} + ${null} | ${null} + ${undefined} | ${undefined} + ${' '} | ${''} + ${'hello '} | ${'hello'} + ${'hello'} | ${'hello'} + ${0} | ${0} + `('returns $returnValue for value $input', ({ input, returnValue }) => { + expect(safeTrim(input)).toEqual(returnValue); + }); + }); + describe('isEmptyValue', () => { it.each` input | returnValue @@ -106,14 +127,102 @@ describe('lib/utils/forms', () => { ${[]} | ${true} ${null} | ${true} ${undefined} | ${true} + ${' '} | ${true} ${'hello'} | ${false} - ${' '} | ${false} ${0} | ${false} `('returns $returnValue for value $input', ({ input, returnValue }) => { expect(isEmptyValue(input)).toBe(returnValue); }); }); + describe('hasMinimumLength', () => { + it.each` + input | minLength | returnValue + ${['o', 't']} | ${1} | ${true} + ${'hello'} | ${3} | ${true} + ${' '} | ${2} | ${false} + ${''} | ${0} | ${false} + ${''} | ${8} | ${false} + ${[]} | ${0} | ${false} + ${null} | ${8} | ${false} + ${undefined} | ${8} | ${false} + ${'hello'} | ${8} | ${false} + ${0} | ${8} | ${false} + ${4} | ${1} | ${false} + `( + 'returns $returnValue for value $input and minLength $minLength', + ({ input, minLength, returnValue }) => { + expect(hasMinimumLength(input, minLength)).toBe(returnValue); + }, + ); + }); + + describe('isPareseableInteger', () => { + it.each` + input | returnValue + ${'0'} | ${true} + ${'12'} | ${true} + ${''} | ${false} + ${[]} | ${false} + ${null} | ${false} + ${undefined} | ${false} + ${'hello'} | ${false} + ${' '} | ${false} + ${'12.4'} | ${false} + ${'12ef'} | ${false} + `('returns $returnValue for value $input', ({ input, returnValue }) => { + expect(isParseableAsInteger(input)).toBe(returnValue); + }); + }); + + describe('isIntegerGreaterThan', () => { + it.each` + input | greaterThan | returnValue + ${25} | ${8} | ${true} + ${'25'} | ${8} | ${true} + ${'4'} | ${1} | ${true} + ${'4'} | ${8} | ${false} + ${'9.5'} | ${8} | ${false} + ${'9.5e'} | ${8} | ${false} + ${['o', 't']} | ${0} | ${false} + ${'hello'} | ${0} | ${false} + ${' '} | ${0} | ${false} + ${''} | ${0} | ${false} + ${''} | ${8} | ${false} + ${[]} | ${0} | ${false} + ${null} | ${0} | ${false} + ${undefined} | ${0} | ${false} + ${'hello'} | ${0} | ${false} + ${0} | ${0} | ${false} + `( + 'returns $returnValue for value $input and greaterThan $greaterThan', + ({ input, greaterThan, returnValue }) => { + expect(isIntegerGreaterThan(input, greaterThan)).toBe(returnValue); + }, + ); + }); + + describe('isEmail', () => { + it.each` + input | returnValue + ${'user-with_special-chars@example.com'} | ${true} + ${'user@subdomain.example.com'} | ${true} + ${'user@example.com'} | ${true} + ${'user@example.co'} | ${true} + ${'user@example.c'} | ${false} + ${'user@example'} | ${false} + ${''} | ${false} + ${[]} | ${false} + ${null} | ${false} + ${undefined} | ${false} + ${'hello'} | ${false} + ${' '} | ${false} + ${'12'} | ${false} + `('returns $returnValue for value $input', ({ input, returnValue }) => { + expect(isEmail(input)).toBe(returnValue); + }); + }); + describe('serializeFormObject', () => { it('returns an serialized object', () => { const form = { |