1
1
import React from 'react' ;
2
2
import { act } from 'react-dom/test-utils' ;
3
+ import { render , screen } from '@testing-library/react' ;
4
+ import userEvent from '@testing-library/user-event' ;
5
+
3
6
import { FormRenderer , componentTypes } from '@data-driven-forms/react-form-renderer' ;
4
7
import { mount } from 'enzyme' ;
5
8
@@ -17,8 +20,6 @@ describe('DualListSelect', () => {
17
20
18
21
const ALL_LEFT_POSITION = 3 ;
19
22
const ALL_RIGHT_POSITION = 0 ;
20
- const RIGHT_POSITION = 1 ;
21
- const LEFT_POSITION = 2 ;
22
23
23
24
beforeEach ( ( ) => {
24
25
onSubmit = jest . fn ( ) ;
@@ -78,128 +79,64 @@ describe('DualListSelect', () => {
78
79
} ) ;
79
80
80
81
it ( 'switch left option' , async ( ) => {
81
- const wrapper = mount ( < FormRenderer { ...initialProps } /> ) ;
82
+ render ( < FormRenderer { ...initialProps } /> ) ;
82
83
83
- wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
84
- expect ( onSubmit ) . toHaveBeenCalledWith ( { } ) ;
85
- onSubmit . mockClear ( ) ;
86
-
87
- await act ( async ( ) => {
88
- wrapper . find ( ListItem ) . first ( ) . simulate ( 'click' ) ;
89
- } ) ;
90
- wrapper . update ( ) ;
84
+ userEvent . click ( screen . getByText ( 'Submit' ) ) ;
91
85
92
- await act ( async ( ) => {
93
- wrapper . find ( '#buttons-grid' ) . find ( Button ) . at ( RIGHT_POSITION ) . props ( ) . onClick ( ) ;
94
- } ) ;
95
- wrapper . update ( ) ;
86
+ expect ( onSubmit ) . toHaveBeenCalledWith ( { } ) ;
96
87
97
- await act ( async ( ) => {
98
- wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
99
- } ) ;
88
+ userEvent . click ( screen . getByText ( 'cats' ) ) ;
89
+ userEvent . click ( screen . getByLabelText ( 'Move selected to right' ) ) ;
90
+ userEvent . click ( screen . getByText ( 'Submit' ) ) ;
100
91
101
- expect ( onSubmit ) . toHaveBeenCalledWith ( { 'dual-list' : [ 'cats' ] } ) ;
92
+ expect ( onSubmit ) . toHaveBeenLastCalledWith ( { 'dual-list' : [ 'cats' ] } ) ;
102
93
} ) ;
103
94
104
95
it ( 'switch left option with holding ctrl' , async ( ) => {
105
- const wrapper = mount ( < FormRenderer { ...initialProps } /> ) ;
96
+ render ( < FormRenderer { ...initialProps } /> ) ;
106
97
107
- await act ( async ( ) => {
108
- wrapper . find ( List ) . first ( ) . find ( ListItem ) . first ( ) . simulate ( 'click' ) ;
109
- } ) ;
110
- wrapper . update ( ) ;
98
+ userEvent . click ( screen . getByText ( 'cats' ) ) ;
99
+ userEvent . click ( screen . getByText ( 'zebras' ) , { ctrlKey : true } ) ;
100
+ userEvent . click ( screen . getByLabelText ( 'Move selected to right' ) ) ;
101
+ userEvent . click ( screen . getByText ( 'Submit' ) ) ;
111
102
112
- await act ( async ( ) => {
113
- wrapper . find ( List ) . first ( ) . find ( ListItem ) . last ( ) . simulate ( 'click' , { ctrlKey : true } ) ;
114
- } ) ;
115
- wrapper . update ( ) ;
116
-
117
- await act ( async ( ) => {
118
- wrapper . find ( '#buttons-grid' ) . find ( Button ) . at ( RIGHT_POSITION ) . props ( ) . onClick ( ) ;
119
- } ) ;
120
- wrapper . update ( ) ;
121
-
122
- wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
123
-
124
- expect ( onSubmit ) . toHaveBeenCalledWith ( { 'dual-list' : [ 'cats' , 'zebras' ] } ) ;
103
+ expect ( onSubmit ) . toHaveBeenLastCalledWith ( { 'dual-list' : [ 'cats' , 'zebras' ] } ) ;
125
104
} ) ;
126
105
127
106
it ( 'switch left option with holding shift' , async ( ) => {
128
- const wrapper = mount ( < FormRenderer { ...initialProps } /> ) ;
129
-
130
- await act ( async ( ) => {
131
- wrapper . find ( List ) . first ( ) . find ( ListItem ) . first ( ) . simulate ( 'click' ) ;
132
- } ) ;
133
- wrapper . update ( ) ;
134
-
135
- await act ( async ( ) => {
136
- wrapper . find ( List ) . first ( ) . find ( ListItem ) . last ( ) . simulate ( 'click' , { shiftKey : true } ) ;
137
- } ) ;
138
- wrapper . update ( ) ;
139
-
140
- await act ( async ( ) => {
141
- wrapper . find ( '#buttons-grid' ) . find ( Button ) . at ( RIGHT_POSITION ) . props ( ) . onClick ( ) ;
142
- } ) ;
143
- wrapper . update ( ) ;
107
+ render ( < FormRenderer { ...initialProps } /> ) ;
144
108
145
- await act ( async ( ) => {
146
- wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
147
- } ) ;
109
+ userEvent . click ( screen . getByText ( 'cats' ) ) ;
110
+ userEvent . click ( screen . getByText ( 'zebras' ) , { shiftKey : true } ) ;
111
+ userEvent . click ( screen . getByLabelText ( 'Move selected to right' ) ) ;
112
+ userEvent . click ( screen . getByText ( 'Submit' ) ) ;
148
113
149
- expect ( onSubmit ) . toHaveBeenCalledWith ( { 'dual-list' : [ 'cats' , 'cats_1' , 'cats_2' , 'pigeons' , 'zebras' ] } ) ;
114
+ expect ( onSubmit ) . toHaveBeenLastCalledWith ( { 'dual-list' : [ 'cats' , 'cats_1' , 'cats_2' , 'pigeons' , 'zebras' ] } ) ;
150
115
} ) ;
151
116
152
117
it ( 'switch left option with holding and removing by ctrl' , async ( ) => {
153
- const wrapper = mount ( < FormRenderer { ...initialProps } /> ) ;
154
-
155
- await act ( async ( ) => {
156
- wrapper . find ( List ) . first ( ) . find ( ListItem ) . first ( ) . simulate ( 'click' ) ;
157
- } ) ;
158
- wrapper . update ( ) ;
118
+ render ( < FormRenderer { ...initialProps } /> ) ;
159
119
160
- await act ( async ( ) => {
161
- wrapper . find ( List ) . first ( ) . find ( ListItem ) . last ( ) . simulate ( 'click' , { shiftKey : true } ) ;
162
- } ) ;
163
- wrapper . update ( ) ;
164
-
165
- await act ( async ( ) => {
166
- wrapper . find ( List ) . first ( ) . find ( ListItem ) . first ( ) . simulate ( 'click' , { ctrlKey : true } ) ;
167
- } ) ;
168
- wrapper . update ( ) ;
120
+ userEvent . click ( screen . getByText ( 'cats' ) ) ;
121
+ userEvent . click ( screen . getByText ( 'zebras' ) , { shiftKey : true } ) ;
122
+ userEvent . click ( screen . getByText ( 'cats' ) , { ctrlKey : true } ) ;
123
+ userEvent . click ( screen . getByLabelText ( 'Move selected to right' ) ) ;
124
+ userEvent . click ( screen . getByText ( 'Submit' ) ) ;
169
125
170
- await act ( async ( ) => {
171
- wrapper . find ( '#buttons-grid' ) . find ( Button ) . at ( RIGHT_POSITION ) . props ( ) . onClick ( ) ;
172
- } ) ;
173
- wrapper . update ( ) ;
174
-
175
- await act ( async ( ) => {
176
- wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
177
- } ) ;
178
-
179
- expect ( onSubmit ) . toHaveBeenCalledWith ( { 'dual-list' : [ 'cats_1' , 'cats_2' , 'pigeons' , 'zebras' ] } ) ;
126
+ expect ( onSubmit ) . toHaveBeenLastCalledWith ( { 'dual-list' : [ 'cats_1' , 'cats_2' , 'pigeons' , 'zebras' ] } ) ;
180
127
} ) ;
181
128
182
129
it ( 'switch right option' , async ( ) => {
183
- const wrapper = mount ( < FormRenderer { ...initialProps } initialValues = { { 'dual-list' : [ 'cats' ] } } /> ) ;
184
- await act ( async ( ) => {
185
- wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
186
- } ) ;
187
- expect ( onSubmit ) . toHaveBeenCalledWith ( { 'dual-list' : [ 'cats' ] } ) ;
188
- onSubmit . mockClear ( ) ;
189
- await act ( async ( ) => {
190
- wrapper . find ( ListItem ) . last ( ) . simulate ( 'click' ) ;
191
- } ) ;
192
- wrapper . update ( ) ;
130
+ render ( < FormRenderer { ...initialProps } initialValues = { { 'dual-list' : [ 'cats' ] } } /> ) ;
193
131
194
- await act ( async ( ) => {
195
- wrapper . find ( '#buttons-grid' ) . find ( Button ) . at ( LEFT_POSITION ) . props ( ) . onClick ( ) ;
196
- } ) ;
197
- wrapper . update ( ) ;
198
- await act ( async ( ) => {
199
- wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
200
- } ) ;
132
+ userEvent . click ( screen . getByText ( 'Submit' ) ) ;
133
+ expect ( onSubmit ) . toHaveBeenLastCalledWith ( { 'dual-list' : [ 'cats' ] } ) ;
201
134
202
- expect ( onSubmit ) . toHaveBeenCalledWith ( { } ) ;
135
+ userEvent . click ( screen . getByText ( 'cats' ) ) ;
136
+ userEvent . click ( screen . getByLabelText ( 'Move selected to left' ) ) ;
137
+ userEvent . click ( screen . getByText ( 'Submit' ) ) ;
138
+
139
+ expect ( onSubmit ) . toHaveBeenLastCalledWith ( { } ) ;
203
140
} ) ;
204
141
205
142
it ( 'switch all to right' , async ( ) => {
0 commit comments