1 import React from "react"
2 import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"
3 import { cleanup, fireEvent, render } from "@testing-library/react"
4 import { NotificationProvider, useNotifications } from "../index"
5 import type { NotificationItemType } from "../index"
6
7 // mock components
8 vi.mock("@/components/Notification", () => ({
9 NotificationContainer: () => <div data-testid="notification-container" />,
10 }))
11
12 vi.mock("react-uuid", () => ({
13 default: vi.fn(() => "mock-uuid"),
14 }))
15
16 const TestComponent = () => {
17 const notifications = useNotifications()
18 if (!notifications) {
19 return null
20 }
21
22 const {
23 notifications: notifs,
24 addNotification,
25 removeNotification,
26 updateNotification,
27 generateId,
28 } = notifications
29
30 return (
31 <div>
32 <div data-testid="count">{notifs.length}</div>
33 <div data-testid="notifications">
34 {notifs.map((notif) => (
35 <div key={notif.id} data-testid={`notification-${notif.id}`}>
36 {notif.title}
37 </div>
38 ))}
39 </div>
40 <button
41 data-testid="add"
42 onClick={() =>
43 addNotification({
44 title: "Test Notification",
45 type: "info",
46 })
47 }
48 >
49 Add
50 </button>
51 <button
52 data-testid="remove"
53 onClick={() => {
54 if (notifs[0]) {
55 removeNotification(notifs[0].id!)
56 }
57 }}
58 >
59 Remove
60 </button>
61 <button
62 data-testid="update"
63 onClick={() => {
64 if (notifs[0]) {
65 updateNotification(notifs[0].id!, { title: "Updated" })
66 }
67 }}
68 >
69 Update
70 </button>
71 <button data-testid="generate-id" onClick={() => generateId()}>
72 Generate ID
73 </button>
74 </div>
75 )
76 }
77
78 beforeEach(() => {
79 vi.clearAllMocks()
80 })
81
82 afterEach(() => {
83 cleanup()
84 })
85
86 describe("rendering", () => {
87 test("renders children", () => {
88 const { container } = render(
89 <NotificationProvider>
90 <div>Test</div>
91 </NotificationProvider>
92 )
93 expect(container).toHaveTextContent("Test")
94 })
95
96 test("renders NotificationContainer", () => {
97 const { getByTestId } = render(
98 <NotificationProvider>
99 <div>Test</div>
100 </NotificationProvider>
101 )
102 expect(getByTestId("notification-container")).toBeInTheDocument()
103 })
104 })
105
106 describe("useNotifications hook", () => {
107 test("initializes with empty notifications", () => {
108 const { getByTestId } = render(
109 <NotificationProvider>
110 <TestComponent />
111 </NotificationProvider>
112 )
113
114 expect(getByTestId("count")).toHaveTextContent("0")
115 })
116
117 test("initializes with initial notifications", () => {
118 const initial: NotificationItemType[] = [
119 {
120 id: "1",
121 title: "Initial",
122 type: "info",
123 },
124 ]
125
126 const { getByTestId } = render(
127 <NotificationProvider initial={initial}>
128 <TestComponent />
129 </NotificationProvider>
130 )
131
132 expect(getByTestId("count")).toHaveTextContent("1")
133 expect(getByTestId("notification-1")).toHaveTextContent("Initial")
134 })
135
136 test("addNotification adds a notification", () => {
137 const { getByTestId } = render(
138 <NotificationProvider>
139 <TestComponent />
140 </NotificationProvider>
141 )
142
143 expect(getByTestId("count")).toHaveTextContent("0")
144
145 fireEvent.click(getByTestId("add"))
146
147 expect(getByTestId("count")).toHaveTextContent("1")
148 expect(getByTestId("notification-mock-uuid")).toHaveTextContent(
149 "Test Notification"
150 )
151 })
152
153 test("addNotification generates id if not provided", () => {
154 const { getByTestId } = render(
155 <NotificationProvider>
156 <TestComponent />
157 </NotificationProvider>
158 )
159
160 fireEvent.click(getByTestId("add"))
161
162 expect(getByTestId("notification-mock-uuid")).toBeInTheDocument()
163 })
164
165 test("removeNotification removes a notification", () => {
166 const initial: NotificationItemType[] = [
167 {
168 id: "1",
169 title: "Test",
170 type: "info",
171 },
172 ]
173
174 const { getByTestId } = render(
175 <NotificationProvider initial={initial}>
176 <TestComponent />
177 </NotificationProvider>
178 )
179
180 expect(getByTestId("count")).toHaveTextContent("1")
181
182 fireEvent.click(getByTestId("remove"))
183
184 expect(getByTestId("count")).toHaveTextContent("0")
185 })
186
187 test("updateNotification updates a notification", () => {
188 const initial: NotificationItemType[] = [
189 {
190 id: "1",
191 title: "Original",
192 type: "info",
193 },
194 ]
195
196 const { getByTestId } = render(
197 <NotificationProvider initial={initial}>
198 <TestComponent />
199 </NotificationProvider>
200 )
201
202 expect(getByTestId("notification-1")).toHaveTextContent("Original")
203
204 fireEvent.click(getByTestId("update"))
205
206 expect(getByTestId("notification-1")).toHaveTextContent("Updated")
207 })
208
209 test("generateId generates a unique id", () => {
210 const { getByTestId } = render(
211 <NotificationProvider>
212 <TestComponent />
213 </NotificationProvider>
214 )
215
216 getByTestId("generate-id").click()
217 // The generateId function is mocked to return "mock-uuid"
218 // This test verifies it's callable
219 expect(getByTestId("generate-id")).toBeInTheDocument()
220 })
221
222 test("throws error when used outside provider", () => {
223 const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {})
224
225 expect(() => {
226 render(<TestComponent />)
227 }).toThrow("useNotifications must be used within a NotificationProvider")
228
229 consoleSpy.mockRestore()
230 })
231
232 test("returns null when suppressError is true", () => {
233 const TestComponentSuppress = () => {
234 const notifications = useNotifications(true)
235 return <div>{notifications ? "has context" : "no context"}</div>
236 }
237
238 const { container } = render(<TestComponentSuppress />)
239 expect(container).toHaveTextContent("no context")
240 })
241 })