Skip to content

Commit

Permalink
Add deleteTransaction tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Mar 30, 2023
1 parent 6dbbe03 commit 9b7ca91
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 8 deletions.
8 changes: 4 additions & 4 deletions app/accounts/[id]/transactions/index.tsx
Expand Up @@ -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);
Expand Down
60 changes: 60 additions & 0 deletions lib/account.test.ts
Expand Up @@ -4,6 +4,7 @@ import {
createCategory,
createTransaction,
deleteCategory,
deleteTransaction,
} from "./account";

describe("createAccount", () => {
Expand Down Expand Up @@ -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);
});
});
});
10 changes: 6 additions & 4 deletions lib/account.ts
Expand Up @@ -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
Expand Down

0 comments on commit 9b7ca91

Please sign in to comment.