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

Fix empty meta fields input in Advanced Settings #78576

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

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