Skip to content

Commit

Permalink
Merge pull request #209 from actiontech/tes/DMS-833
Browse files Browse the repository at this point in the history
[test]:(Data Export) Add unit tests for common
  • Loading branch information
anny1021 authored Feb 4, 2024
2 parents e242f9e + 57e6097 commit ff261b5
Show file tree
Hide file tree
Showing 22 changed files with 4,805 additions and 127 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import { act, cleanup, fireEvent, screen } from '@testing-library/react';
import AuditResultList from '.';
import { superRender } from '../../../../testUtils/customRender';
import dataExport from '../../../../testUtils/mockApi/dataExport';
import {
BatchGetDataExportTaskResponseData,
ListDataExportTaskSQLsResponseData
} from '../../../../testUtils/mockApi/dataExport/data';
import { createSpySuccessResponse } from '@actiontech/shared/lib/testUtil/mockApi';
import { GetDataExportTaskStatusEnum } from '@actiontech/shared/lib/api/base/service/common.enum';
import { getAllBySelector } from '@actiontech/shared/lib/testUtil/customQuery';

describe('test DataExport/Common/AuditResultList', () => {
let batchGetTaskSpy: jest.SpyInstance;
let getTaskSQLsSpy: jest.SpyInstance;
const taskIDs = ['1233234654', '324234234'];
const projectID = '400300';

beforeEach(() => {
jest.useFakeTimers();
batchGetTaskSpy = dataExport.BatchGetDataExportTask();
getTaskSQLsSpy = dataExport.ListDataExportTaskSQLs();
});
afterEach(() => {
jest.useRealTimers();
jest.clearAllMocks();
jest.clearAllTimers();
});

it('should match snapshot', async () => {
const { baseElement } = superRender(
<AuditResultList taskIDs={taskIDs} projectID={projectID} />
);
expect(baseElement).toMatchSnapshot();

expect(batchGetTaskSpy).toBeCalledTimes(1);
expect(batchGetTaskSpy).toBeCalledWith({
project_uid: projectID,
data_export_task_uids: '1233234654,324234234'
});

await act(async () => jest.advanceTimersByTime(3000));
expect(baseElement).toMatchSnapshot();

expect(getTaskSQLsSpy).toBeCalledTimes(1);
expect(getTaskSQLsSpy).toBeCalledWith({
project_uid: projectID,
data_export_task_uid: BatchGetDataExportTaskResponseData[0].task_uid,
page_index: 1,
page_size: 20
});

await act(async () => jest.advanceTimersByTime(3000));
expect(baseElement).toMatchSnapshot();

fireEvent.click(getAllBySelector('.audit-result-exec-sql-column')[0]);
expect(baseElement).toMatchSnapshot();
});

it('should switch tab when click label', async () => {
batchGetTaskSpy.mockImplementation(() =>
createSpySuccessResponse({
data: [
...BatchGetDataExportTaskResponseData,
{
task_uid: '1752623436938612735',
db_info: {
uid: '1752583372904861696',
name: 'test-mysql-1',
db_type: '',
database_name: ''
},
status: GetDataExportTaskStatusEnum.init,
file_name: '',
audit_result: {
audit_level: '',
score: 100,
pass_rate: 1
},
export_type: 'SQL',
export_file_type: 'CSV'
}
]
})
);

const { container } = superRender(
<AuditResultList taskIDs={taskIDs} projectID={projectID} />
);
await act(async () => jest.advanceTimersByTime(3000));

await act(async () => jest.advanceTimersByTime(3000));

fireEvent.click(screen.getByText('test-mysql-1'));
expect(getTaskSQLsSpy).toBeCalledTimes(2);
expect(getTaskSQLsSpy).toBeCalledWith({
project_uid: projectID,
data_export_task_uid: '1752623436938612735',
page_index: 1,
page_size: 20
});
await act(async () => jest.advanceTimersByTime(3000));
expect(container).toMatchSnapshot();
});

it('should execute updateExecuteSQLsTypeIsDQL', async () => {
const updateExecuteSQLsTypeIsDQLSpy = jest.fn();
superRender(
<AuditResultList
taskIDs={taskIDs}
projectID={projectID}
updateExecuteSQLsTypeIsDQL={updateExecuteSQLsTypeIsDQLSpy}
/>
);
await act(async () => jest.advanceTimersByTime(3000));
await act(async () => jest.advanceTimersByTime(3000));

expect(updateExecuteSQLsTypeIsDQLSpy).toBeCalledTimes(1);
expect(updateExecuteSQLsTypeIsDQLSpy).toBeCalledWith(true);

jest.clearAllMocks();
cleanup();

getTaskSQLsSpy.mockImplementation(() =>
createSpySuccessResponse({
data: [
...ListDataExportTaskSQLsResponseData,
{
uid: 7,
sql: 'INSERT INTO t1 values (name, "test")',
export_status: '',
export_sql_type: 'dml',
audit_level: ''
}
]
})
);
superRender(
<AuditResultList
taskIDs={taskIDs}
projectID={projectID}
updateExecuteSQLsTypeIsDQL={updateExecuteSQLsTypeIsDQLSpy}
/>
);
await act(async () => jest.advanceTimersByTime(3000));
await act(async () => jest.advanceTimersByTime(3000));
expect(updateExecuteSQLsTypeIsDQLSpy).toBeCalledTimes(1);
expect(updateExecuteSQLsTypeIsDQLSpy).toBeCalledWith(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { useRequest } from 'ahooks';
import dms from '@actiontech/shared/lib/api/base/service/dms';
import { IGetDataExportTask } from '@actiontech/shared/lib/api/base/service/common';
import { ResponseCode } from '@actiontech/shared/lib/enum';
import DbServiceSegmentedLabel from '../DbServiceSegmentedLabel';
import { AuditTaskResV1AuditLevelEnum } from '@actiontech/shared/lib/api/sqle/service/common.enum';

const AuditResultList: React.FC<AuditResultListProps> = ({
taskIDs,
Expand Down Expand Up @@ -50,7 +52,15 @@ const AuditResultList: React.FC<AuditResultListProps> = ({
value={currentTaskID}
onChange={(v) => handleChangeCurrentTask(v as string)}
options={tasks.map((v) => ({
label: v.db_info?.name,
label: (
<DbServiceSegmentedLabel
dbServiceName={v.db_info?.name ?? ''}
auditLevel={
(v.audit_result
?.audit_level as AuditTaskResV1AuditLevelEnum) ?? ''
}
/>
),
value: !!v?.task_uid ? `${v.task_uid}` : '',
key: v.task_uid
}))}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`test BackToWorkflowList should match snapshot 1`] = `
<div>
<a
href="/project/700300/data/export"
>
<button
class="ant-btn css-dev-only-do-not-override-674gwq ant-btn-default basic-button-wrapper css-geipcv"
type="button"
>
<span
class="ant-btn-icon"
>
<svg
fill="none"
height="16"
viewBox="0 0 16 16"
width="16"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M5.21863 7.33333H13.3333V8.66667H5.21863L8.79463 12.2427L7.85196 13.1853L2.66663 8L7.85196 2.81467L8.79463 3.75733L5.21863 7.33333Z"
fill="currentColor"
/>
</svg>
</span>
<span>
返回工单列表
</span>
</button>
</a>
</div>
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { mockUseCurrentProject } from '@actiontech/shared/lib/testUtil/mockHook/mockUseCurrentProject';
import { superRender } from '../../../../testUtils/customRender';
import BackToWorkflowList from '.';

describe('test BackToWorkflowList', () => {
it('should match snapshot', () => {
mockUseCurrentProject();

const { container } = superRender(<BackToWorkflowList />);
expect(container).toMatchSnapshot();
});
});
Loading

0 comments on commit ff261b5

Please sign in to comment.