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

Bugfix: WizardBuilder removes page when saving other component with duplicate API key #5568

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/WizardBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ export default class WizardBuilder extends WebformBuilder {
this.rebuild();
}
else {
// Fallback to look for panel based on key.
const formComponentIndex = this._form.components.findIndex((comp) => originalComponent.key === comp.key);
// Fallback to look for panel based on id.
const formComponentIndex = this._form.components.findIndex((comp) => originalComponent.id === comp.id);
if (formComponentIndex !== -1) {
this._form.components[formComponentIndex] = component;
this.rebuild();
Expand Down
55 changes: 55 additions & 0 deletions src/WizardBuilder.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,59 @@ describe('WizardBuilder tests', function() {
done();
}, 500);
});

describe('Test saveComponent', () => {
it('should replace page component correctly in form object', function(done) {
const builder = createWizardBuilder(simpleWizard);
const page = builder.instance.webform.components[0];
const editComponentRef = page.refs.editComponent;

Harness.clickElement(page, editComponentRef);
setTimeout(() => {
assert(builder.instance.editForm, 'Should create the settings form on component edit');
const labelComponent = builder.instance.editForm.getComponent('label');
labelComponent.updateValue('This is page 1');
const saveButton = builder.instance.componentEdit.querySelector('[ref="saveButton"]');
Harness.clickElement(page, saveButton);
setTimeout(() => {
assert.equal(builder.instance.form.components[0].label, 'This is page 1');
done();
}, 100);
}, 100);
});

it('should not remove page when component with duplicate API key is changed', function(done) {
const builder = createWizardBuilder(simpleWizard);
const page1 = builder.instance.webform.components[0];

let component1 = builder.instance.webform.components[0].components[0];
Harness.clickElement(component1, component1.refs.editComponent);
setTimeout(() => {
assert(builder.instance.editForm, 'Should create the settings form on component edit');
const keyComponent = builder.instance.editForm.getComponent('key');
keyComponent.updateValue(page1.key);
const saveButton = builder.instance.componentEdit.querySelector('[ref="saveButton"]');
Harness.clickElement(component1, saveButton);
setTimeout(() => {
component1 = builder.instance.webform.components[0].components[0];
assert.equal(component1.errors.length, 1, 'Should have error due to duplicate API key');
assert.equal(component1.errors[0].message, `API Key is not unique: ${page1.key}`);

Harness.clickElement(component1, component1.refs.editComponent);
setTimeout(() => {
assert(builder.instance.editForm, 'Should create the settings form on component edit');
const keyComponent = builder.instance.editForm.getComponent('key');
keyComponent.updateValue('some-unique-key');
const saveButton = builder.instance.componentEdit.querySelector('[ref="saveButton"]');
Harness.clickElement(component1, saveButton);
setTimeout(() => {
assert.equal(builder.instance.form.components[0].label, page1.label, 'Page 1 should still exist');
assert.equal(builder.instance.form.components[0].components[0].key, 'some-unique-key', 'Component should have new key');
done();
}, 200);
}, 200);
}, 200);
}, 200);
});
});
});