Skip to content
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 @@ -8,7 +8,7 @@
import React from 'react';
import type { DateHistogramIndexPatternColumn } from './date_histogram';
import { dateHistogramOperation } from '.';
import { shallow } from 'enzyme';
import { mount, shallow } from 'enzyme';
import { EuiSwitch } from '@elastic/eui';
import { unifiedSearchPluginMock } from '@kbn/unified-search-plugin/public/mocks';
import type { IUiSettingsClient, SavedObjectsClientContract, HttpSetup } from '@kbn/core/public';
Expand All @@ -18,6 +18,7 @@ import { dataPluginMock, getCalculateAutoTimeExpression } from '@kbn/data-plugin
import { createMockedIndexPattern } from '../../mocks';
import type { IndexPatternLayer, IndexPattern } from '../../types';
import { getFieldByNameFactory } from '../../pure_helpers';
import { act } from 'react-dom/test-utils';

const dataStart = dataPluginMock.createStartContract();
const unifiedSearchStart = unifiedSearchPluginMock.createStartContract();
Expand Down Expand Up @@ -312,8 +313,9 @@ describe('date_histogram', () => {
/>
);

expect(instance.find('[data-test-subj="lensDateHistogramValue"]').prop('value')).toEqual(42);
expect(instance.find('[data-test-subj="lensDateHistogramUnit"]').prop('value')).toEqual('w');
expect(
instance.find('[data-test-subj="lensDateHistogramInterval"]').prop('selectedOptions')
).toEqual([expect.objectContaining({ label: '42w' })]);
});

