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

Test/knowledge base #593

Merged
merged 2 commits into from
Feb 18, 2025
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
6 changes: 6 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ module.exports = {
'@uiw/react-md-editor':
'<rootDir>/packages/shared/lib/testUtil/mockEditor.jsx',
'@actiontech/(.*)': '<rootDir>/packages/$1',
'@react-sigma/core(.*)$':
'<rootDir>/packages/shared/lib/testUtil/mockSigmaCore.tsx',
'@react-sigma/graph-search$':
'<rootDir>/packages/shared/lib/testUtil/mockSigmaGraphSearch.tsx',
'@sigma/node-image$':
'<rootDir>/packages/shared/lib/testUtil/mockSigmaNodeImage.tsx',
...pathsToModuleNameMapper(compilerOptions.paths)
},
collectCoverageFrom: [
Expand Down
43 changes: 43 additions & 0 deletions packages/shared/lib/testUtil/mockSigmaCore.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const mockSigmaGraph = {
addNode: jest.fn(),
addEdge: jest.fn(),
clear: jest.fn(),
dropNode: jest.fn(),
dropEdge: jest.fn(),
getNodeAttributes: jest.fn(),
getEdgeAttributes: jest.fn(),
forEachNode: jest.fn(),
forEachEdge: jest.fn()
};

const mockSigma = {
getGraph: jest.fn(() => mockSigmaGraph),
getCamera: jest.fn(() => ({
animate: jest.fn(),
goTo: jest.fn(),
reset: jest.fn()
})),
refresh: jest.fn(),
kill: jest.fn()
};

export const SigmaContainer = jest.fn(({ children }) => (
<div data-testid="sigma-container">{children}</div>
));

export const ControlsContainer = jest.fn(({ children }) => (
<div data-testid="controls-container">{children}</div>
));

export const FullScreenControl = jest.fn(({ children }) => (
<div data-testid="full-screen-container">{children}</div>
));

export const ZoomControl = jest.fn(({ children }) => (
<div data-testid="zoom-container">{children}</div>
));

export const useLoadGraph = jest.fn();
export const useRegisterEvents = jest.fn();
export const useSigma = jest.fn(() => mockSigma);
export const useSetSettings = jest.fn();
3 changes: 3 additions & 0 deletions packages/shared/lib/testUtil/mockSigmaGraphSearch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const GraphSearch = jest.fn(({ children }) => (
<div data-testid="sigma-graph-search">{children}</div>
));
1 change: 1 addition & 0 deletions packages/shared/lib/testUtil/mockSigmaNodeImage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const NodeImageProgram = () => null;
7 changes: 7 additions & 0 deletions packages/shared/lib/utils/Common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AxiosResponse } from 'axios';
import i18n from 'i18next';
import { MIMETypeEnum, ResponseBlobJsonType } from '../enum';
import dayjs, { Dayjs } from 'dayjs';
import queryString from 'query-string';

export const emailValidate = (email: string): boolean => {
if (!email || typeof email !== 'string') {
Expand Down Expand Up @@ -190,3 +191,9 @@ export const getCookie = (name: string): string => {
}
return '';
};

export const paramsSerializer = <T extends Record<string, any>>(query: T) => {
return queryString.stringify(query, {
arrayFormat: 'none'
});
};
8 changes: 7 additions & 1 deletion packages/shared/lib/utils/__tests__/Common.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
getFileFromUploadChangeEvent,
jsonParse,
translateTimeForRequest,
findDuplicateKeys
findDuplicateKeys,
paramsSerializer
} from '../Common';
import { act } from '@testing-library/react';
import 'blob-polyfill';
Expand Down Expand Up @@ -332,4 +333,9 @@ describe('utils/Common', () => {
expect(result).toEqual([]);
});
});

