Skip to content

Commit

Permalink
refactor test code
Browse files Browse the repository at this point in the history
  • Loading branch information
LunaTK committed Nov 8, 2023
1 parent 564dc8f commit d3c1c66
Showing 1 changed file with 19 additions and 31 deletions.
50 changes: 19 additions & 31 deletions packages/core/src/AutoForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,8 @@ describe("AutoForm", () => {
}
`).root;

const handleSubmit = vi.fn();
const dom = render(
<MockApp
onSubmit={handleSubmit}
namespace={namespace}
messageType="RepeatedString"
>
<button id="submit" />
</MockApp>
);
const { handleSubmit, dom } = renderAutoForm(namespace, "RepeatedString");

fireEvent.click(dom.queryByTestId("add-btn")!);
await vi.waitUntil(() => dom.queryByTestId("delete-btn") !== null);
fireEvent.click(dom.container.querySelector("#submit")!);
Expand Down Expand Up @@ -82,17 +74,8 @@ describe("AutoForm", () => {
repeated Child children = 1;
}
`).root;

const handleSubmit = vi.fn();
const dom = render(
<MockApp
onSubmit={handleSubmit}
namespace={namespace}
messageType="Parent"
>
<button id="submit" />
</MockApp>
);

const { handleSubmit, dom } = renderAutoForm(namespace, "Parent");

fireEvent.click(dom.queryByTestId("add-btn")!);
await vi.waitUntil(() => dom.queryByTestId("delete-btn") !== null);
Expand All @@ -119,16 +102,7 @@ describe("AutoForm", () => {
}
`).root;

const handleSubmit = vi.fn();
const dom = render(
<MockApp
onSubmit={handleSubmit}
namespace={namespace}
messageType="Parent"
>
<button id="submit" />
</MockApp>
);
const { handleSubmit, dom } = renderAutoForm(namespace, "Parent");

fireEvent.click(dom.queryByTestId("add-btn")!);
await vi.waitUntil(() => dom.queryByTestId("delete-btn") !== null);
Expand All @@ -141,3 +115,17 @@ describe("AutoForm", () => {
).toEqual([{ children: {"": {"val": {}}} }]);
})
});

function renderAutoForm(namespace: protobuf.Namespace, messageType: string) {
const handleSubmit = vi.fn();
const dom = render(
<MockApp
onSubmit={handleSubmit}
namespace={namespace}
messageType={messageType}
>
<button id="submit" />
</MockApp>
);
return { handleSubmit, dom };
}

0 comments on commit d3c1c66

Please sign in to comment.