|
1 |
| -import React, { useEffect } from 'react'; |
2 | 1 | import { fireEvent, render } from '@testing-library/react';
|
| 2 | +import React, { useEffect } from 'react'; |
3 | 3 | import { act } from 'react-dom/test-utils';
|
4 | 4 | import Form, { Field, useForm } from '../src';
|
| 5 | +import type { FormInstance, ValidateMessages } from '../src/interface'; |
| 6 | +import { changeValue, getInput, matchError } from './common'; |
5 | 7 | import InfoField, { Input } from './common/InfoField';
|
6 |
| -import { changeValue, matchError, getInput } from './common'; |
7 | 8 | import timeout from './common/timeout';
|
8 |
| -import type { FormInstance, ValidateMessages } from '../src/interface'; |
9 | 9 |
|
10 | 10 | describe('Form.Validate', () => {
|
11 | 11 | it('required', async () => {
|
@@ -964,4 +964,73 @@ describe('Form.Validate', () => {
|
964 | 964 | await timeout();
|
965 | 965 | expect(container.querySelector('.errors').textContent).toEqual(`'test' is required`);
|
966 | 966 | });
|
| 967 | + |
| 968 | + it('validateDebounce', async () => { |
| 969 | + jest.useFakeTimers(); |
| 970 | + |
| 971 | + const validator = jest.fn( |
| 972 | + () => |
| 973 | + new Promise((_, reject) => { |
| 974 | + setTimeout(() => { |
| 975 | + reject(new Error('Not Correct')); |
| 976 | + }, 100); |
| 977 | + }), |
| 978 | + ); |
| 979 | + |
| 980 | + const formRef = React.createRef<FormInstance>(); |
| 981 | + |
| 982 | + const { container } = render( |
| 983 | + <Form ref={formRef}> |
| 984 | + <InfoField name="test" rules={[{ validator }]} validateDebounce={1000}> |
| 985 | + <Input /> |
| 986 | + </InfoField> |
| 987 | + </Form>, |
| 988 | + ); |
| 989 | + |
| 990 | + fireEvent.change(container.querySelector('input'), { |
| 991 | + target: { |
| 992 | + value: 'light', |
| 993 | + }, |
| 994 | + }); |
| 995 | + |
| 996 | + // Debounce should wait |
| 997 | + await act(async () => { |
| 998 | + await Promise.resolve(); |
| 999 | + jest.advanceTimersByTime(900); |
| 1000 | + await Promise.resolve(); |
| 1001 | + }); |
| 1002 | + expect(validator).not.toHaveBeenCalled(); |
| 1003 | + |
| 1004 | + // Debounce should work |
| 1005 | + await act(async () => { |
| 1006 | + await Promise.resolve(); |
| 1007 | + jest.advanceTimersByTime(1000); |
| 1008 | + await Promise.resolve(); |
| 1009 | + }); |
| 1010 | + expect(validator).toHaveBeenCalled(); |
| 1011 | + |
| 1012 | + // `validateFields` should ignore `validateDebounce` |
| 1013 | + validator.mockReset(); |
| 1014 | + formRef.current.validateFields(); |
| 1015 | + |
| 1016 | + await act(async () => { |
| 1017 | + await Promise.resolve(); |
| 1018 | + jest.advanceTimersByTime(200); |
| 1019 | + await Promise.resolve(); |
| 1020 | + }); |
| 1021 | + expect(validator).toHaveBeenCalled(); |
| 1022 | + |
| 1023 | + // `submit` should ignore `validateDebounce` |
| 1024 | + validator.mockReset(); |
| 1025 | + fireEvent.submit(container.querySelector('form')); |
| 1026 | + |
| 1027 | + await act(async () => { |
| 1028 | + await Promise.resolve(); |
| 1029 | + jest.advanceTimersByTime(200); |
| 1030 | + await Promise.resolve(); |
| 1031 | + }); |
| 1032 | + expect(validator).toHaveBeenCalled(); |
| 1033 | + |
| 1034 | + jest.useRealTimers(); |
| 1035 | + }); |
967 | 1036 | });
|
0 commit comments