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

[Multiple Datasource Test] Add test for error_menu, item, data_source_multi_selectable #6752

Merged
merged 4 commits into from
May 10, 2024
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
2 changes: 2 additions & 0 deletions changelogs/fragments/6752.yml
Original file line number Diff line number Diff line change
@@ -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))

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import React from 'react';
import { shallow } from 'enzyme';
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(<DataSourceErrorMenu application={applicationMock} />);
yujin-emma marked this conversation as resolved.
Show resolved Hide resolved
expect(component).toMatchSnapshot();
});

it('should toggle popover when icon button is clicked', () => {
const container = render(<DataSourceErrorMenu application={applicationMock} />);
const iconButton = container.getByTestId('dataSourceErrorMenuHeaderLink');
iconButton.click();
expect(container.getByTestId('dataSourceErrorPopover')).toBeVisible();
yujin-emma marked this conversation as resolved.
Show resolved Hide resolved
expect(container.getByTestId('datasourceTableEmptyState')).toHaveTextContent(
'Failed to fetch data sources'
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<DataSourceItem className="test" option={item} defaultDataSource={defaultDataSource} />
);

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');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 () => {
Expand All @@ -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(
<DataSourceMultiSelectable
Expand All @@ -129,7 +134,7 @@ describe('DataSourceMultiSelectable', () => {
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(
<DataSourceMultiSelectable
Expand All @@ -146,4 +151,38 @@ describe('DataSourceMultiSelectable', () => {
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(
<DataSourceMultiSelectable
savedObjectsClient={client}
notifications={toasts}
onSelectedDataSources={jest.fn()}
hideLocalCluster={true}
fullWidth={false}
uiSettings={uiSettings}
/>
);
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(
<DataSourceMultiSelectable
savedObjectsClient={client}
notifications={toasts}
onSelectedDataSources={jest.fn()}
hideLocalCluster={false}
fullWidth={false}
uiSettings={uiSettings}
/>
);
await wrapper.instance().componentDidMount!();
expect(wrapper.state('selectedOptions')).toHaveLength(1);
expect(wrapper.state('showEmptyState')).toBe(false);
});
});
Loading