diff --git a/src/WebformBuilder.js b/src/WebformBuilder.js index 6581e6b4a1..047abb30ef 100644 --- a/src/WebformBuilder.js +++ b/src/WebformBuilder.js @@ -1361,6 +1361,7 @@ export default class WebformBuilder extends Component { const rebuild = parentComponent.rebuild() || Promise.resolve(); return rebuild.then(() => { + parentComponent.resetValue(); const schema = parentContainer ? parentContainer[index] : (comp ? comp.schema : []); this.emitSaveComponentEvent( schema, diff --git a/test/unit/WebformBuilder.unit.js b/test/unit/WebformBuilder.unit.js index 3352982454..e9413a5c18 100644 --- a/test/unit/WebformBuilder.unit.js +++ b/test/unit/WebformBuilder.unit.js @@ -602,6 +602,154 @@ describe('Select Component selectData property', () => { }).catch(done); }); + it('Should show correct default value for select component in form builder', (done) => { + const builder = Harness.getBuilder(); + builder.setForm({}).then(() => { + Harness.buildComponent('select'); + + setTimeout(() => { + const dataSrc = builder.editForm.getComponent('dataSrc'); + dataSrc.setValue('url'); + const url = builder.editForm.getComponent(['data.url']); + const valueProperty = builder.editForm.getComponent('valueProperty'); + url.setValue('htts//fakeurl.com'); + valueProperty.setValue('value'); + + setTimeout(() => { + const defaultValue = builder.editForm.getComponent('defaultValue'); + assert.equal(defaultValue.type, 'select'); + defaultValue.setValue('value1'); + defaultValue.updateItems(null, true); + + setTimeout(() => { + assert.deepEqual(builder.editForm.data.selectData, { + label: 'Label 1', + }); + Harness.saveComponent(); + setTimeout(() => { + assert.equal( + builder.webform.getComponent('select').element.querySelector('[aria-selected="true"] span').innerHTML, + 'Label 1', + ); + + const click = new MouseEvent('click', { + view: window, + bubbles: true, + cancelable: true + }); + + builder.webform.getComponent('select').element + .querySelector('.component-settings-button-edit') + .dispatchEvent(click); + + setTimeout(() => { + const defaultValue = builder.editForm.getComponent('defaultValue'); + assert.equal(defaultValue.type, 'select'); + defaultValue.setValue('value2'); + defaultValue.updateItems(null, true); + + setTimeout(() => { + assert.deepEqual(builder.editForm.data.selectData, { + label: 'Label 2', + }); + Harness.saveComponent(); + setTimeout(() => { + assert.equal( + builder.webform.getComponent('select').element.querySelector('[aria-selected="true"] span').innerHTML, + 'Label 2', + ); + done(); + }, 150); + }, 250); + }, 500); + }, 150); + }, 250); + }, 250); + }, 150); + }).catch(done); + }); + + it('Should show correct default value for multiple select component in form builder', (done) => { + const builder = Harness.getBuilder(); + builder.setForm({}).then(() => { + Harness.buildComponent('select'); + + setTimeout(() => { + const multiple = builder.editForm.getComponent('multiple'); + multiple.setValue(true); + const dataSrc = builder.editForm.getComponent('dataSrc'); + dataSrc.setValue('url'); + const url = builder.editForm.getComponent(['data.url']); + const valueProperty = builder.editForm.getComponent('valueProperty'); + url.setValue('htts//fakeurl.com'); + valueProperty.setValue('value'); + + setTimeout(() => { + const defaultValue = builder.editForm.getComponent('defaultValue'); + assert.equal(defaultValue.type, 'select'); + defaultValue.setValue(['value1', 'value3']); + defaultValue.updateItems(null, true); + + setTimeout(() => { + assert.deepEqual(builder.editForm.data.selectData, { + value1: { + label: 'Label 1', + }, + value3: { + label: 'Label 3', + }, + }); + Harness.saveComponent(); + setTimeout(() => { + const elements = builder.webform.getComponent('select').element.querySelectorAll('.choices__list--multiple span'); + assert.deepEqual( + Array.prototype.map.call(elements, (element) => element.innerHTML), + ['Label 1', 'Label 3'], + ); + + const click = new MouseEvent('click', { + view: window, + bubbles: true, + cancelable: true + }); + + builder.webform.getComponent('select').element + .querySelector('.component-settings-button-edit') + .dispatchEvent(click); + + setTimeout(() => { + const defaultValue = builder.editForm.getComponent('defaultValue'); + assert.equal(defaultValue.type, 'select'); + defaultValue.setValue(['value2', 'value3']); + defaultValue.updateItems(null, true); + + setTimeout(() => { + assert.deepEqual(builder.editForm.data.selectData, { + value2: { + label: 'Label 2', + }, + value3: { + label: 'Label 3', + }, + }); + Harness.saveComponent(); + setTimeout(() => { + const elements = builder.webform.getComponent('select').element.querySelectorAll('.choices__list--multiple span'); + assert.deepEqual( + Array.prototype.map.call(elements, (element) => element.innerHTML), + ['Label 2', 'Label 3'], + ); + done(); + }, 150); + }, 250); + }, 500); + }, 150); + }, 250); + }, 250); + }, 150); + }).catch(done); + }); + after((done) => { Formio.makeRequest = originalMakeRequest; Harness.builderAfter(done);