From 9870ca79b5164b91236021dd409ad65a308b0aeb Mon Sep 17 00:00:00 2001 From: maryia-lapata Date: Fri, 28 Feb 2020 16:46:34 +0300 Subject: [PATCH 1/4] Revert setting time field to empty string when it's undefined --- .../components/step_time_field/step_time_field.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/create_index_pattern_wizard/components/step_time_field/step_time_field.tsx b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/create_index_pattern_wizard/components/step_time_field/step_time_field.tsx index dff2a07e460e2..80582cc1fbd92 100644 --- a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/create_index_pattern_wizard/components/step_time_field/step_time_field.tsx +++ b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/create_index_pattern_wizard/components/step_time_field/step_time_field.tsx @@ -41,7 +41,7 @@ interface StepTimeFieldProps { indexPattern: string; indexPatternsService: DataPublicPluginStart['indexPatterns']; goToPreviousStep: () => void; - createIndexPattern: (selectedTimeField: string, indexPatternId: string) => void; + createIndexPattern: (selectedTimeField: string | undefined, indexPatternId: string) => void; indexPatternCreationType: IndexPatternCreationConfig; } @@ -143,7 +143,7 @@ export class StepTimeField extends Component Date: Wed, 4 Mar 2020 14:59:51 +0300 Subject: [PATCH 2/4] Add unit test --- .../step_time_field/step_time_field.test.tsx | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/create_index_pattern_wizard/components/step_time_field/step_time_field.test.tsx b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/create_index_pattern_wizard/components/step_time_field/step_time_field.test.tsx index f37dc088ac78e..c2c9b2d46d32f 100644 --- a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/create_index_pattern_wizard/components/step_time_field/step_time_field.test.tsx +++ b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/create_index_pattern_wizard/components/step_time_field/step_time_field.test.tsx @@ -292,4 +292,28 @@ describe('StepTimeField', () => { error: 'foobar', }); }); + + it('should call createIndexPattern with undefined time field when no time filter chosen', async () => { + const createIndexPattern = jest.fn(); + + const component = shallowWithI18nProvider( + + ); + + (component.instance() as StepTimeField).onTimeFieldChanged(({ + target: { value: undefined }, + } as unknown) as React.ChangeEvent); + component.update(); + + await (component.instance() as StepTimeField).createIndexPattern(); + component.update(); + + expect(createIndexPattern).toHaveBeenCalledWith(undefined, ''); + }); }); From b363f110806ebc1a33275e1be4f6606b982ed3fb Mon Sep 17 00:00:00 2001 From: maryia-lapata Date: Wed, 4 Mar 2020 16:05:45 +0300 Subject: [PATCH 3/4] Mock timeFields --- .../step_time_field/step_time_field.test.tsx | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/create_index_pattern_wizard/components/step_time_field/step_time_field.test.tsx b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/create_index_pattern_wizard/components/step_time_field/step_time_field.test.tsx index c2c9b2d46d32f..ec8beb443ac92 100644 --- a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/create_index_pattern_wizard/components/step_time_field/step_time_field.test.tsx +++ b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/create_index_pattern_wizard/components/step_time_field/step_time_field.test.tsx @@ -29,8 +29,9 @@ jest.mock('./components/header', () => ({ Header: 'Header' })); jest.mock('./components/time_field', () => ({ TimeField: 'TimeField' })); jest.mock('./components/advanced_options', () => ({ AdvancedOptions: 'AdvancedOptions' })); jest.mock('./components/action_buttons', () => ({ ActionButtons: 'ActionButtons' })); -jest.mock('./../../lib/extract_time_fields', () => ({ - extractTimeFields: (fields: IFieldType) => fields, +jest.mock('./../../lib', () => ({ + extractTimeFields: require.requireActual('./../../lib').extractTimeFields, + ensureMinimumTime: async (fields: IFieldType) => Promise.resolve(fields), })); jest.mock('ui/chrome', () => ({ addBasePath: () => {}, @@ -295,24 +296,39 @@ describe('StepTimeField', () => { it('should call createIndexPattern with undefined time field when no time filter chosen', async () => { const createIndexPattern = jest.fn(); + const fields = [ + { + name: '@timestamp', + type: 'date', + }, + ]; + const indPatternsService = { + make: () => ({ + fieldsFetcher: { + fetchForWildcard: jest.fn().mockReturnValue(Promise.resolve(fields)), + }, + }), + } as any; const component = shallowWithI18nProvider( ); + await (component.instance() as StepTimeField).fetchTimeFields(); + + expect((component.state() as any).timeFields).toHaveLength(3); + (component.instance() as StepTimeField).onTimeFieldChanged(({ target: { value: undefined }, } as unknown) as React.ChangeEvent); - component.update(); await (component.instance() as StepTimeField).createIndexPattern(); - component.update(); expect(createIndexPattern).toHaveBeenCalledWith(undefined, ''); }); From bebc47aabe7271c5448d92d891e1ee4a37622735 Mon Sep 17 00:00:00 2001 From: maryia-lapata Date: Wed, 4 Mar 2020 19:20:50 +0300 Subject: [PATCH 4/4] Update step_time_field.test.tsx --- .../step_time_field/step_time_field.test.tsx | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/create_index_pattern_wizard/components/step_time_field/step_time_field.test.tsx b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/create_index_pattern_wizard/components/step_time_field/step_time_field.test.tsx index ec8beb443ac92..e0c43105cb320 100644 --- a/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/create_index_pattern_wizard/components/step_time_field/step_time_field.test.tsx +++ b/src/legacy/core_plugins/kibana/public/management/sections/index_patterns/create_index_pattern_wizard/components/step_time_field/step_time_field.test.tsx @@ -21,7 +21,6 @@ import React from 'react'; import { shallowWithI18nProvider } from 'test_utils/enzyme_helpers'; import { IndexPatternCreationConfig } from '../../../../../../../../management/public'; import { IFieldType } from '../../../../../../../../../../plugins/data/public'; -import { dataPluginMock } from '../../../../../../../../../../plugins/data/public/mocks'; import { StepTimeField } from '../step_time_field'; @@ -43,7 +42,19 @@ const mockIndexPatternCreationType = new IndexPatternCreationConfig({ }); const noop = () => {}; -const indexPatternsService = dataPluginMock.createStartContract().indexPatterns; +const fields = [ + { + name: '@timestamp', + type: 'date', + }, +]; +const indexPatternsService = { + make: () => ({ + fieldsFetcher: { + fetchForWildcard: jest.fn().mockReturnValue(Promise.resolve(fields)), + }, + }), +} as any; describe('StepTimeField', () => { it('should render normally', () => { @@ -296,24 +307,11 @@ describe('StepTimeField', () => { it('should call createIndexPattern with undefined time field when no time filter chosen', async () => { const createIndexPattern = jest.fn(); - const fields = [ - { - name: '@timestamp', - type: 'date', - }, - ]; - const indPatternsService = { - make: () => ({ - fieldsFetcher: { - fetchForWildcard: jest.fn().mockReturnValue(Promise.resolve(fields)), - }, - }), - } as any; const component = shallowWithI18nProvider(