-
Notifications
You must be signed in to change notification settings - Fork 2
/
form.spec.tsx
48 lines (45 loc) · 1.57 KB
/
form.spec.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { createForm } from "@formily/core";
import { Field, FormConsumer, FormProvider, ObjectField, VoidField, useParentForm } from "@formily/react";
import { render } from "@testing-library/react";
import { FC, HTMLAttributes, PropsWithChildren } from "react";
const DisplayParentForm: FC<PropsWithChildren<HTMLAttributes<HTMLDivElement>>> = ({ children, ...props }) => {
const form = useParentForm();
return (
<div {...props}>
{children}
{form.displayName}
</div>
);
};
// 渲染表单
test("render form", () => {
const form = createForm();
render(
<FormProvider form={form}>
<FormConsumer>{form => `${form.mounted}`}</FormConsumer>
</FormProvider>,
);
expect(form.mounted).toBeTruthy();
});
// 查询最近的 ObjectField 或 Form
test("useParentForm", () => {
const form = createForm();
const { queryByTestId } = render(
<FormProvider form={form}>
<ObjectField name="aa">
<Field name="bb">
<DisplayParentForm data-testid="111" />
</Field>
</ObjectField>
<VoidField name="cc">
<Field name="dd">
<DisplayParentForm data-testid="222" />
</Field>
</VoidField>
<DisplayParentForm data-testid="333" />
</FormProvider>,
);
expect(queryByTestId("111")?.textContent).toBe("ObjectField");
expect(queryByTestId("222")?.textContent).toBe("Form");
expect(queryByTestId("333")?.textContent).toBe("Form");
});