From f7c032a7c694316acd3ce5a3cd62aaa901f6a945 Mon Sep 17 00:00:00 2001 From: HannaKurban <96909212+HannaKurban@users.noreply.github.com> Date: Wed, 15 May 2024 11:42:49 +0300 Subject: [PATCH] FIO-8330 fixed saving draft if saveDraft and skipDraftRestore are true (#5593) --- src/Webform.js | 17 ++++++++++++----- src/Webform.unit.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/Webform.js b/src/Webform.js index 48de614951..ac7619a264 100644 --- a/src/Webform.js +++ b/src/Webform.js @@ -303,12 +303,19 @@ export default class Webform extends NestedDataComponent { this.language = this.i18next.language; // See if we need to restore the draft from a user. - if (this.options.saveDraft && !this.options.skipDraftRestore) { + if (this.options.saveDraft) { this.formReady.then(()=> { - const user = Formio.getUser(); - // Only restore a draft if the submission isn't explicitly set. - if (user && !this.submissionSet) { - this.restoreDraft(user._id); + if (!this.options.skipDraftRestore) { + const user = Formio.getUser(); + // Only restore a draft if the submission isn't explicitly set. + if (user && !this.submissionSet) { + this.restoreDraft(user._id); + } + } + else { + // Enable drafts + this.draftEnabled = true; + this.savingDraft = false; } }); } diff --git a/src/Webform.unit.js b/src/Webform.unit.js index f2c5ac71d8..53c9ae846b 100644 --- a/src/Webform.unit.js +++ b/src/Webform.unit.js @@ -4601,6 +4601,7 @@ describe('Webform tests', function() { const originalMakeRequest = Formio.makeRequest; let saveDraftCalls = 0; let restoreDraftCalls = 0; + let state = null; const scenario = { restoreDraftError: false, saveDraftError: false, @@ -4624,6 +4625,11 @@ describe('Webform tests', function() { ? Promise.reject('Save Draft Error') : Promise.resolve(fastCloneDeep(data)); } + if (type === 'submission' && method === 'post') { + state = data.state; + saveDraftCalls = ++saveDraftCalls; + return Promise.resolve(fastCloneDeep(data)); + } if (type === 'form' && method === 'get') { return Promise.resolve(fastCloneDeep({ _id: '65cdd69efb1b9683c216fa1d', @@ -4707,6 +4713,7 @@ describe('Webform tests', function() { afterEach(() => { saveDraftCalls = 0; restoreDraftCalls = 0; + state = null; scenario.restoreDraftError = false; scenario.saveDraftError = false; }); @@ -4810,6 +4817,34 @@ describe('Webform tests', function() { }, 200); }).catch((err) => done(err)); }); + + it('Should save the draft after changing the data if skipDraftRestore is set as true', function(done) { + const formElement = document.createElement('div'); + Formio.createForm( + formElement, + 'http://localhost:3000/zarbzxibjafpcjb/testdrafterrors', + { + saveDraft: true, + skipDraftRestore: true + } + ).then((form) => { + setTimeout(() => { + assert.equal(restoreDraftCalls, 0, 'Should not restore Draft'); + assert.equal(saveDraftCalls, 0); + assert.equal(_.isUndefined(form.submission.state), true); + const tfInput = form.getComponent('textField').refs.input[0]; + tfInput.value = 'test'; + const inputEvent = new Event('input'); + tfInput.dispatchEvent(inputEvent); + setTimeout(() => { + assert.equal(restoreDraftCalls, 0); + assert.equal(saveDraftCalls, 1, 'Should save Draft'); + assert.equal(state, 'draft'); + done(); + }, 300); + },200); + }).catch((err) => done(err)); + }); }); for (const formTest of FormTests) {