From 40edcf2d718bced59cd501b248029eb4ebf6f961 Mon Sep 17 00:00:00 2001 From: Rohin Bhargava Date: Mon, 13 Jan 2025 18:12:46 -0500 Subject: [PATCH] add unit tests --- .../XFernGlobalHeadersConverter.node.test.ts | 104 ++++++++++++++++++ .../__test__/EnumConverter.node.test.ts | 29 ++++- 2 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 packages/parsers/src/openapi/3.1/extensions/__test__/XFernGlobalHeadersConverter.node.test.ts diff --git a/packages/parsers/src/openapi/3.1/extensions/__test__/XFernGlobalHeadersConverter.node.test.ts b/packages/parsers/src/openapi/3.1/extensions/__test__/XFernGlobalHeadersConverter.node.test.ts new file mode 100644 index 0000000000..e221faaae2 --- /dev/null +++ b/packages/parsers/src/openapi/3.1/extensions/__test__/XFernGlobalHeadersConverter.node.test.ts @@ -0,0 +1,104 @@ +import { createMockContext } from "../../../__test__/createMockContext.util"; +import { X_FERN_GLOBAL_HEADERS } from "../fernExtension.consts"; +import { XFernGlobalHeadersConverterNode } from "../XFernGlobalHeadersConverter.node"; + +describe("XFernGlobalHeadersConverterNode", () => { + const mockContext = createMockContext(); + const mockAccessPath = ["test", "path"]; + const mockPathId = "test-path"; + + beforeEach(() => { + mockContext.errors.warning.mockClear(); + }); + + it("should parse global headers correctly", () => { + const input = { + [X_FERN_GLOBAL_HEADERS]: [ + { + header: "X-API-Key", + type: "string", + description: "API Key for authentication", + }, + ], + }; + + const converter = new XFernGlobalHeadersConverterNode({ + input, + context: mockContext, + accessPath: mockAccessPath, + pathId: mockPathId, + }); + + expect(converter.globalHeaders).toBeDefined(); + expect(converter.globalHeaders?.length).toBe(1); + expect(converter.globalHeaders?.[0][0]).toBe("X-API-Key"); + }); + + it("should handle empty input", () => { + const input = {}; + + const converter = new XFernGlobalHeadersConverterNode({ + input, + context: mockContext, + accessPath: mockAccessPath, + pathId: mockPathId, + }); + + expect(converter.globalHeaders).toBeUndefined(); + }); + + it("should convert global headers to ObjectProperties", () => { + const input = { + [X_FERN_GLOBAL_HEADERS]: [ + { + header: "X-API-Key", + type: "string", + description: "API Key for authentication", + }, + ], + }; + + const converter = new XFernGlobalHeadersConverterNode({ + input, + context: mockContext, + accessPath: mockAccessPath, + pathId: mockPathId, + }); + + const result = converter.convert(); + + expect(result).toBeDefined(); + expect(result?.length).toBe(1); + expect(result?.[0].key).toBe("X-API-Key"); + expect(result?.[0].description).toBe("API Key for authentication"); + }); + + it("should handle multiple global headers", () => { + const input = { + [X_FERN_GLOBAL_HEADERS]: [ + { + header: "X-API-Key", + type: "string", + }, + { + header: "X-Client-ID", + type: "string", + }, + ], + }; + + const converter = new XFernGlobalHeadersConverterNode({ + input, + context: mockContext, + accessPath: mockAccessPath, + pathId: mockPathId, + }); + + const result = converter.convert(); + + expect(result).toBeDefined(); + expect(result?.length).toBe(2); + expect(result?.[0].key).toBe("X-API-Key"); + expect(result?.[1].key).toBe("X-Client-ID"); + }); +}); diff --git a/packages/parsers/src/openapi/3.1/schemas/primitives/__test__/EnumConverter.node.test.ts b/packages/parsers/src/openapi/3.1/schemas/primitives/__test__/EnumConverter.node.test.ts index 81b2b5c228..253100bd6e 100644 --- a/packages/parsers/src/openapi/3.1/schemas/primitives/__test__/EnumConverter.node.test.ts +++ b/packages/parsers/src/openapi/3.1/schemas/primitives/__test__/EnumConverter.node.test.ts @@ -1,9 +1,16 @@ import { OpenAPIV3_1 } from "openapi-types"; +import { expect } from "vitest"; import { createMockContext } from "../../../../__test__/createMockContext.util"; import { EnumConverterNode } from "../EnumConverter.node"; describe("EnumConverterNode", () => { - const mockContext = createMockContext(); + const mockContext = createMockContext({ + components: { + schemas: { + Status: { type: "string", default: "ACTIVE" }, + }, + }, + } as unknown as OpenAPIV3_1.Document); beforeEach(() => { vi.clearAllMocks(); @@ -70,6 +77,26 @@ describe("EnumConverterNode", () => { path: ["test", "enum[1]"], }); }); + + it("should handle enum schema with $ref", () => { + const input: OpenAPIV3_1.SchemaObject = { + enum: [{ $ref: "#/components/schemas/Status" }], + }; + const node = new EnumConverterNode({ + input, + context: mockContext, + accessPath: [], + pathId: "test", + }); + expect(node.convert()).toEqual({ + type: "enum", + values: [ + { + value: "ACTIVE", + }, + ], + }); + }); }); describe("convert", () => {