Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RohinBhargava committed Jan 13, 2025
1 parent 028e77e commit 40edcf2
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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");
});
});
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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", () => {
Expand Down

0 comments on commit 40edcf2

Please sign in to comment.