Skip to content

Commit

Permalink
Adding more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
travist committed Dec 3, 2024
1 parent e8a58b3 commit 9986a3c
Show file tree
Hide file tree
Showing 3 changed files with 254 additions and 0 deletions.
2 changes: 2 additions & 0 deletions test/forms/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -37,5 +38,6 @@ export default [
ClearOnHide,
WizardWithPrefixComps,
WizardWithCheckboxes,
NestedFormWithConditionals,
...ComponentsBasicSettingsTests,
];
166 changes: 166 additions & 0 deletions test/forms/nestedFormWithConditionals.json
Original file line number Diff line number Diff line change
@@ -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"
}
86 changes: 86 additions & 0 deletions test/unit/Webform.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit 9986a3c

Please sign in to comment.