Skip to content

Commit

Permalink
feat: setup test for Map (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
LunaTK committed Nov 7, 2023
1 parent ea03363 commit a8f4d00
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 19 deletions.
74 changes: 57 additions & 17 deletions packages/core/src/AutoForm.test.tsx
Original file line number Diff line number Diff line change
@@ -1,41 +1,81 @@
import { describe, it, vi, expect } from 'vitest';
import { fireEvent, render, prettyDOM } from '@testing-library/react';
import { createAutoForm } from './AutoForm';
import { useForm } from 'react-hook-form';
import protobuf from 'protobufjs';
import React from 'react';
import { describe, it, vi, expect, afterEach } from "vitest";
import {
fireEvent,
render,
prettyDOM,
cleanup,
waitFor,
} from "@testing-library/react";
import { createAutoForm } from "./AutoForm";
import { useForm } from "react-hook-form";
import protobuf from "protobufjs";
import React from "react";

const namespace = protobuf.parse(`
syntax = "proto3";
message Child {
message GrandChild {
map<int32, string> items = 1;
}
message Child {
GrandChild gc = 1;
}
message Parent {
repeated Child children = 1;
repeated string children = 1;
}
`).root;

const MockApp: React.FC<{ onSubmit: (values: unknown) => void }> = (props) => {
const form = useForm();
const { AutoForm } = createAutoForm({
form,
messageType: 'Parent',
messageType: "Parent",
namespace,
})
});

return <AutoForm onSubmitValid={props.onSubmit}><button id="submit">submit</button></AutoForm>
}
return (
<AutoForm onSubmitValid={props.onSubmit}>
<button id="submit">submit</button>
</AutoForm>
);
};

describe('AutoForm', () => {
it('Form submission', async () => {
afterEach(cleanup);

describe("AutoForm", () => {
it("Form submission", async () => {
const handleSubmit = vi.fn();
const dom = render(<MockApp onSubmit={handleSubmit}><button id="submit" /></MockApp>);
const submitButton = dom.container.querySelector('#submit');
const dom = render(
<MockApp onSubmit={handleSubmit}>
<button id="submit" />
</MockApp>
);
const submitButton = dom.container.querySelector("#submit");
expect(submitButton).toBeDefined();
fireEvent.click(submitButton!);
await vi.waitFor(() => handleSubmit.mock.calls.length > 0);
expect(handleSubmit.mock.calls.length).toBe(1);
});
})

it("Add map item", async () => {
const handleSubmit = vi.fn();
const dom = render(
<MockApp
onSubmit={(values) => {
console.log(values);
handleSubmit(values);
}}
>
<button id="submit" />
</MockApp>
);
fireEvent.click(dom.queryByTestId("add-btn")!);
await vi.waitUntil(() => dom.queryByTestId("delete-btn") !== null);
fireEvent.click(dom.container.querySelector("#submit")!);
await waitFor(() => expect(handleSubmit).toHaveBeenCalledTimes(1));

expect(handleSubmit.mock.calls[0]).toEqual([{ children: [""] }]);
});
});
2 changes: 1 addition & 1 deletion packages/core/src/common/AddButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface Props {
}

const AddButton: React.FC<Props> = ({ onClick }) => (
<button type="button" className="btn btn-xs" onClick={onClick}>
<button type="button" className="btn btn-xs" onClick={onClick} data-testid="add-btn">
<PlusIcon />
Add
</button>
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/common/DelButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface Props {
}

const AddButton: React.FC<Props> = ({ onClick }) => (
<button type="button" className="btn btn-xs btn-error" onClick={onClick}>
<button type="button" className="btn btn-xs btn-error" onClick={onClick} data-testid="delete-btn">
<MinusIcon />
</button>
);
Expand Down

0 comments on commit a8f4d00

Please sign in to comment.