From 9b7ca917a246c9715f07349d8a53c29500db296b Mon Sep 17 00:00:00 2001 From: bouzuya Date: Thu, 30 Mar 2023 16:22:59 +0900 Subject: [PATCH] Add deleteTransaction tests --- app/accounts/[id]/transactions/index.tsx | 8 ++-- lib/account.test.ts | 60 ++++++++++++++++++++++++ lib/account.ts | 10 ++-- 3 files changed, 70 insertions(+), 8 deletions(-) diff --git a/app/accounts/[id]/transactions/index.tsx b/app/accounts/[id]/transactions/index.tsx index baf33ab..7c7acc6 100644 --- a/app/accounts/[id]/transactions/index.tsx +++ b/app/accounts/[id]/transactions/index.tsx @@ -113,10 +113,10 @@ export default function Transactions(): JSX.Element { onClickOk={() => { if (account === null || transactionId === null) return; // update local state - const [newAccount, newEvent] = deleteTransaction( - account, - transactionId - ); + const result = deleteTransaction(account, transactionId); + // TODO: error handling + if (result.isErr()) return; + const [newAccount, newEvent] = result.value; // update remote state setAccount(newAccount); diff --git a/lib/account.test.ts b/lib/account.test.ts index d09eed7..082cfe5 100644 --- a/lib/account.test.ts +++ b/lib/account.test.ts @@ -4,6 +4,7 @@ import { createCategory, createTransaction, deleteCategory, + deleteTransaction, } from "./account"; describe("createAccount", () => { @@ -236,3 +237,62 @@ describe("deleteCategory", () => { }); }); }); + +describe("deleteTransaction", () => { + describe("happy path", () => { + it("works", () => { + const v1 = createAccount("account name 1")._unsafeUnwrap()[0]; + const v2 = createCategory(v1, "category name 1")._unsafeUnwrap()[0]; + const selectedCategory = v2.categories[0]; + if (selectedCategory === undefined) throw new Error(); + const v3 = createTransaction(v2, { + amount: "123", + categoryId: selectedCategory.id, + comment: "comment1", + date: "2023-01-02", + })._unsafeUnwrap()[0]; + const selectedTransaction = v3.transactions[0]; + if (selectedTransaction === undefined) throw new Error(); + const transactionId = selectedTransaction.id; + const result = deleteTransaction(v3, transactionId); + if (result.isErr()) throw new Error(); + const [v4, event] = result.value; + + expect(v4.categories).toStrictEqual(v3.categories); + expect(v4.events).not.toStrictEqual(v3.events); + expect(v4.id).toStrictEqual(v3.id); + expect(v4.name).toStrictEqual(v3.name); + expect(v4.transactions).not.toStrictEqual(v3.transactions); + + if (event.type !== "transactionDeleted") throw new Error(); + const transactionDeleted = event; + expect(v4.events[v4.events.length - 1]).toStrictEqual(event); + expect(transactionDeleted.accountId).toStrictEqual(v4.id); + expect(transactionDeleted.at).not.toStrictEqual(""); + expect(transactionDeleted.id).not.toStrictEqual(""); + expect(transactionDeleted.transactionId).toStrictEqual(transactionId); + + expect(v4.transactions.length).toBe(0); + }); + }); + + describe("when transactionId is invalid", () => { + it("returns err", () => { + const v1 = createAccount("account name 1")._unsafeUnwrap()[0]; + const v2 = createCategory(v1, "category name 1")._unsafeUnwrap()[0]; + const selectedCategory = v2.categories[0]; + if (selectedCategory === undefined) throw new Error(); + const v3 = createTransaction(v2, { + amount: "123", + categoryId: selectedCategory.id, + comment: "comment1", + date: "2023-01-02", + })._unsafeUnwrap()[0]; + const selectedTransaction = v3.transactions[0]; + if (selectedTransaction === undefined) throw new Error(); + const transactionId = selectedTransaction.id + "s"; // invalid + const result = deleteTransaction(v2, transactionId); + expect(result.isErr()).toBe(true); + }); + }); +}); diff --git a/lib/account.ts b/lib/account.ts index 4980887..5ff4289 100644 --- a/lib/account.ts +++ b/lib/account.ts @@ -128,15 +128,17 @@ export const deleteCategory = ( export const deleteTransaction = ( self: Account, transactionId: string -): [Account, AccountEvent] => { +): Result<[Account, AccountEvent], string> => { + if (!self.transactions.some(({ id }) => id === transactionId)) + return err("transactionId not found"); const event: TransactionDeleted = { - type: "transactionDeleted", - transactionId, accountId: self.id, at: new Date().toISOString(), id: generateUuidV4(), + transactionId, + type: "transactionDeleted", }; - return [applyEvent(self, event), event]; + return ok([applyEvent(self, event), event]); }; // query