1 import React from "react"
2 import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"
3 import { cleanup, fireEvent, render, waitFor } from "@testing-library/react"
4 import { GITHUB_ISSUES_LINK } from "../../../constants"
5 import { ButtonProps } from "../../Button"
6 import { LabelProps } from "../../Label"
7 import { RadioItemProps } from "../../RadioItem"
8
9 // mock data
10 const defaultUseSiteConfigReturn = {
11 config: {
12 reportIssueLink: GITHUB_ISSUES_LINK as string | undefined,
13 },
14 }
15
16 // mock functions
17 const mockTrack = vi.fn(({ event }: { event: { callback?: () => void } }) => {
18 // Call callback immediately to simulate synchronous behavior
19 event.callback?.()
20 })
21 const mockUseSiteConfig = vi.fn(() => defaultUseSiteConfigReturn)
22
23 // mock components
24 vi.mock("@/providers/Analytics", () => ({
25 useAnalytics: () => ({
26 track: mockTrack,
27 }),
28 }))
29
30 vi.mock("@/providers/SiteConfig", () => ({
31 useSiteConfig: () => mockUseSiteConfig(),
32 }))
33
34 vi.mock("@/components/Button", () => ({
35 Button: (props: ButtonProps) => <button {...props} />,
36 }))
37
38 vi.mock("@/components/TextArea", () => ({
39 TextArea: ({
40 value,
41 onChange,
42 placeholder,
43 rows,
44 }: {
45 value: string
46 onChange: (e: React.ChangeEvent<HTMLTextAreaElement>) => void
47 placeholder?: string
48 rows?: number
49 }) => (
50 <textarea
51 data-testid="textarea"
52 value={value}
53 onChange={onChange}
54 placeholder={placeholder}
55 rows={rows}
56 />
57 ),
58 }))
59
60 vi.mock("@/components/Label", () => ({
61 Label: (props: LabelProps) => <label {...props} />,
62 }))
63
64 vi.mock("@/components/DottedSeparator", () => ({
65 DottedSeparator: ({ wrapperClassName }: { wrapperClassName?: string }) => (
66 <div data-testid="dotted-separator" className={wrapperClassName} />
67 ),
68 }))
69
70 vi.mock("@/components/RadioItem", () => ({
71 RadioItem: (props: RadioItemProps) => <input {...props} />,
72 }))
73
74 vi.mock("@/components/Feedback/Solutions", () => ({
75 Solutions: ({
76 message,
77 feedback,
78 }: {
79 message: string
80 feedback: boolean
81 }) => (
82 <div
83 data-testid="solutions"
84 data-message={message}
85 data-feedback={feedback}
86 >
87 Solutions
88 </div>
89 ),
90 }))
91
92 vi.mock("react-transition-group", () => ({
93 CSSTransition: ({ children }: { children: React.ReactNode }) => (
94 <>{children}</>
95 ),
96 SwitchTransition: ({ children }: { children: React.ReactNode }) => (
97 <>{children}</>
98 ),
99 }))
100
101 import { Feedback } from "../../Feedback"
102
103 afterEach(() => {
104 cleanup()
105 })
106
107 beforeEach(() => {
108 mockTrack.mockClear()
109 mockUseSiteConfig.mockImplementation(() => defaultUseSiteConfigReturn)
110 })
111
112 describe("rendering", () => {
113 test("default render", () => {
114 const { container } = render(<Feedback event="test-event" />)
115 const positiveButton = container.querySelector(
116 "button[data-testid='positive-button']"
117 )
118 expect(positiveButton).toBeInTheDocument()
119 expect(positiveButton).toHaveTextContent("It was helpful")
120 const negativeButton = container.querySelector(
121 "button[data-testid='negative-button']"
122 )
123 expect(negativeButton).toBeInTheDocument()
124 expect(negativeButton).toHaveTextContent("It wasn't helpful")
125 const label = container.querySelector("label[data-testid='question-label']")
126 expect(label).toBeInTheDocument()
127 expect(label).toHaveTextContent("Was this page helpful?")
128
129 const reportIssueButton = container.querySelector(
130 "button[data-testid='report-issue-button']"
131 )
132 expect(reportIssueButton).toBeInTheDocument()
133 expect(reportIssueButton).toHaveTextContent("Report Issue")
134 const link = reportIssueButton?.querySelector("a")
135 expect(link).toBeInTheDocument()
136 expect(link).toHaveAttribute("href", GITHUB_ISSUES_LINK)
137 const separators = container.querySelectorAll(
138 "[data-testid='dotted-separator']"
139 )
140 expect(separators).toHaveLength(2)
141 })
142
143 test("renders custom question", () => {
144 const { container } = render(
145 <Feedback event="test-event" question="Custom question?" />
146 )
147 const label = container.querySelector("label[data-testid='question-label']")
148 expect(label).toBeInTheDocument()
149 expect(label).toHaveTextContent("Custom question?")
150 })
151
152 test("renders custom positive button text", () => {
153 const { container } = render(
154 <Feedback event="test-event" positiveBtn="Yes!" />
155 )
156 const buttons = container.querySelector(
157 "button[data-testid='positive-button']"
158 )
159 expect(buttons).toBeInTheDocument()
160 expect(buttons).toHaveTextContent("Yes!")
161 })
162
163 test("renders custom negative button text", () => {
164 const { container } = render(
165 <Feedback event="test-event" negativeBtn="No!" />
166 )
167 const buttons = container.querySelector(
168 "button[data-testid='negative-button']"
169 )
170 expect(buttons).toBeInTheDocument()
171 expect(buttons).toHaveTextContent("No!")
172 })
173
174 test("renders report issue button when initReportLink is provided", () => {
175 const customReportLink = "https://custom-link.com"
176 const { container } = render(
177 <Feedback event="test-event" reportLink={customReportLink} />
178 )
179 const reportIssueButton = container.querySelector(
180 "button[data-testid='report-issue-button']"
181 )
182 expect(reportIssueButton).toBeInTheDocument()
183 expect(reportIssueButton).toHaveTextContent("Report Issue")
184 const link = reportIssueButton?.querySelector("a")
185 expect(link).toBeInTheDocument()
186 expect(link).toHaveAttribute("href", customReportLink)
187 })
188
189 test("does not render report issue button when no reportLink is available", () => {
190 // Override the mock to return undefined for reportIssueLink
191 // Use mockImplementation (not Once) to ensure it applies to all calls during render
192 // const originalImplementation = mockUseSiteConfig.getMockImplementation()
193 mockUseSiteConfig.mockImplementation(() => ({
194 config: {
195 reportIssueLink: undefined,
196 },
197 }))
198
199 const { container } = render(<Feedback event="test-event" />)
200 const reportIssueButton = container.querySelector(
201 "button[data-testid='report-issue-button']"
202 )
203 expect(reportIssueButton).not.toBeInTheDocument()
204 })
205
206 test("does not render dotted separator when showDottedSeparator is false", () => {
207 const { container } = render(
208 <Feedback event="test-event" showDottedSeparator={false} />
209 )
210 const separators = container.querySelectorAll(
211 "[data-testid='dotted-separator']"
212 )
213 expect(separators).toHaveLength(0)
214 })
215
216 test("applies custom className", () => {
217 const { container } = render(
218 <Feedback event="test-event" className="custom-class" />
219 )
220 const wrapper = container.firstChild
221 expect(wrapper).toHaveClass("custom-class")
222 })
223
224 test("shows custom submit button text", () => {
225 const { container } = render(
226 <Feedback event="test-event" submitBtn="Send" />
227 )
228 const positiveButton = container.querySelector(
229 "button[data-testid='positive-button']"
230 )
231 fireEvent.click(positiveButton!)
232
233 const submitButton = container.querySelector(
234 "button[data-testid='submit-button']"
235 )
236 expect(submitButton).toBeInTheDocument()
237 expect(submitButton).toHaveTextContent("Send")
238 })
239
240 test("shows custom positive question", () => {
241 const { container } = render(
242 <Feedback event="test-event" positiveQuestion="What did you like most?" />
243 )
244 const positiveButton = container.querySelector(
245 "button[data-testid='positive-button']"
246 )
247 fireEvent.click(positiveButton!)
248
249 const label = container.querySelector(
250 "label[data-testid='submit-question-label']"
251 )
252 expect(label).toBeInTheDocument()
253 expect(label).toHaveTextContent("What did you like most?")
254 })
255
256 test("shows custom negative question", () => {
257 const { container } = render(
258 <Feedback event="test-event" negativeQuestion="What went wrong?" />
259 )
260 const negativeButton = container.querySelector(
261 "button[data-testid='negative-button']"
262 )
263 fireEvent.click(negativeButton!)
264
265 const label = container.querySelector(
266 "label[data-testid='submit-question-label']"
267 )
268 expect(label).toBeInTheDocument()
269 expect(label).toHaveTextContent("What went wrong?")
270 })
271 })
272
273 describe("positive feedback flow", () => {
274 test("shows form when positive button is clicked", () => {
275 const { container } = render(<Feedback event="test-event" />)
276 const positiveButton = container.querySelector(
277 "button[data-testid='positive-button']"
278 )
279 fireEvent.click(positiveButton!)
280
281 const label = container.querySelector(
282 "label[data-testid='submit-question-label']"
283 )
284 expect(label).toBeInTheDocument()
285 expect(label).toHaveTextContent("What did you like?")
286 })
287
288 test("shows positive feedback options", () => {
289 const { container } = render(<Feedback event="test-event" />)
290 const positiveButton = container.querySelector(
291 "button[data-testid='positive-button']"
292 )
293 fireEvent.click(positiveButton!)
294
295 const radioOptions = [
296 "Easy to understand",
297 "Accurate code and text",
298 "Exactly what I was looking for",
299 "Ease of use",
300 "Other",
301 ]
302
303 const feedbackOptions = Array.from(
304 container.querySelectorAll<HTMLInputElement>(
305 "[data-testid='feedback-option']"
306 )
307 )
308 expect(feedbackOptions).toHaveLength(5)
309 const optionValues = feedbackOptions.map((radio) => radio.value)
310 radioOptions.forEach((option) => {
311 expect(optionValues).toContain(option)
312 })
313 })
314
315 test("tracks positive feedback when button is clicked", () => {
316 const { container } = render(<Feedback event="test-event" />)
317 const positiveButton = container.querySelector(
318 "button[data-testid='positive-button']"
319 )
320 expect(positiveButton).toBeInTheDocument()
321 fireEvent.click(positiveButton!)
322
323 expect(mockTrack).toHaveBeenCalledWith({
324 event: {
325 event: "test-event",
326 options: {
327 feedback: "yes",
328 message: null,
329 feedbackOption: "Other",
330 },
331 callback: expect.any(Function),
332 },
333 })
334 })
335 })
336
337 describe("negative feedback flow", () => {
338 test("shows form when negative button is clicked", () => {
339 const { container } = render(<Feedback event="test-event" />)
340 const negativeButton = container.querySelector(
341 "button[data-testid='negative-button']"
342 )
343 expect(negativeButton).toBeInTheDocument()
344 fireEvent.click(negativeButton!)
345
346 const label = container.querySelector(
347 "label[data-testid='submit-question-label']"
348 )
349 expect(label).toBeInTheDocument()
350 expect(label).toHaveTextContent("What was the problem?")
351 })
352
353 test("shows negative feedback options", () => {
354 const { container } = render(<Feedback event="test-event" />)
355 const negativeButton = container.querySelector(
356 "button[data-testid='negative-button']"
357 )
358 expect(negativeButton).toBeInTheDocument()
359 fireEvent.click(negativeButton!)
360
361 const radioOptions = [
362 "Difficult to understand",
363 "Inaccurate code or text",
364 "Didn't find what I was looking for",
365 "Trouble using the documentation",
366 "Other",
367 ]
368
369 const feedbackOptions = Array.from(
370 container.querySelectorAll<HTMLInputElement>(
371 "[data-testid='feedback-option']"
372 )
373 )
374 expect(feedbackOptions).toHaveLength(5)
375 const optionValues = feedbackOptions.map((radio) => radio.value)
376 radioOptions.forEach((option) => {
377 expect(optionValues).toContain(option)
378 })
379 })
380
381 test("tracks negative feedback when button is clicked", () => {
382 const { container } = render(<Feedback event="test-event" />)
383 const negativeButton = container.querySelector(
384 "button[data-testid='negative-button']"
385 )
386 expect(negativeButton).toBeInTheDocument()
387 fireEvent.click(negativeButton!)
388
389 expect(mockTrack).toHaveBeenCalledWith({
390 event: {
391 event: "test-event",
392 options: {
393 feedback: "no",
394 message: null,
395 feedbackOption: "Other",
396 },
397 callback: expect.any(Function),
398 },
399 })
400 })
401 })
402
403 describe("form interaction", () => {
404 test("submits feedback with selected option and message", () => {
405 const { container } = render(<Feedback event="test-event" />)
406 const positiveButton = container.querySelector(
407 "button[data-testid='positive-button']"
408 )
409 fireEvent.click(positiveButton!)
410
411 // Select an option
412 const feedbackOption = container.querySelector(
413 "[data-testid='feedback-option']"
414 )
415 fireEvent.click(feedbackOption!)
416
417 // Enter message
418 const textarea = container.querySelector("textarea") as HTMLTextAreaElement
419 fireEvent.change(textarea, { target: { value: "Great documentation!" } })
420
421 // Clear previous track calls
422 mockTrack.mockClear()
423
424 // Submit
425 const submitButton = container.querySelector(
426 "button[data-testid='submit-button']"
427 )
428 expect(submitButton).toBeInTheDocument()
429 fireEvent.click(submitButton!)
430
431 expect(mockTrack).toHaveBeenCalledWith({
432 event: expect.objectContaining({
433 options: expect.objectContaining({
434 feedback: "yes",
435 message: "Great documentation!",
436 feedbackOption: (feedbackOption as HTMLInputElement)?.value,
437 }),
438 }),
439 })
440 })
441 })
442
443 describe("submission success", () => {
444 test("shows thank you message after submission", async () => {
445 const { container } = render(<Feedback event="test-event" />)
446 const positiveButton = container.querySelector(
447 "button[data-testid='positive-button']"
448 )
449 fireEvent.click(positiveButton!)
450
451 const submitButton = container.querySelector(
452 "button[data-testid='submit-button']"
453 )
454 expect(submitButton).toBeInTheDocument()
455 fireEvent.click(submitButton!)
456
457 await waitFor(() => {
458 const submittedMessage = container.querySelector(
459 "[data-testid='submitted-message']"
460 )
461 expect(submittedMessage).toBeInTheDocument()
462 expect(submittedMessage).toHaveTextContent(
463 "Thank you for helping improve our documentation!"
464 )
465 })
466 })
467
468 test("shows custom submit message", async () => {
469 const { container } = render(
470 <Feedback event="test-event" submitMessage="Custom thank you message!" />
471 )
472 const positiveButton = container.querySelector(
473 "button[data-testid='positive-button']"
474 )
475 fireEvent.click(positiveButton!)
476
477 const submitButton = container.querySelector(
478 "button[data-testid='submit-button']"
479 )
480 expect(submitButton).toBeInTheDocument()
481 fireEvent.click(submitButton!)
482
483 await waitFor(() => {
484 const submittedMessage = container.querySelector(
485 "[data-testid='submitted-message']"
486 )
487 expect(submittedMessage).toBeInTheDocument()
488 expect(submittedMessage).toHaveTextContent("Custom thank you message!")
489 })
490 })
491
492 test("shows solutions after submission when showPossibleSolutions is true", async () => {
493 const { container } = render(<Feedback event="test-event" />)
494 const positiveButton = container.querySelector(
495 "button[data-testid='positive-button']"
496 )
497 fireEvent.click(positiveButton!)
498
499 const textarea = container.querySelector("textarea") as HTMLTextAreaElement
500 fireEvent.change(textarea, { target: { value: "Test message" } })
501
502 const submitButton = container.querySelector(
503 "button[data-testid='submit-button']"
504 )
505 expect(submitButton).toBeInTheDocument()
506 fireEvent.click(submitButton!)
507
508 await waitFor(() => {
509 const solutions = container.querySelector("[data-testid='solutions']")
510 expect(solutions).toBeInTheDocument()
511 expect(solutions).toHaveAttribute("data-message", "Test message")
512 expect(solutions).toHaveAttribute("data-feedback", "true")
513 })
514 })
515
516 test("does not show solutions when showPossibleSolutions is false", async () => {
517 const { container } = render(
518 <Feedback event="test-event" showPossibleSolutions={false} />
519 )
520 const positiveButton = container.querySelector(
521 "button[data-testid='positive-button']"
522 )
523 fireEvent.click(positiveButton!)
524
525 const submitButton = container.querySelector(
526 "button[data-testid='submit-button']"
527 )
528 expect(submitButton).toBeInTheDocument()
529 fireEvent.click(submitButton!)
530
531 await waitFor(() => {
532 const solutions = container.querySelector("[data-testid='solutions']")
533 expect(solutions).not.toBeInTheDocument()
534 })
535 })
536
537 test("passes negative feedback to solutions", async () => {
538 const { container } = render(<Feedback event="test-event" />)
539 const negativeButton = container.querySelector(
540 "button[data-testid='negative-button']"
541 )
542 fireEvent.click(negativeButton!)
543
544 const textarea = container.querySelector("textarea") as HTMLTextAreaElement
545 fireEvent.change(textarea, { target: { value: "Issue message" } })
546
547 const submitButton = container.querySelector(
548 "button[data-testid='submit-button']"
549 )
550 expect(submitButton).toBeInTheDocument()
551 fireEvent.click(submitButton!)
552
553 await waitFor(() => {
554 const solutions = container.querySelector("[data-testid='solutions']")
555 expect(solutions).toHaveAttribute("data-feedback", "false")
556 })
557 })
558 })
559
560 describe("extra data tracking", () => {
561 test("includes extraData in tracking", () => {
562 const { container } = render(
563 <Feedback event="test-event" extraData={{ customField: "customValue" }} />
564 )
565 const positiveButton = container.querySelector(
566 "button[data-testid='positive-button']"
567 )
568 fireEvent.click(positiveButton!)
569
570 expect(mockTrack).toHaveBeenCalledWith({
571 event: expect.objectContaining({
572 options: expect.objectContaining({
573 customField: "customValue",
574 }),
575 }),
576 })
577 })
578 })
579
580 describe("layout", () => {
581 test("applies vertical layout classes when vertical is true", () => {
582 const { container } = render(<Feedback event="test-event" vertical />)
583 const feedbackForm = container.querySelector(
584 "[data-testid='feedback-form']"
585 )
586 expect(feedbackForm).toBeInTheDocument()
587 expect(feedbackForm).toHaveClass("flex-col justify-center")
588 })
589
590 test("applies horizontal layout classes when vertical is false", () => {
591 const { container } = render(
592 <Feedback event="test-event" vertical={false} />
593 )
594 const feedbackForm = container.querySelector(
595 "[data-testid='feedback-form']"
596 )
597 expect(feedbackForm).toBeInTheDocument()
598 expect(feedbackForm).toHaveClass("flex-col md:flex-row md:items-center")
599 })
600 })