-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
42 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { renderHook, cleanup } from "@testing-library/react-hooks"; | ||
import { useDeepMemo } from "../src/common/utils.js"; | ||
import { expect, describe, it, afterEach } from "vitest"; | ||
|
||
describe("useDeepMemo", () => { | ||
afterEach(cleanup); | ||
|
||
it("returns the initial value", () => { | ||
const initialValue = { a: 1 }; | ||
const { result } = renderHook(() => useDeepMemo(initialValue)); | ||
expect(result.current).toEqual(initialValue); | ||
}); | ||
|
||
it("updates the value on deep change", () => { | ||
const initialValue = { a: 1 }; | ||
const changedValue = { a: 2 }; | ||
const { result, rerender } = renderHook(({ value }) => useDeepMemo(value), { | ||
initialProps: { value: initialValue }, | ||
}); | ||
|
||
expect(result.current).toEqual(initialValue); | ||
|
||
// Change the value deeply | ||
rerender({ value: changedValue }); | ||
expect(result.current).toEqual(changedValue); | ||
}); | ||
|
||
it("does not update the value on shallow/no change", () => { | ||
const initialValue = { a: 1 }; | ||
const sameValue = { a: 1 }; | ||
const { result, rerender } = renderHook(({ value }) => useDeepMemo(value), { | ||
initialProps: { value: initialValue }, | ||
}); | ||
|
||
expect(result.current).toEqual(initialValue); | ||
|
||
// Re-render with the same value (shallow change) | ||
rerender({ value: sameValue }); | ||
expect(result.current).toEqual(initialValue); | ||
}); | ||
}); |