Skip to content

Commit c3f3e84

Browse files
authored
Migrate to testing-lib (#553)
* add * add * fix type * add test case * fix test case * fix key * add * add type * add
1 parent dd3cc36 commit c3f3e84

15 files changed

+566
-783
lines changed

docs/examples/components/LabelField.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface ErrorProps {
99
children?: React.ReactNode[];
1010
}
1111

12-
const Error = ({ children, warning }: ErrorProps) => (
12+
const Error: React.FC<ErrorProps> = ({ children, warning }) => (
1313
<ul style={{ color: warning ? 'orange' : 'red' }}>
1414
{children.map((error: React.ReactNode, index: number) => (
1515
<li key={index}>{error}</li>

docs/examples/components/useDraggable.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useRef } from 'react';
2-
import { DragObjectWithType, useDrag, useDrop } from 'react-dnd';
2+
import type { DragObjectWithType } from 'react-dnd';
3+
import { useDrag, useDrop } from 'react-dnd';
34

45
type DragWithIndex = DragObjectWithType & {
56
index: number;

docs/examples/renderProps.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable jsx-a11y/label-has-associated-control */
21
import React from 'react';
32
import Form from 'rc-field-form';
43
import Input from './components/Input';
@@ -45,7 +44,6 @@ export default class Demo extends React.Component {
4544
</div>
4645
)}
4746
</Field>
48-
4947
{list.map((_, index) => (
5048
<Field key={index} name={`list_field_${index}`}>
5149
<Input placeholder={`list_field_${index}`} />

docs/examples/validate.tsx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import Input from './components/Input';
66

77
const { Field } = Form;
88

9-
const Error = ({ children }) => (
9+
interface ErrorProps {
10+
children?: React.ReactNode[];
11+
}
12+
13+
const Error: React.FC<ErrorProps> = ({ children }) => (
1014
<ul style={{ color: 'red' }}>
1115
{children.map((error, i) => (
1216
<li key={i}>{error}</li>
@@ -19,14 +23,7 @@ const FieldState = ({ form, name }) => {
1923
const validating = form.isFieldValidating(name);
2024

2125
return (
22-
<div
23-
style={{
24-
color: 'green',
25-
position: 'absolute',
26-
marginTop: -35,
27-
left: 200,
28-
}}
29-
>
26+
<div style={{ color: 'green', position: 'absolute', marginTop: -35, left: 200 }}>
3027
{touched ? <span>Touched!</span> : null}
3128
{validating ? <span>Validating!</span> : null}
3229
</div>

tests/common/InfoField.tsx

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

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

1113
/**
1214
* Return a wrapped Field with meta info

tests/common/index.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import timeout from './timeout';
44
import { Field } from '../../src';
55
import { getNamePath, matchNamePath } from '../../src/utils/valueUtil';
66

7-
export async function changeValue(wrapper, value) {
7+
export async function changeValue(wrapper: ReactWrapper, value: string | string[]) {
88
wrapper.find('input').simulate('change', { target: { value } });
99
await act(async () => {
1010
await timeout();
@@ -40,36 +40,34 @@ export function matchError(
4040
}
4141
}
4242

43-
export function getField(wrapper, index: string | number | string[] = 0) {
43+
export function getField(wrapper: ReactWrapper, index: string | number | string[] = 0) {
4444
if (typeof index === 'number') {
4545
return wrapper.find(Field).at(index);
4646
}
47-
4847
const name = getNamePath(index);
4948
const fields = wrapper.find(Field);
5049
for (let i = 0; i < fields.length; i += 1) {
5150
const field = fields.at(i);
52-
const fieldName = getNamePath(field.props().name);
53-
51+
const fieldName = getNamePath((field.props() as any).name);
5452
if (matchNamePath(name, fieldName)) {
5553
return field;
5654
}
5755
}
5856
return null;
5957
}
6058

61-
export function matchArray(source, target, matchKey) {
59+
export function matchArray(source: any[], target: any[], matchKey: React.Key) {
6260
expect(matchKey).toBeTruthy();
6361

6462
try {
6563
expect(source.length).toBe(target.length);
6664
} catch (err) {
6765
throw new Error(
6866
`
69-
Array length not match.
70-
source(${source.length}): ${JSON.stringify(source)}
71-
target(${target.length}): ${JSON.stringify(target)}
72-
`.trim(),
67+
Array length not match.
68+
source(${source.length}): ${JSON.stringify(source)}
69+
target(${target.length}): ${JSON.stringify(target)}
70+
`.trim(),
7371
);
7472
}
7573

tests/context.test.tsx

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import React from 'react';
22
import { mount } from 'enzyme';
3+
import { render } from '@testing-library/react';
4+
import type { FormInstance } from '../src';
35
import Form, { FormProvider } from '../src';
46
import InfoField from './common/InfoField';
57
import { changeValue, matchError, getField } from './common';
@@ -77,7 +79,7 @@ describe('Form.Context', () => {
7779
it('multiple context', async () => {
7880
const onFormChange = jest.fn();
7981

80-
const Demo = changed => (
82+
const Demo: React.FC<{ changed?: boolean }> = ({ changed }) => (
8183
<FormProvider onFormChange={onFormChange}>
8284
<FormProvider>
8385
{!changed ? (
@@ -105,17 +107,12 @@ describe('Form.Context', () => {
105107

106108
it('submit', async () => {
107109
const onFormFinish = jest.fn();
108-
let form1;
110+
const form = React.createRef<FormInstance>();
109111

110112
const wrapper = mount(
111113
<div>
112114
<FormProvider onFormFinish={onFormFinish}>
113-
<Form
114-
name="form1"
115-
ref={instance => {
116-
form1 = instance;
117-
}}
118-
>
115+
<Form name="form1" ref={form}>
119116
<InfoField name="name" rules={[{ required: true }]} />
120117
</Form>
121118
<Form name="form2" />
@@ -124,12 +121,12 @@ describe('Form.Context', () => {
124121
);
125122

126123
await changeValue(getField(wrapper), '');
127-
form1.submit();
124+
form.current?.submit();
128125
await timeout();
129126
expect(onFormFinish).not.toHaveBeenCalled();
130127

131128
await changeValue(getField(wrapper), 'Light');
132-
form1.submit();
129+
form.current?.submit();
133130
await timeout();
134131
expect(onFormFinish).toHaveBeenCalled();
135132

@@ -140,14 +137,11 @@ describe('Form.Context', () => {
140137
});
141138

142139
it('do nothing if no Provider in use', () => {
143-
const wrapper = mount(
140+
const { rerender } = render(
144141
<div>
145142
<Form name="no" />
146143
</div>,
147144
);
148-
149-
wrapper.setProps({
150-
children: null,
151-
});
145+
rerender(<div>{null}</div>);
152146
});
153147
});

tests/control.test.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,25 @@ import { mount } from 'enzyme';
33
import Form from '../src';
44
import InfoField from './common/InfoField';
55
import { changeValue, matchError } from './common';
6+
import { render } from '@testing-library/react';
67

78
describe('Form.Control', () => {
89
it('fields', () => {
9-
const wrapper = mount(
10+
const { container, rerender } = render(
1011
<Form>
1112
<InfoField name="username" />
1213
</Form>,
1314
);
14-
15-
wrapper.setProps({
16-
fields: [{ name: 'username', value: 'Bamboo' }],
17-
});
18-
wrapper.update();
19-
20-
expect(wrapper.find('input').props().value).toEqual('Bamboo');
15+
rerender(
16+
<Form fields={[{ name: 'username', value: 'Bamboo' }]}>
17+
<InfoField name="username" />
18+
</Form>,
19+
);
20+
expect(container.querySelector<HTMLInputElement>('input')?.value).toBe('Bamboo');
2121
});
2222

2323
it('fully test', async () => {
24-
const Test = () => {
24+
const Test: React.FC = () => {
2525
const [fields, setFields] = React.useState([]);
2626

2727
return (

tests/dependencies.test.tsx

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
import React from 'react';
22
import { mount } from 'enzyme';
3+
import type { FormInstance } from '../src';
34
import Form, { Field } from '../src';
45
import timeout from './common/timeout';
56
import InfoField, { Input } from './common/InfoField';
67
import { changeValue, matchError, getField } from './common';
78

89
describe('Form.Dependencies', () => {
910
it('touched', async () => {
10-
let form = null;
11+
const form = React.createRef<FormInstance>();
1112

1213
const wrapper = mount(
1314
<div>
14-
<Form
15-
ref={instance => {
16-
form = instance;
17-
}}
18-
>
15+
<Form ref={form}>
1916
<InfoField name="field_1" />
2017
<InfoField name="field_2" rules={[{ required: true }]} dependencies={['field_1']} />
2118
</Form>
@@ -27,13 +24,13 @@ describe('Form.Dependencies', () => {
2724
matchError(getField(wrapper, 1), false);
2825

2926
// Trigger if touched
30-
form.setFields([{ name: 'field_2', touched: true }]);
27+
form.current?.setFields([{ name: 'field_2', touched: true }]);
3128
await changeValue(getField(wrapper, 0), '');
3229
matchError(getField(wrapper, 1), true);
3330
});
3431

3532
describe('initialValue', () => {
36-
function test(name, formProps, fieldProps = {}) {
33+
function test(name: string, formProps = {}, fieldProps = {}) {
3734
it(name, async () => {
3835
let validated = false;
3936

@@ -68,16 +65,12 @@ describe('Form.Dependencies', () => {
6865
});
6966

7067
it('nest dependencies', async () => {
71-
let form = null;
68+
const form = React.createRef<FormInstance>();
7269
let rendered = false;
7370

7471
const wrapper = mount(
7572
<div>
76-
<Form
77-
ref={instance => {
78-
form = instance;
79-
}}
80-
>
73+
<Form ref={form}>
8174
<Field name="field_1">
8275
<Input />
8376
</Field>
@@ -94,7 +87,7 @@ describe('Form.Dependencies', () => {
9487
</div>,
9588
);
9689

97-
form.setFields([
90+
form.current?.setFields([
9891
{ name: 'field_1', touched: true },
9992
{ name: 'field_2', touched: true },
10093
{ name: 'field_3', touched: true },

0 commit comments

Comments
 (0)