Skip to content

Commit

Permalink
[Discover] Dont trigger onChange when the same index pattern is selec…
Browse files Browse the repository at this point in the history
  • Loading branch information
kertal committed Jun 23, 2021
1 parent eea0add commit b859c36
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import React from 'react';
import { EuiSelectable } from '@elastic/eui';
import { ShallowWrapper } from 'enzyme';
import { act } from 'react-dom/test-utils';
import { shallowWithIntl } from '@kbn/test/jest';
import { ChangeIndexPattern } from './change_indexpattern';
import { indexPatternMock } from '../../../../../__mocks__/index_pattern';
import { indexPatternWithTimefieldMock } from '../../../../../__mocks__/index_pattern_with_timefield';
import { IndexPatternRef } from './types';

function getProps() {
return {
indexPatternId: indexPatternMock.id,
indexPatternRefs: [
indexPatternMock as IndexPatternRef,
indexPatternWithTimefieldMock as IndexPatternRef,
],
onChangeIndexPattern: jest.fn(),
trigger: {
label: indexPatternMock.title,
title: indexPatternMock.title,
'data-test-subj': 'indexPattern-switch-link',
},
};
}

function getIndexPatternPickerList(instance: ShallowWrapper) {
return instance.find(EuiSelectable).first();
}

function getIndexPatternPickerOptions(instance: ShallowWrapper) {
return getIndexPatternPickerList(instance).prop('options');
}

export function selectIndexPatternPickerOption(instance: ShallowWrapper, selectedLabel: string) {
const options: Array<{ label: string; checked?: 'on' | 'off' }> = getIndexPatternPickerOptions(
instance
).map((option: { label: string }) =>
option.label === selectedLabel
? { ...option, checked: 'on' }
: { ...option, checked: undefined }
);
return getIndexPatternPickerList(instance).prop('onChange')!(options);
}

describe('ChangeIndexPattern', () => {
test('switching index pattern to the same index pattern does not trigger onChangeIndexPattern', async () => {
const props = getProps();
const comp = shallowWithIntl(<ChangeIndexPattern {...props} />);
await act(async () => {
selectIndexPatternPickerOption(comp, indexPatternMock.title);
});
expect(props.onChangeIndexPattern).toHaveBeenCalledTimes(0);
});
test('switching index pattern to a different index pattern triggers onChangeIndexPattern', async () => {
const props = getProps();
const comp = shallowWithIntl(<ChangeIndexPattern {...props} />);
await act(async () => {
selectIndexPatternPickerOption(comp, indexPatternWithTimefieldMock.title);
});
expect(props.onChangeIndexPattern).toHaveBeenCalledTimes(1);
expect(props.onChangeIndexPattern).toHaveBeenCalledWith(indexPatternWithTimefieldMock.id);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ export type ChangeIndexPatternTriggerProps = EuiButtonProps & {
// TODO: refactor to shared component with ../../../../../../../../x-pack/legacy/plugins/lens/public/indexpattern_plugin/change_indexpattern

export function ChangeIndexPattern({
indexPatternRefs,
indexPatternId,
indexPatternRefs,
onChangeIndexPattern,
trigger,
selectableProps,
trigger,
}: {
trigger: ChangeIndexPatternTriggerProps;
indexPatternId?: string;
indexPatternRefs: IndexPatternRef[];
onChangeIndexPattern: (newId: string) => void;
indexPatternId?: string;
selectableProps?: EuiSelectableProps<{ value: string }>;
trigger: ChangeIndexPatternTriggerProps;
}) {
const [isPopoverOpen, setPopoverIsOpen] = useState(false);

Expand Down Expand Up @@ -86,7 +86,9 @@ export function ChangeIndexPattern({
const choice = (choices.find(({ checked }) => checked) as unknown) as {
value: string;
};
onChangeIndexPattern(choice.value);
if (choice.value !== indexPatternId) {
onChangeIndexPattern(choice.value);
}
setPopoverIsOpen(false);
}}
searchProps={{
Expand Down

0 comments on commit b859c36

Please sign in to comment.