lib/lambda/getSubTypes.test.ts
import { describe, it, expect, vi, beforeEach } from "vitest";
import { APIGatewayEvent } from "aws-lambda";
import { handler } from "./getSubTypes";
import { response } from "libs/handler-lib";
import * as os from "libs/opensearch-lib";
vi.mock("libs/handler-lib", () => ({
response: vi.fn(),
}));
vi.mock("libs/opensearch-lib", () => ({
search: vi.fn(),
}));
describe("getSubTypes Handler", () => {
beforeEach(() => {
vi.clearAllMocks();
process.env.osDomain = "test-domain"; // Set the environment variable before each test
process.env.indexNamespace = "test-namespace-"; // Set the environment variable before each test
});
it("should return 400 if event body is missing", async () => {
const event = {} as APIGatewayEvent;
await handler(event);
expect(response).toHaveBeenCalledWith({
statusCode: 400,
body: { message: "Event body required" },
});
});
it("should return 400 if no subtypes are found", async () => {
(os.search as vi.Mock).mockResolvedValueOnce(null);
const event = {
body: JSON.stringify({
authorityId: "test-authority",
typeIds: ["type1", "type2"],
}),
} as APIGatewayEvent;
await handler(event);
expect(response).toHaveBeenCalledWith({
statusCode: 400,
body: { message: "No record found for the given authority" },
});
});
it("should return 200 with the result if subtypes are found", async () => {
const mockResult = {
hits: { hits: [{ _source: { name: "test-subtype" } }] },
};
(os.search as vi.Mock).mockResolvedValueOnce(mockResult);
const event = {
body: JSON.stringify({
authorityId: "test-authority",
typeIds: ["type1", "type2"],
}),
} as APIGatewayEvent;
await handler(event);
expect(response).toHaveBeenCalledWith({
statusCode: 200,
body: mockResult,
});
});
it("should return 500 if an error occurs during processing", async () => {
(os.search as vi.Mock).mockRejectedValueOnce(new Error("Test error"));
const event = {
body: JSON.stringify({
authorityId: "test-authority",
typeIds: ["type1", "type2"],
}),
} as APIGatewayEvent;
await handler(event);
expect(response).toHaveBeenCalledWith({
statusCode: 500,
body: { message: "Internal server error" },
});
});
});