-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: sync split hook with field & test use list field actions
- Loading branch information
1 parent
b942d2b
commit 4e6fa06
Showing
2 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [] }); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,7 +75,6 @@ export const useListFieldActions = < | |
} | ||
}, fields); | ||
|
||
return fields; | ||
return { ...fields }; | ||
}); | ||
}), | ||
|