Skip to content

Commit

Permalink
Add createCategory tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Mar 30, 2023
1 parent 3eb20bb commit 782fdd9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
5 changes: 4 additions & 1 deletion app/accounts/[id]/categories/new.tsx
Expand Up @@ -16,7 +16,10 @@ export default function CategoryNew(): JSX.Element {

const onClickOk = () => {
if (account === null) return;
const [newAccount, event] = createCategory(account, name);
const result = createCategory(account, name);
// TODO: error handling
if (result.isErr()) return;
const [newAccount, event] = result.value;
storeEvent(getLastEventId(account), event).then((_) => {
setAccount(newAccount);
router.back();
Expand Down
36 changes: 35 additions & 1 deletion lib/account.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from "@jest/globals";
import { createAccount } from "./account";
import { createAccount, createCategory } from "./account";

describe("createAccount", () => {
describe("happy path", () => {
Expand Down Expand Up @@ -28,3 +28,37 @@ describe("createAccount", () => {
});
});
});

describe("createCategory", () => {
describe("happy path", () => {
it("works", () => {
const before = createAccount("account name 1")._unsafeUnwrap()[0];
const name = "category name 1";
const result = createCategory(before, name);
if (result.isErr()) throw new Error();
const [account, event] = result.value;
if (event.type !== "categoryAdded") throw new Error();
const categoryAdded = event;
expect(account.categories.length).toBe(1);
expect(account.categories[0]?.accountId).toStrictEqual(before.id);
expect(account.categories[0]?.createdAt).toStrictEqual(event.at);
expect(account.categories[0]?.deletedAt).toBeNull();
expect(account.categories[0]?.name).toStrictEqual(name);
expect(account.events[account.events.length - 1]).toStrictEqual(event);
expect(account.id).toStrictEqual(before.id);
expect(account.name).toStrictEqual(before.name);
expect(account.transactions).toStrictEqual(before.transactions);
expect(categoryAdded.accountId).toStrictEqual(account.id);
expect(categoryAdded.name).toStrictEqual(name);
});
});

describe("when name is empty", () => {
it("returns err", () => {
const before = createAccount("account name 1")._unsafeUnwrap()[0];
const name = "";
const result = createCategory(before, name);
expect(result.isErr()).toBe(true);
});
});
});
11 changes: 6 additions & 5 deletions lib/account.ts
Expand Up @@ -60,16 +60,17 @@ export const createAccount = (
export const createCategory = (
self: Account,
name: string
): [Account, AccountEvent] => {
): Result<[Account, AccountEvent], string> => {
if (name.length === 0) return err("name is empty");
const event: CategoryAdded = {
type: "categoryAdded",
categoryId: generateUuidV4(),
accountId: self.id,
at: new Date().toISOString(),
categoryId: generateUuidV4(),
id: generateUuidV4(),
name,
at: new Date().toISOString(),
type: "categoryAdded",
};
return [applyEvent(self, event), event];
return ok([applyEvent(self, event), event]);
};

export const createTransaction = (
Expand Down

0 comments on commit 782fdd9

Please sign in to comment.