forked from chakra-ui/chakra-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request chakra-ui#5445 from rjokelai/fix/useConst-init-fn-…
…type fix: useConst ts issue
- Loading branch information
Showing
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@chakra-ui/hooks": patch | ||
--- | ||
|
||
fix useConst types when using init function |
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,33 @@ | ||
import { renderHook } from "@chakra-ui/test-utils" | ||
import { act } from "react-test-renderer" | ||
import { useConst } from "../src" | ||
|
||
type FooType = { foo: string } | ||
|
||
test("should return constant value for constant initialization", () => { | ||
const obj: FooType = { foo: "bar" } | ||
const { rerender, result } = renderHook(() => useConst<FooType>(obj)) | ||
expect(result.current).toBe(obj) | ||
act(() => rerender()) | ||
expect(result.current).toBe(obj) | ||
}) | ||
|
||
test("should return constant value even if props change", () => { | ||
const obj: FooType = { foo: "bar" } | ||
const obj2: FooType = { foo: "baz" } | ||
const { rerender, result } = renderHook(({ p }) => useConst<FooType>(p), { | ||
initialProps: { p: obj }, | ||
}) | ||
expect(result.current).toBe(obj) | ||
act(() => rerender({ p: obj2 })) | ||
expect(result.current).toBe(obj) | ||
}) | ||
|
||
test("should return constant value from init function", () => { | ||
const { rerender, result } = renderHook(() => | ||
useConst<FooType>(() => ({ foo: "bar" })), | ||
) | ||
const obj: FooType = result.current | ||
act(() => rerender()) | ||
expect(result.current).toBe(obj) | ||
}) |