Skip to content

Commit c68bc8a

Browse files
committed
test: add test case.
1 parent c678db9 commit c68bc8a

File tree

3 files changed

+135
-4
lines changed

3 files changed

+135
-4
lines changed

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
]
2626
},
2727
"jest": {
28+
"setupFiles": ["jest-canvas-mock"],
2829
"collectCoverageFrom": [
2930
"<rootDir>/core/src/**/*.{js,jsx,ts,tsx}"
3031
],
@@ -45,11 +46,16 @@
4546
"devDependencies": {
4647
"@kkt/less-modules": "^7.5.4",
4748
"@kkt/ncc": "^1.1.1",
49+
"@types/react-test-renderer": "^18.0.0",
4850
"compile-less-cli": "^1.8.13",
4951
"husky": "^8.0.1",
5052
"lerna": "^8.0.0",
5153
"lint-staged": "^15.0.0",
5254
"prettier": "^3.0.1",
55+
"react-test-renderer": "^18.2.0",
5356
"tsbb": "^4.1.14"
57+
},
58+
"dependencies": {
59+
"jest-canvas-mock": "^2.5.2"
5460
}
5561
}

test/canvas.test.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { render, screen, fireEvent } from '@testing-library/react';
2+
import '@testing-library/jest-dom';
3+
import React, { useRef } from 'react';
4+
import Signature from '../core/src/canvas';
5+
6+
it('renders <Signature /> empty path test case', () => {
7+
render(
8+
<Signature width="450" height="230" data-testid="keyname" />
9+
);
10+
const svg = screen.getByTestId('keyname');
11+
expect(svg.tagName).toEqual('CANVAS');
12+
expect(svg.style).toHaveProperty('position', 'relative');
13+
expect(svg.getAttribute('width')).toEqual('450');
14+
expect(svg.getAttribute('height')).toEqual('230');
15+
expect(svg.getAttribute('class')).toEqual('w-signature');
16+
17+
});

test/index.test.tsx

Lines changed: 112 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,118 @@
1-
import { render, screen } from '@testing-library/react';
1+
import { render, screen, fireEvent } from '@testing-library/react';
22
import '@testing-library/jest-dom';
3-
import React from 'react';
4-
import MonorepoTemplate from '../core/src';
3+
import React, { useRef } from 'react';
4+
import Signature, { SignatureRef } from '../core/src';
5+
6+
const points = {
7+
"path-1": [[83.52734375,63.6015625],[83.22265625,64.02734375],[81.86328125,66.0390625],[78.69140625,70.90625],[72.76171875,80.44140625],[67.01171875,91.421875],[64.5390625,98.19921875],[63.83203125,101.25390625],[63.640625,102.5078125],[63.62109375,102.7109375],[63.96484375,102.22265625],[64.890625,100.87890625],[66.3671875,98.515625]],
8+
"path-2": [[116.5546875,65.8359375],[117.3125,65.8359375],[119.23046875,65.90625],[122.078125,66.39453125],[125.44140625,67.51171875],[128.33203125,69.2421875],[130.6484375,71.53515625],[131.94140625,73.6796875],[132.28125,75.65234375],[132.0625,77.5],[130.33203125,79.78125],[126.4921875,83.24609375],[120.9375,87.5234375],[114.859375,91.13671875],[108.09765625,93.66796875],[101.8359375,94.7734375],[96.26953125,94.7734375],[92.23828125,94.90625],[89.94921875,94.96484375],[88.234375,95.04296875],[88.03515625,95.08984375],[89.6015625,95.4296875],[94.75,96.640625],[107.55859375,98.640625],[123.6171875,100.09375],[135.5546875,100.734375],[141.140625,101.03515625],[142.2578125,101.08984375]]
9+
}
510

