Skip to content

Commit f7885f7

Browse files
committed
add jest
1 parent 857e2ad commit f7885f7

File tree

5 files changed

+72
-3
lines changed

5 files changed

+72
-3
lines changed

jest.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
preset: 'ts-jest',
3+
testEnvironment: 'jsdom',
4+
moduleNameMapper: {
5+
'\\.(css|less|scss|sass)$': 'identity-obj-proxy',
6+
},
7+
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
8+
};

jest.setup.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import '@testing-library/jest-dom';

jest/ComponentBus.test.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import '@testing-library/jest-dom';
2+
import * as React from 'react';
3+
import { render, screen, fireEvent } from '@testing-library/react';
4+
import { ComponentBus } from '../src';
5+
import MyButton, { MyButtonProps } from './components/MyButton';
6+
7+
test('ComponentBus Test', () => {
8+
const mockOnClick = jest.fn();
9+
ComponentBus.getInstance().registerComponent('Button', MyButton);
10+
const Button = ComponentBus.getInstance().getComponent('Button') as React.ComponentType<MyButtonProps>;
11+
expect(Button).toEqual(MyButton);
12+
render(<Button text="Click me" onClick={mockOnClick} />);
13+
14+
// 断言按钮文本
15+
const button = screen.getByRole('button', { name: 'my-button' });
16+
17+
// 断言按钮存在
18+
expect(button).toBeInTheDocument();
19+
20+
// 模拟点击按钮
21+
fireEvent.click(button);
22+
23+
// 断言点击事件被调用
24+
expect(mockOnClick).toHaveBeenCalledTimes(1);
25+
});

jest/components/MyButton/index.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import * as React from "react";
2+
3+
export interface MyButtonProps {
4+
onClick?: () => void;
5+
text?: string;
6+
}
7+
8+
const MyButton:React.FC<MyButtonProps> = (props)=>{
9+
return (
10+
<a
11+
href={"#"}
12+
role={'button'}
13+
aria-label={'my-button'}
14+
onClick={()=>{
15+
props.onClick && props.onClick();
16+
}}>{props.text}</a>
17+
)
18+
}
19+
20+
export default MyButton;

package.json

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
"name": "@codingapi/ui-framework",
33
"version": "0.0.12",
44
"description": "A UI Framework built with React and Typescript",
5-
"keywords": ["ui", "framework", "react", "typescript"],
5+
"keywords": [
6+
"ui",
7+
"framework",
8+
"react",
9+
"typescript"
10+
],
611
"homepage": "https://github.com/codingapi/ui-framework",
712
"bugs": {
813
"url": "https://github.com/codingapi/ui-framework/issues"
@@ -33,7 +38,9 @@
3338
"scripts": {
3439
"clean": "rm -rf dist",
3540
"build": "yarn clean && rollup -c --bundleConfigAsCjs",
36-
"push": "yarn build && yarn publish --access public"
41+
"push": "yarn build && yarn publish --access public",
42+
"test": "jest",
43+
"test:watch": "jest --watch"
3744
},
3845
"eslintConfig": {
3946
"extends": [
@@ -56,13 +63,21 @@
5663
"@rollup/plugin-alias": "^5.1.1",
5764
"@rollup/plugin-commonjs": "^25.0.7",
5865
"@rollup/plugin-node-resolve": "^15.2.3",
66+
"@testing-library/dom": "^10.4.0",
67+
"@testing-library/jest-dom": "^6.6.3",
68+
"@testing-library/react": "^16.3.0",
69+
"@types/jest": "^29.5.14",
5970
"@types/react": "^18.3.5",
6071
"@types/react-dom": "^18.3.0",
72+
"babel-jest": "^29.7.0",
73+
"jest": "^29.7.0",
74+
"jest-environment-jsdom": "^29.7.0",
6175
"postcss": "^8.4.32",
6276
"rollup": "^4.9.1",
6377
"rollup-plugin-delete": "^2.0.0",
6478
"rollup-plugin-peer-deps-external": "^2.2.4",
6579
"rollup-plugin-postcss": "^4.0.2",
66-
"rollup-plugin-typescript2": "^0.36.0"
80+
"rollup-plugin-typescript2": "^0.36.0",
81+
"ts-jest": "^29.3.2"
6782
}
6883
}

0 commit comments

Comments
 (0)