Skip to content

Commit

Permalink
FIO-8330 fixed saving draft if saveDraft and skipDraftRestore are true
Browse files Browse the repository at this point in the history
  • Loading branch information
HannaKurban committed May 13, 2024
1 parent eeaff43 commit 4f4b210
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/Webform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});
}
Expand Down
35 changes: 35 additions & 0 deletions src/Webform.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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',
Expand Down Expand Up @@ -4707,6 +4713,7 @@ describe('Webform tests', function() {
afterEach(() => {
saveDraftCalls = 0;
restoreDraftCalls = 0;
state = null;
scenario.restoreDraftError = false;
scenario.saveDraftError = false;
});
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 4f4b210

Please sign in to comment.