import Harness from '../harness'; import assert from 'power-assert'; import TimeComponent from '../../src/components/time/Time'; import { comp1, comp2, comp3, comp4, timeForm2, timeForm } from './fixtures/time/index'; import Webform from '../../src/Webform'; import { Formio } from '../../src/Formio'; describe('Time Component', function () { it('Should build a time component', function () { return Harness.testCreate(TimeComponent, comp1); }); it('Should format value on blur', function (done) { const formElement = document.createElement('div'); const form = new Webform(formElement); form .setForm(timeForm) .then(() => { const component = form.components[0]; const inputEvent = new Event('input', { bubbles: true, cancelable: true }); const blurEvent = new Event('blur'); const timeInput = component.element.querySelector('input[name="data[time]"]'); timeInput.value = '10:0_ __'; timeInput.dispatchEvent(inputEvent); setTimeout(() => { assert.equal(timeInput.value, '10:0_ __'); assert.equal(component.dataValue, '10:00:00'); timeInput.dispatchEvent(blurEvent); setTimeout(() => { assert.equal(timeInput.value, '10:00 AM'); done(); }, 500); }, 250); }) .catch(done); }); it('Should not show error if value corresponds to the mask', function (done) { Harness.testCreate(TimeComponent, comp2).then((component) => { const inputEvent = new Event('input', { bubbles: true, cancelable: true }); const timeInput = component.element.querySelector('input[name="data[time]"]'); timeInput.value = '12:0_'; timeInput.dispatchEvent(inputEvent); setTimeout(() => { component.setValue('12:00'); setTimeout(() => { assert.equal(component.errors.length, 0); done(); }, 700); }, 500); }); }); it('Should be invalid if time is not real', function (done) { const formElement = document.createElement('div'); const form = new Webform(formElement); form .setForm(timeForm2) .then(() => { const component = form.components[0]; Harness.setInputValue(component, 'data[time]', '89:19'); setTimeout(() => { assert.equal(component.errors[0].message, 'Invalid time', 'Should have an error'); done(); }, 650); }) .catch(done); }); it('Should return error if data in multiple time component is not valid', function (done) { const formElement = document.createElement('div'); const form = new Webform(formElement); form .setForm(timeForm2) .then(() => { const component = form.components[1]; Harness.setInputValue(component, 'data[multipleTime]', ['89:19']); setTimeout(() => { assert.equal(component.errors[0].message, 'Invalid time', 'Should have an error'); done(); }, 650); }) .catch(done); }); it('Should not return error if data in multiple time component is not valid', function (done) { const formElement = document.createElement('div'); const form = new Webform(formElement); form .setForm(timeForm2) .then(() => { const component = form.components[1]; Harness.setInputValue(component, 'data[multipleTime]', ['10:00:00']); setTimeout(() => { assert.equal(component.errors.length, 0, 'Should not have an error'); done(); }, 650); }) .catch(done); }); it('Should build a time component 2', function (done) { Harness.testCreate(TimeComponent, comp3) .then((time) => { assert.deepEqual( time.dataValue, ['10:00:00', '11:00:00'], 'Should be set to default value', ); done(); }) .catch(done); }); // FIO-7184: the Data tab's table renders its cells through getValueAsString, which used // to format only the first value of a multiple-value Time component. it('Should format every value of a multiple-value component', function () { return Harness.testCreate(TimeComponent, comp3).then((time) => { assert.equal(time.getValueAsString(['10:00:00', '11:30:00']), '10:00, 11:30'); }); }); it('Should not display error when embedded in a wizard form', function () { return Formio.createForm(document.createElement('div'), comp4, {}).then((form) => { const timeComponent = form.getComponent('time1'); assert.equal(timeComponent._errors.length, 0); assert.equal(timeComponent.rawData, '03:03'); }); }); });