diff --git a/src/components/form/Form.js b/src/components/form/Form.js index 0141cb6c6e..04cab1613d 100644 --- a/src/components/form/Form.js +++ b/src/components/form/Form.js @@ -230,6 +230,9 @@ export default class FormComponent extends Component { if (this.options.saveDraftThrottle) { options.saveDraftThrottle = this.options.saveDraftThrottle; } + if (this.options.skipDraftRestore) { + options.skipDraftRestore = this.options.skipDraftRestore; + } return options; } /* eslint-enable max-statements */ diff --git a/src/components/form/Form.unit.js b/src/components/form/Form.unit.js index 9d2d8c5474..d03ee21827 100644 --- a/src/components/form/Form.unit.js +++ b/src/components/form/Form.unit.js @@ -1,3 +1,5 @@ +import _ from 'lodash'; +import { fastCloneDeep } from '../../utils/utils'; import Harness from '../../../test/harness'; import FormComponent from './Form'; import { expect } from 'chai'; @@ -8,9 +10,12 @@ import { comp3, comp4, comp5, - comp6 + comp6, + comp7, + comp8 } from './fixtures'; import Webform from '../../Webform'; +import { Formio } from '../../formio.form.js'; import formModalEdit from './fixtures/formModalEdit'; import { formComponentWithConditionalRenderingForm } from '../../../test/formtest'; @@ -273,3 +278,120 @@ describe('Wizard Component', () => { }); }); }); + +describe('SaveDraft functionality for Nested Form', () => { + const originalMakeRequest = Formio.makeRequest; + let saveDraftCalls = 0; + let restoreDraftCalls = 0; + let state = null; + + const restoredDraftData = { + parent: 'test Parent', + form: { nested: 'test Nested' }, + submit: false, + }; + + before((done) => { + Formio.setUser({ + _id: '123' + }); + + Formio.makeRequest = (formio, type, url, method, data) => { + if (type === 'submission' && ['put', 'post'].includes(method)) { + state = data.state; + if (state === 'draft') { + saveDraftCalls = ++saveDraftCalls; + } + return Promise.resolve(fastCloneDeep(data)); + } + + if (type === 'form' && method === 'get') { + return Promise.resolve(fastCloneDeep(_.endsWith(url, 'parent') ? comp7 : comp8)); + } + + if (type === 'submissions' && method === 'get' && _.includes(url, 'parent')) { + restoreDraftCalls = ++restoreDraftCalls; + return Promise.resolve([ + fastCloneDeep({ + _id: '662259f500773e9994360c72', + form: '66051dae494c977c47028fac', + owner: '63ceaccebe0090345b109da7', + data: restoredDraftData, + project: '65b0ccbaf019a907ac01a869', + state: 'draft', + }), + ]); + } + + if (type === 'submissions' && method === 'get') { + restoreDraftCalls = ++restoreDraftCalls; + return Promise.resolve([ + fastCloneDeep({ + _id: '660e75e4e8c776f1225142aa', + form: '63e4deda12b88c4f05c125cf', + owner: '63ceaccebe0090345b109da7', + data: restoredDraftData.form, + project: '65b0ccbaf019a907ac01a869', + state: 'draft', + }), + ]); + } + }; + + done(); + }); + + afterEach(() => { + saveDraftCalls = 0; + restoreDraftCalls = 0; + state = null; + }); + + after((done) => { + Formio.makeRequest = originalMakeRequest; + Formio.setUser(); + done(); + }); + + it('Should save draft for Nested Form', function(done) { + const formElement = document.createElement('div'); + Formio.createForm( + formElement, + 'http://localhost:3000/idwqwhclwioyqbw/testdraftparent', + { + saveDraft: true + } + ).then((form) => { + setTimeout(() => { + const tfNestedInput = form.getComponent('form.nested').refs.input[0]; + tfNestedInput.value = 'testNested'; + const inputEvent = new Event('input'); + tfNestedInput.dispatchEvent(inputEvent); + setTimeout(() => { + assert.equal(saveDraftCalls, 1); + assert.equal(state, 'draft'); + done(); + }, 1000); + }, 200); + }).catch((err) => done(err)); + }); + + it('Should not restore draft for Nested Form if skipDraftRestore is set as true', function(done) { + const formElement = document.createElement('div'); + Formio.createForm( + formElement, + 'http://localhost:3000/idwqwhclwioyqbw/testdraftparent', + { + saveDraft: true, + skipDraftRestore: true + } + ).then((form) => { + setTimeout(() => { + assert.equal(restoreDraftCalls, 0); + assert.equal(saveDraftCalls, 0); + assert.equal(_.isUndefined(form.submission.state), true); + done(); + }, 200); + }).catch((err) => done(err)); + }); +}); diff --git a/src/components/form/fixtures/comp7.js b/src/components/form/fixtures/comp7.js new file mode 100644 index 0000000000..a71c9a459e --- /dev/null +++ b/src/components/form/fixtures/comp7.js @@ -0,0 +1,35 @@ +export default { + _id: '66051dae494c977c47028fac', + title: 'test draft parent', + name: 'testDraftParent', + path: 'testdraftparent', + type: 'form', + display: 'form', + components: [ + { + label: 'Parent Form Field', + tableView: true, + key: 'parent', + type: 'textfield', + input: true, + }, + { + label: 'Form', + tableView: true, + src: 'http://localhost:3000/idwqwhclwioyqbw/testdraftnested', + key: 'form', + type: 'form', + input: true, + }, + { + type: 'button', + label: 'Submit', + key: 'submit', + disableOnInvalid: true, + input: true, + tableView: false, + }, + ], + project: '63cead09be0090345b109e22', + machineName: 'idwqwhclwioyqbw:testdraftparent' +}; diff --git a/src/components/form/fixtures/comp8.js b/src/components/form/fixtures/comp8.js new file mode 100644 index 0000000000..c31c73e202 --- /dev/null +++ b/src/components/form/fixtures/comp8.js @@ -0,0 +1,27 @@ +export default { + _id: '63e4deda12b88c4f05c125cf', + title: 'test draft nested', + name: 'testDraftNested', + path: 'testdraftnested', + type: 'form', + display: 'form', + components: [ + { + label: 'Nested Form Field', + tableView: true, + key: 'nested', + type: 'textfield', + input: true, + }, + { + type: 'button', + label: 'Submit', + key: 'submit', + disableOnInvalid: true, + input: true, + tableView: false, + }, + ], + project: '63cead09be0090345b109e22', + machineName: 'idwqwhclwioyqbw:testdraftparent' +}; diff --git a/src/components/form/fixtures/index.js b/src/components/form/fixtures/index.js index 182144551c..df7499d337 100644 --- a/src/components/form/fixtures/index.js +++ b/src/components/form/fixtures/index.js @@ -5,5 +5,7 @@ import comp3 from './comp3'; import comp4 from './comp4'; import comp5 from './comp5'; import comp6 from './comp6'; -export { formModalEdit, comp1, comp2, comp3, comp4, comp5, comp6 }; +import comp7 from './comp7'; +import comp8 from './comp8'; +export { formModalEdit, comp1, comp2, comp3, comp4, comp5, comp6, comp7, comp8 };