1 import React from "react"
2 import { beforeEach, describe, expect, test, vi } from "vitest"
3 import { fireEvent, render, waitFor } from "@testing-library/react"
4 import { CodeBlockProps } from "../../CodeBlock"
5 import { ButtonProps } from "../../Button"
6
7 // mock data
8 const mockApiRunnerSimpleGet = {
9 apiMethod: "GET" as ApiMethod,
10 apiUrl: "https://api.example.com",
11 }
12 const mockApiRunnerGet = {
13 apiMethod: "GET" as ApiMethod,
14 apiUrl: "https://api.example.com",
15 pathData: {
16 id: "123",
17 },
18 queryData: {
19 limit: 10,
20 },
21 }
22 const mockApiRunnerPost = {
23 apiMethod: "POST" as ApiMethod,
24 apiUrl: "https://api.example.com",
25 bodyData: {
26 name: "John Doe",
27 },
28 }
29 const mockApiRunnerDelete = {
30 apiMethod: "DELETE" as ApiMethod,
31 apiUrl: "https://api.example.com",
32 pathData: {
33 id: "123",
34 },
35 }
36
37 // mock functions
38 let capturedOnFinish: ((message: string, statusCode: string) => void) | null =
39 null
40
41 // mock components and hooks
42 vi.mock("@/components/CodeBlock", () => ({
43 CodeBlock: ({ badgeColor, badgeLabel, ...props }: CodeBlockProps) => (
44 <div data-badge-color={badgeColor} data-badge-label={badgeLabel}>
45 <pre {...props} />
46 </div>
47 ),
48 }))
49 vi.mock("@/components/Button", () => ({
50 Button: (props: ButtonProps) => <button {...props} />,
51 }))
52 vi.mock("@/components/ApiRunner/ParamInputs", () => ({
53 ApiRunnerParamInputs: () => <div>ApiRunnerParamInputs</div>,
54 }))
55 vi.mock("@/components/ApiRunner/FooterBackground", () => ({
56 ApiRunnerFooterBackground: () => <div>ApiRunnerFooterBackground</div>,
57 }))
58 vi.mock("@/components/Icons/ArrowRightDown", () => ({
59 ArrowRightDownIcon: () => <div>ArrowRightDownIcon</div>,
60 }))
61 vi.mock("@/hooks/use-request-runner", () => ({
62 useRequestRunner: ({
63 pushLog,
64 onFinish,
65 }: {
66 pushLog: (...message: string[]) => void
67 onFinish: (message: string, statusCode: string) => void
68 replaceLog?: (message: string) => void
69 }) => {
70 capturedOnFinish = onFinish
71 return {
72 runRequest: vi.fn(() => {
73 pushLog("Response data")
74 onFinish("Finished running request.", "")
75 }),
76 }
77 },
78 }))
79
80 import { ApiRunner } from "../../ApiRunner"
81 import { ApiMethod } from "types"
82
83 beforeEach(() => {
84 capturedOnFinish = null
85 })
86
87 describe("rendering", () => {
88 test("renders manual test trigger when there is data", () => {
89 const { container } = render(<ApiRunner {...mockApiRunnerGet} />)
90 expect(container).toBeInTheDocument()
91 expect(container).toHaveTextContent("ApiRunnerParamInputs")
92 expect(container).toHaveTextContent("ApiRunnerFooterBackground")
93 expect(container).toHaveTextContent("ArrowRightDownIcon")
94 const submitButton = container.querySelector("button")
95 expect(submitButton).toBeInTheDocument()
96 expect(submitButton).toHaveTextContent("Send Request")
97 })
98 test("renders code block when request is running or has been run", () => {
99 const { container } = render(<ApiRunner {...mockApiRunnerGet} />)
100 expect(container).toBeInTheDocument()
101 const submitButton = container.querySelector("button")
102 expect(submitButton).toBeInTheDocument()
103 fireEvent.click(submitButton!)
104 const pre = container.querySelector("pre")
105 expect(pre).toBeInTheDocument()
106 })
107 })
108
109 describe("interactions", () => {
110 test("clicking submit button should trigger request", () => {
111 const { container } = render(<ApiRunner {...mockApiRunnerGet} />)
112 expect(container).toBeInTheDocument()
113 const submitButton = container.querySelector("button")
114 expect(submitButton).toBeInTheDocument()
115 fireEvent.click(submitButton!)
116 // Verify the CodeBlock appears after clicking
117 expect(container.querySelector("pre")).toBeInTheDocument()
118 })
119 test("clicking submit button should trigger POST request", () => {
120 const { container } = render(<ApiRunner {...mockApiRunnerPost} />)
121 expect(container).toBeInTheDocument()
122 const submitButton = container.querySelector("button")
123 expect(submitButton).toBeInTheDocument()
124 fireEvent.click(submitButton!)
125 expect(container.querySelector("pre")).toBeInTheDocument()
126 })
127 test("clicking submit button should trigger DELETE request", () => {
128 const { container } = render(<ApiRunner {...mockApiRunnerDelete} />)
129 expect(container).toBeInTheDocument()
130 const submitButton = container.querySelector("button")
131 expect(submitButton).toBeInTheDocument()
132 fireEvent.click(submitButton!)
133 expect(container.querySelector("pre")).toBeInTheDocument()
134 })
135
136 test("should send request automatically for simple GET request", () => {
137 const { container } = render(<ApiRunner {...mockApiRunnerSimpleGet} />)
138 expect(container).toBeInTheDocument()
139 // The request is automatically triggered, so we just verify the component renders
140 expect(container.querySelector("pre")).toBeInTheDocument()
141 })
142 })
143
144 describe("badge display", () => {
145 test("should show green badge with status code for successful request (2xx)", async () => {
146 const { container } = render(<ApiRunner {...mockApiRunnerGet} />)
147 const submitButton = container.querySelector("button")
148 fireEvent.click(submitButton!)
149
150 // Wait for the component to render the CodeBlock
151 await waitFor(() => {
152 expect(container.querySelector("pre")).toBeInTheDocument()
153 })
154
155 // Simulate successful response by calling onFinish with 200 status code
156 if (capturedOnFinish) {
157 capturedOnFinish("Finished running request.", "200")
158 }
159
160 await waitFor(() => {
161 const codeBlock = container.querySelector("[data-badge-color]")
162 expect(codeBlock).toBeInTheDocument()
163 expect(codeBlock).toHaveAttribute("data-badge-color", "green")
164 expect(codeBlock).toHaveAttribute("data-badge-label", "200")
165 })
166 })
167
168 test("should show red badge with status code for failed request (4xx)", async () => {
169 const { container } = render(<ApiRunner {...mockApiRunnerGet} />)
170 const submitButton = container.querySelector("button")
171 fireEvent.click(submitButton!)
172
173 await waitFor(() => {
174 expect(container.querySelector("pre")).toBeInTheDocument()
175 })
176
177 // Simulate failed response by calling onFinish with 404 status code
178 if (capturedOnFinish) {
179 capturedOnFinish("Finished running request.", "404")
180 }
181
182 await waitFor(() => {
183 const codeBlock = container.querySelector("[data-badge-color]")
184 expect(codeBlock).toBeInTheDocument()
185 expect(codeBlock).toHaveAttribute("data-badge-color", "red")
186 expect(codeBlock).toHaveAttribute("data-badge-label", "404")
187 })
188 })
189
190 test("should show red badge with 'Failed' label when no status code", async () => {
191 const { container } = render(<ApiRunner {...mockApiRunnerGet} />)
192 const submitButton = container.querySelector("button")
193 fireEvent.click(submitButton!)
194
195 await waitFor(() => {
196 expect(container.querySelector("pre")).toBeInTheDocument()
197 })
198
199 // Simulate error with no status code (empty string)
200 if (capturedOnFinish) {
201 capturedOnFinish("Finished running request.", "")
202 }
203
204 await waitFor(() => {
205 const codeBlock = container.querySelector("[data-badge-color]")
206 expect(codeBlock).toBeInTheDocument()
207 expect(codeBlock).toHaveAttribute("data-badge-color", "red")
208 expect(codeBlock).toHaveAttribute("data-badge-label", "Failed")
209 })
210 })
211
212 test("should show green badge for 201 status code", async () => {
213 const { container } = render(<ApiRunner {...mockApiRunnerPost} />)
214 const submitButton = container.querySelector("button")
215 fireEvent.click(submitButton!)
216
217 await waitFor(() => {
218 expect(container.querySelector("pre")).toBeInTheDocument()
219 })
220
221 // Simulate successful POST response with 201 status code
222 if (capturedOnFinish) {
223 capturedOnFinish("Finished running request.", "201")
224 }
225
226 await waitFor(() => {
227 const codeBlock = container.querySelector("[data-badge-color]")
228 expect(codeBlock).toBeInTheDocument()
229 expect(codeBlock).toHaveAttribute("data-badge-color", "green")
230 expect(codeBlock).toHaveAttribute("data-badge-label", "201")
231 })
232 })
233 })