Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIO-8240 fixed skipDraftRestore effect for the nested Forms #5599

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/components/form/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
124 changes: 123 additions & 1 deletion src/components/form/Form.unit.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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';

Expand Down Expand Up @@ -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));
});
});
35 changes: 35 additions & 0 deletions src/components/form/fixtures/comp7.js
Original file line number Diff line number Diff line change
@@ -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'
};
27 changes: 27 additions & 0 deletions src/components/form/fixtures/comp8.js
Original file line number Diff line number Diff line change
@@ -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'
};
4 changes: 3 additions & 1 deletion src/components/form/fixtures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Loading