Skip to content

Commit

Permalink
[Fix for Vis Editor] Revert setting time field to empty string when i…
Browse files Browse the repository at this point in the history
…t's undefined (#58873) (#59407)

* 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 <[email protected]>

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
maryia-lapata and elasticmachine authored Mar 5, 2020
1 parent f3f8672 commit 0342399
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ 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';

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: () => {},
Expand All @@ -42,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', () => {
Expand Down Expand Up @@ -292,4 +304,30 @@ 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(
<StepTimeField
indexPattern="ki*"
indexPatternsService={indexPatternsService}
goToPreviousStep={noop}
createIndexPattern={createIndexPattern}
indexPatternCreationType={mockIndexPatternCreationType}
/>
);

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<HTMLSelectElement>);

await (component.instance() as StepTimeField).createIndexPattern();

expect(createIndexPattern).toHaveBeenCalledWith(undefined, '');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

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

0 comments on commit 0342399

Please sign in to comment.