Enterprise-CMCS/macpro-mako

View on GitHub
lib/lambda/getForm.test.ts

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
import { describe, it, expect, vi, beforeEach } from "vitest";
import { getForm } from "./getForm";

vi.mock("../libs/webforms", () => ({
  webformVersions: {
    TESTFORM: {
      v022024: { name: "Test Form", data: "hello world" },
    },
  },
}));

describe("forms handler", () => {
  beforeEach(() => {
    // Reset mocks before each test
    vi.resetAllMocks();
  });

  it("returns 400 if event body is missing", async () => {
    const event = {};
    const result = await getForm(event as any); // Casting as any to simulate APIGatewayEvent
    expect(result.statusCode).toBe(400);
    expect(result.body).toContain("Event body required");
  });

  it("returns 400 if form ID is not provided", async () => {
    const event = {
      body: JSON.stringify({}), // Empty body
    };
    const result = await getForm(event as any);
    expect(result.statusCode).toBe(400);
    expect(result.body).toContain("File ID was not provided");
  });

  it("returns 400 if form ID is not found", async () => {
    const event = {
      body: JSON.stringify({ formId: "NONEXISTENT" }),
    };
    const result = await getForm(event as any);
    expect(result.statusCode).toBe(400);
    expect(result.body).toContain("Form ID not found");
  });

  it("returns 200 with form data if form ID and version are valid", async () => {
    const event = {
      body: JSON.stringify({ formId: "TESTFORM", formVersion: "022024" }),
    };
    const result = await getForm(event as any);
    expect(result.statusCode).toBe(200);
    expect(result.body).toContain("Test Form");
  });
});