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 { ModalProvider, useModal } from "../index"
5
6 // mock components
7 vi.mock("@/components/Modal", () => ({
8 Modal: ({
9 children,
10 open,
11 onClose,
12 ...props
13 }: {
14 children?: React.ReactNode
15 open?: boolean
16 onClose?: () => void
17 [key: string]: unknown
18 }) => (
19 <div data-testid="modal" data-open={open} onClick={onClose} {...props}>
20 {children}
21 </div>
22 ),
23 }))
24
25 const TestComponent = () => {
26 const { modalProps, setModalProps, closeModal } = useModal()
27 return (
28 <div>
29 <div data-testid="has-modal">{modalProps ? "yes" : "no"}</div>
30 <button
31 data-testid="open-modal"
32 onClick={() =>
33 setModalProps({
34 children: <div>Modal Content</div>,
35 open: true,
36 })
37 }
38 >
39 Open Modal
40 </button>
41 <button data-testid="close-modal" onClick={closeModal}>
42 Close Modal
43 </button>
44 </div>
45 )
46 }
47
48 beforeEach(() => {
49 vi.clearAllMocks()
50 })
51
52 afterEach(() => {
53 cleanup()
54 })
55
56 describe("rendering", () => {
57 test("renders children", () => {
58 const { container } = render(
59 <ModalProvider>
60 <div>Test</div>
61 </ModalProvider>
62 )
63 expect(container).toHaveTextContent("Test")
64 })
65
66 test("renders modal when modalProps is set", () => {
67 const { getByTestId } = render(
68 <ModalProvider>
69 <TestComponent />
70 </ModalProvider>
71 )
72
73 expect(getByTestId("has-modal")).toHaveTextContent("no")
74 expect(() => getByTestId("modal")).toThrow()
75
76 fireEvent.click(getByTestId("open-modal"))
77
78 expect(getByTestId("has-modal")).toHaveTextContent("yes")
79 expect(getByTestId("modal")).toBeInTheDocument()
80 expect(getByTestId("modal")).toHaveAttribute("data-open", "true")
81 })
82
83 test("does not render its own overlay — the modal's dialog is the overlay", () => {
84 const { getByTestId, container } = render(
85 <ModalProvider>
86 <TestComponent />
87 </ModalProvider>
88 )
89
90 fireEvent.click(getByTestId("open-modal"))
91
92 expect(container.querySelector(".bg-medusa-bg-overlay")).toBeNull()
93 })
94 })
95
96 describe("useModal hook", () => {
97 test("modalProps is null initially", () => {
98 const { getByTestId } = render(
99 <ModalProvider>
100 <TestComponent />
101 </ModalProvider>
102 )
103
104 expect(getByTestId("has-modal")).toHaveTextContent("no")
105 })
106
107 test("setModalProps sets modalProps", () => {
108 const { getByTestId } = render(
109 <ModalProvider>
110 <TestComponent />
111 </ModalProvider>
112 )
113
114 fireEvent.click(getByTestId("open-modal"))
115
116 expect(getByTestId("has-modal")).toHaveTextContent("yes")
117 expect(getByTestId("modal")).toBeInTheDocument()
118 })
119
120 test("closeModal clears modalProps", () => {
121 const { getByTestId } = render(
122 <ModalProvider>
123 <TestComponent />
124 </ModalProvider>
125 )
126
127 fireEvent.click(getByTestId("open-modal"))
128 expect(getByTestId("has-modal")).toHaveTextContent("yes")
129
130 fireEvent.click(getByTestId("close-modal"))
131
132 expect(getByTestId("has-modal")).toHaveTextContent("no")
133 expect(() => getByTestId("modal")).toThrow()
134 })
135
136 test("throws error when used outside provider", () => {
137 const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {})
138
139 expect(() => {
140 render(<TestComponent />)
141 }).toThrow("useModal must be used within a ModalProvider")
142
143 consoleSpy.mockRestore()
144 })
145 })