Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix for Vis Editor] Revert setting time field to empty string when it's undefined #58873

Merged
merged 7 commits into from
Mar 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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