|
1 | 1 | import React from 'react'; |
2 | | -import { act } from 'react-dom/test-utils'; |
3 | 2 | import { render, screen } from '@testing-library/react'; |
4 | 3 | import userEvent from '@testing-library/user-event'; |
5 | 4 |
|
6 | 5 | import { FormRenderer, componentTypes } from '@data-driven-forms/react-form-renderer'; |
7 | | -import { mount } from 'enzyme'; |
8 | | - |
9 | | -import { List, ListItem, Toolbar, TextField, Paper, Button, IconButton } from '@mui/material'; |
10 | | - |
11 | | -import SortIcon from '@mui/icons-material/ArrowUpward'; |
12 | 6 |
|
13 | 7 | import { componentMapper, FormTemplate } from '../index'; |
14 | | -import FormFieldGrid from '../form-field-grid'; |
15 | 8 |
|
16 | 9 | describe('DualListSelect', () => { |
17 | 10 | let onSubmit; |
18 | 11 | let initialProps; |
19 | 12 | let schema; |
20 | 13 |
|
21 | | - const ALL_LEFT_POSITION = 3; |
22 | | - const ALL_RIGHT_POSITION = 0; |
23 | | - |
24 | 14 | beforeEach(() => { |
25 | 15 | onSubmit = jest.fn(); |
26 | 16 |
|
@@ -67,15 +57,16 @@ describe('DualListSelect', () => { |
67 | 57 | }); |
68 | 58 |
|
69 | 59 | it('renders correctly', () => { |
70 | | - const wrapper = mount(<FormRenderer {...initialProps} />); |
71 | | - |
72 | | - expect(wrapper.find(FormFieldGrid)).toHaveLength(1); |
73 | | - expect(wrapper.find(Toolbar)).toHaveLength(2); |
74 | | - expect(wrapper.find(TextField)).toHaveLength(2); |
75 | | - expect(wrapper.find(SortIcon)).toHaveLength(2); |
76 | | - expect(wrapper.find(List)).toHaveLength(2); |
77 | | - expect(wrapper.find(Paper)).toHaveLength(2); |
78 | | - expect(wrapper.find(ListItem)).toHaveLength(schema.fields[0].options.length + 1); // + empty placeholder |
| 60 | + render(<FormRenderer {...initialProps} />); |
| 61 | + |
| 62 | + expect(screen.getByText('cats')).toBeInTheDocument(); |
| 63 | + expect(screen.getByText('cats_1')).toBeInTheDocument(); |
| 64 | + expect(screen.getByText('cats_2')).toBeInTheDocument(); |
| 65 | + expect(screen.getByText('zebras')).toBeInTheDocument(); |
| 66 | + expect(screen.getByText('pigeons')).toBeInTheDocument(); |
| 67 | + expect(screen.getByLabelText('Filter options')).toBeInTheDocument(); |
| 68 | + expect(screen.getByLabelText('Filter selected value')).toBeInTheDocument(); |
| 69 | + expect(screen.getByText('No selected')).toBeInTheDocument(); |
79 | 70 | }); |
80 | 71 |
|
81 | 72 | it('switch left option', async () => { |
@@ -140,157 +131,178 @@ describe('DualListSelect', () => { |
140 | 131 | }); |
141 | 132 |
|
142 | 133 | it('switch all to right', async () => { |
143 | | - const wrapper = mount(<FormRenderer {...initialProps} />); |
144 | | - await act(async () => { |
145 | | - wrapper.find('#buttons-grid').find(Button).at(ALL_RIGHT_POSITION).props().onClick(); |
146 | | - }); |
147 | | - wrapper.update(); |
148 | | - await act(async () => { |
149 | | - wrapper.find('form').simulate('submit'); |
150 | | - }); |
| 134 | + render(<FormRenderer {...initialProps} />); |
| 135 | + |
| 136 | + userEvent.click(screen.getByLabelText('Move all to right')); |
| 137 | + userEvent.click(screen.getByText('Submit')); |
151 | 138 |
|
152 | 139 | expect(onSubmit).toHaveBeenCalledWith({ 'dual-list': ['cats', 'cats_1', 'cats_2', 'pigeons', 'zebras'] }); |
153 | 140 | }); |
154 | 141 |
|
155 | 142 | it('switch all to left', async () => { |
156 | | - const wrapper = mount(<FormRenderer {...initialProps} initialValues={{ 'dual-list': schema.fields[0].options.map(({ value }) => value) }} />); |
157 | | - await act(async () => { |
158 | | - wrapper.find('#buttons-grid').find(Button).at(ALL_LEFT_POSITION).props().onClick(); |
159 | | - }); |
160 | | - wrapper.update(); |
161 | | - await act(async () => { |
162 | | - wrapper.find('form').simulate('submit'); |
163 | | - }); |
| 143 | + render(<FormRenderer {...initialProps} initialValues={{ 'dual-list': schema.fields[0].options.map(({ value }) => value) }} />); |
| 144 | + |
| 145 | + userEvent.click(screen.getByLabelText('Move all to left')); |
| 146 | + userEvent.click(screen.getByText('Submit')); |
164 | 147 |
|
165 | 148 | expect(onSubmit).toHaveBeenCalledWith({}); |
166 | 149 | }); |
167 | 150 |
|
168 | 151 | it('filters options', async () => { |
169 | | - const wrapper = mount(<FormRenderer {...initialProps} />); |
| 152 | + render(<FormRenderer {...initialProps} />); |
170 | 153 |
|
171 | | - expect(wrapper.find(List).first().find(ListItem)).toHaveLength(schema.fields[0].options.length); |
172 | | - await act(async () => { |
173 | | - wrapper.find('input').first().instance().value = 'cats'; |
174 | | - }); |
175 | | - await act(async () => { |
176 | | - wrapper.find('input').first().simulate('change'); |
177 | | - }); |
178 | | - wrapper.update(); |
179 | | - |
180 | | - expect(wrapper.find(List).first().find(ListItem)).toHaveLength(3); |
181 | | - wrapper |
182 | | - .find(List) |
183 | | - .first() |
184 | | - .find(ListItem) |
185 | | - .forEach((option) => expect(option.text()).toEqual(expect.stringContaining('cats'))); |
| 154 | + userEvent.type(screen.getByLabelText('Filter options'), 'cats'); |
| 155 | + |
| 156 | + expect(screen.getByText('cats')).toBeInTheDocument(); |
| 157 | + expect(screen.getByText('cats_1')).toBeInTheDocument(); |
| 158 | + expect(screen.getByText('cats_2')).toBeInTheDocument(); |
| 159 | + expect(() => screen.getByText('zebras')).toThrow(); |
| 160 | + expect(() => screen.getByText('pigeons')).toThrow(); |
186 | 161 | }); |
187 | 162 |
|
188 | 163 | it('filters value', async () => { |
189 | | - const wrapper = mount(<FormRenderer {...initialProps} initialValues={{ 'dual-list': schema.fields[0].options.map(({ value }) => value) }} />); |
| 164 | + render(<FormRenderer {...initialProps} initialValues={{ 'dual-list': schema.fields[0].options.map(({ value }) => value) }} />); |
190 | 165 |
|
191 | | - expect(wrapper.find(List).last().find(ListItem)).toHaveLength(schema.fields[0].options.length); |
192 | | - await act(async () => { |
193 | | - wrapper.find('input').last().instance().value = 'cats'; |
194 | | - }); |
| 166 | + userEvent.type(screen.getByLabelText('Filter selected value'), 'cats'); |
195 | 167 |
|
196 | | - await act(async () => { |
197 | | - wrapper.find('input').last().simulate('change'); |
198 | | - }); |
199 | | - wrapper.update(); |
200 | | - |
201 | | - expect(wrapper.find(List).last().find(ListItem)).toHaveLength(3); |
202 | | - wrapper |
203 | | - .find(List) |
204 | | - .last() |
205 | | - .find(ListItem) |
206 | | - .forEach((option) => expect(option.text()).toEqual(expect.stringContaining('cats'))); |
| 168 | + expect(screen.getByText('cats')).toBeInTheDocument(); |
| 169 | + expect(screen.getByText('cats_1')).toBeInTheDocument(); |
| 170 | + expect(screen.getByText('cats_2')).toBeInTheDocument(); |
| 171 | + expect(() => screen.getByText('zebras')).toThrow(); |
| 172 | + expect(() => screen.getByText('pigeons')).toThrow(); |
207 | 173 | }); |
208 | 174 |
|
209 | 175 | it('sort options', async () => { |
210 | | - const wrapper = mount(<FormRenderer {...initialProps} />); |
211 | | - |
212 | | - expect(wrapper.find(List).first().find(ListItem).first().text()).toEqual('cats'); |
213 | | - expect(wrapper.find(List).first().find(ListItem).last().text()).toEqual('zebras'); |
214 | | - await act(async () => { |
215 | | - wrapper.find(IconButton).at(0).props().onClick(); |
216 | | - }); |
217 | | - wrapper.update(); |
218 | | - |
219 | | - expect(wrapper.find(List).first().find(ListItem).first().text()).toEqual('zebras'); |
220 | | - expect(wrapper.find(List).first().find(ListItem).last().text()).toEqual('cats'); |
221 | | - await act(async () => { |
222 | | - wrapper.find(IconButton).at(0).props().onClick(); |
223 | | - }); |
224 | | - wrapper.update(); |
| 176 | + render(<FormRenderer {...initialProps} />); |
225 | 177 |
|
226 | | - expect(wrapper.find(List).first().find(ListItem).first().text()).toEqual('cats'); |
227 | | - expect(wrapper.find(List).first().find(ListItem).last().text()).toEqual('zebras'); |
| 178 | + expect(screen.getAllByRole('button').map((b) => b.textContent)).toEqual([ |
| 179 | + '', |
| 180 | + 'cats', |
| 181 | + 'cats_1', |
| 182 | + 'cats_2', |
| 183 | + 'pigeons', |
| 184 | + 'zebras', |
| 185 | + '≫', |
| 186 | + '>', |
| 187 | + '<', |
| 188 | + '≪', |
| 189 | + '', |
| 190 | + 'No selected', |
| 191 | + 'Submit', |
| 192 | + ]); |
| 193 | + |
| 194 | + userEvent.click(screen.getByLabelText('sort options')); |
| 195 | + |
| 196 | + expect(screen.getAllByRole('button').map((b) => b.textContent)).toEqual([ |
| 197 | + '', |
| 198 | + 'zebras', |
| 199 | + 'pigeons', |
| 200 | + 'cats_2', |
| 201 | + 'cats_1', |
| 202 | + 'cats', |
| 203 | + '≫', |
| 204 | + '>', |
| 205 | + '<', |
| 206 | + '≪', |
| 207 | + '', |
| 208 | + 'No selected', |
| 209 | + 'Submit', |
| 210 | + ]); |
| 211 | + |
| 212 | + userEvent.click(screen.getByLabelText('sort options')); |
| 213 | + |
| 214 | + expect(screen.getAllByRole('button').map((b) => b.textContent)).toEqual([ |
| 215 | + '', |
| 216 | + 'cats', |
| 217 | + 'cats_1', |
| 218 | + 'cats_2', |
| 219 | + 'pigeons', |
| 220 | + 'zebras', |
| 221 | + '≫', |
| 222 | + '>', |
| 223 | + '<', |
| 224 | + '≪', |
| 225 | + '', |
| 226 | + 'No selected', |
| 227 | + 'Submit', |
| 228 | + ]); |
228 | 229 | }); |
229 | 230 |
|
230 | 231 | it('sort value', async () => { |
231 | | - const wrapper = mount(<FormRenderer {...initialProps} initialValues={{ 'dual-list': schema.fields[0].options.map(({ value }) => value) }} />); |
232 | | - |
233 | | - expect(wrapper.find(List).last().find(ListItem).first().text()).toEqual('cats'); |
234 | | - expect(wrapper.find(List).last().find(ListItem).last().text()).toEqual('zebras'); |
235 | | - await act(async () => { |
236 | | - wrapper.find(IconButton).last().props().onClick(); |
237 | | - }); |
238 | | - wrapper.update(); |
239 | | - |
240 | | - expect(wrapper.find(List).last().find(ListItem).first().text()).toEqual('zebras'); |
241 | | - expect(wrapper.find(List).last().find(ListItem).last().text()).toEqual('cats'); |
242 | | - await act(async () => { |
243 | | - wrapper.find(IconButton).last().props().onClick(); |
244 | | - }); |
245 | | - wrapper.update(); |
246 | | - |
247 | | - expect(wrapper.find(List).last().find(ListItem).first().text()).toEqual('cats'); |
248 | | - expect(wrapper.find(List).last().find(ListItem).last().text()).toEqual('zebras'); |
| 232 | + render(<FormRenderer {...initialProps} initialValues={{ 'dual-list': schema.fields[0].options.map(({ value }) => value) }} />); |
| 233 | + |
| 234 | + expect(screen.getAllByRole('button').map((b) => b.textContent)).toEqual([ |
| 235 | + '', |
| 236 | + 'No available options', |
| 237 | + '≫', |
| 238 | + '>', |
| 239 | + '<', |
| 240 | + '≪', |
| 241 | + '', |
| 242 | + 'cats', |
| 243 | + 'cats_1', |
| 244 | + 'cats_2', |
| 245 | + 'pigeons', |
| 246 | + 'zebras', |
| 247 | + 'Submit', |
| 248 | + ]); |
| 249 | + |
| 250 | + userEvent.click(screen.getByLabelText('sort value')); |
| 251 | + |
| 252 | + expect(screen.getAllByRole('button').map((b) => b.textContent)).toEqual([ |
| 253 | + '', |
| 254 | + 'No available options', |
| 255 | + '≫', |
| 256 | + '>', |
| 257 | + '<', |
| 258 | + '≪', |
| 259 | + '', |
| 260 | + 'zebras', |
| 261 | + 'pigeons', |
| 262 | + 'cats_2', |
| 263 | + 'cats_1', |
| 264 | + 'cats', |
| 265 | + 'Submit', |
| 266 | + ]); |
| 267 | + |
| 268 | + userEvent.click(screen.getByLabelText('sort value')); |
| 269 | + |
| 270 | + expect(screen.getAllByRole('button').map((b) => b.textContent)).toEqual([ |
| 271 | + '', |
| 272 | + 'No available options', |
| 273 | + '≫', |
| 274 | + '>', |
| 275 | + '<', |
| 276 | + '≪', |
| 277 | + '', |
| 278 | + 'cats', |
| 279 | + 'cats_1', |
| 280 | + 'cats_2', |
| 281 | + 'pigeons', |
| 282 | + 'zebras', |
| 283 | + 'Submit', |
| 284 | + ]); |
249 | 285 | }); |
250 | 286 |
|
251 | 287 | describe('filtered options', () => { |
252 | 288 | it('switch all visible to right', async () => { |
253 | | - const wrapper = mount(<FormRenderer {...initialProps} />); |
254 | | - await act(async () => { |
255 | | - wrapper.find('input').first().instance().value = 'cats'; |
256 | | - }); |
257 | | - |
258 | | - await act(async () => { |
259 | | - wrapper.find('input').first().simulate('change'); |
260 | | - }); |
261 | | - wrapper.update(); |
262 | | - await act(async () => { |
263 | | - wrapper.find('#buttons-grid').find(Button).at(ALL_RIGHT_POSITION).props().onClick(); |
264 | | - }); |
265 | | - wrapper.update(); |
266 | | - |
267 | | - await act(async () => { |
268 | | - wrapper.find('form').simulate('submit'); |
269 | | - }); |
| 289 | + render(<FormRenderer {...initialProps} />); |
| 290 | + |
| 291 | + userEvent.type(screen.getByLabelText('Filter options'), 'cats'); |
| 292 | + userEvent.click(screen.getByLabelText('Move all to right')); |
| 293 | + userEvent.click(screen.getByText('Submit')); |
270 | 294 |
|
271 | 295 | expect(onSubmit).toHaveBeenCalledWith({ 'dual-list': ['cats', 'cats_1', 'cats_2'] }); |
272 | 296 | }); |
273 | 297 | }); |
274 | 298 |
|
275 | 299 | describe('filtered value', () => { |
276 | 300 | it('switch all visible to left', async () => { |
277 | | - const wrapper = mount(<FormRenderer {...initialProps} initialValues={{ 'dual-list': schema.fields[0].options.map(({ value }) => value) }} />); |
278 | | - await act(async () => { |
279 | | - wrapper.find('input').last().instance().value = 'cats'; |
280 | | - }); |
281 | | - await act(async () => { |
282 | | - wrapper.find('input').last().simulate('change'); |
283 | | - }); |
284 | | - wrapper.update(); |
285 | | - |
286 | | - await act(async () => { |
287 | | - wrapper.find('#buttons-grid').find(Button).at(ALL_LEFT_POSITION).props().onClick(); |
288 | | - }); |
289 | | - wrapper.update(); |
290 | | - |
291 | | - await act(async () => { |
292 | | - wrapper.find('form').simulate('submit'); |
293 | | - }); |
| 301 | + render(<FormRenderer {...initialProps} initialValues={{ 'dual-list': schema.fields[0].options.map(({ value }) => value) }} />); |
| 302 | + |
| 303 | + userEvent.type(screen.getByLabelText('Filter selected value'), 'cats'); |
| 304 | + userEvent.click(screen.getByLabelText('Move all to left')); |
| 305 | + userEvent.click(screen.getByText('Submit')); |
294 | 306 |
|
295 | 307 | expect(onSubmit).toHaveBeenCalledWith({ 'dual-list': ['zebras', 'pigeons'] }); |
296 | 308 | }); |
|
0 commit comments