1- import { render , screen } from '@testing-library/react' ;
1+ import { render , screen , fireEvent } from '@testing-library/react' ;
22import '@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
611test ( 'renders learn react link' , ( ) => {
7- render ( < MonorepoTemplate > learn react</ MonorepoTemplate > ) ;
12+ render ( < Signature > learn react</ Signature > ) ;
813 const linkElement = screen . getByText ( / l e a r n r e a c t / 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