Skip to content

Commit

Permalink
Fix empty meta fields input in Advanced Settings (elastic#78576)
Browse files Browse the repository at this point in the history
  • Loading branch information
kertal committed Sep 30, 2020
1 parent 9e19d91 commit afc3946
Show file tree
Hide file tree
Showing 3 changed files with 285 additions and 1 deletion.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ const settings = {
description: 'foo',
category: ['general'],
},
{
...defaults,
name: 'general:test:array',
ariaName: 'array test',
displayName: 'Test array setting',
description: 'array foo',
type: 'array' as UiSettingsType,
category: ['general'],
defVal: ['test'],
},
],
'x-pack': [
{
Expand Down Expand Up @@ -256,4 +266,60 @@ describe('Form', () => {
})
);
});

it('should save an array typed field when user provides an empty string correctly', async () => {
const wrapper = mountWithI18nProvider(
<Form
settings={settings}
visibleSettings={settings}
categories={categories}
categoryCounts={categoryCounts}
save={save}
clearQuery={clearQuery}
showNoResultsMessage={true}
enableSaving={false}
toasts={{} as any}
dockLinks={{} as any}
/>
);

(wrapper.instance() as Form).setState({
unsavedChanges: {
'general:test:array': {
value: '',
},
},
});

findTestSubject(wrapper.update(), `advancedSetting-saveButton`).simulate('click');
expect(save).toHaveBeenCalledWith({ 'general:test:array': [] });
});

it('should save an array typed field when user provides a comma separated string correctly', async () => {
const wrapper = mountWithI18nProvider(
<Form
settings={settings}
visibleSettings={settings}
categories={categories}
categoryCounts={categoryCounts}
save={save}
clearQuery={clearQuery}
showNoResultsMessage={true}
enableSaving={false}
toasts={{} as any}
dockLinks={{} as any}
/>
);

(wrapper.instance() as Form).setState({
unsavedChanges: {
'general:test:array': {
value: 'test1, test2',
},
},
});

findTestSubject(wrapper.update(), `advancedSetting-saveButton`).simulate('click');
expect(save).toHaveBeenCalledWith({ 'general:test:array': ['test1', 'test2'] });
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ export class Form extends PureComponent<FormProps> {
let equalsToDefault = false;
switch (type) {
case 'array':
valueToSave = valueToSave.split(',').map((val: string) => val.trim());
valueToSave = valueToSave.trim();
valueToSave =
valueToSave === '' ? [] : valueToSave.split(',').map((val: string) => val.trim());
equalsToDefault = valueToSave.join(',') === (defVal as string[]).join(',');
break;
case 'json':
Expand Down

0 comments on commit afc3946

Please sign in to comment.