Skip to content

Commit

Permalink
Add unit tests for AssociationDataSourceModal
Browse files Browse the repository at this point in the history
Signed-off-by: Lin Wang <[email protected]>
  • Loading branch information
wanglam committed Aug 29, 2024
1 parent 97a5c9f commit c32b960
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import React from 'react';
import { IntlProvider } from 'react-intl';

import { DataSourceConnectionType } from '../../../common/types';
import { coreMock } from '../../../../../core/public/mocks';
import * as utilsExports from '../../utils';

import { DataSourceModalOption, getUpdatedOptions } from './association_data_source_modal';
import {
getUpdatedOptions,
DataSourceModalOption,
AssociationDataSourceModal,
AssociationDataSourceModalProps,
} from './association_data_source_modal';

const mockPrevAllOptions = [
{
Expand Down Expand Up @@ -301,3 +312,149 @@ describe('AssociationDataSourceModal utils: getUpdatedOptions', () => {
]);
});
});

const setupAssociationDataSourceModal = ({
assignedConnections,
handleAssignDataSourceConnections,
}: Partial<AssociationDataSourceModalProps> = {}) => {
const coreServices = coreMock.createStart();
jest.spyOn(utilsExports, 'getDataSourcesList').mockResolvedValue([]);
jest.spyOn(utilsExports, 'fetchDataSourceConnections').mockResolvedValue([
{
id: 'ds1',
name: 'Data Source 1',
connectionType: DataSourceConnectionType.OpenSearchConnection,
type: 'OpenSearch',
relatedConnections: [
{
id: 'ds1-dqc1',
name: 'dqc1',
parentId: 'ds1',
connectionType: DataSourceConnectionType.DirectQueryConnection,
type: 'S3',
},
],
},
{
id: 'ds1-dqc1',
name: 'dqc1',
parentId: 'ds1',
connectionType: DataSourceConnectionType.DirectQueryConnection,
type: 'S3',
},
{
id: 'ds2',
name: 'Data Source 2',
connectionType: DataSourceConnectionType.OpenSearchConnection,
type: 'OpenSearch',
},
]);
render(
<IntlProvider locale="en">
<AssociationDataSourceModal
http={coreServices.http}
notifications={coreServices.notifications}
savedObjects={coreServices.savedObjects}
closeModal={jest.fn()}
assignedConnections={assignedConnections || []}
handleAssignDataSourceConnections={handleAssignDataSourceConnections || jest.fn()}
/>
</IntlProvider>
);
return {};
};

describe('AssociationDataSourceModal', () => {
const originalOffsetHeight = Object.getOwnPropertyDescriptor(
HTMLElement.prototype,
'offsetHeight'
);
const originalOffsetWidth = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'offsetWidth');
beforeEach(() => {
Object.defineProperty(HTMLElement.prototype, 'offsetHeight', {
configurable: true,
value: 600,
});
Object.defineProperty(HTMLElement.prototype, 'offsetWidth', {
configurable: true,
value: 600,
});
});

afterEach(() => {
Object.defineProperty(
HTMLElement.prototype,
'offsetHeight',
originalOffsetHeight as PropertyDescriptor
);
Object.defineProperty(
HTMLElement.prototype,
'offsetWidth',
originalOffsetWidth as PropertyDescriptor
);
});

it('should display data sources options by default', async () => {
setupAssociationDataSourceModal();
expect(screen.getByText('Associate OpenSearch connections')).toBeInTheDocument();
await waitFor(() => {
expect(screen.getByRole('option', { name: 'Data Source 1' })).toBeInTheDocument();
expect(screen.getByRole('option', { name: 'Data Source 2' })).toBeInTheDocument();
});
});

it('should hide associated data sources', async () => {
setupAssociationDataSourceModal({
assignedConnections: [
{
id: 'ds2',
name: 'Data Source 2',
connectionType: DataSourceConnectionType.OpenSearchConnection,
type: 'OpenSearch',
},
],
});
expect(screen.getByText('Associate OpenSearch connections')).toBeInTheDocument();
await waitFor(() => {
expect(screen.getByRole('option', { name: 'Data Source 1' })).toBeInTheDocument();
expect(screen.queryByRole('option', { name: 'Data Source 2' })).not.toBeInTheDocument();
});
});

it('should call handleAssignDataSourceConnections with data source and DQCs after data source assigned', async () => {
const handleAssignDataSourceConnectionsMock = jest.fn();
setupAssociationDataSourceModal({
handleAssignDataSourceConnections: handleAssignDataSourceConnectionsMock,
});

await waitFor(() => {
fireEvent.click(screen.getByRole('option', { name: 'Data Source 1' }));
fireEvent.click(screen.getByRole('button', { name: 'Associate data sources' }));
});

expect(handleAssignDataSourceConnectionsMock).toHaveBeenCalledWith([
{
id: 'ds1',
name: 'Data Source 1',
connectionType: DataSourceConnectionType.OpenSearchConnection,
type: 'OpenSearch',
relatedConnections: [
{
id: 'ds1-dqc1',
name: 'dqc1',
parentId: 'ds1',
connectionType: DataSourceConnectionType.DirectQueryConnection,
type: 'S3',
},
],
},
{
id: 'ds1-dqc1',
name: 'dqc1',
parentId: 'ds1',
connectionType: DataSourceConnectionType.DirectQueryConnection,
type: 'S3',
},
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,13 @@ export const getUpdatedOptions = ({
const tabOptions: EuiButtonGroupOptionProps[] = [
{
id: AssociationDataSourceModalTab.OpenSearchConnections,
label: i18n.translate('workspace.form.selectDataSource.subTitle', {
label: i18n.translate('workspace.form.dataSourceModal.tab.openSearchConnections', {
defaultMessage: 'OpenSearch connections',
}),
},
{
id: AssociationDataSourceModalTab.DirectQueryConnections,
label: i18n.translate('workspace.form.selectDataSource.subTitle', {
label: i18n.translate('workspace.form.dataSourceModal.tab.directQueryConnections', {
defaultMessage: 'Direct query connections',
}),
},
Expand All @@ -153,7 +153,7 @@ export interface AssociationDataSourceModalProps {
savedObjects: SavedObjectsStart;
assignedConnections: DataSourceConnection[];
closeModal: () => void;
handleAssignDataSourceConnections: (connections: DataSourceConnection[]) => Promise<void>;
handleAssignDataSourceConnections: (connections: DataSourceConnection[]) => void;
}

export const AssociationDataSourceModal = ({
Expand Down Expand Up @@ -229,10 +229,14 @@ export const AssociationDataSourceModal = ({
</EuiText>
<EuiSpacer />
<EuiButtonGroup
legend="Data source tab"
legend={i18n.translate('workspace.form.dataSourceModal.connectionTypeTab', {
defaultMessage: 'Connection type tab',
})}
options={tabOptions}
idSelected={currentTab}
onChange={(id) => setCurrentTab(id)}
onChange={(id) => {
setCurrentTab(id);
}}
buttonSize="compressed"
/>
<EuiSpacer size="s" />
Expand Down

0 comments on commit c32b960

Please sign in to comment.