From d9fc3650681151851deb824a27d13e841b2063e3 Mon Sep 17 00:00:00 2001 From: Enric Bisbe Gil Date: Sun, 7 Jul 2024 20:10:26 +0200 Subject: [PATCH] test create list lambda function --- src/functions/list.ts | 0 src/functions/list/__tests__/create.test.js | 30 ++++++++++++++++++++- src/functions/list/create.ts | 2 -- src/models/table.ts | 15 +++++------ 4 files changed, 36 insertions(+), 11 deletions(-) delete mode 100644 src/functions/list.ts diff --git a/src/functions/list.ts b/src/functions/list.ts deleted file mode 100644 index e69de29..0000000 diff --git a/src/functions/list/__tests__/create.test.js b/src/functions/list/__tests__/create.test.js index 5d956aa..d3a1d93 100644 --- a/src/functions/list/__tests__/create.test.js +++ b/src/functions/list/__tests__/create.test.js @@ -18,7 +18,35 @@ describe("CREATE List", () => { }); }); - it("creates a new list", () => {}); + it("creates a new list", async () => { + const event = { + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + userId: "userId", + name: "My Wedding List", + description: "This is my wedding gift list.", + }), + }; + 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.userId).toBe("userId"); + expect(body.name).toBe("My Wedding List"); + expect(body.description).toBe("This is my wedding gift list."); + expect(body.totalValue).toBe(0); + expect(body.totalItems).toBe(0); + + 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 = { diff --git a/src/functions/list/create.ts b/src/functions/list/create.ts index 12a78f3..04f41ca 100644 --- a/src/functions/list/create.ts +++ b/src/functions/list/create.ts @@ -20,8 +20,6 @@ const createListHandler: Handler = async (event) => { } const createdList = await List.create(list.data); - delete createdList.pk; - delete createdList.sk; return { statusCode: 200, diff --git a/src/models/table.ts b/src/models/table.ts index e2180bb..12408ac 100644 --- a/src/models/table.ts +++ b/src/models/table.ts @@ -20,8 +20,7 @@ const MySchema = { primary: { hash: "pk", sort: "sk" }, }, params: { - typeField: "__typename", - isoDates: true, + typeField: "__typeName", separator: "#", timestamps: true, createdField: "createdAt", @@ -37,8 +36,8 @@ const MySchema = { description: { type: String }, totalValue: { type: Number, default: 0 }, totalItems: { type: Number, default: 0 }, - createdAt: { type: String }, - updatedAt: { type: String }, + createdAt: { type: Date }, + updatedAt: { type: Date }, }, Item: { pk: { type: String, value: "LIST#${listId}" }, @@ -48,8 +47,8 @@ const MySchema = { name: { type: String }, description: { type: String }, value: { type: Number, default: 0 }, - createdAt: { type: String }, - updatedAt: { type: String }, + createdAt: { type: Date }, + updatedAt: { type: Date }, }, Buyer: { pk: { type: String, value: "ITEM#${itemId}" }, @@ -57,8 +56,8 @@ const MySchema = { userId: { type: String, required: true }, name: { type: String, required: true }, itemId: { type: String, required: true }, - createdAt: { type: String }, - updatedAt: { type: String }, + createdAt: { type: Date }, + updatedAt: { type: Date }, }, } as const, };