Skip to content

Commit dd3cc36

Browse files
authored
Migrate to testing-lib (#552)
* js => ts * Migrate to testing-lib
1 parent a0fc461 commit dd3cc36

24 files changed

+367
-461
lines changed

jest.config.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

jest.config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { Config } from 'jest';
2+
3+
const config: Config = {
4+
setupFilesAfterEnv: ['<rootDir>/tests/setupAfterEnv.ts'],
5+
};
6+
7+
export default config;

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@
5555
},
5656
"devDependencies": {
5757
"@testing-library/jest-dom": "^5.16.4",
58-
"@testing-library/react": "^13.0.0",
58+
"@testing-library/react": "^12.1.5",
5959
"@types/enzyme": "^3.10.5",
60-
"@types/jest": "^26.0.20",
60+
"@types/jest": "^29.2.5",
6161
"@types/lodash": "^4.14.135",
6262
"@types/react": "^18.0.0",
6363
"@types/react-dom": "^18.0.0",
@@ -71,6 +71,7 @@
7171
"father": "^2.13.6",
7272
"father-build": "^1.18.6",
7373
"gh-pages": "^3.1.0",
74+
"jest": "^29.3.1",
7475
"np": "^5.0.3",
7576
"prettier": "^2.1.2",
7677
"react": "^16.14.0",

src/utils/valueUtil.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export function matchNamePath(
9090
}
9191

9292
// Like `shallowEqual`, but we not check the data which may cause re-render
93-
type SimilarObject = string | number | {};
93+
type SimilarObject = string | number | object;
9494
export function isSimilar(source: SimilarObject, target: SimilarObject) {
9595
if (source === target) {
9696
return true;

tests/common/index.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* eslint-disable import/no-extraneous-dependencies */
2-
31
import { act } from 'react-dom/test-utils';
42
import type { ReactWrapper } from 'enzyme';
53
import timeout from './timeout';
@@ -42,7 +40,7 @@ export function matchError(
4240
}
4341
}
4442

45-
export function getField(wrapper, index: string | number = 0) {
43+
export function getField(wrapper, index: string | number | string[] = 0) {
4644
if (typeof index === 'number') {
4745
return wrapper.find(Field).at(index);
4846
}
@@ -88,5 +86,3 @@ export async function validateFields(form, ...args) {
8886
await form.validateFields(...args);
8987
});
9088
}
91-
92-
/* eslint-enable import/no-extraneous-dependencies */

tests/common/timeout.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export default (timeout: number = 0) => {
2-
return new Promise(resolve => {
2+
return new Promise<void>(resolve => {
33
setTimeout(resolve, timeout);
44
});
55
};
File renamed without changes.
File renamed without changes.

tests/dependencies.test.js renamed to tests/dependencies.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe('Form.Dependencies', () => {
3333
});
3434

3535
describe('initialValue', () => {
36-
function test(name, formProps, fieldProps) {
36+
function test(name, formProps, fieldProps = {}) {
3737
it(name, async () => {
3838
let validated = false;
3939

tests/index.test.js renamed to tests/index.test.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { mount } from 'enzyme';
22
import { resetWarned } from 'rc-util/lib/warning';
33
import React from 'react';
4+
import type { FormInstance } from '../src';
45
import Form, { Field, useForm } from '../src';
56
import { changeValue, getField, matchError } from './common';
67
import InfoField, { Input } from './common/InfoField';
@@ -406,7 +407,7 @@ describe('Form.Basic', () => {
406407
</div>,
407408
);
408409

409-
expect(wrapper.find('.anything').props().light).toEqual('bamboo');
410+
expect((wrapper.find('.anything').props() as any).light).toEqual('bamboo');
410411
});
411412

412413
describe('shouldUpdate', () => {
@@ -514,7 +515,7 @@ describe('Form.Basic', () => {
514515

515516
it('should trigger by setField', () => {
516517
const triggerUpdate = jest.fn();
517-
const formRef = React.createRef();
518+
const formRef = React.createRef<FormInstance>();
518519

519520
const wrapper = mount(
520521
<div>
@@ -632,6 +633,7 @@ describe('Form.Basic', () => {
632633
mount(
633634
<div>
634635
<Form>
636+
{/* @ts-ignore */}
635637
<Field>
636638
<h1 key="1">Light</h1>
637639
<h2 key="2">Bamboo</h2>
@@ -706,7 +708,7 @@ describe('Form.Basic', () => {
706708
});
707709

708710
it('should not crash when return value contains target field', async () => {
709-
const CustomInput = ({ value, onChange }) => {
711+
const CustomInput: React.FC<any> = ({ value, onChange }) => {
710712
const onInputChange = e => {
711713
onChange({
712714
value: e.target.value,
@@ -749,7 +751,7 @@ describe('Form.Basic', () => {
749751
autoComplete="off"
750752
>
751753
<Form.List name="users">
752-
{(fields, { add, remove }) => (
754+
{fields => (
753755
<>
754756
{fields.map(({ key, name, ...restField }) => (
755757
<Field
@@ -774,13 +776,13 @@ describe('Form.Basic', () => {
774776
};
775777

776778
const wrapper = mount(<Demo />);
777-
expect(wrapper.find('input').first().getDOMNode().value).toBe('11');
779+
expect(wrapper.find('input').first().getDOMNode<HTMLInputElement>().value).toBe('11');
778780
wrapper.find('.reset-btn').first().simulate('click');
779781
expect(wrapper.find('input').length).toBe(0);
780782
});
781783

782784
it('setFieldsValue should work for multiple Select', () => {
783-
const Select = ({ value, defaultValue }) => {
785+
const Select: React.FC<any> = ({ value, defaultValue }) => {
784786
return <div className="select-div">{(value || defaultValue || []).toString()}</div>;
785787
};
786788

@@ -808,7 +810,7 @@ describe('Form.Basic', () => {
808810
it('remount should not clear current value', () => {
809811
let refForm;
810812

811-
const Demo = ({ remount }) => {
813+
const Demo: React.FC<any> = ({ remount }) => {
812814
const [form] = Form.useForm();
813815
refForm = form;
814816

@@ -840,9 +842,9 @@ describe('Form.Basic', () => {
840842
});
841843

842844
it('setFieldValue', () => {
843-
const formRef = React.createRef();
845+
const formRef = React.createRef<FormInstance>();
844846

845-
const Demo = () => (
847+
const Demo: React.FC = () => (
846848
<Form ref={formRef} initialValues={{ list: ['bamboo', 'little', 'light'] }}>
847849
<Form.List name="list">
848850
{fields =>

0 commit comments

Comments
 (0)