|
1
|
import React from "react" |
|
2
|
import { beforeEach, describe, expect, test, vi } from "vitest" |
|
3
|
import { cleanup, fireEvent, render } from "@testing-library/react" |
|
4
|
import { usePathname } from "next/navigation" |
|
5
|
import AreaProvider, { useArea } from "../area" |
|
6
|
import { OpenAPI } from "types" |
|
7
|
|
|
8
|
// Mock functions |
|
9
|
const mockSetActivePath = vi.fn() |
|
10
|
const mockUseSidebar = vi.fn(() => ({ |
|
11
|
setActivePath: mockSetActivePath, |
|
12
|
})) |
|
13
|
const mockUsePathname = vi.fn(() => "/store/test") |
|
14
|
const mockCapitalize = vi.fn((str: string) => str.charAt(0).toUpperCase() + str.slice(1)) |
|
15
|
// Track previous values for usePrevious mock |
|
16
|
// usePrevious returns the value from the previous render |
|
17
|
let previousValue: unknown = undefined |
|
18
|
const mockUsePrevious = vi.fn((value: unknown) => { |
|
19
|
const result = previousValue |
|
20
|
previousValue = value |
|
21
|
return result |
|
22
|
}) |
|
23
|
|
|
24
|
// Test component that uses the hook |
|
25
|
const TestComponent = () => { |
|
26
|
const { area, prevArea, displayedArea, setArea } = useArea() |
|
27
|
return ( |
|
28
|
<div> |
|
29
|
<div data-testid="area">{area}</div> |
|
30
|
<div data-testid="prev-area">{prevArea || "undefined"}</div> |
|
31
|
<div data-testid="displayed-area">{displayedArea}</div> |
|
32
|
<button |
|
33
|
data-testid="set-area-store" |
|
34
|
onClick={() => setArea("store" as OpenAPI.Area)} |
|
35
|
> |
|
36
|
Set Store |
|
37
|
</button> |
|
38
|
<button |
|
39
|
data-testid="set-area-admin" |
|
40
|
onClick={() => setArea("admin" as OpenAPI.Area)} |
|
41
|
> |
|
42
|
Set Admin |
|
43
|
</button> |
|
44
|
</div> |
|
45
|
) |
|
46
|
} |
|
47
|
|
|
48
|
vi.mock("docs-ui", () => ({ |
|
49
|
capitalize: (str: string) => mockCapitalize(str), |
|
50
|
usePrevious: (value: unknown) => mockUsePrevious(value), |
|
51
|
useSidebar: () => mockUseSidebar(), |
|
52
|
})) |
|
53
|
|
|
54
|
vi.mock("next/navigation", () => ({ |
|
55
|
usePathname: () => mockUsePathname(), |
|
56
|
})) |
|
57
|
|
|
58
|
describe("AreaProvider", () => { |
|
59
|
beforeEach(() => { |
|
60
|
vi.clearAllMocks() |
|
61
|
cleanup() |
|
62
|
previousValue = undefined |
|
63
|
mockUsePathname.mockReturnValue("/store/test") |
|
64
|
mockCapitalize.mockImplementation((str: string) => str.charAt(0).toUpperCase() + str.slice(1)) |
|
65
|
}) |
|
66
|
|
|
67
|
describe("rendering", () => { |
|
68
|
test("renders children", () => { |
|
69
|
const { getByText } = render( |
|
70
|
<AreaProvider area="store"> |
|
71
|
<div>Test Content</div> |
|
72
|
</AreaProvider> |
|
73
|
) |
|
74
|
expect(getByText("Test Content")).toBeInTheDocument() |
|
75
|
}) |
|
76
|
}) |
|
77
|
|
|
78
|
describe("initial state", () => { |
|
79
|
test("initializes with passed area", () => { |
|
80
|
const { getByTestId } = render( |
|
81
|
<AreaProvider area="store"> |
|
82
|
<TestComponent /> |
|
83
|
</AreaProvider> |
|
84
|
) |
|
85
|
expect(getByTestId("area")).toHaveTextContent("store") |
|
86
|
}) |
|
87
|
|
|
88
|
test("displays capitalized area", () => { |
|
89
|
const { getByTestId } = render( |
|
90
|
<AreaProvider area="store"> |
|
91
|
<TestComponent /> |
|
92
|
</AreaProvider> |
|
93
|
) |
|
94
|
expect(getByTestId("displayed-area")).toHaveTextContent("Store") |
|
95
|
expect(mockCapitalize).toHaveBeenCalledWith("store") |
|
96
|
}) |
|
97
|
|
|
98
|
test("prevArea is undefined initially", () => { |
|
99
|
const { getByTestId } = render( |
|
100
|
<AreaProvider area="store"> |
|
101
|
<TestComponent /> |
|
102
|
</AreaProvider> |
|
103
|
) |
|
104
|
expect(getByTestId("prev-area")).toHaveTextContent("undefined") |
|
105
|
}) |
|
106
|
}) |
|
107
|
|
|
108
|
describe("setArea", () => { |
|
109
|
test("updates area when setArea is called", () => { |
|
110
|
const { getByTestId } = render( |
|
111
|
<AreaProvider area="store"> |
|
112
|
<TestComponent /> |
|
113
|
</AreaProvider> |
|
114
|
) |
|
115
|
const setAdminButton = getByTestId("set-area-admin") |
|
116
|
const areaElement = getByTestId("area") |
|
117
|
|
|
118
|
expect(areaElement).toHaveTextContent("store") |
|
119
|
fireEvent.click(setAdminButton) |
|
120
|
expect(areaElement).toHaveTextContent("admin") |
|
121
|
}) |
|
122
|
|
|
123
|
test("updates displayedArea when area changes", () => { |
|
124
|
const { getByTestId } = render( |
|
125
|
<AreaProvider area="store"> |
|
126
|
<TestComponent /> |
|
127
|
</AreaProvider> |
|
128
|
) |
|
129
|
const setAdminButton = getByTestId("set-area-admin") |
|
130
|
const displayedAreaElement = getByTestId("displayed-area") |
|
131
|
|
|
132
|
expect(displayedAreaElement).toHaveTextContent("Store") |
|
133
|
fireEvent.click(setAdminButton) |
|
134
|
expect(displayedAreaElement).toHaveTextContent("Admin") |
|
135
|
}) |
|
136
|
}) |
|
137
|
|
|
138
|
describe("useArea hook", () => { |
|
139
|
test("throws error when used outside provider", () => { |
|
140
|
const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {}) |
|
141
|
|
|
142
|
expect(() => { |
|
143
|
render(<TestComponent />) |
|
144
|
}).toThrow("useAreaProvider must be used inside an AreaProvider") |
|
145
|
|
|
146
|
consoleSpy.mockRestore() |
|
147
|
}) |
|
148
|
|
|
149
|
test("returns area, prevArea, displayedArea, and setArea", () => { |
|
150
|
const { getByTestId } = render( |
|
151
|
<AreaProvider area="store"> |
|
152
|
<TestComponent /> |
|
153
|
</AreaProvider> |
|
154
|
) |
|
155
|
expect(getByTestId("area")).toBeInTheDocument() |
|
156
|
expect(getByTestId("prev-area")).toBeInTheDocument() |
|
157
|
expect(getByTestId("displayed-area")).toBeInTheDocument() |
|
158
|
expect(getByTestId("set-area-store")).toBeInTheDocument() |
|
159
|
expect(getByTestId("set-area-admin")).toBeInTheDocument() |
|
160
|
}) |
|
161
|
}) |
|
162
|
}) |
|
163
|
|