Skip to content

Commit

Permalink
fix: sync split hook with field & test use list field actions
Browse files Browse the repository at this point in the history
  • Loading branch information
MiroslavPetrik committed Oct 23, 2023
1 parent b942d2b commit 4e6fa06
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
65 changes: 65 additions & 0 deletions src/components/list-field/useListFieldActions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { renderHook, act } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";

import { useListFieldActions } from "./useListFieldActions";
import { formAtom, useFormSubmit } from "form-atoms";
import { numberField } from "../../fields";

describe("useListFieldActions()", () => {
describe("adding item to the list", () => {
it("submits the new item", async () => {
const form = formAtom({
luckyNumbers: [numberField({ value: 6 })],
});

const builder = () => numberField({ value: 9 });

const { result: listFieldAction } = renderHook(() =>
useListFieldActions(
form,
builder,
["luckyNumbers"],
(field) => `${field}`
)
);

const { result: formSubmit } = renderHook(() => useFormSubmit(form));

await act(() => listFieldAction.current.add());
await act(() => listFieldAction.current.add());

const onSubmit = vi.fn();
await act(() => formSubmit.current(onSubmit)());

expect(onSubmit).toHaveBeenCalledWith({ luckyNumbers: [6, 9, 9] });
});
});

describe("removing item from the list", () => {
it("submits without the removed item", async () => {
const form = formAtom({
luckyNumbers: [numberField({ value: 6 })],
});

const builder = () => numberField();

const { result: listFieldAction } = renderHook(() =>
useListFieldActions(
form,
builder,
["luckyNumbers"],
(field) => `${field}`
)
);

const { result: formSubmit } = renderHook(() => useFormSubmit(form));

await act(() => listFieldAction.current.items[0]?.remove());

const onSubmit = vi.fn();
await act(() => formSubmit.current(onSubmit)());

expect(onSubmit).toHaveBeenCalledWith({ luckyNumbers: [] });
});
});
});
1 change: 0 additions & 1 deletion src/components/list-field/useListFieldActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ export const useListFieldActions = <
}
}, fields);

return fields;
return { ...fields };
});
}),
Expand Down

0 comments on commit 4e6fa06

Please sign in to comment.