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-9347: Fixed select preview issue in form builder #5915

Merged
merged 2 commits into from
Nov 21, 2024
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
1 change: 1 addition & 0 deletions src/WebformBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
148 changes: 148 additions & 0 deletions test/unit/WebformBuilder.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading