From d245e80b2a4579a1fe164d564ceda44e211f3b21 Mon Sep 17 00:00:00 2001 From: Viicos <65306057+Viicos@users.noreply.github.com> Date: Mon, 20 Nov 2023 16:32:07 +0100 Subject: [PATCH] [open-formulieren/open-forms#3611] Refactor tests to be parametrized --- src/jstests/formio/components/time.spec.js | 285 ++++++++------------- 1 file changed, 111 insertions(+), 174 deletions(-) diff --git a/src/jstests/formio/components/time.spec.js b/src/jstests/formio/components/time.spec.js index 02101c97d..43be61f24 100644 --- a/src/jstests/formio/components/time.spec.js +++ b/src/jstests/formio/components/time.spec.js @@ -1,5 +1,4 @@ import _ from 'lodash'; -import React from 'react'; import {Formio} from 'react-formio'; import OpenFormsModule from 'formio/module'; @@ -10,195 +9,133 @@ import {timeForm} from './fixtures/time'; Formio.use(OpenFormsModule); describe('Time Component', () => { - test('Time component with min/max time validation', done => { + it.each([ + ['09:00:00', true], + ['10:30:00', true], + ['11:11:11', true], + ['17:00:00', true], + ['17:30:00', false], + ['08:30:00', false], + ])('Time component with min/max time validation', async (value, valid) => { let formJSON = _.cloneDeep(timeForm); formJSON.components[0].validate.minTime = '09:00:00'; formJSON.components[0].validate.maxTime = '17:00:00'; - const validValues = ['09:00:00', '10:30:00', '11:11:11', '17:00:00']; - - const invalidValues = ['17:30:00', '08:30:00']; - - const testValidity = (values, valid) => { - values.forEach(value => { - const element = document.createElement('div'); - - Formio.createForm(element, formJSON) - .then(form => { - form.setPristine(false); - const component = form.getComponent('time'); - const changed = component.setValue(value); - expect(changed).toBeTruthy(); - - setTimeout(() => { - if (valid) { - expect(!!component.error).toBeFalsy(); - } else { - expect(!!component.error).toBeTruthy(); - expect(component.error.message).toEqual('invalid_time'); - } - - if (value === invalidValues[2]) { - done(); - } - }, 300); - }) - .catch(done); - }); - }; - - testValidity(validValues, true); - testValidity(invalidValues, false); - }); - - test('Time component without min/max time validation', done => { - let formJSON = _.cloneDeep(timeForm); - - const validValues = ['00:00:00', '23:59:59', '11:11:11']; - - const testValidity = values => { - values.forEach(value => { - const element = document.createElement('div'); - - Formio.createForm(element, formJSON) - .then(form => { - form.setPristine(false); - const component = form.getComponent('time'); - const changed = component.setValue(value); - expect(changed).toBeTruthy(); - - setTimeout(() => { - expect(!!component.error).toBeFalsy(); - - if (value === validValues[2]) { - done(); - } - }, 300); - }) - .catch(done); - }); - }; - - testValidity(validValues); + const element = document.createElement('div'); + const form = await Formio.createForm(element, formJSON); + form.setPristine(false); + + const component = form.getComponent('time'); + const changed = component.setValue(value); + expect(changed).toBeTruthy(); + setTimeout(() => { + if (valid) { + expect(!!component.error).toBeFalsy(); + } else { + expect(!!component.error).toBeTruthy(); + expect(component.error.message).toEqual('invalid_time'); + } + }, 300); }); - test('Time component with only min time validation', done => { + it.each(['00:00:00', '23:59:59', '11:11:11'])( + 'Time component without min/max time validation', + async value => { + let formJSON = _.cloneDeep(timeForm); + + const element = document.createElement('div'); + const form = await Formio.createForm(element, formJSON); + form.setPristine(false); + + const component = form.getComponent('time'); + const changed = component.setValue(value); + + expect(changed).toBeTruthy(); + setTimeout(() => { + expect(!!component.error).toBeFalsy(); + }, 300); + } + ); + + it.each([ + ['17:00:00', true], + ['08:00:00', false], + ])('Time component with only min time validation', async (value, valid) => { let formJSON = _.cloneDeep(timeForm); formJSON.components[0].validate.minTime = '09:00:00'; - const validValues = ['17:00:00']; - - const invalidValues = ['08:00:00']; - - const testValidity = (values, valid) => { - values.forEach(value => { - const element = document.createElement('div'); - - Formio.createForm(element, formJSON) - .then(form => { - form.setPristine(false); - const component = form.getComponent('time'); - const changed = component.setValue(value); - expect(changed).toBeTruthy(); - - setTimeout(() => { - if (valid) { - expect(!!component.error).toBeFalsy(); - } else { - expect(!!component.error).toBeTruthy(); - } - - if (value === invalidValues[0]) { - done(); - } - }, 300); - }) - .catch(done); - }); - }; - - testValidity(validValues, true); - testValidity(invalidValues, false); + const element = document.createElement('div'); + const form = await Formio.createForm(element, formJSON); + form.setPristine(false); + + const component = form.getComponent('time'); + const changed = component.setValue(value); + expect(changed).toBeTruthy(); + setTimeout(() => { + if (valid) { + expect(!!component.error).toBeFalsy(); + } else { + expect(!!component.error).toBeTruthy(); + expect(component.error.message).toEqual('invalid_time'); + } + }, 300); }); - test('Time component with only max time validation', done => { + it.each([ + ['08:00:00', true], + ['09:00:00', true], + ['17:00:00', false], + ])('Time component with only max time validation', async (value, valid) => { let formJSON = _.cloneDeep(timeForm); formJSON.components[0].validate.maxTime = '09:00:00'; - const validValues = ['08:00:00', '09:00:00']; - - const invalidValues = ['17:00:00']; - - const testValidity = (values, valid) => { - values.forEach(value => { - const element = document.createElement('div'); - - Formio.createForm(element, formJSON) - .then(form => { - form.setPristine(false); - const component = form.getComponent('time'); - const changed = component.setValue(value); - expect(changed).toBeTruthy(); - - setTimeout(() => { - if (valid) { - expect(!!component.error).toBeFalsy(); - } else { - expect(!!component.error).toBeTruthy(); - } - - if (value === invalidValues[0]) { - done(); - } - }, 300); - }) - .catch(done); - }); - }; - - testValidity(validValues, true); - testValidity(invalidValues, false); + const element = document.createElement('div'); + const form = await Formio.createForm(element, formJSON); + form.setPristine(false); + + const component = form.getComponent('time'); + const changed = component.setValue(value); + expect(changed).toBeTruthy(); + setTimeout(() => { + if (valid) { + expect(!!component.error).toBeFalsy(); + } else { + expect(!!component.error).toBeTruthy(); + expect(component.error.message).toEqual('invalid_time'); + } + }, 300); }); - test('Time component with min time boundary larger than max time boundary', done => { - let formJSON = _.cloneDeep(timeForm); - formJSON.components[0].validate.maxTime = '01:00:00'; - formJSON.components[0].validate.minTime = '08:00:00'; - - const validValues = ['09:00:00', '00:30:00', '01:00:00', '08:00:00']; - - const invalidValues = ['02:00:00']; - - const testValidity = (values, valid) => { - values.forEach(value => { - const element = document.createElement('div'); - - Formio.createForm(element, formJSON) - .then(form => { - form.setPristine(false); - const component = form.getComponent('time'); - const changed = component.setValue(value); - expect(changed).toBeTruthy(); - - setTimeout(() => { - if (valid) { - expect(!!component.error).toBeFalsy(); - } else { - expect(!!component.error).toBeTruthy(); - } - - if (value === invalidValues[0]) { - done(); - } - }, 300); - }) - .catch(done); - }); - }; - - testValidity(validValues, true); - testValidity(invalidValues, false); - }); + it.each([ + ['09:00:00', true], + ['00:30:00', true], + ['01:00:00', true], + ['08:00:00', true], + ['02:00:00', false], + ])( + 'Time component with min time boundary larger than max time boundary', + async (value, valid) => { + let formJSON = _.cloneDeep(timeForm); + formJSON.components[0].validate.maxTime = '01:00:00'; + formJSON.components[0].validate.minTime = '08:00:00'; + + const element = document.createElement('div'); + const form = await Formio.createForm(element, formJSON); + form.setPristine(false); + + const component = form.getComponent('time'); + const changed = component.setValue(value); + expect(changed).toBeTruthy(); + setTimeout(() => { + if (valid) { + expect(!!component.error).toBeFalsy(); + } else { + expect(!!component.error).toBeTruthy(); + expect(component.error.message).toEqual('invalid_time'); + } + }, 300); + } + ); test('Time component with both min/max and max > min validation and custom error', done => { let formJSON = _.cloneDeep(timeForm);