From fba39fa9dc8d75ea9b60a5deeff1c87454302b03 Mon Sep 17 00:00:00 2001 From: yujin-emma Date: Wed, 8 May 2024 22:50:39 +0000 Subject: [PATCH 1/3] add test for data_source_error_menu, data_source_item, data_source_multi_selectable Signed-off-by: yujin-emma --- .../data_source_error_menu.test.tsx.snap | 95 +++++++++++++++++++ .../data_source_error_menu.test.tsx | 27 ++++++ .../data_source_item.test.tsx | 17 ++++ .../data_source_multi_selectable.test.tsx | 47 ++++++++- 4 files changed, 182 insertions(+), 4 deletions(-) create mode 100644 src/plugins/data_source_management/public/components/data_source_error_menu/__snapshots__/data_source_error_menu.test.tsx.snap create mode 100644 src/plugins/data_source_management/public/components/data_source_error_menu/data_source_error_menu.test.tsx diff --git a/src/plugins/data_source_management/public/components/data_source_error_menu/__snapshots__/data_source_error_menu.test.tsx.snap b/src/plugins/data_source_management/public/components/data_source_error_menu/__snapshots__/data_source_error_menu.test.tsx.snap new file mode 100644 index 000000000000..f1f00f7c0ca8 --- /dev/null +++ b/src/plugins/data_source_management/public/components/data_source_error_menu/__snapshots__/data_source_error_menu.test.tsx.snap @@ -0,0 +1,95 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`DataSourceErrorMenu renders without crashing 1`] = ` + + + } + closePopover={[Function]} + data-test-subj="dataSourceErrorPopover" + display="inlineBlock" + hasArrow={true} + id="dataSourceErrorPopover" + isOpen={false} + ownFocus={true} + panelPaddingSize="none" + > + + + + Failed to fetch data sources + + + + + + + Refresh the page + + + + + + +`; diff --git a/src/plugins/data_source_management/public/components/data_source_error_menu/data_source_error_menu.test.tsx b/src/plugins/data_source_management/public/components/data_source_error_menu/data_source_error_menu.test.tsx new file mode 100644 index 000000000000..bdf8e3e744eb --- /dev/null +++ b/src/plugins/data_source_management/public/components/data_source_error_menu/data_source_error_menu.test.tsx @@ -0,0 +1,27 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import React from 'react'; +import { mount, shallow } from 'enzyme'; +import { ApplicationStart } from 'opensearch-dashboards/public'; +import { EuiButton, EuiButtonIcon, EuiPopover } from '@elastic/eui'; +import { DataSourceErrorMenu } from './data_source_error_menu'; +import { coreMock } from '../../../../../core/public/mocks'; +import { render } from '@testing-library/react'; + +describe('DataSourceErrorMenu', () => { + const applicationMock = coreMock.createStart().application; + it('renders without crashing', () => { + const component = shallow(); + expect(component).toMatchSnapshot(); + }); + + it('should toggle popover when icon button is clicked', () => { + const container = render(); + const iconButton = container.getByTestId('dataSourceErrorMenuHeaderLink'); + iconButton.click(); + expect(container.getByTestId('dataSourceErrorPopover')).toBeVisible(); + }); +}); diff --git a/src/plugins/data_source_management/public/components/data_source_item/data_source_item.test.tsx b/src/plugins/data_source_management/public/components/data_source_item/data_source_item.test.tsx index 8cb736f2fb61..49c6c34a84f6 100644 --- a/src/plugins/data_source_management/public/components/data_source_item/data_source_item.test.tsx +++ b/src/plugins/data_source_management/public/components/data_source_item/data_source_item.test.tsx @@ -43,4 +43,21 @@ describe('Test on ShowDataSourceOption', () => { expect(component.find(EuiFlexItem)).toHaveLength(2); expect(component.find(EuiBadge)).toHaveLength(1); }); + + it('should render the component with a default data source', () => { + const item: DataSourceOption = { + id: 'default data source', + label: 'DataSource 1', + visible: true, + }; + const defaultDataSource = 'default data source'; + const wrapper = shallow( + + ); + + expect(wrapper.find(EuiFlexGroup).exists()).toBe(true); + expect(wrapper.find(EuiFlexItem).exists()).toBe(true); + expect(wrapper.find(EuiBadge).exists()).toBe(true); + expect(wrapper.find(EuiBadge).prop('children')).toBe('Default'); + }); }); diff --git a/src/plugins/data_source_management/public/components/data_source_multi_selectable/data_source_multi_selectable.test.tsx b/src/plugins/data_source_management/public/components/data_source_multi_selectable/data_source_multi_selectable.test.tsx index 957080dbd1ab..7a9f1f96ed0c 100644 --- a/src/plugins/data_source_management/public/components/data_source_multi_selectable/data_source_multi_selectable.test.tsx +++ b/src/plugins/data_source_management/public/components/data_source_multi_selectable/data_source_multi_selectable.test.tsx @@ -4,13 +4,13 @@ */ import { SavedObjectsClientContract } from 'opensearch-dashboards/public'; -import { notificationServiceMock } from '../../../../../core/public/mocks'; +import { fatalErrorsServiceMock, notificationServiceMock } from '../../../../../core/public/mocks'; import { getDataSourcesWithFieldsResponse, mockResponseForSavedObjectsCalls, mockManagementPlugin, } from '../../mocks'; -import { ShallowWrapper, shallow } from 'enzyme'; +import { ShallowWrapper, mount, shallow } from 'enzyme'; import { DataSourceMultiSelectable } from './data_source_multi_selectable'; import React from 'react'; import { render, fireEvent, screen } from '@testing-library/react'; @@ -91,6 +91,11 @@ describe('DataSourceMultiSelectable', () => { ); await nextTick(); expect(toasts.add).toBeCalledTimes(1); + expect(toasts.add).toBeCalledWith({ + color: 'danger', + text: expect.any(Function), + title: 'Failed to fetch data sources', + }); }); it('should callback when onChange happens', async () => { @@ -111,7 +116,7 @@ describe('DataSourceMultiSelectable', () => { expect(callbackMock).toBeCalledWith([]); }); - it('should retrun correct state when ui Settings provided', async () => { + it('should return correct state when ui Settings provided', async () => { spyOn(uiSettings, 'get').and.returnValue('test1'); component = shallow( { expect(component.state('selectedOptions')).toHaveLength(3); }); - it('should retrun correct state when ui Settings provided and hide cluster is false', async () => { + it('should return correct state when ui Settings provided and hide cluster is false', async () => { spyOn(uiSettings, 'get').and.returnValue('test1'); component = shallow( { expect(component.state('defaultDataSource')).toEqual('test1'); expect(component.state('selectedOptions')).toHaveLength(4); }); + + it('should handle no available data source error when selected option is empty and hide localcluster', async () => { + mockResponseForSavedObjectsCalls(client, 'find', {}); + const wrapper = mount( + + ); + await wrapper.instance().componentDidMount!(); + expect(wrapper.state('selectedOptions')).toHaveLength(0); + expect(wrapper.state('showEmptyState')).toBe(true); + }); + + it('should not handle no available data source error when selected option is empty and not hide localcluster', async () => { + mockResponseForSavedObjectsCalls(client, 'find', {}); + const wrapper = mount( + + ); + await wrapper.instance().componentDidMount!(); + expect(wrapper.state('selectedOptions')).toHaveLength(1); + expect(wrapper.state('showEmptyState')).toBe(false); + }); }); From 96ac85003e77d48b323f0d4f57b78dfb802056a8 Mon Sep 17 00:00:00 2001 From: "opensearch-changeset-bot[bot]" <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com> Date: Wed, 8 May 2024 22:55:18 +0000 Subject: [PATCH 2/3] Changeset file for PR #6752 created/updated --- changelogs/fragments/6752.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelogs/fragments/6752.yml diff --git a/changelogs/fragments/6752.yml b/changelogs/fragments/6752.yml new file mode 100644 index 000000000000..f43473bea2b8 --- /dev/null +++ b/changelogs/fragments/6752.yml @@ -0,0 +1,2 @@ +fix: +- Add test for data_source_error_menu, data_source_item, data_source_multi_selectable ([#6752](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6752)) \ No newline at end of file From 714df6e8c35cb2555d2861ea9c7d67b2e0030130 Mon Sep 17 00:00:00 2001 From: yujin-emma Date: Fri, 10 May 2024 03:33:56 +0000 Subject: [PATCH 3/3] add content verify in test Signed-off-by: yujin-emma --- .../data_source_error_menu/data_source_error_menu.test.tsx | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/plugins/data_source_management/public/components/data_source_error_menu/data_source_error_menu.test.tsx b/src/plugins/data_source_management/public/components/data_source_error_menu/data_source_error_menu.test.tsx index bdf8e3e744eb..90a1ad1a4a0b 100644 --- a/src/plugins/data_source_management/public/components/data_source_error_menu/data_source_error_menu.test.tsx +++ b/src/plugins/data_source_management/public/components/data_source_error_menu/data_source_error_menu.test.tsx @@ -4,9 +4,7 @@ */ import React from 'react'; -import { mount, shallow } from 'enzyme'; -import { ApplicationStart } from 'opensearch-dashboards/public'; -import { EuiButton, EuiButtonIcon, EuiPopover } from '@elastic/eui'; +import { shallow } from 'enzyme'; import { DataSourceErrorMenu } from './data_source_error_menu'; import { coreMock } from '../../../../../core/public/mocks'; import { render } from '@testing-library/react'; @@ -23,5 +21,8 @@ describe('DataSourceErrorMenu', () => { const iconButton = container.getByTestId('dataSourceErrorMenuHeaderLink'); iconButton.click(); expect(container.getByTestId('dataSourceErrorPopover')).toBeVisible(); + expect(container.getByTestId('datasourceTableEmptyState')).toHaveTextContent( + 'Failed to fetch data sources' + ); }); });