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

Adding tests for acceleration components #1495

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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

Large diffs are not rendered by default.

Large diffs are not rendered by default.

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

import { configure, mount } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import React from 'react';
import { dummyAccelerations } from '../../../../../../../test/datasources';
import { AccelerationDetailsFlyout } from '../acceleration_details_flyout';
import { cleanup, fireEvent, render } from '@testing-library/react';

describe('Acceleration Details Flyout test', () => {
configure({ adapter: new Adapter() });

afterEach(() => {
cleanup();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this clean up for?

});

it('Render acceleration detail flyout', () => {
const wrapper = mount(<AccelerationDetailsFlyout acceleration={dummyAccelerations[0]} />);

expect(wrapper).toMatchSnapshot();
});

it('Render acceleration detail flyout and click on schema tab', () => {
const utils = render(<AccelerationDetailsFlyout acceleration={dummyAccelerations[0]} />);

fireEvent.click(utils.getByTestId('accelerationDetailsFlyoutTab_schema'));
expect(utils.container.firstChild).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { configure, mount } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import { AccelerationTable } from '../acceleration_table';

import React from 'react';
import { dummyAccelerations } from '../../../../../../../test/datasources';
import * as Plugin from '../../../../../../plugin';

describe('Acceleration Table Test', () => {
configure({ adapter: new Adapter() });

Plugin.getRenderAccelerationDetailsFlyout = jest.fn();

it('Render empty acceleration table', () => {
const wrapper = mount(<AccelerationTable accelerations={[]} />);

expect(wrapper).toMatchSnapshot();
Copy link
Member

@ps48 ps48 Mar 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add more interaction tests for flyouts and table once we connect these with the backend. These should be good for now

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, you can mock like this:

jest.mock('../../../../plugin', () => ({
  getRenderAccelerationDetailsFlyout: jest.fn(() =>
    jest.fn().mockImplementation(() => console.log('Acceleration Details Flyout Rendered'))
  ),
  getRenderAssociatedObjectsDetailsFlyout: jest.fn(() =>
    jest.fn().mockImplementation(() => console.log('Associated Objects Details Flyout Rendered'))
  ),
}));

so that we can test the render:

 it('renders acceleration details correctly and triggers flyout on click', () => {
   const wrapper = mount(<AssociatedObjectsDetailsFlyout tableDetail={mockTableDetail} />);
   expect(wrapper.find('EuiInMemoryTable').at(0).find('EuiLink').length).toBeGreaterThan(0);

   wrapper.find('EuiInMemoryTable').at(0).find('EuiLink').first().simulate('click');
   expect(plugin.getRenderAccelerationDetailsFlyout).toHaveBeenCalled();
 });

Reference: https://github.com/opensearch-project/dashboards-observability/pull/1496/files#diff-9a67ab280ec2234577feebc0875415a357dcf4e0f6f4d264a96b18ea5264221eR32

});

it('Render acceleration table with dummy acceleration', () => {
const wrapper = mount(<AccelerationTable accelerations={dummyAccelerations} />);

expect(wrapper).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
} from '../accelerations/helpers/utils';

export interface AccelerationDetailsFlyoutProps {
acceleration: any;

Check warning on line 30 in public/components/datasources/components/manage/accelerations/acceleration_details_flyout.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
}

export const AccelerationDetailsFlyout = (props: AccelerationDetailsFlyoutProps) => {
const { acceleration } = props;
const [selectedTab, setSelectedTab] = useState('details');
const tabsMap: { [key: string]: any } = {

Check warning on line 36 in public/components/datasources/components/manage/accelerations/acceleration_details_flyout.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
details: AccelerationDetailsTab,
schema: AccelerationSchemaTab,
sql_definition: AccelerationSqlTab,
Expand All @@ -42,23 +42,32 @@
const DiscoverButton = () => {
// TODO: display button if can be sent to discover
return (
<EuiButtonEmpty onClick={onDiscoverButtonClick}>
<EuiButtonEmpty
onClick={onDiscoverButtonClick}
data-test-subj="accelerationDetailsFlyoutDiscoverButton"
>
<EuiIcon type={'discoverApp'} size="m" />
</EuiButtonEmpty>
);
};

const RefreshButton = () => {
return (
<EuiButtonEmpty onClick={onRefreshButtonClick}>
<EuiButtonEmpty
onClick={onRefreshButtonClick}
data-test-subj="accelerationDetailsFlyoutRefreshButton"
>
<EuiIcon type={getRefreshButtonIcon()} size="m" />
</EuiButtonEmpty>
);
};

const DeleteButton = () => {
return (
<EuiButtonEmpty onClick={onDeleteButtonClick}>
<EuiButtonEmpty
onClick={onDeleteButtonClick}
data-test-subj="accelerationDetailsFlyoutDeleteButton"
>
<EuiIcon type="trash" size="m" />
</EuiButtonEmpty>
);
Expand Down Expand Up @@ -90,6 +99,7 @@
isSelected={tab.id === selectedTab}
disabled={tab.disabled}
key={index}
data-test-subj={'accelerationDetailsFlyoutTab_' + tab.id}
>
{tab.name}
</EuiTab>
Expand All @@ -97,7 +107,7 @@
});
};

const renderTabContent = (tab: string, tabAcceleration: any) => {

Check warning on line 110 in public/components/datasources/components/manage/accelerations/acceleration_details_flyout.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
const TabToDisplay = tabsMap[tab];
return <TabToDisplay acceleration={tabAcceleration} />;
};
Expand All @@ -107,7 +117,7 @@
<EuiFlyoutHeader hasBorder>
<EuiFlexGroup direction="row" alignItems="center" gutterSize="m">
<EuiFlexItem>
<EuiText>
<EuiText data-test-subj="accelerationDetailsFlyoutTitle">
<h2 className="panel-title">{acceleration.name}</h2>
</EuiText>
</EuiFlexItem>
Expand All @@ -122,7 +132,12 @@
</EuiFlexItem>
</EuiFlexGroup>
<EuiSpacer size="m" />
<EuiTabs style={{ marginBottom: '-25px' }}>{renderTabs()}</EuiTabs>
<EuiTabs
style={{ marginBottom: '-25px' }}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should avoid this inline style, and we can switch to a SCSS to define this. But yeah, this is not related to this PR, and we can do that as a follow up.

data-test-subj="accelerationDetailsTabFlyoutTabs"
>
{renderTabs()}
</EuiTabs>
</EuiFlyoutHeader>
<EuiFlyoutBody>{renderTabContent(selectedTab, acceleration)}</EuiFlyoutBody>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

interface AccelerationTableTabProps {
// TODO: Add acceleration type to plugin types
accelerations: any[];

Check warning on line 30 in public/components/datasources/components/manage/accelerations/acceleration_table.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
}

export const AccelerationTable = (props: AccelerationTableTabProps) => {
Expand All @@ -47,7 +47,11 @@
// over from dashboards-query-workbench/public/components/acceleration/create/create_accelerations.tsx
return (
<>
<EuiButton onClick={() => console.log('clicked on create accelerations button')} fill>
<EuiButton
data-test-subj="createAccelerationButton"
onClick={() => console.log('clicked on create accelerations button')}

Check warning on line 52 in public/components/datasources/components/manage/accelerations/acceleration_table.tsx

View check run for this annotation

Codecov / codecov/patch

public/components/datasources/components/manage/accelerations/acceleration_table.tsx#L52

Added line #L52 was not covered by tests
fill
>
Create acceleration
</EuiButton>
</>
Expand All @@ -59,7 +63,7 @@
<>
<EuiFlexGroup direction="row" alignItems="center">
<EuiFlexItem>
<EuiText>
<EuiText data-test-subj="accelerationTableHeaderTitle">
<h3 className="panel-title">Accelerations</h3>
<p>
Accelerations optimize query performance by indexing external data into OpenSearch.
Expand Down Expand Up @@ -100,7 +104,7 @@
},
];

const accelerationTableColumns: Array<EuiBasicTableColumn<any>> = [

Check warning on line 107 in public/components/datasources/components/manage/accelerations/acceleration_table.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
// TODO: fields should be determined by what the acceleration is
// Show N/A if not applicable
{
Expand Down Expand Up @@ -164,7 +168,12 @@
name: 'Destination Index',
sortable: true,
render: (destination: string) => (
<EuiLink onClick={() => console.log('clicked on', destination)}>{destination}</EuiLink>
<EuiLink
onClick={() => console.log('clicked on', destination)}

Check warning on line 172 in public/components/datasources/components/manage/accelerations/acceleration_table.tsx

View check run for this annotation

Codecov / codecov/patch

public/components/datasources/components/manage/accelerations/acceleration_table.tsx#L172

Added line #L172 was not covered by tests
data-test-subj="accelerationTableDiscoverButton"
>
{destination}
</EuiLink>
),
},
{
Expand Down Expand Up @@ -200,6 +209,7 @@
columns={accelerationTableColumns}
pagination={pagination}
sorting={sorting}
data-test-subj="accelerationTable"
/>
</EuiPanel>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import { AssociatedObjectsTab } from './associated_objects/associated_objects_tab';
import { AccelerationTable } from './accelerations/acceleration_table';
import { mockAssociatedObjects } from './associated_objects/utils/associated_objects_tab_utils';
import { dummyAccelerations } from '../../../../../test/datasources';

interface DatasourceDetails {
allowedRoles: string[];
Expand All @@ -52,7 +53,7 @@
'prometheus.uri': string;
}

export const DataConnection = (props: any) => {

Check warning on line 56 in public/components/datasources/components/manage/data_connection.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
const { dataSource } = props;
const [datasourceDetails, setDatasourceDetails] = useState<DatasourceDetails>({
allowedRoles: [],
Expand All @@ -64,24 +65,6 @@
const [hasAccess, setHasAccess] = useState(true);
const { http, chrome, application } = coreRefs;

// Dummy accelerations variables for mock purposes
// Actual accelerations should be retrieved from the backend
const sampleSql = 'select * from `httplogs`.`default`.`table2` limit 10';
const dummyAccelerations = [
{
name: 'dummy_acceleration_1',
status: 'ACTIVE',
type: 'skip',
database: 'default',
table: 'table1',
destination: 'N/A',
dateCreated: 1709339290,
dateUpdated: 1709339290,
index: 'security_logs_2022',
sql: sampleSql,
},
];

const DefaultDatasourceCards = () => {
return (
<EuiFlexGroup>
Expand Down Expand Up @@ -157,7 +140,7 @@
.catch((_err) => {
setHasAccess(false);
});
}, [chrome, http]);

Check warning on line 143 in public/components/datasources/components/manage/data_connection.tsx

View workflow job for this annotation

GitHub Actions / Lint

React Hook useEffect has a missing dependency: 'dataSource'. Either include it or remove the dependency array

const tabs = [
{
Expand Down
17 changes: 17 additions & 0 deletions test/datasources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -806,3 +806,20 @@ export const mockRoleData = {
},
},
};

export const sampleSql = 'select * from `httplogs`.`default`.`table2` limit 10';

export const dummyAccelerations = [
{
name: 'dummy_acceleration_1',
status: 'ACTIVE',
type: 'skip',
database: 'default',
table: 'table1',
destination: 'N/A',
dateCreated: 1709339290,
dateUpdated: 1709339290,
index: 'security_logs_2022',
sql: sampleSql,
},
];
Loading