Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RohinBhargava committed Nov 27, 2024
1 parent 77dd169 commit bf358d3
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 118 deletions.
Original file line number Diff line number Diff line change
@@ -1,156 +1,142 @@
import { OpenAPIV3_1 } from "openapi-types";
import { createMockContext } from "../../../../../__test__/createMockContext.util";
import { ReferenceConverterNode } from "../../../schemas/ReferenceConverter.node";
import { RequestMediaTypeObjectConverterNode } from "../../request/RequestMediaTypeObjectConverter.node";
import { ResponsesObjectConverterNode } from "../../response/ResponsesObjectConverter.node";

describe("RequestMediaTypeObjectConverterNode", () => {
describe("ResponsesObjectConverterNode", () => {
const mockContext = createMockContext();

it("should handle application/json content type", () => {
const input: OpenAPIV3_1.MediaTypeObject = {
schema: {
type: "object",
properties: {
name: { type: "string" },
it("should handle success responses", () => {
const input: OpenAPIV3_1.ResponsesObject = {
"200": {
description: "Success response",
content: {
"application/json": {
schema: {
type: "object",
properties: {
data: { type: "string" },
},
},
},
},
},
};

const converter = new RequestMediaTypeObjectConverterNode(
{
input,
context: mockContext,
accessPath: [],
pathId: "test",
},
"application/json",
);
const converter = new ResponsesObjectConverterNode({
input,
context: mockContext,
accessPath: [],
pathId: "test",
});

expect(converter.contentType).toBe("application/json");
expect(converter.schema).toBeDefined();
const result = converter.convert();
expect(result?.responses).toBeDefined();
expect(result?.responses[0]?.response.statusCode).toBe(200);
expect(result?.responses[0]?.response.description).toBe("Success response");
});

it("should handle application/octet-stream content type", () => {
const input: OpenAPIV3_1.MediaTypeObject = {
schema: {
type: "object",
required: ["data"],
it("should handle error responses", () => {
const input: OpenAPIV3_1.ResponsesObject = {
"400": {
description: "Bad request",
content: {
"application/json": {
schema: {
type: "object",
properties: {
error: { type: "string" },
},
},
},
},
},
};

const converter = new RequestMediaTypeObjectConverterNode(
{
input,
context: mockContext,
accessPath: [],
pathId: "test",
},
"application/octet-stream",
);
const converter = new ResponsesObjectConverterNode({
input,
context: mockContext,
accessPath: [],
pathId: "test",
});

expect(converter.contentType).toBe("application/octet-stream");
expect(converter.isOptional).toBe(false);
const result = converter.convert();
expect(result?.errors).toBeDefined();
expect(result?.errors[0]?.statusCode).toBe(400);
expect(result?.errors[0]?.description).toBe("Bad request");
});

it("should handle multipart/form-data content type", () => {
const input: OpenAPIV3_1.MediaTypeObject = {
schema: {
type: "object",
properties: {
file: {
type: "string",
format: "binary",
it("should handle responses with headers", () => {
const input: OpenAPIV3_1.ResponsesObject = {
"200": {
description: "Success with headers",
headers: {
"X-Rate-Limit": {
schema: {
type: "integer",
},
},
},
content: {
"application/json": {
schema: {
type: "object",
},
},
},
required: ["file"],
},
};

const converter = new RequestMediaTypeObjectConverterNode(
{
input,
context: mockContext,
accessPath: [],
pathId: "test",
},
"multipart/form-data",
);
const converter = new ResponsesObjectConverterNode({
input,
context: mockContext,
accessPath: [],
pathId: "test",
});

expect(converter.contentType).toBe("multipart/form-data");
expect(converter.fields).toBeDefined();
expect(converter.requiredFields).toContain("file");
const result = converter.convert();
expect(result?.responses[0]?.headers).toBeDefined();
});

it("should handle reference objects", () => {
const input: OpenAPIV3_1.MediaTypeObject = {
schema: {
$ref: "#/components/schemas/Request",
},
};

mockContext.document.components ??= {};
mockContext.document.components.schemas = {
Request: {
type: "object",
properties: {
data: { type: "string" },
it("should handle default responses", () => {
const input: OpenAPIV3_1.ResponsesObject = {
default: {
description: "Default response",
content: {
"application/json": {
schema: {
type: "object",
},
},
},
},
"200": {
description: "Success",
},
};

const converter = new RequestMediaTypeObjectConverterNode(
{
input,
context: mockContext,
accessPath: [],
pathId: "test",
},
"application/json",
);
const converter = new ResponsesObjectConverterNode({
input,
context: mockContext,
accessPath: [],
pathId: "test",
});

expect(converter.schema).toBeDefined();
expect(converter.schema instanceof ReferenceConverterNode).toBe(true);
const result = converter.convert();
expect(result?.responses).toBeDefined();
});

it("should warn when schema is missing", () => {
const input: OpenAPIV3_1.MediaTypeObject = {};
it("should handle empty responses", () => {
const input: OpenAPIV3_1.ResponsesObject = {};

new RequestMediaTypeObjectConverterNode(
{
input,
context: mockContext,
accessPath: [],
pathId: "test",
},
"application/json",
);

expect(mockContext.errors.warning).toHaveBeenCalledWith({
message: expect.stringContaining("Expected media type schema"),
path: ["test"],
const converter = new ResponsesObjectConverterNode({
input,
context: mockContext,
accessPath: [],
pathId: "test",
});
});

it("should warn for unsupported content types", () => {
const input: OpenAPIV3_1.MediaTypeObject = {
schema: {
type: "object",
},
};

new RequestMediaTypeObjectConverterNode(
{
input,
context: mockContext,
accessPath: [],
pathId: "test",
},
"text/plain",
);

expect(mockContext.errors.warning).toHaveBeenCalledWith({
message: expect.stringContaining("Expected request body"),
path: ["test"],
});
const result = converter.convert();
expect(result?.responses).toEqual([]);
expect(result?.errors).toEqual([]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,65 @@ describe("SchemaConverterNode", () => {
expect(node.typeShapeNode).toBeDefined();
});

it("should handle array type", () => {
const input: OpenAPIV3_1.SchemaObject = {
type: "array",
items: {
type: "string",
},
};
const node = new SchemaConverterNode({
input,
context: mockContext,
accessPath: [],
pathId: "test",
});
expect(node.typeShapeNode).toBeDefined();
});

it("should handle enum type", () => {
const input: OpenAPIV3_1.SchemaObject = {
type: "string",
enum: ["RED", "GREEN", "BLUE"],
};
const node = new SchemaConverterNode({
input,
context: mockContext,
accessPath: [],
pathId: "test",
});
expect(node.typeShapeNode).toBeDefined();
});

it("should handle oneOf type", () => {
const input: OpenAPIV3_1.SchemaObject = {
oneOf: [{ type: "string" }, { type: "number" }],
};
const node = new SchemaConverterNode({
input,
context: mockContext,
accessPath: [],
pathId: "test",
});
expect(node.typeShapeNode).toBeDefined();
});

it("should handle allOf type", () => {
const input: OpenAPIV3_1.SchemaObject = {
allOf: [
{ type: "object", properties: { name: { type: "string" } } },
{ type: "object", properties: { age: { type: "number" } } },
],
};
const node = new SchemaConverterNode({
input,
context: mockContext,
accessPath: [],
pathId: "test",
});
expect(node.typeShapeNode).toBeDefined();
});

it("should set description", () => {
const input: OpenAPIV3_1.SchemaObject = {
type: "string",
Expand Down

0 comments on commit bf358d3

Please sign in to comment.