Skip to content

Commit afc3946

Browse files
committed
Fix empty meta fields input in Advanced Settings (elastic#78576)
1 parent 9e19d91 commit afc3946

File tree

3 files changed

+285
-1
lines changed

3 files changed

+285
-1
lines changed

src/plugins/advanced_settings/public/management_app/components/form/__snapshots__/form.test.tsx.snap

+216
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/plugins/advanced_settings/public/management_app/components/form/form.test.tsx

+66
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ const settings = {
9191
description: 'foo',
9292
category: ['general'],
9393
},
94+
{
95+
...defaults,
96+
name: 'general:test:array',
97+
ariaName: 'array test',
98+
displayName: 'Test array setting',
99+
description: 'array foo',
100+
type: 'array' as UiSettingsType,
101+
category: ['general'],
102+
defVal: ['test'],
103+
},
94104
],
95105
'x-pack': [
96106
{
@@ -256,4 +266,60 @@ describe('Form', () => {
256266
})
257267
);
258268
});
269+
270+
it('should save an array typed field when user provides an empty string correctly', async () => {
271+
const wrapper = mountWithI18nProvider(
272+
<Form
273+
settings={settings}
274+
visibleSettings={settings}
275+
categories={categories}
276+
categoryCounts={categoryCounts}
277+
save={save}
278+
clearQuery={clearQuery}
279+
showNoResultsMessage={true}
280+
enableSaving={false}
281+
toasts={{} as any}
282+
dockLinks={{} as any}
283+
/>
284+
);
285+
286+
(wrapper.instance() as Form).setState({
287+
unsavedChanges: {
288+
'general:test:array': {
289+
value: '',
290+
},
291+
},
292+
});
293+
294+
findTestSubject(wrapper.update(), `advancedSetting-saveButton`).simulate('click');
295+
expect(save).toHaveBeenCalledWith({ 'general:test:array': [] });
296+
});
297+
298+
it('should save an array typed field when user provides a comma separated string correctly', async () => {
299+
const wrapper = mountWithI18nProvider(
300+
<Form
301+
settings={settings}
302+
visibleSettings={settings}
303+
categories={categories}
304+
categoryCounts={categoryCounts}
305+
save={save}
306+
clearQuery={clearQuery}
307+
showNoResultsMessage={true}
308+
enableSaving={false}
309+
toasts={{} as any}
310+
dockLinks={{} as any}
311+
/>
312+
);
313+
314+
(wrapper.instance() as Form).setState({
315+
unsavedChanges: {
316+
'general:test:array': {
317+
value: 'test1, test2',
318+
},
319+
},
320+
});
321+
322+
findTestSubject(wrapper.update(), `advancedSetting-saveButton`).simulate('click');
323+
expect(save).toHaveBeenCalledWith({ 'general:test:array': ['test1', 'test2'] });
324+
});
259325
});

src/plugins/advanced_settings/public/management_app/components/form/form.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ export class Form extends PureComponent<FormProps> {
154154
let equalsToDefault = false;
155155
switch (type) {
156156
case 'array':
157-
valueToSave = valueToSave.split(',').map((val: string) => val.trim());
157+
valueToSave = valueToSave.trim();
158+
valueToSave =
159+
valueToSave === '' ? [] : valueToSave.split(',').map((val: string) => val.trim());
158160
equalsToDefault = valueToSave.join(',') === (defVal as string[]).join(',');
159161
break;
160162
case 'json':

0 commit comments

Comments
 (0)