From 964ea5ce948a711e2786e6f395b89ff5e3e8f01b Mon Sep 17 00:00:00 2001 From: Alison Goryachev Date: Mon, 28 Sep 2020 20:57:13 -0400 Subject: [PATCH 1/2] fix bug when switching between mapping tabs --- .../components/templates_form/templates_form.tsx | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/x-pack/plugins/index_management/public/application/components/mappings_editor/components/templates_form/templates_form.tsx b/x-pack/plugins/index_management/public/application/components/mappings_editor/components/templates_form/templates_form.tsx index 4b813b4edbabd..46e7bbd5e094a 100644 --- a/x-pack/plugins/index_management/public/application/components/mappings_editor/components/templates_form/templates_form.tsx +++ b/x-pack/plugins/index_management/public/application/components/mappings_editor/components/templates_form/templates_form.tsx @@ -36,11 +36,10 @@ const formSerializer: SerializerFunc = (formData) // Silently swallow errors } - return Array.isArray(parsedTemplates) && parsedTemplates.length > 0 - ? { - dynamic_templates: parsedTemplates, - } - : undefined; + return { + dynamic_templates: + Array.isArray(parsedTemplates) && parsedTemplates.length > 0 ? parsedTemplates : [], + }; }; const formDeserializer = (formData: { [key: string]: any }) => { From e5b4ad09fab238f8a8c2e8fdc5a1cc40020f0817 Mon Sep 17 00:00:00 2001 From: Alison Goryachev Date: Tue, 29 Sep 2020 09:16:12 -0400 Subject: [PATCH 2/2] add test --- .../mappings_editor.test.tsx | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/x-pack/plugins/index_management/public/application/components/mappings_editor/__jest__/client_integration/mappings_editor.test.tsx b/x-pack/plugins/index_management/public/application/components/mappings_editor/__jest__/client_integration/mappings_editor.test.tsx index 68933ddc9a935..f5fcff9f96254 100644 --- a/x-pack/plugins/index_management/public/application/components/mappings_editor/__jest__/client_integration/mappings_editor.test.tsx +++ b/x-pack/plugins/index_management/public/application/components/mappings_editor/__jest__/client_integration/mappings_editor.test.tsx @@ -223,6 +223,33 @@ describe('Mappings editor: core', () => { isNumericDetectionVisible = exists('advancedConfiguration.numericDetection'); expect(isNumericDetectionVisible).toBe(false); }); + + test('should keep default dynamic templates value when switching tabs', async () => { + await act(async () => { + testBed = setup({ + value: { ...defaultMappings, dynamic_templates: [] }, // by default, the UI will provide an empty array for dynamic templates + onChange: onChangeHandler, + }); + }); + testBed.component.update(); + + const { + actions: { selectTab, getJsonEditorValue }, + } = testBed; + + // Navigate to dynamic templates tab and verify empty array + await selectTab('templates'); + let templatesValue = getJsonEditorValue('dynamicTemplatesEditor'); + expect(templatesValue).toEqual([]); + + // Navigate to advanced tab + await selectTab('advanced'); + + // Navigate back to dynamic templates tab and verify empty array persists + await selectTab('templates'); + templatesValue = getJsonEditorValue('dynamicTemplatesEditor'); + expect(templatesValue).toEqual([]); + }); }); describe('component props', () => {