-
Notifications
You must be signed in to change notification settings - Fork 560
Expand file tree
/
Copy pathuse-controlled-state.test.ts
More file actions
50 lines (44 loc) · 1.25 KB
/
use-controlled-state.test.ts
File metadata and controls
50 lines (44 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { afterEach, describe, expect, it } from "vitest";
import { renderHook, cleanupHooks, actHooks } from "@reach-internal/test/utils";
import { useControlledState } from "@reach/utils";
afterEach(cleanupHooks);
describe("useControlledState", () => {
const DEFAULT_VALUE = 10;
const CONTROLLED_VALUE = 42;
it("should return value and setter", () => {
const { result } = renderHook(() =>
useControlledState({
defaultValue: DEFAULT_VALUE,
controlledValue: undefined,
})
);
expect(result.current[0]).toBe(DEFAULT_VALUE);
expect(typeof result.current[1]).toBe("function");
});
it("should work as uncontrolled", () => {
const { result } = renderHook(() =>
useControlledState({
defaultValue: DEFAULT_VALUE,
controlledValue: undefined,
})
);
expect(result.current[0]).toBe(DEFAULT_VALUE);
actHooks(() => {
result.current[1](17);
});
expect(result.current[0]).toBe(17);
});
it("should work as controlled", () => {
const { result } = renderHook(() =>
useControlledState({
defaultValue: DEFAULT_VALUE,
controlledValue: CONTROLLED_VALUE,
})
);
expect(result.current[0]).toBe(CONTROLLED_VALUE);
actHooks(() => {
result.current[1](17);
});
expect(result.current[0]).toBe(CONTROLLED_VALUE);
});
});