diff --git a/src/plugins/discover/public/application/apps/main/components/sidebar/change_indexpattern.test.tsx b/src/plugins/discover/public/application/apps/main/components/sidebar/change_indexpattern.test.tsx
new file mode 100644
index 0000000000000..8c32942740a76
--- /dev/null
+++ b/src/plugins/discover/public/application/apps/main/components/sidebar/change_indexpattern.test.tsx
@@ -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();
+ 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();
+ await act(async () => {
+ selectIndexPatternPickerOption(comp, indexPatternWithTimefieldMock.title);
+ });
+ expect(props.onChangeIndexPattern).toHaveBeenCalledTimes(1);
+ expect(props.onChangeIndexPattern).toHaveBeenCalledWith(indexPatternWithTimefieldMock.id);
+ });
+});
diff --git a/src/plugins/discover/public/application/apps/main/components/sidebar/change_indexpattern.tsx b/src/plugins/discover/public/application/apps/main/components/sidebar/change_indexpattern.tsx
index d5076e4daa990..5f2f35e2419dd 100644
--- a/src/plugins/discover/public/application/apps/main/components/sidebar/change_indexpattern.tsx
+++ b/src/plugins/discover/public/application/apps/main/components/sidebar/change_indexpattern.tsx
@@ -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);
@@ -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={{