1
+ import '@testing-library/jest-dom' ;
2
+ import React , { act } from 'react' ;
3
+ import { fireEvent , render , screen } from '@testing-library/react' ;
4
+ import MyForm from "./MyForm" ;
5
+
6
+ test ( 'Form set/get Value Test' , ( ) => {
7
+ const consoleSpy = jest . spyOn ( console , 'log' ) . mockImplementation ( ) ;
8
+
9
+ render ( < MyForm /> ) ;
10
+
11
+ const setValue = screen . getByRole ( 'button' , { name : 'setValue' } ) ;
12
+ expect ( setValue ) . toBeInTheDocument ( ) ;
13
+
14
+ act ( ( ) => {
15
+ fireEvent . click ( setValue ) ;
16
+ } ) ;
17
+
18
+ const getValue = screen . getByRole ( 'button' , { name : 'getValue' } ) ;
19
+ expect ( getValue ) . toBeInTheDocument ( ) ;
20
+
21
+ act ( ( ) => {
22
+ fireEvent . click ( getValue ) ;
23
+ } ) ;
24
+
25
+ expect ( consoleSpy ) . toHaveBeenCalledWith ( 'test' ) ;
26
+ consoleSpy . mockRestore ( ) ;
27
+ } ) ;
28
+
29
+ test ( 'Form validate Test 1 ' , async ( ) => {
30
+ const consoleSpy = jest . spyOn ( console , 'log' ) . mockImplementation ( ) ;
31
+
32
+ render ( < MyForm /> ) ;
33
+
34
+ const setValue = screen . getByRole ( 'button' , { name : 'setValue' } ) ;
35
+ expect ( setValue ) . toBeInTheDocument ( ) ;
36
+
37
+ await act ( async ( ) => {
38
+ fireEvent . click ( setValue ) ;
39
+ } ) ;
40
+
41
+ const validate = screen . getByRole ( 'button' , { name : 'validate' } ) ;
42
+ expect ( validate ) . toBeInTheDocument ( ) ;
43
+
44
+ await act ( async ( ) => {
45
+ fireEvent . click ( validate ) ;
46
+ } ) ;
47
+
48
+ expect ( consoleSpy ) . toHaveBeenCalledWith ( "true" ) ;
49
+ consoleSpy . mockRestore ( ) ;
50
+ } ) ;
51
+
52
+ test ( 'Form validate Test 2 ' , async ( ) => {
53
+ const consoleSpy = jest . spyOn ( console , 'log' ) . mockImplementation ( ) ;
54
+
55
+ render ( < MyForm /> ) ;
56
+
57
+ const validate = screen . getByRole ( 'button' , { name : 'validate' } ) ;
58
+ expect ( validate ) . toBeInTheDocument ( ) ;
59
+
60
+ await act ( async ( ) => {
61
+ fireEvent . click ( validate ) ;
62
+ } ) ;
63
+
64
+ expect ( consoleSpy ) . toHaveBeenCalledWith ( "false" ) ;
65
+ consoleSpy . mockRestore ( ) ;
66
+ } ) ;
67
+
68
+
69
+ test ( 'Form getFieldProps Test' , async ( ) => {
70
+
71
+ render ( < MyForm /> ) ;
72
+
73
+ const getTest = screen . getByRole ( 'button' , { name : 'getTest' } ) ;
74
+ expect ( getTest ) . toBeInTheDocument ( ) ;
75
+
76
+ await act ( async ( ) => {
77
+ fireEvent . click ( getTest ) ;
78
+ } ) ;
79
+
80
+ } ) ;
81
+
82
+
83
+ test ( 'Form submit Test' , async ( ) => {
84
+
85
+ render ( < MyForm /> ) ;
86
+
87
+ const submit = screen . getByRole ( 'button' , { name : 'submit' } ) ;
88
+ expect ( submit ) . toBeInTheDocument ( ) ;
89
+
90
+ await act ( async ( ) => {
91
+ fireEvent . click ( submit ) ;
92
+ } ) ;
93
+
94
+ } ) ;
0 commit comments