Skip to content

Commit 61a8b78

Browse files
[Fix for Vis Editor] Revert setting time field to empty string when it's undefined (#58873)
* Revert setting time field to empty string when it's undefined * Add unit test * Mock timeFields * Update step_time_field.test.tsx Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent d7d35f7 commit 61a8b78

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

src/legacy/core_plugins/kibana/public/management/sections/index_patterns/create_index_pattern_wizard/components/step_time_field/step_time_field.test.tsx

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ import React from 'react';
2121
import { shallowWithI18nProvider } from 'test_utils/enzyme_helpers';
2222
import { IndexPatternCreationConfig } from '../../../../../../../../management/public';
2323
import { IFieldType } from '../../../../../../../../../../plugins/data/public';
24-
import { dataPluginMock } from '../../../../../../../../../../plugins/data/public/mocks';
2524

2625
import { StepTimeField } from '../step_time_field';
2726

2827
jest.mock('./components/header', () => ({ Header: 'Header' }));
2928
jest.mock('./components/time_field', () => ({ TimeField: 'TimeField' }));
3029
jest.mock('./components/advanced_options', () => ({ AdvancedOptions: 'AdvancedOptions' }));
3130
jest.mock('./components/action_buttons', () => ({ ActionButtons: 'ActionButtons' }));
32-
jest.mock('./../../lib/extract_time_fields', () => ({
33-
extractTimeFields: (fields: IFieldType) => fields,
31+
jest.mock('./../../lib', () => ({
32+
extractTimeFields: require.requireActual('./../../lib').extractTimeFields,
33+
ensureMinimumTime: async (fields: IFieldType) => Promise.resolve(fields),
3434
}));
3535
jest.mock('ui/chrome', () => ({
3636
addBasePath: () => {},
@@ -42,7 +42,19 @@ const mockIndexPatternCreationType = new IndexPatternCreationConfig({
4242
});
4343

4444
const noop = () => {};
45-
const indexPatternsService = dataPluginMock.createStartContract().indexPatterns;
45+
const fields = [
46+
{
47+
name: '@timestamp',
48+
type: 'date',
49+
},
50+
];
51+
const indexPatternsService = {
52+
make: () => ({
53+
fieldsFetcher: {
54+
fetchForWildcard: jest.fn().mockReturnValue(Promise.resolve(fields)),
55+
},
56+
}),
57+
} as any;
4658

4759
describe('StepTimeField', () => {
4860
it('should render normally', () => {
@@ -292,4 +304,30 @@ describe('StepTimeField', () => {
292304
error: 'foobar',
293305
});
294306
});
307+
308+
it('should call createIndexPattern with undefined time field when no time filter chosen', async () => {
309+
const createIndexPattern = jest.fn();
310+
311+
const component = shallowWithI18nProvider(
312+
<StepTimeField
313+
indexPattern="ki*"
314+
indexPatternsService={indexPatternsService}
315+
goToPreviousStep={noop}
316+
createIndexPattern={createIndexPattern}
317+
indexPatternCreationType={mockIndexPatternCreationType}
318+
/>
319+
);
320+
321+
await (component.instance() as StepTimeField).fetchTimeFields();
322+
323+
expect((component.state() as any).timeFields).toHaveLength(3);
324+
325+
(component.instance() as StepTimeField).onTimeFieldChanged(({
326+
target: { value: undefined },
327+
} as unknown) as React.ChangeEvent<HTMLSelectElement>);
328+
329+
await (component.instance() as StepTimeField).createIndexPattern();
330+
331+
expect(createIndexPattern).toHaveBeenCalledWith(undefined, '');
332+
});
295333
});

src/legacy/core_plugins/kibana/public/management/sections/index_patterns/create_index_pattern_wizard/components/step_time_field/step_time_field.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ interface StepTimeFieldProps {
4141
indexPattern: string;
4242
indexPatternsService: DataPublicPluginStart['indexPatterns'];
4343
goToPreviousStep: () => void;
44-
createIndexPattern: (selectedTimeField: string, indexPatternId: string) => void;
44+
createIndexPattern: (selectedTimeField: string | undefined, indexPatternId: string) => void;
4545
indexPatternCreationType: IndexPatternCreationConfig;
4646
}
4747

@@ -143,7 +143,7 @@ export class StepTimeField extends Component<StepTimeFieldProps, StepTimeFieldSt
143143
const { selectedTimeField, indexPatternId } = this.state;
144144
this.setState({ isCreating: true });
145145
try {
146-
await createIndexPattern(selectedTimeField || '', indexPatternId);
146+
await createIndexPattern(selectedTimeField, indexPatternId);
147147
} catch (error) {
148148
if (!this.mounted) return;
149149
this.setState({

0 commit comments

Comments
 (0)