|
1 | 1 | import { act } from 'react-dom/test-utils';
|
2 |
| -import type { ReactWrapper } from 'enzyme'; |
| 2 | +import { mountNameByPath, matchNamePath } from '../../src/utils/valueUtil'; |
| 3 | +import { fireEvent, waitFor } from '../../tests/test-utils'; |
| 4 | +import type { NamePath } from '../../src/interface'; |
3 | 5 | import timeout from './timeout';
|
4 |
| -import { Field } from '../../src'; |
5 |
| -import { getNamePath, matchNamePath } from '../../src/utils/valueUtil'; |
6 | 6 |
|
7 |
| -export async function changeValue(wrapper: ReactWrapper, value: string | string[]) { |
8 |
| - wrapper.find('input').simulate('change', { target: { value } }); |
9 |
| - await act(async () => { |
| 7 | +export async function changeValue( |
| 8 | + input: HTMLElement, |
| 9 | + value: string | string[], |
| 10 | + ignoreTest = false, |
| 11 | +): Promise<void> { |
| 12 | + expect(input).toBeTruthy(); |
| 13 | + |
| 14 | + fireEvent.focus(input); |
| 15 | + // Force change value, because if empty and set empty, change not trigger effetct in test |
| 16 | + if (!value) { |
| 17 | + console.debug('changeValue called if "" (empty) value'); |
| 18 | + fireEvent.change(input, { target: { value: `${value}any` } }); |
10 | 19 | await timeout();
|
11 |
| - }); |
12 |
| - wrapper.update(); |
| 20 | + } |
| 21 | + |
| 22 | + fireEvent.change(input, { target: { value } }); |
| 23 | + if (!ignoreTest) { |
| 24 | + await waitFor(() => expect((input as HTMLInputElement).value).toBe(value)); |
| 25 | + } |
13 | 26 | }
|
14 | 27 |
|
15 | 28 | export function matchError(
|
16 |
| - wrapper: ReactWrapper, |
| 29 | + wrapper: HTMLElement, |
17 | 30 | error?: boolean | string,
|
18 | 31 | warning?: boolean | string,
|
19 | 32 | ) {
|
20 | 33 | // Error
|
21 |
| - if (error) { |
22 |
| - expect(wrapper.find('.errors li').length).toBeTruthy(); |
23 |
| - } else { |
24 |
| - expect(wrapper.find('.errors li').length).toBeFalsy(); |
25 |
| - } |
| 34 | + const errorsFound = wrapper.querySelectorAll('.errors li').length; |
| 35 | + expect(!!errorsFound).toBe(!!error); |
26 | 36 |
|
27 | 37 | if (error && typeof error !== 'boolean') {
|
28 |
| - expect(wrapper.find('.errors li').text()).toBe(error); |
| 38 | + const errorFound = wrapper.querySelector('.errors li').textContent; |
| 39 | + expect(errorFound).toBe(error); |
29 | 40 | }
|
30 | 41 |
|
31 | 42 | // Warning
|
32 |
| - if (warning) { |
33 |
| - expect(wrapper.find('.warnings li').length).toBeTruthy(); |
34 |
| - } else { |
35 |
| - expect(wrapper.find('.warnings li').length).toBeFalsy(); |
36 |
| - } |
| 43 | + const warningsFound = wrapper.querySelectorAll('.warnings li').length; |
| 44 | + expect(!!warningsFound).toBe(!!warning); |
37 | 45 |
|
38 | 46 | if (warning && typeof warning !== 'boolean') {
|
39 |
| - expect(wrapper.find('.warnings li').text()).toBe(warning); |
| 47 | + const warningFound = wrapper.querySelector('.warnings li').textContent; |
| 48 | + expect(warningFound).toBe(warning); |
40 | 49 | }
|
41 | 50 | }
|
42 | 51 |
|
43 |
| -export function getField(wrapper: ReactWrapper, index: string | number | string[] = 0) { |
44 |
| - if (typeof index === 'number') { |
45 |
| - return wrapper.find(Field).at(index); |
| 52 | +export function getField( |
| 53 | + wrapper: HTMLElement | Element, |
| 54 | + index: NamePath | null = 0, |
| 55 | +): HTMLInputElement | null { |
| 56 | + let name = index; |
| 57 | + if (Array.isArray(index)) { |
| 58 | + name = mountNameByPath(index); |
46 | 59 | }
|
47 |
| - const name = getNamePath(index); |
48 |
| - const fields = wrapper.find(Field); |
49 |
| - for (let i = 0; i < fields.length; i += 1) { |
50 |
| - const field = fields.at(i); |
51 |
| - const fieldName = getNamePath((field.props() as any).name); |
52 |
| - if (matchNamePath(name, fieldName)) { |
53 |
| - return field; |
54 |
| - } |
| 60 | + if (typeof index === 'number') { |
| 61 | + return wrapper.querySelectorAll('form input')?.item(index) as HTMLInputElement; |
55 | 62 | }
|
56 |
| - return null; |
| 63 | + return wrapper.querySelector(`form input[name="${name}"]`); |
57 | 64 | }
|
58 | 65 |
|
59 | 66 | export function matchArray(source: any[], target: any[], matchKey: React.Key) {
|
|
0 commit comments