1 | import { act, renderHook } from "@testing-library/react";
|
2 |
|
3 | import { useControlledValue } from "./useControlledValue";
|
4 |
|
5 | describe("when the value is controlled", () => {
|
6 | const defaultValue: string | undefined = "foo";
|
7 | const controlledValue: string | undefined = "bar";
|
8 | test("should return the controlled value", () => {
|
9 | const { result } = renderHook(() =>
|
10 | useControlledValue<string>(defaultValue, controlledValue)
|
11 | );
|
12 | expect(result.current[0]).toBe(controlledValue);
|
13 | });
|
14 | describe("when setting a new value", () => {
|
15 | const newValue = "taz";
|
16 | test("should return the controlled value instead", async () => {
|
17 | const { result } = renderHook(() =>
|
18 | useControlledValue<string>(defaultValue, controlledValue)
|
19 | );
|
20 | act(() => result.current[1](newValue));
|
21 | expect(result.current[0]).toBe(controlledValue);
|
22 | });
|
23 | });
|
24 | });
|
25 |
|
26 | describe("when the value is not controlled", () => {
|
27 | const defaultValue = "foo";
|
28 | const controlledValue = undefined;
|
29 | test("should return the value", () => {
|
30 | const { result } = renderHook(() =>
|
31 | useControlledValue<string>(defaultValue, controlledValue)
|
32 | );
|
33 | expect(result.current[0]).toBe(defaultValue);
|
34 | });
|
35 | describe("when setting a new value", () => {
|
36 | const newValue = "bar";
|
37 | test("should return the new value", async () => {
|
38 | const { result } = renderHook(() =>
|
39 | useControlledValue<string>(defaultValue, controlledValue)
|
40 | );
|
41 | act(() => result.current[1](newValue));
|
42 | expect(result.current[0]).toBe(newValue);
|
43 | });
|
44 | });
|
45 | });
|