it('should render current value for other index pattern', () => {
Expand Down Expand Up @@ -348,11 +350,12 @@ describe('date_histogram', () => {
/>
);

expect(instance.find('[data-test-subj="lensDateHistogramValue"]').prop('value')).toEqual('');
expect(instance.find('[data-test-subj="lensDateHistogramUnit"]').prop('value')).toEqual('d');
expect(
instance.find('[data-test-subj="lensDateHistogramInterval"]').prop('selectedOptions')
).toEqual([expect.objectContaining({ key: 'd' })]);
});

it('should render disabled switch and no time interval control for auto interval', () => {
it('should render time interval control set to auto for auto interval', () => {
const thirdLayer: IndexPatternLayer = {
indexPatternId: '1',
columnOrder: ['col1'],
Expand Down Expand Up @@ -382,9 +385,9 @@ describe('date_histogram', () => {
indexPattern={indexPattern1}
/>
);
expect(instance.find('[data-test-subj="lensDateHistogramValue"]').exists()).toBeFalsy();
expect(instance.find('[data-test-subj="lensDateHistogramUnit"]').exists()).toBeFalsy();
expect(instance.find(EuiSwitch).at(1).prop('checked')).toBe(false);
expect(
instance.find('[data-test-subj="lensDateHistogramInterval"]').prop('selectedOptions')
).toEqual([expect.objectContaining({ key: 'auto' })]);
});

it('should allow switching to manual interval', () => {
Expand Down Expand Up @@ -461,7 +464,7 @@ describe('date_histogram', () => {
);
instance
.find(EuiSwitch)
.at(2)
.at(1)
.simulate('change', {
target: { checked: false },
});
Expand Down Expand Up @@ -502,16 +505,14 @@ describe('date_histogram', () => {
indexPattern={{ ...indexPattern1, timeFieldName: undefined }}
/>
);
instance
.find(EuiSwitch)
.at(1)
.simulate('change', {
target: { checked: false },
});
(
instance
.find('[data-test-subj="lensDateHistogramInterval"]')
.prop('onChange') as unknown as (v: Array<{ key: string }>) => void
)([{ key: 'auto' }]);
expect(updateLayerSpy).toHaveBeenCalled();
const newLayer = updateLayerSpy.mock.calls[0][0];
expect(newLayer).toHaveProperty('columns.col1.params.ignoreTimeRange', false);
expect(newLayer).toHaveProperty('columns.col1.params.interval', 'auto');
});

it('turns off drop partial bucket on tuning off time range ignore', () => {
Expand Down Expand Up @@ -560,12 +561,14 @@ describe('date_histogram', () => {
currentColumn={layer.columns.col1 as DateHistogramIndexPatternColumn}
/>
);
instance.find('[data-test-subj="lensDateHistogramValue"]').simulate('change', {
target: {
value: '2',
},
});
expect(updateLayerSpy).toHaveBeenCalledWith(layerWithInterval('1w'));
(
instance.find('[data-test-subj="lensDateHistogramInterval"]').prop('onCreateOption') as (
s: string
) => void
)('2w');
expect(
instance.find('[data-test-subj="lensDateHistogramInterval"]').prop('isInvalid')
).toBeTruthy();
});

it('should display error if an invalid interval is specified', () => {
Expand All @@ -580,7 +583,9 @@ describe('date_histogram', () => {
currentColumn={testLayer.columns.col1 as DateHistogramIndexPatternColumn}
/>
);
expect(instance.find('[data-test-subj="lensDateHistogramError"]').exists()).toBeTruthy();
expect(
instance.find('[data-test-subj="lensDateHistogramInterval"]').prop('isInvalid')
).toBeTruthy();
});

it('should not display error if interval value is blank', () => {
Expand All @@ -595,7 +600,9 @@ describe('date_histogram', () => {
currentColumn={testLayer.columns.col1 as DateHistogramIndexPatternColumn}
/>
);
expect(instance.find('[data-test-subj="lensDateHistogramError"]').exists()).toBeFalsy();
expect(
instance.find('[data-test-subj="lensDateHistogramInterval"]').prop('isInvalid')
).toBeFalsy();
});

it('should display error if interval value is 0', () => {
Expand All @@ -610,12 +617,14 @@ describe('date_histogram', () => {
currentColumn={testLayer.columns.col1 as DateHistogramIndexPatternColumn}
/>
);
expect(instance.find('[data-test-subj="lensDateHistogramError"]').exists()).toBeTruthy();
expect(
instance.find('[data-test-subj="lensDateHistogramInterval"]').prop('isInvalid')
).toBeTruthy();
});

it('should update the unit', () => {
it('should update the unit', async () => {
const updateLayerSpy = jest.fn();
const instance = shallow(
const instance = mount(
<InlineOptions
{...defaultOptions}
layer={layer}
Expand All @@ -624,19 +633,22 @@ describe('date_histogram', () => {
currentColumn={layer.columns.col1 as DateHistogramIndexPatternColumn}
/>
);
instance.find('[data-test-subj="lensDateHistogramUnit"]').simulate('change', {
target: {
value: 'd',
},
act(() => {
(
instance
.find('[data-test-subj="lensDateHistogramInterval"]')
.at(0)
.prop('onCreateOption') as (s: string) => void
)('42d');
});
expect(updateLayerSpy).toHaveBeenCalledWith(layerWithInterval('42d'));
expect(updateLayerSpy.mock.calls[0][0](layer)).toEqual(layerWithInterval('42d'));
});

it('should update the value', () => {
const updateLayerSpy = jest.fn();
const testLayer = layerWithInterval('42d');

const instance = shallow(
const instance = mount(
<InlineOptions
{...defaultOptions}
layer={testLayer}
Expand All @@ -645,12 +657,15 @@ describe('date_histogram', () => {
currentColumn={testLayer.columns.col1 as DateHistogramIndexPatternColumn}
/>
);
instance.find('[data-test-subj="lensDateHistogramValue"]').simulate('change', {
target: {
value: '9',
},
});
expect(updateLayerSpy).toHaveBeenCalledWith(layerWithInterval('9d'));
act(() =>
(
instance
.find('[data-test-subj="lensDateHistogramInterval"]')
.at(0)
.prop('onCreateOption') as (s: string) => void
)('9d')
);
expect(updateLayerSpy.mock.calls[0][0](layer)).toEqual(layerWithInterval('9d'));
});

it('should not render options if they are restricted', () => {
Expand Down Expand Up @@ -695,7 +710,7 @@ describe('date_histogram', () => {
/>
);

expect(instance.find('[data-test-subj="lensDateHistogramValue"]').exists()).toBeFalsy();
expect(instance.find('[data-test-subj="lensDateHistogramInterval"]').exists()).toBeFalsy();
});

it('should allow the drop of partial buckets', () => {
Expand Down Expand Up @@ -735,7 +750,7 @@ describe('date_histogram', () => {
target: { checked: true },
});
expect(updateLayerSpy).toHaveBeenCalled();
const newLayer = updateLayerSpy.mock.calls[0][0];
const newLayer = updateLayerSpy.mock.calls[0][0](layer);
expect(newLayer).toHaveProperty('columns.col1.params.dropPartials', true);
});
});
Expand Down
Loading