Skip to content

Commit 0c0bdc3

Browse files
committed
feat: upgrade all tests with Vitest
1 parent 66da1e4 commit 0c0bdc3

21 files changed

+1327
-822
lines changed

tests/common/InfoField.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ interface InfoFieldProps extends FieldProps {
66
children?: React.ReactElement;
77
}
88

9-
export const Input: React.FC<any> = ({ value = '', ...props }) => (
10-
<input {...props} value={value} />
11-
);
9+
export const Input: React.FC<any> = ({ value = '', ...props }) => {
10+
return <input {...props} value={value} />;
11+
};
1212

1313
/**
1414
* Return a wrapped Field with meta info
@@ -20,7 +20,21 @@ const InfoField: React.FC<InfoFieldProps> = ({ children, ...props }) => (
2020

2121
return (
2222
<div>
23-
{children ? React.cloneElement(children, control) : <Input {...control} />}
23+
{children ? (
24+
React.cloneElement(children, {
25+
...(children?.props ?? {}),
26+
...control,
27+
name: children?.props?.name || control?.name,
28+
})
29+
) : (
30+
<Input
31+
{...{
32+
...(children?.props ?? {}),
33+
...control,
34+
name: children?.props?.name || control?.name,
35+
}}
36+
/>
37+
)}
2438
<ul className="errors">
2539
{errors.map((error, index) => (
2640
<li key={index}>{error}</li>

tests/common/index.ts

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,66 @@
11
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';
35
import timeout from './timeout';
4-
import { Field } from '../../src';
5-
import { getNamePath, matchNamePath } from '../../src/utils/valueUtil';
66

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` } });
1019
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+
}
1326
}
1427

1528
export function matchError(
16-
wrapper: ReactWrapper,
29+
wrapper: HTMLElement,
1730
error?: boolean | string,
1831
warning?: boolean | string,
1932
) {
2033
// 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);
2636

2737
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);
2940
}
3041

3142
// 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);
3745

3846
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);
4049
}
4150
}
4251

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);
4659
}
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;
5562
}
56-
return null;
63+
return wrapper.querySelector(`form input[name="${name}"]`);
5764
}
5865

5966
export function matchArray(source: any[], target: any[], matchKey: React.Key) {

0 commit comments

Comments
 (0)