-
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(list-atom): make pristine (!dirty) after new initialValue is set (#…
…107) Fixes #105 This requires the user to control the referential equality of the initialValue for the same values in order to prevent unnecessary updates.
- Loading branch information
1 parent
46d4753
commit 170312d
Showing
7 changed files
with
76 additions
and
42 deletions.
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
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
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 @@ | ||
export * from "./useListFieldInitialValue"; |
42 changes: 42 additions & 0 deletions
42
src/hooks/use-list-field-initial-value/useListFieldInitialValue.test.ts
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,42 @@ | ||
import { act, renderHook } from "@testing-library/react"; | ||
import { useFieldState } from "form-atoms"; | ||
import { describe, expect, it } from "vitest"; | ||
|
||
import { useListFieldInitialValue } from "./useListFieldInitialValue"; | ||
import { listAtom } from "../../atoms"; | ||
import { numberField } from "../../fields"; | ||
import { useListActions } from "../use-list-actions"; | ||
|
||
describe("useListFieldInitialValue()", () => { | ||
it("reinitializes the field value", async () => { | ||
const field = listAtom({ | ||
value: [] as number[], | ||
builder: (value) => numberField({ value }), | ||
}); | ||
|
||
const { result: state } = renderHook(() => useFieldState(field)); | ||
const { rerender } = renderHook( | ||
(props) => useListFieldInitialValue(field, props.initialValue), | ||
{ initialProps: { initialValue: [1, 2] } }, | ||
); | ||
|
||
// make list dirty | ||
const { result: listActions } = renderHook(() => useListActions(field)); | ||
await act(async () => listActions.current.add()); | ||
expect(state.current.dirty).toBe(true); | ||
|
||
const initialValue = [42, 84]; | ||
|
||
// initialization makes field pristine | ||
rerender({ initialValue }); | ||
expect(state.current.dirty).toBe(false); | ||
|
||
// make list dirty again | ||
await act(async () => listActions.current.add()); | ||
expect(state.current.dirty).toBe(true); | ||
|
||
// re-inititialation skipped with the same initialValue (useEffect dependency) | ||
rerender({ initialValue }); | ||
expect(state.current.dirty).toBe(true); | ||
}); | ||
}); |
File renamed without changes.
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 |
---|---|---|
@@ -1,2 +1 @@ | ||
export * from "./useListField"; | ||
export * from "./useListFieldInitialValue"; |
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