it('should stringify params', () => {
expect(paramsSerializer({ a: 1, b: 2 })).toBe('a=1&b=2');
expect(paramsSerializer({ a: 1, b: [2, 3, 4] })).toBe('a=1&b=2&b=3&b=4');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { getGlobalWorkflowsV1FilterStatusListEnum } from '@actiontech/shared/lib
import { ListProjectProjectPriorityEnum } from '@actiontech/shared/lib/api/base/service/common.enum';
import eventEmitter from '../../../../../utils/EventEmitter';
import EmitterKey from '../../../../../data/EmitterKey';
import { paramsSerializer } from '../../../utils';
import { paramsSerializer } from '@actiontech/shared';

describe('sqle/GlobalDashboard/InitiatedWorkOrder', () => {
let getGlobalWorkflowsSpy: jest.SpyInstance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import { GlobalDashboardListProps } from '../../index.type';
import { GlobalDashboardPendingWorkflowListColumn } from '../PendingWorkOrder/column';
import { useCurrentUser } from '@actiontech/shared/lib/features';
import { paramsSerializer } from '../../utils';
import { paramsSerializer } from '@actiontech/shared';

const InitiatedWorkOrder: React.FC<GlobalDashboardListProps> = ({
filterValues,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { getGlobalWorkflowsV1FilterStatusListEnum } from '@actiontech/shared/lib
import { ListProjectProjectPriorityEnum } from '@actiontech/shared/lib/api/base/service/common.enum';
import eventEmitter from '../../../../../utils/EventEmitter';
import EmitterKey from '../../../../../data/EmitterKey';
import { paramsSerializer } from '../../../utils';
import { paramsSerializer } from '@actiontech/shared';

describe('sqle/GlobalDashboard/PendingWorkOrder', () => {
let getGlobalWorkflowsSpy: jest.SpyInstance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from '@actiontech/shared/lib/api/sqle/service/workflow/index.enum';
import { GlobalDashboardListProps } from '../../index.type';
import { GlobalDashboardPendingWorkflowListColumn } from './column';
import { paramsSerializer } from '../../utils';
import { paramsSerializer } from '@actiontech/shared';

const PendingWorkOrder: React.FC<GlobalDashboardListProps> = ({
filterValues,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from '@actiontech/shared/lib/api/sqle/service/workflow/index.enum';
import { useRequest } from 'ahooks';
import { ResponseCode } from '@actiontech/shared/lib/enum';
import { paramsSerializer } from '../utils';
import { paramsSerializer } from '@actiontech/shared';
import { useBoolean } from 'ahooks';

const useDashboardFilter = () => {
Expand Down
7 changes: 0 additions & 7 deletions packages/sqle/src/page/GlobalDashboard/utils.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { act, cleanup, screen, fireEvent } from '@testing-library/react';
import TagSelectorBar from '../TagSelectorBar';
import { superRender } from '../../../../../testUtils/customRender';
import knowledgeBase from '../../../../../testUtils/mockApi/knowledgeBase';
import { mockKnowledgeBaseTagListData } from '../../../../../testUtils/mockApi/knowledgeBase/data';
import {
getAllBySelector,
getBySelector
} from '@actiontech/shared/lib/testUtil/customQuery';

describe('TagSelectorBar', () => {
let getKnowledgeBaseTagList: jest.SpyInstance;

beforeEach(() => {
jest.useFakeTimers();
getKnowledgeBaseTagList = knowledgeBase.getKnowledgeBaseTagList();
});

afterEach(() => {
jest.useRealTimers();
cleanup();
});

it('render init snap', () => {
const { container } = superRender(
<TagSelectorBar value={['RAND']} onChange={jest.fn()} />
);
expect(container).toMatchSnapshot();
expect(getKnowledgeBaseTagList).toHaveBeenCalled();
});

it('render select tag', async () => {
const onChangeSpy = jest.fn();
const { baseElement } = superRender(
<TagSelectorBar value={[]} onChange={onChangeSpy} />
);
expect(getKnowledgeBaseTagList).toHaveBeenCalledTimes(1);
await act(async () => jest.advanceTimersByTime(3000));
fireEvent.click(screen.getByText('已选择标签(0)'));
await act(async () => jest.advanceTimersByTime(0));
expect(baseElement).toMatchSnapshot();

fireEvent.change(getBySelector('.ant-input'), {
target: {
value: 'rand'
}
});
await act(async () => jest.advanceTimersByTime(0));
expect(screen.getByText('RAND')).toBeInTheDocument();
expect(getAllBySelector('.hidden-checkbox')).toHaveLength(
mockKnowledgeBaseTagListData.length - 1
);
fireEvent.click(getAllBySelector('.ant-checkbox-input')[0]);
await act(async () => jest.advanceTimersByTime(0));
expect(onChangeSpy).toHaveBeenCalledTimes(1);
expect(onChangeSpy).toHaveBeenCalledWith(['RAND']);
});

it('render delete tag', async () => {
const onChangeSpy = jest.fn();
superRender(<TagSelectorBar value={['RAND']} onChange={onChangeSpy} />);
expect(getKnowledgeBaseTagList).toHaveBeenCalledTimes(1);
await act(async () => jest.advanceTimersByTime(3000));
expect(screen.getByText('已选择标签(1)')).toBeInTheDocument();
expect(screen.getByText('RAND')).toBeInTheDocument();

fireEvent.click(getBySelector('.anticon-close'));
await act(async () => jest.advanceTimersByTime(0));
expect(onChangeSpy).toHaveBeenCalledTimes(1);
expect(onChangeSpy).toHaveBeenCalledWith([]);
});
});
Loading
Loading