|
1
|
import { act, renderHook } from "@testing-library/react" |
|
2
|
|
|
3
|
import { useToggleState } from "./use-toggle-state" |
|
4
|
|
|
5
|
describe("useToggleState", () => { |
|
6
|
test("should return a state and a function to toggle it", () => { |
|
7
|
const { result } = renderHook(() => useToggleState()) |
|
8
|
|
|
9
|
expect(result.current[0]).toBe(false) |
|
10
|
expect(typeof result.current[1]).toBe("function") |
|
11
|
}) |
|
12
|
|
|
13
|
test("should return a state and a function to toggle it with an initial value", () => { |
|
14
|
const { result } = renderHook(() => useToggleState(true)) |
|
15
|
|
|
16
|
expect(result.current[0]).toBe(true) |
|
17
|
}) |
|
18
|
|
|
19
|
test("should update the state when the toggle function is called", () => { |
|
20
|
const { result } = renderHook(() => useToggleState()) |
|
21
|
|
|
22
|
expect(result.current[0]).toBe(false) |
|
23
|
|
|
24
|
act(() => { |
|
25
|
result.current[1]() |
|
26
|
}) |
|
27
|
|
|
28
|
expect(result.current[0]).toBe(true) |
|
29
|
}) |
|
30
|
|
|
31
|
test("should update the state when the close function is called", () => { |
|
32
|
const { result } = renderHook(() => useToggleState(true)) |
|
33
|
|
|
34
|
expect(result.current[0]).toBe(true) |
|
35
|
|
|
36
|
act(() => { |
|
37
|
result.current[2]() |
|
38
|
}) |
|
39
|
|
|
40
|
expect(result.current[0]).toBe(false) |
|
41
|
}) |
|
42
|
|
|
43
|
test("should update the state when the open function is called", () => { |
|
44
|
const { result } = renderHook(() => useToggleState()) |
|
45
|
|
|
46
|
expect(result.current[0]).toBe(false) |
|
47
|
|
|
48
|
act(() => { |
|
49
|
result.current[3]() |
|
50
|
}) |
|
51
|
|
|
52
|
expect(result.current[0]).toBe(true) |
|
53
|
}) |
|
54
|
}) |