611
test('renders learn react link', () => {
7-
render(<MonorepoTemplate>learn react</MonorepoTemplate>);
12+
render(<Signature>learn react</Signature>);
813
const linkElement = screen.getByText(/learn react/i);
914
expect(linkElement).toBeInTheDocument();
1015
});
16+
17+
it('renders <Signature /> defaultPoints props test case', () => {
18+
const { debug } = render(
19+
<Signature width="450" height="230" data-testid="keyname" defaultPoints={points} />
20+
);
21+
const copied = screen.getByTestId('keyname');
22+
expect(copied.tagName).toEqual('svg');
23+
expect(copied.style).toHaveProperty('height', '100%');
24+
expect(copied.style).toHaveProperty('width', '100%');
25+
expect(copied.style).toHaveProperty('position', 'relative');
26+
expect(copied.getAttribute('width')).toEqual('450');
27+
expect(copied.getAttribute('height')).toEqual('230');
28+
expect(copied.getAttribute('class')).toEqual('w-signature');
29+
30+
if (copied.firstChild) {
31+
const path = copied.firstChild as SVGPathElement;
32+
expect(path.tagName).toEqual('path');
33+
const lastChild = copied.lastChild as SVGPathElement;
34+
expect(lastChild.tagName).toEqual('path');
35+
}
36+
});
37+
38+
39+
it('renders <Signature /> empty path test case', () => {
40+
render(
41+
<Signature width="450" height="230" data-testid="keyname" />
42+
);
43+
const svg = screen.getByTestId('keyname');
44+
expect(svg.tagName).toEqual('svg');
45+
expect(svg.style).toHaveProperty('height', '100%');
46+
expect(svg.style).toHaveProperty('width', '100%');
47+
expect(svg.style).toHaveProperty('position', 'relative');
48+
expect(svg.getAttribute('width')).toEqual('450');
49+
expect(svg.getAttribute('height')).toEqual('230');
50+
expect(svg.getAttribute('class')).toEqual('w-signature');
51+
52+
const path = svg.firstChild as SVGPathElement;
53+
expect(path).toBeNull();
54+
});
55+
56+
57+
it('renders <Signature /> clear test case', () => {
58+
function App() {
59+
const $svg = useRef<SignatureRef>(null);
60+
const handle = (evn: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
61+
$svg.current?.clear()
62+
};
63+
return (
64+
<>
65+
<Signature data-testid="signature" ref={$svg} defaultPoints={points} fill="red" />
66+
<button onClick={handle}>Clear</button>
67+
</>
68+
);
69+
}
70+
render(<App />);
71+
72+
const svg = screen.getByTestId("signature");
73+
const clearButton = screen.getByText("Clear");
74+
75+
// Ensure the Signature component is rendered
76+
expect(svg).toBeInTheDocument();
77+
78+
const path = svg.firstChild as SVGPathElement;
79+
expect(path).toBeInTheDocument();
80+
81+
// Simulate a click on the Clear button
82+
fireEvent.click(clearButton);
83+
const path2 = svg.firstChild as SVGPathElement;
84+
expect(path2).toBeNull();
85+
86+
});
87+
88+
89+
it('renders <Signature /> clear test case', () => {
90+
const { debug } = render(<Signature width="450" height="230" data-testid="signature" fill="red" />);
91+
92+
const svg = screen.getByTestId("signature");
93+
// Ensure the Signature component is rendered
94+
expect(svg).toBeInTheDocument();
95+
96+
// Create a mock event object
97+
const mockEvent = new MouseEvent('pointerup', {
98+
bubbles: true,
99+
cancelable: true,
100+
clientX: 100, // Set clientX to a desired value
101+
clientY: 200, // Set clientY to a desired value
102+
// Add other properties as needed
103+
});
104+
fireEvent(svg, mockEvent);
105+
106+
// Create a mock event object
107+
const moveMockEvent = new MouseEvent('pointermove', {
108+
bubbles: true,
109+
cancelable: true,
110+
clientX: 150, // Set clientX to a desired value
111+
clientY: 250, // Set clientY to a desired value
112+
// Add other properties as needed
113+
});
114+
115+
// Trigger a pointermove event with the mock event object
116+
fireEvent(svg, moveMockEvent);
117+
118+
});

0 commit comments

Comments
 (0)