Skip to content

Commit

Permalink
example object tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RohinBhargava committed Dec 20, 2024
1 parent 1c6f33a commit 223a89f
Showing 1 changed file with 140 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import { createMockContext } from "../../../../__test__/createMockContext.util";
import { ExampleObjectConverterNode } from "../ExampleObjectConverter.node";
import { RequestMediaTypeObjectConverterNode } from "../request/RequestMediaTypeObjectConverter.node";

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

describe("validateFormDataRequestExample", () => {
it("should return false if input value is not an object", () => {
const node = new ExampleObjectConverterNode(
{
input: {
requestExample: { value: "not an object" },
responseExample: undefined,
},
context: mockContext,
accessPath: [],
pathId: "test",
},
"/test",
200,
undefined,
undefined,
undefined,
undefined,
);

node.resolvedRequestInput = { value: "not an object" };
expect(node.validateFormDataRequestExample()).toBe(false);
});

it("should validate file fields", () => {
const node = new ExampleObjectConverterNode(
{
input: {
requestExample: {
value: {
file: { filename: "test.txt", data: "base64data" },
},
},
responseExample: undefined,
},
context: mockContext,
accessPath: [],
pathId: "test",
},
"/test",
200,
undefined,
{
contentType: "form-data",
fields: {
file: { multipartType: "file" },
},
} as RequestMediaTypeObjectConverterNode,
undefined,
undefined,
);

node.resolvedRequestInput = {
value: {
file: { filename: "test.txt", data: "base64data" },
},
};
expect(node.validateFormDataRequestExample()).toBe(true);
});
});

describe("convert", () => {
it("should convert JSON request example", () => {
const node = new ExampleObjectConverterNode(
{
input: {
requestExample: { value: { foo: "bar" } },
responseExample: undefined,
},
context: mockContext,
accessPath: [],
pathId: "test",
},
"/test",
200,
"test example",
{ contentType: "json" } as RequestMediaTypeObjectConverterNode,
undefined,
undefined,
);

node.resolvedRequestInput = { value: { foo: "bar" } };
const result = node.convert();

expect(result).toEqual({
path: "/test",
responseStatusCode: 200,
name: "test example",
description: undefined,
pathParameters: undefined,
queryParameters: undefined,
headers: undefined,
requestBody: {
type: "json",
value: { foo: "bar" },
},
responseBody: undefined,
snippets: undefined,
});
});

it("should convert bytes request example", () => {
const node = new ExampleObjectConverterNode(
{
input: {
requestExample: { value: "base64string" },
responseExample: undefined,
},
context: mockContext,
accessPath: [],
pathId: "test",
},
"/test",
200,
undefined,
{ contentType: "bytes" } as RequestMediaTypeObjectConverterNode,
undefined,
undefined,
);

node.resolvedRequestInput = { value: "base64string" };
const result = node.convert();

expect(result?.requestBody).toEqual({
type: "bytes",
value: {
type: "base64",
value: "base64string",
},
});
});
});
});

0 comments on commit 223a89f

Please sign in to comment.