Skip to content

Commit a22ed69

Browse files
committed
add jest
1 parent 3d1994c commit a22ed69

File tree

7 files changed

+158
-5
lines changed

7 files changed

+158
-5
lines changed

admin-ui/__mocks__/axios.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// __mocks__/axios.ts
2+
const mockAxios = {
3+
create: jest.fn(() => ({
4+
interceptors: {
5+
request: {
6+
use: jest.fn(),
7+
eject: jest.fn()
8+
},
9+
response: {
10+
use: jest.fn(),
11+
eject: jest.fn()
12+
}
13+
},
14+
get: jest.fn(),
15+
post: jest.fn(),
16+
put: jest.fn(),
17+
delete: jest.fn(),
18+
patch: jest.fn()
19+
})),
20+
interceptors: {
21+
request: {
22+
use: jest.fn(),
23+
eject: jest.fn()
24+
},
25+
response: {
26+
use: jest.fn(),
27+
eject: jest.fn()
28+
}
29+
},
30+
get: jest.fn(),
31+
post: jest.fn(),
32+
put: jest.fn(),
33+
delete: jest.fn(),
34+
patch: jest.fn()
35+
};
36+
37+
export default mockAxios;

admin-ui/__mocks__/monaco-editor.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
editor: {
3+
create: jest.fn(() => ({
4+
dispose: jest.fn(),
5+
getValue: jest.fn(() => ''),
6+
setValue: jest.fn(),
7+
})),
8+
},
9+
};

admin-ui/jest.config.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,38 @@ const config: Config = {
44
preset: 'ts-jest',
55
testEnvironment: 'jsdom',
66
transform: {
7-
'^.+\\.(ts|tsx)$': 'ts-jest',
8-
'^.+\\.(js|jsx)$': 'babel-jest',
7+
'^.+\\.(ts|tsx)$': ['ts-jest', {
8+
useESM: true,
9+
}],
10+
'^.+\\.(js|jsx|mjs)$': ['babel-jest', {
11+
presets: [
12+
['@babel/preset-env', {
13+
targets: {
14+
node: 'current',
15+
},
16+
}],
17+
'@babel/preset-react',
18+
'@babel/preset-typescript'
19+
],
20+
}],
921
},
1022
moduleNameMapper: {
23+
'^monaco-editor$': '<rootDir>/__mocks__/monaco-editor.js',
24+
"@logicflow": "<rootDir>/node_modules/@logicflow/core/dist/index.min.js",
1125
'\\.(css|less|scss|sass)$': 'identity-obj-proxy',
1226
'\\.(jpg|jpeg|png|gif|webp|svg)$': '<rootDir>/__mocks__/fileMock.js',
1327
'^@/(.*)$': '<rootDir>/src/$1'
1428
},
1529
setupFilesAfterEnv: ['<rootDir>/src/jest.setup.ts'],
16-
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$',
17-
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node']
30+
testMatch: [
31+
"**/__test__/**/*.[jt]s?(x)",
32+
"**/__tests__/**/*.[jt]s?(x)",
33+
"**/?(*.)+(spec|test).[jt]s?(x)"
34+
],
35+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
36+
transformIgnorePatterns: [
37+
'node_modules/(?!(lodash-es|@ant-design|@logicflow|other-esm-modules)/)'
38+
],
1839
};
1940

2041
export default config;

admin-ui/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"dev": "webpack serve --config webpack.config.dev.js --open",
3636
"build": "webpack --mode production --config webpack.config.prod.js",
3737
"test": "jest",
38-
"test:watch": "jest --watch"
38+
"test:watch": "jest --watchAll"
3939
},
4040
"eslintConfig": {
4141
"extends": [
@@ -55,12 +55,16 @@
5555
]
5656
},
5757
"devDependencies": {
58+
"@babel/preset-env": "^7.26.0",
59+
"@babel/preset-react": "^7.26.3",
60+
"@babel/preset-typescript": "^7.26.0",
5861
"@testing-library/dom": "^10.4.0",
5962
"@testing-library/jest-dom": "^6.6.3",
6063
"@testing-library/react": "^16.1.0",
6164
"@types/jest": "^29.5.14",
6265
"@types/lodash": "^4.17.7",
6366
"@types/lodash-es": "^4.17.12",
67+
"babel-jest": "^29.7.0",
6468
"clean-webpack-plugin": "^4.0.0",
6569
"copy-webpack-plugin": "^12.0.2",
6670
"css-loader": "^7.1.2",

admin-ui/src/jest.setup.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,42 @@
11
import '@testing-library/jest-dom';
2+
3+
4+
// src/jest.setup.ts
5+
import '@testing-library/jest-dom';
6+
7+
// Mock window.matchMedia
8+
Object.defineProperty(window, 'matchMedia', {
9+
writable: true,
10+
value: jest.fn().mockImplementation(query => ({
11+
matches: false,
12+
media: query,
13+
onchange: null,
14+
addListener: jest.fn(),
15+
removeListener: jest.fn(),
16+
addEventListener: jest.fn(),
17+
removeEventListener: jest.fn(),
18+
dispatchEvent: jest.fn(),
19+
})),
20+
});
21+
22+
// Mock ResizeObserver
23+
global.ResizeObserver = class ResizeObserver {
24+
observe() {}
25+
unobserve() {}
26+
disconnect() {}
27+
};
28+
29+
// Suppress findDOMNode warnings in test output
30+
const originalError = console.error;
31+
console.error = (...args) => {
32+
if (/Warning.*findDOMNode/.test(args[0])) {
33+
return;
34+
}
35+
originalError.call(console, ...args);
36+
};
37+
38+
// Mock getComputedStyle
39+
global.getComputedStyle = jest.fn().mockImplementation(element => ({
40+
getPropertyValue: jest.fn().mockReturnValue(''),
41+
// Add any other style properties your tests might need
42+
}));
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import React from 'react';
2+
import {render} from "@testing-library/react";
3+
import axios from 'axios';
4+
import FlowPage from "@/pages/flow/work";
5+
6+
jest.mock('axios');
7+
const mockedAxios = axios as jest.Mocked<typeof axios>;
8+
9+
// test description for the component
10+
describe('Welcome Component', () => {
11+
test('renders with default initial value', () => {
12+
13+
mockedAxios.get.mockImplementation((url) => {
14+
if (url === '/api/users/123') {
15+
return Promise.resolve({
16+
data: { name: 'John Doe', email: 'john@example.com' }
17+
});
18+
}
19+
if (url === '/api/users/123/posts') {
20+
return Promise.resolve({
21+
data: [{ id: 1, title: 'Post 1' }]
22+
});
23+
}
24+
if (url === '/api/users/123/followers') {
25+
return Promise.resolve({
26+
data: [{ id: 1, name: 'Follower 1' }]
27+
});
28+
}
29+
return Promise.reject(new Error('Not found'));
30+
});
31+
32+
// render the component
33+
const {getByTestId} =render(
34+
<FlowPage/>
35+
);
36+
37+
const flowTable = getByTestId("flow-table");
38+
expect(flowTable).toBeInTheDocument();
39+
});
40+
});

admin-ui/src/pages/flow/work/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ const FlowPage = () => {
171171
return (
172172
<PageContainer>
173173
<ProTable
174+
data-testid={"flow-table"}
174175
actionRef={actionRef}
175176
rowKey={"id"}
176177
columns={columns}

0 commit comments

Comments
 (0)