Skip to content

Commit

Permalink
Merge branch 'main' into dashboard-sort
Browse files Browse the repository at this point in the history
  • Loading branch information
tbolt committed Jan 13, 2025
2 parents cb2ed87 + c48365e commit 84669fe
Show file tree
Hide file tree
Showing 29 changed files with 12,723 additions and 9,722 deletions.
22 changes: 11 additions & 11 deletions lib/lambda/getForm.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
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" },
},
},
}));

import { webformVersions } from "libs/webforms";
describe("forms handler", () => {
beforeEach(() => {
// Reset mocks before each test
Expand Down Expand Up @@ -42,10 +34,18 @@ describe("forms handler", () => {

it("returns 200 with form data if form ID and version are valid", async () => {
const event = {
body: JSON.stringify({ formId: "TESTFORM", formVersion: "022024" }),
body: JSON.stringify({ formId: "ABP1", formVersion: "202401" }),
};
const result = await getForm(event as any);
expect(result.statusCode).toBe(200);
expect(JSON.parse(result.body).header).toBe(webformVersions["ABP1"]["v202401"].header);
});
it("returns 200 with form data if form ID and version are valid", async () => {
const event = {
body: JSON.stringify({ formId: "ABP1", formVersion: "" }),
};
const result = await getForm(event as any);
expect(result.statusCode).toBe(200);
expect(result.body).toContain("Test Form");
expect(result.body).toBe("{}");
});
});
2 changes: 1 addition & 1 deletion lib/lambda/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe("getSearchData Handler", () => {
const body = JSON.parse(res.body);
expect(body).toBeTruthy();
expect(body?.hits?.hits).toBeTruthy();
expect(body?.hits?.hits?.length).toEqual(13);
expect(body?.hits?.hits?.length).toEqual(14);
});

it("should handle errors during processing", async () => {
Expand Down
288 changes: 288 additions & 0 deletions lib/lambda/update/updatePackage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,288 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import { handler } from "./updatePackage";
import { APIGatewayEvent } from "node_modules/shared-types";

import {
EXISTING_ITEM_ID,
EXISTING_ITEM_PENDING_ID,
CAPITATED_INITIAL_ITEM_ID,
CAPITATED_INITIAL_NEW_ITEM_ID,
WEIRD_ID,
} from "mocks";
vi.mock("libs/handler-lib", () => ({
response: vi.fn((data) => data),
}));

describe("handler", () => {
beforeEach(() => {
vi.clearAllMocks();
process.env.topicName = "test-topic";
});

it("should return 400 if event body is missing", async () => {
const event = {} as APIGatewayEvent;
const result = await handler(event);
const expectedResult = { statusCode: 400, body: { message: "Event body required" } };

expect(result).toStrictEqual(expectedResult);
});

it("should return 400 if package ID is not found", async () => {
const noActionevent = {
body: JSON.stringify({ packageId: "123", changeReason: "Nunya" }),
} as APIGatewayEvent;

const resultPackage = await handler(noActionevent);

expect(resultPackage.statusCode).toBe(400);
});
it("should return 400 if action is not found", async () => {
const noApackageEvent = {
body: JSON.stringify({ action: "123", changeReason: "Nunya" }),
} as APIGatewayEvent;

const resultAction = await handler(noApackageEvent);

expect(resultAction.statusCode).toBe(400);
});
it("should get a package", async () => {
const noActionevent = {
body: JSON.stringify({ packageId: "123", action: "delete", changeReason: "Nunya" }),
} as APIGatewayEvent;

const result = await handler(noActionevent);

const expectedResult = {
statusCode: 404,
body: { message: "No record found for the given id" },
};
expect(result).toStrictEqual(expectedResult);
});
it("should delete a package", async () => {
const noActionevent = {
body: JSON.stringify({
packageId: EXISTING_ITEM_ID,
action: "delete",
changeReason: "Nunya",
}),
} as APIGatewayEvent;

const result = await handler(noActionevent);

const expectedResult = {
statusCode: 200,
body: { message: "MD-00-0000 has been deleted." },
};
expect(result).toStrictEqual(expectedResult);
});
it("should delete a package but no topic name", async () => {
process.env.topicName = "";
const noActionevent = {
body: JSON.stringify({
packageId: EXISTING_ITEM_ID,
action: "delete",
changeReason: "Nunya",
}),
} as APIGatewayEvent;

const result = await handler(noActionevent);

const expectedResult = {
statusCode: 500,
body: { message: "Topic name is not defined" },
};
expect(result).toStrictEqual(expectedResult);
});

it("should fail to update an ID on a package - missing new iD", async () => {
const noActionevent = {
body: JSON.stringify({
packageId: EXISTING_ITEM_ID,
action: "update-id",
changeReason: "Nunya",
}),
} as APIGatewayEvent;

const result = await handler(noActionevent);

const expectedResult = {
statusCode: 400,
body: { message: "New ID required to update package" },
};
expect(result).toStrictEqual(expectedResult);
});
it("should fail to update an ID on a package - missing or same ID", async () => {
const noActionevent = {
body: JSON.stringify({
packageId: EXISTING_ITEM_ID,
action: "update-id",
changeReason: "Nunya",
}),
} as APIGatewayEvent;

const result = await handler(noActionevent);

const expectedResult = {
statusCode: 400,
body: { message: "New ID required to update package" },
};
expect(result).toStrictEqual(expectedResult);
});
it("should fail to update an ID on a package - missing or same ID", async () => {
const noActionevent = {
body: JSON.stringify({
packageId: EXISTING_ITEM_ID,
action: "update-id",
changeReason: "Nunya",
updatedId: EXISTING_ITEM_PENDING_ID,
}),
} as APIGatewayEvent;

const result = await handler(noActionevent);

const expectedResult = {
statusCode: 400,
body: { message: "This ID already exists" },
};
expect(result).toStrictEqual(expectedResult);
});
it("should update an ID on a package", async () => {
const noActionevent = {
body: JSON.stringify({
packageId: CAPITATED_INITIAL_ITEM_ID,
action: "update-id",
changeReason: "Nunya",
updatedId: CAPITATED_INITIAL_NEW_ITEM_ID,
}),
} as APIGatewayEvent;

const result = await handler(noActionevent);

const expectedResult = {
statusCode: 200,
body: {
message: `The ID of package ${CAPITATED_INITIAL_ITEM_ID} has been updated to ${CAPITATED_INITIAL_NEW_ITEM_ID}.`,
},
};
expect(result).toStrictEqual(expectedResult);
});
it("should fail to update a package ID with no topic name", async () => {
process.env.topicName = "";
const noActionevent = {
body: JSON.stringify({
packageId: CAPITATED_INITIAL_ITEM_ID,
action: "update-id",
changeReason: "Nunya",
updatedId: "SS-1235.R00.00",
}),
} as APIGatewayEvent;

const result = await handler(noActionevent);
const expectedResult = {
statusCode: 500,
body: { message: "Topic name is not defined" },
};
expect(result).toStrictEqual(expectedResult);
});

it("should fail to update a package ID with bad new id format", async () => {
const noActionevent = {
body: JSON.stringify({
packageId: CAPITATED_INITIAL_ITEM_ID,
action: "update-id",
changeReason: "Nunya",
updatedId: "SS-120",
}),
} as APIGatewayEvent;

const result = await handler(noActionevent);
const expectedResult =
"The Initial Waiver Number must be in the format of SS-####.R00.00 or SS-#####.R00.00";

expect(JSON.parse(result?.body)[0].message).toStrictEqual(expectedResult);
});
it("should fail to update a package with bad existing id format", async () => {
const noActionevent = {
body: JSON.stringify({
packageId: WEIRD_ID,
action: "update-id",
changeReason: "Nunya",
updatedId: "SS-120",
}),
} as APIGatewayEvent;

const result = await handler(noActionevent);
const expectedResult = "Cannot read properties of undefined (reading 'baseSchema')";
expect(result?.statusCode).toStrictEqual(500);
expect(result?.body.message).toStrictEqual(expectedResult);
});
it("should fail to update a package - no topic name ", async () => {
process.env.topicName = "";
const noActionevent = {
body: JSON.stringify({
packageId: WEIRD_ID,
action: "update-values",
changeReason: "Nunya",
updatedFields: {},
}),
} as APIGatewayEvent;

const result = await handler(noActionevent);
const expectedResult = {
statusCode: 500,
body: { message: "Topic name is not defined" },
};
expect(result).toStrictEqual(expectedResult);
});
it("should fail to update a package - No valid fields ", async () => {
const noActionevent = {
body: JSON.stringify({
packageId: WEIRD_ID,
action: "update-values",
changeReason: "Nunya",
updatedFields: { badfield: "nothing" },
}),
} as APIGatewayEvent;

const result = await handler(noActionevent);
const expectedResult = {
statusCode: 400,
body: { message: "Cannot update invalid field(s): badfield" },
};
expect(result).toStrictEqual(expectedResult);
});
it("should fail to update a package - Id can not be updated ", async () => {
const noActionevent = {
body: JSON.stringify({
packageId: WEIRD_ID,
action: "update-values",
changeReason: "Nunya",
updatedFields: { id: "cant update ID here" },
}),
} as APIGatewayEvent;

const result = await handler(noActionevent);
const expectedResult = {
statusCode: 400,
body: { message: "ID is not a valid field to update" },
};
expect(result).toStrictEqual(expectedResult);
});
it("should fail to update a package - nothing to update ", async () => {
const noActionevent = {
body: JSON.stringify({
packageId: CAPITATED_INITIAL_ITEM_ID,
action: "update-values",
changeReason: "Nunya",
updatedFields: { state: "TX" },
}),
} as APIGatewayEvent;

const result = await handler(noActionevent);
const expectedResult = {
statusCode: 200,
body: { message: `state has been updated in package ${CAPITATED_INITIAL_ITEM_ID}.` },
};
expect(result).toStrictEqual(expectedResult);
});
});
Loading

0 comments on commit 84669fe

Please sign in to comment.