Skip to content

Commit

Permalink
add test for create Item function
Browse files Browse the repository at this point in the history
  • Loading branch information
ebisbe committed Jul 8, 2024
1 parent 4f7b580 commit 931996c
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/functions/item/__tests__/create.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { describe, expect, it } from "@jest/globals";

import { handler } from "../create";

describe("CREATE Item", () => {
it("receives an empty body", async () => {
const response = await handler({
headers: {
"Content-Type": "application/json",
},
body: null,
});

expect(response).toStrictEqual({
statusCode: 400,
headers: { "Content-Type": "application/json" },
body: '{"issues":[{"code":"invalid_type","expected":"object","received":"null","path":[],"message":"Expected object, received null"}],"name":"ZodError"}',
});
});

it("creates a new item", async () => {
const event = {
headers: {
"Content-Type": "application/json",
},
pathParameters: { listId: "1" },
body: JSON.stringify({
name: "My Wedding Item",
description: "This is my wedding gift item.",
price: 15,
}),
};
const response = await handler(event);

expect(response.statusCode).toBe(200);

const body = JSON.parse(response.body);

expect(body.id).toMatch(/^[A-Z0-9]{26}$/);
expect(body.listId).toBe("1");
expect(body.name).toBe("My Wedding Item");
expect(body.description).toBe("This is my wedding gift item.");
expect(body.price).toBe(15);

const dateRegExp =
/^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}Z$/;
expect(body.createdAt).toMatch(dateRegExp);
expect(body.updatedAt).toMatch(dateRegExp);
});

it("does not received all required properties", async () => {
const event = {
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ param: "1" }),
};
const response = await handler(event);

expect(response).toStrictEqual({
statusCode: 400,
headers: { "Content-Type": "application/json" },
body: '{"issues":[{"code":"invalid_type","expected":"string","received":"undefined","path":["name"],"message":"Required"},{"code":"invalid_type","expected":"string","received":"undefined","path":["description"],"message":"Required"},{"code":"invalid_type","expected":"number","received":"undefined","path":["price"],"message":"Required"}],"name":"ZodError"}',
});
});
});
36 changes: 36 additions & 0 deletions src/functions/item/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import middy from "@middy/core";
import httpErrorHandler from "@middy/http-error-handler";
import httpJsonBodyParser from "@middy/http-json-body-parser";
import { APIGatewayProxyEventV2, Handler } from "aws-lambda";
import { z } from "zod";

import { Item } from "../../models/table";
import { ValidationError } from "../../utils/validationError";

const createItemHandler: Handler<APIGatewayProxyEventV2> = async (event) => {
const itemSchema = z.object({
name: z.string(),
description: z.string(),
price: z.number(),
}) satisfies z.ZodType<Item>;

const item = itemSchema.safeParse(event.body);
if (!item.success) {
throw new ValidationError(400, item.error);
}

const createdItem = await Item.create({
listId: event.pathParameters?.listId,
...item.data,
});

return {
statusCode: 200,
body: JSON.stringify(createdItem),
};
};

export const handler = middy()
.use(httpErrorHandler({ logger: false }))
.use(httpJsonBodyParser())
.handler(createItemHandler);

0 comments on commit 931996c

Please sign in to comment.