From 9986a3ca2cc3a13e818aeb1cdf30832d95531e4c Mon Sep 17 00:00:00 2001 From: Travis Tidwell Date: Tue, 3 Dec 2024 16:43:31 -0600 Subject: [PATCH] Adding more tests. --- test/forms/index.js | 2 + test/forms/nestedFormWithConditionals.json | 166 +++++++++++++++++++++ test/unit/Webform.unit.js | 86 +++++++++++ 3 files changed, 254 insertions(+) create mode 100644 test/forms/nestedFormWithConditionals.json diff --git a/test/forms/index.js b/test/forms/index.js index 65a82412da..192d658686 100644 --- a/test/forms/index.js +++ b/test/forms/index.js @@ -17,6 +17,7 @@ import ComponentsBasicSettingsTests from './componentsBasicSettingsTests'; import NestedFormValidation from './nested-form-validation'; import WizardWithPrefixComps from './wizardWithPrefixComps'; import WizardWithCheckboxes from './wizardWithCheckboxes'; +import NestedFormWithConditionals from './nestedFormWithConditionals.json'; export default [ Simple, @@ -37,5 +38,6 @@ export default [ ClearOnHide, WizardWithPrefixComps, WizardWithCheckboxes, + NestedFormWithConditionals, ...ComponentsBasicSettingsTests, ]; diff --git a/test/forms/nestedFormWithConditionals.json b/test/forms/nestedFormWithConditionals.json new file mode 100644 index 0000000000..54fde8e24e --- /dev/null +++ b/test/forms/nestedFormWithConditionals.json @@ -0,0 +1,166 @@ +{ + "_id": "674f851fcce87ee02734388a", + "title": "parent", + "name": "parent", + "path": "parent", + "type": "form", + "display": "form", + "tags": [], + "access": [], + "submissionAccess": [ + { + "type": "create_own", + "roles": [] + }, + { + "type": "create_all", + "roles": [] + }, + { + "type": "read_own", + "roles": [] + }, + { + "type": "read_all", + "roles": [] + }, + { + "type": "update_own", + "roles": [] + }, + { + "type": "update_all", + "roles": [] + }, + { + "type": "delete_own", + "roles": [] + }, + { + "type": "delete_all", + "roles": [] + }, + { + "type": "team_read", + "roles": [] + }, + { + "type": "team_write", + "roles": [] + }, + { + "type": "team_admin", + "roles": [] + } + ], + "owner": null, + "components": [ + { + "label": "Number", + "applyMaskOn": "change", + "mask": false, + "tableView": false, + "delimiter": false, + "requireDecimal": false, + "inputFormat": "plain", + "truncateMultipleSpaces": false, + "validateWhenHidden": false, + "key": "number", + "type": "number", + "input": true + }, + { + "label": "Text Area", + "applyMaskOn": "change", + "autoExpand": false, + "tableView": true, + "validateWhenHidden": false, + "key": "textArea", + "conditional": { + "show": true, + "conjunction": "all", + "conditions": [ + { + "component": "number", + "operator": "isEqual", + "value": 5 + } + ] + }, + "type": "textarea", + "input": true + }, + { + "label": "Form", + "tableView": true, + "form": "674f851fcce87ee027343883", + "useOriginalRevision": false, + "key": "form", + "type": "form", + "input": true, + "components": [ + { + "label": "Number", + "applyMaskOn": "change", + "mask": false, + "tableView": false, + "delimiter": false, + "requireDecimal": false, + "inputFormat": "plain", + "truncateMultipleSpaces": false, + "validateWhenHidden": false, + "key": "number", + "type": "number", + "input": true + }, + { + "label": "Text Field", + "applyMaskOn": "change", + "tableView": true, + "validateWhenHidden": false, + "key": "textField", + "conditional": { + "show": true, + "conjunction": "all", + "conditions": [ + { + "component": "number", + "operator": "isEqual", + "value": 5 + } + ] + }, + "type": "textfield", + "input": true + }, + { + "type": "button", + "label": "Submit", + "key": "submit", + "disableOnInvalid": true, + "input": true, + "tableView": false + } + ] + }, + { + "type": "button", + "label": "Submit", + "key": "submit", + "disableOnInvalid": true, + "input": true, + "tableView": false + } + ], + "pdfComponents": [], + "settings": {}, + "properties": {}, + "machineName": "authoring-fbjzvjaybjhpflj:parent", + "project": "674f8511cce87ee0273436ea", + "controller": "", + "revisions": "", + "submissionRevisions": "", + "_vid": 0, + "created": "2024-12-03T22:24:31.250Z", + "modified": "2024-12-03T22:24:31.255Z" +} \ No newline at end of file diff --git a/test/unit/Webform.unit.js b/test/unit/Webform.unit.js index 04e0bb4e04..7d51605d1f 100644 --- a/test/unit/Webform.unit.js +++ b/test/unit/Webform.unit.js @@ -38,6 +38,7 @@ import { formWithObjectValueSelect, } from '../formtest/index.js'; import UpdateErrorClassesWidgets from '../forms/updateErrorClasses-widgets.js'; +import NestedFormWithConditionals from '../forms/nestedFormWithConditionals.json'; import nestedModalWizard from '../forms/nestedModalWizard'; import disableSubmitButton from '../forms/disableSubmitButton'; import formWithAddressComponent from '../forms/formWithAddressComponent.js'; @@ -170,6 +171,58 @@ describe('Webform tests', function() { }).catch((err) => done(err)); }); + it('Should trigger the correct conditional when a nested form uses the same key as a parent form.', done => { + Formio.createForm(document.createElement('div'), NestedFormWithConditionals).then(form => { + const textArea = form.getComponent('textArea'); + const textField = form.getComponent('textField'); + assert.equal(textArea.visible, false); + assert.equal(textField.visible, false); + form.submission = { + data: { + number: 3, + form: { + data: { + number: 5 + } + } + } + }; + setTimeout(() => { + assert.equal(textArea.visible, false); + assert.equal(textField.visible, true); + form.submission = { + data: { + number: 5, + form: { + data: { + number: 3 + } + } + } + }; + setTimeout(() => { + assert.equal(textArea.visible, true); + assert.equal(textField.visible, false); + form.submission = { + data: { + number: 5, + form: { + data: { + number: 5 + } + } + } + }; + setTimeout(() => { + assert.equal(textArea.visible, true); + assert.equal(textField.visible, true); + done(); + }, 300); + }, 300); + }, 300); + }); + }); + it('Should validate email input when it is simple conditionally visible', done => { const formElement = document.createElement('div'); Formio.createForm(formElement, formWithConditionalEmail) @@ -347,6 +400,39 @@ describe('Webform tests', function() { .catch(done); }); + it('Should set a conditional nested form values.', function(done) { + const formElement = document.createElement('div'); + Formio.createForm(formElement, formWithDeeplyNestedConditionalComps, { server: true }).then((form) => { + const submission = { + data: { + submit: false, + radio1: 'yes', + container: { + checkbox: true, + checkboxInPanelInHiddenContainer: true, + textField: 'test', + editGrid: [ + { + number: 1, + textField: 'test2', + }, + { + number: 2, + }, + ], + }, + }, + }; + + form.setValue(fastCloneDeep(submission), { sanitize: true }); + setTimeout(() => { + assert.deepEqual(form.data, submission.data); + assert.deepEqual(form.getValue(), submission); + done(); + }, 500); + }).catch((err) => done(err)); + }); + it('Should not lose values of conditionally visible components on setValue when server option is passed', function(done) { const formElement = document.createElement('div'); Formio.createForm(formElement, formWithDeeplyNestedConditionalComps, { server: true }).then((form) => {