1 import React from "react"
2 import { beforeEach, describe, expect, test, vi } from "vitest"
3 import { cleanup, render, waitFor } from "@testing-library/react"
4 import { OpenAPI } from "types"
5
6 // mock data
7 const mockSpecs: OpenAPI.OpenAPIV3.Document = {
8 openapi: "3.0.0",
9 info: {
10 title: "Test API",
11 version: "1.0.0",
12 },
13 paths: {},
14 components: {
15 securitySchemes: {
16 bearer: {
17 type: "http",
18 scheme: "bearer",
19 },
20 },
21 },
22 }
23
24 const mockSpecsWithRef: OpenAPI.OpenAPIV3.Document = {
25 ...mockSpecs,
26 components: {
27 securitySchemes: {
28 bearer: {
29 $ref: "#/components/securitySchemes/bearer",
30 },
31 },
32 },
33 }
34
35 // mock components
36 vi.mock("@/components/MDXComponents/Security/Description", () => ({
37 default: ({ securitySchema }: { securitySchema: OpenAPI.SecuritySchemeObject }) => (
38 <div data-testid="security-description">{securitySchema.type}</div>
39 ),
40 }))
41
42 import Security from ".."
43
44 beforeEach(() => {
45 vi.clearAllMocks()
46 cleanup()
47 })
48
49 describe("rendering", () => {
50 test("does not render when specs is not provided", () => {
51 const { container } = render(<Security />)
52 const securityDescriptionElements = container.querySelectorAll("[data-testid='security-description']")
53 expect(securityDescriptionElements).toHaveLength(0)
54 })
55 test("renders security information for specs", async () => {
56 const { getByTestId } = render(<Security specs={mockSpecs} />)
57 await waitFor(() => {
58 const securityDescriptionElement = getByTestId("security-description")
59 expect(securityDescriptionElement).toBeInTheDocument()
60 expect(securityDescriptionElement).toHaveTextContent("http")
61 })
62 })
63
64 test("does not render when security scheme is a $ref", () => {
65 const { container } = render(<Security specs={mockSpecsWithRef} />)
66 const securityDescriptionElements = container.querySelectorAll("[data-testid='security-description']")
67 expect(securityDescriptionElements).toHaveLength(0)
68 })
69 })