Raw
1 import React from "react"
2 import { beforeEach, describe, expect, test, vi } from "vitest"
3 import { cleanup, render } from "@testing-library/react"
4 import { OpenAPI } from "types"
5
6 // mock data
7 const mockBaseSpecs: OpenAPI.ExpandedDocument = {
8 openapi: "3.0.0",
9 info: {
10 title: "Test API",
11 version: "1.0.0",
12 },
13 tags: [
14 {
15 name: "TestTag",
16 },
17 ],
18 paths: {},
19 components: {
20 securitySchemes: {
21 "test-security": {
22 type: "http",
23 scheme: "bearer",
24 },
25 },
26 },
27 } as OpenAPI.ExpandedDocument
28
29 import BaseSpecsProvider, { useBaseSpecs } from "../base-specs"
30
31 // Test component that uses the hook
32 const TestComponent = () => {
33 const { baseSpecs, getSecuritySchema } = useBaseSpecs()
34 return (
35 <div>
36 <div data-testid="base-specs">{baseSpecs ? "present" : "null"}</div>
37 <div data-testid="security-schema">
38 {getSecuritySchema("test-security") ? "found" : "null"}
39 </div>
40 </div>
41 )
42 }
43
44 describe("BaseSpecsProvider", () => {
45 beforeEach(() => {
46 vi.clearAllMocks()
47 cleanup()
48 })
49
50 describe("rendering", () => {
51 test("renders children", () => {
52 const { getByText } = render(
53 <BaseSpecsProvider baseSpecs={mockBaseSpecs}>
54 <div>Test Content</div>
55 </BaseSpecsProvider>
56 )
57 expect(getByText("Test Content")).toBeInTheDocument()
58 })
59 })
60
61 describe("useBaseSpecs hook", () => {
62 test("throws error when used outside provider", () => {
63 const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {})
64
65 expect(() => {
66 render(<TestComponent />)
67 }).toThrow("useBaseSpecs must be used inside a BaseSpecsProvider")
68
69 consoleSpy.mockRestore()
70 })
71
72 test("returns baseSpecs and getSecuritySchema", () => {
73 const { getByTestId } = render(
74 <BaseSpecsProvider baseSpecs={mockBaseSpecs}>
75 <TestComponent />
76 </BaseSpecsProvider>
77 )
78 expect(getByTestId("base-specs")).toHaveTextContent("present")
79 expect(getByTestId("security-schema")).toBeInTheDocument()
80 })
81 })
82
83 describe("getSecuritySchema", () => {
84 test("returns security schema when it exists", () => {
85 const { getByTestId } = render(
86 <BaseSpecsProvider baseSpecs={mockBaseSpecs}>
87 <TestComponent />
88 </BaseSpecsProvider>
89 )
90 expect(getByTestId("security-schema")).toHaveTextContent("found")
91 })
92
93 test("returns null when security schema does not exist", () => {
94 const TestComponent2 = () => {
95 const { getSecuritySchema } = useBaseSpecs()
96 return (
97 <div data-testid="security-schema-2">
98 {getSecuritySchema("non-existent") ? "found" : "null"}
99 </div>
100 )
101 }
102 const { getByTestId } = render(
103 <BaseSpecsProvider baseSpecs={mockBaseSpecs}>
104 <TestComponent2 />
105 </BaseSpecsProvider>
106 )
107 expect(getByTestId("security-schema-2")).toHaveTextContent("null")
108 })
109
110 test("returns null when security schema is a ref", () => {
111 const baseSpecsWithRef: OpenAPI.ExpandedDocument = {
112 ...mockBaseSpecs,
113 components: {
114 securitySchemes: {
115 "test-security": {
116 $ref: "#/components/securitySchemes/OtherSecurity",
117 },
118 },
119 },
120 } as OpenAPI.ExpandedDocument
121
122 const TestComponent3 = () => {
123 const { getSecuritySchema } = useBaseSpecs()
124 return (
125 <div data-testid="security-schema-3">
126 {getSecuritySchema("test-security") ? "found" : "null"}
127 </div>
128 )
129 }
130
131 const { getByTestId } = render(
132 <BaseSpecsProvider baseSpecs={baseSpecsWithRef}>
133 <TestComponent3 />
134 </BaseSpecsProvider>
135 )
136 expect(getByTestId("security-schema-3")).toHaveTextContent("null")
137 })
138 })
139 })