Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/hooks/useNotifyWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { FormStore } from './useForm';
/**
* Call action with delay in macro task.
*/
const macroTask = (fn: VoidFunction) => {
export const macroTask = (fn: VoidFunction) => {
const channel = new MessageChannel();
channel.port1.onmessage = fn;
channel.port2.postMessage(null);
Expand Down
7 changes: 5 additions & 2 deletions src/utils/delayUtil.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { macroTask } from '../hooks/useNotifyWatch';
import raf from '@rc-component/util/lib/raf';

export default async function delayFrame() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The async keyword is redundant here as the function body does not use await. Since you are explicitly creating and returning a Promise, you can remove async and add an explicit return type for better clarity and to avoid unnecessarily wrapping a promise in another promise.

Suggested change
export default async function delayFrame() {
export default function delayFrame(): Promise<void> {

return new Promise<void>(resolve => {
raf(() => {
resolve();
macroTask(() => {
raf(() => {
resolve();
});
});
});
}
9 changes: 8 additions & 1 deletion tests/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import timeout from './timeout';
import timeout, { waitFakeTime } from './timeout';
import { matchNamePath } from '../../src/utils/valueUtil';
import { fireEvent, act } from '@testing-library/react';

Expand Down Expand Up @@ -46,6 +46,13 @@ export async function changeValue(wrapper: HTMLElement, value: string | string[]
}
}

export async function changeValueWithMockTimer(wrapper: HTMLElement, value: string | string[]) {
const promise = changeValue(wrapper, value);

await waitFakeTime();
return promise;
}

export function matchError(
wrapper: HTMLElement,
error?: boolean | string,
Expand Down
9 changes: 5 additions & 4 deletions tests/dependencies.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { FormInstance } from '../src';
import Form, { Field } from '../src';
import { waitFakeTime } from './common/timeout';
import InfoField, { Input } from './common/InfoField';
import { changeValue, matchError, getInput } from './common';
import { changeValue, matchError, getInput, changeValueWithMockTimer } from './common';
import { fireEvent, render } from '@testing-library/react';

describe('Form.Dependencies', () => {
Expand All @@ -12,6 +12,7 @@ describe('Form.Dependencies', () => {
});

it('touched', async () => {
jest.useFakeTimers();
const form = React.createRef<FormInstance>();

const { container } = render(
Expand All @@ -24,12 +25,13 @@ describe('Form.Dependencies', () => {
);

// Not trigger if not touched
await changeValue(getInput(container, 0), ['bamboo', '']);
await changeValueWithMockTimer(getInput(container, 0), ['bamboo', '']);
matchError(getInput(container, 1, true), false);

// Trigger if touched
form.current?.setFields([{ name: 'field_2', touched: true }]);
await changeValue(getInput(container, 0), ['bamboo', '']);

await changeValueWithMockTimer(getInput(container, 0), ['bamboo', '']);
matchError(getInput(container, 1, true), true);
});

Expand Down Expand Up @@ -260,7 +262,6 @@ describe('Form.Dependencies', () => {

<Field shouldUpdate={() => false}>
{() => {

counter += 1;
return null;
}}
Expand Down
Loading