Skip to content

Commit

Permalink
test: split into mothers
Browse files Browse the repository at this point in the history
Signed-off-by: rare-magma <[email protected]>
  • Loading branch information
rare-magma committed Aug 31, 2024
1 parent e329947 commit 9fcdca1
Show file tree
Hide file tree
Showing 16 changed files with 415 additions and 335 deletions.
25 changes: 11 additions & 14 deletions src/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ import userEvent from "@testing-library/user-event";
import { describe, expect, it } from "vitest";
import { App } from "./App";
import { budgetsDB, calcHistDB, optionsDB } from "./db";
import {
budgetContextSpy,
testBudget,
testEmptyBudgetContext,
} from "./setupTests";
import { budgetContextSpy, testEmptyBudgetContext } from "./setupTests";
import { BudgetMother } from "./guitos/domain/budget.mother";

describe("App", () => {
const comp = <App />;
Expand All @@ -29,11 +26,11 @@ describe("App", () => {
expect(calcHistDB.config("name")).toBe("guitos");
expect(calcHistDB.config("storeName")).toBe("calcHistDB");
await expect(
budgetsDB.getItem(testBudget.id.toString()),
budgetsDB.getItem(BudgetMother.testBudget().id.toString()),
).resolves.toBeNull();
});

it.skip("shows new budget when clicking new button", async () => {
it("shows new budget when clicking new button", async () => {
render(comp);
const newButton = screen.getAllByRole("button", { name: "new budget" });
await act(async () => {
Expand All @@ -44,17 +41,17 @@ describe("App", () => {
expect(await screen.findByText("Statistics")).toBeInTheDocument();
expect(await screen.findByText("Revenue")).toBeInTheDocument();
expect(await screen.findByText("Expenses")).toBeInTheDocument();
await expect(budgetsDB.getItem(testBudget.id.toString())).resolves.toEqual(
testBudget,
);
await expect(
budgetsDB.getItem(BudgetMother.testBudget().id.toString()),
).resolves.toEqual(BudgetMother.testBudget());
});

it.skip("deletes budget when clicking delete button", async () => {
it("deletes budget when clicking delete button", async () => {
render(comp);
await act(async () => {
await expect(
budgetsDB.getItem(testBudget.id.toString()),
).resolves.toEqual(testBudget);
budgetsDB.getItem(BudgetMother.testBudget().id.toString()),
).resolves.toEqual(BudgetMother.testBudget());
});

const newButton = await screen.findAllByRole("button", {
Expand All @@ -73,7 +70,7 @@ describe("App", () => {

await act(async () => {
await expect(
budgetsDB.getItem(testBudget.id.toString()),
budgetsDB.getItem(BudgetMother.testBudget().id.toString()),
).resolves.toBeNull();
});
});
Expand Down
15 changes: 10 additions & 5 deletions src/components/Budget/BudgetPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ import {
redoMock,
setBudgetMock,
setNotificationsMock,
testBudget,
testBudgetClone,
testBudgetContext,
undoMock,
} from "../../setupTests";
import { BudgetPage } from "./BudgetPage";
import { BudgetMother } from "../../guitos/domain/budget.mother";

describe("BudgetPage", () => {
const comp = (
Expand Down Expand Up @@ -53,7 +52,7 @@ describe("BudgetPage", () => {
await screen.findByRole("button", { name: "confirm budget deletion" }),
);
await expect(
budgetsDB.getItem(testBudget.id.toString()),
budgetsDB.getItem(BudgetMother.testBudget().id.toString()),
).resolves.toBeNull();
});

Expand All @@ -68,7 +67,10 @@ describe("BudgetPage", () => {
name: "clone budget",
});
await userEvent.click(cloneButton[0]);
expect(setBudgetMock).toHaveBeenCalledWith(testBudgetClone, true);
expect(setBudgetMock).toHaveBeenCalledWith(
BudgetMother.testBudgetClone(),
true,
);
});

it.skip("responds to clone budget keyboard shortcut", async () => {
Expand All @@ -79,7 +81,10 @@ describe("BudgetPage", () => {
await userEvent.click(newButton[0]);

await userEvent.type(await screen.findByTestId("header"), "c");
expect(setBudgetMock).toHaveBeenCalledWith(testBudgetClone, true);
expect(setBudgetMock).toHaveBeenCalledWith(
BudgetMother.testBudgetClone(),
true,
);
});

it("responds to undo change keyboard shortcut", async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/components/CalculateButton/CalculateButton.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import userEvent from "@testing-library/user-event";
import { BrowserRouter } from "react-router-dom";
import { vi } from "vitest";
import { describe, expect, it } from "vitest";
import { itemForm1 } from "../../setupTests";
import { CalculateButton } from "./CalculateButton";
import { BudgetItemsMother } from "../../guitos/domain/budgetItem.mother";

describe("CalculateButton", () => {
const onCalculate = vi.fn();
const comp = (
<BrowserRouter>
<CalculateButton
itemForm={itemForm1}
itemForm={BudgetItemsMother.itemForm1()}
label="Expense"
onCalculate={onCalculate}
/>
Expand Down
5 changes: 2 additions & 3 deletions src/components/Chart/Chart.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { render, screen } from "@testing-library/react";
import { vi } from "vitest";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import type { Budget } from "../../guitos/domain/budget";
import { testBudgetList } from "../../setupTests";
import { Chart } from "./Chart";
import { BudgetMother } from "../../guitos/domain/budget.mother";

describe("Chart", () => {
const comp = (
Expand All @@ -14,7 +13,7 @@ describe("Chart", () => {
areaStroke1={"highlight"}
areaFill1={"highlight"}
legend1={"median revenue"}
legendValues1={testBudgetList.map((b: Budget) => {
legendValues1={BudgetMother.testBudgetList().map((b) => {
return b.incomes.total;
})}
/>
Expand Down
18 changes: 9 additions & 9 deletions src/components/ItemForm/ItemFormGroup.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import { BrowserRouter } from "react-router-dom";
import { describe, expect, it } from "vitest";
import {
configContextSpy,
itemForm1,
setBudgetMock,
testBudget,
testSpanishConfigContext,
} from "../../setupTests";
import { ItemFormGroup } from "./ItemFormGroup";
import { BudgetMother } from "../../guitos/domain/budget.mother";
import { BudgetItemsMother } from "../../guitos/domain/budgetItem.mother";

describe("ItemFormGroup", () => {
const ref = createRef<HTMLInputElement>();
const comp = (
<BrowserRouter>
<ItemFormGroup
itemForm={itemForm1}
itemForm={BudgetItemsMother.itemForm1()}
label="Expenses"
inputRef={ref}
costPercentage={1}
Expand All @@ -44,7 +44,7 @@ describe("ItemFormGroup", () => {
expect(screen.getByDisplayValue("name1change name")).toBeInTheDocument();
expect(setBudgetMock).toHaveBeenCalledWith(
{
...testBudget,
...BudgetMother.testBudget(),
expenses: {
items: [{ id: 1, name: "name1change name", value: 10 }],
total: 10,
Expand All @@ -60,13 +60,13 @@ describe("ItemFormGroup", () => {
expect(screen.getByDisplayValue("$123")).toBeInTheDocument();
expect(setBudgetMock).toHaveBeenCalledWith(
{
...testBudget,
...BudgetMother.testBudget(),
expenses: {
items: [{ id: 1, name: "expense1", value: 123 }],
total: 123,
},
stats: {
...testBudget.stats,
...BudgetMother.testBudget().stats,
available: -23,
withGoal: -33,
},
Expand All @@ -87,10 +87,10 @@ describe("ItemFormGroup", () => {

expect(setBudgetMock).toHaveBeenCalledWith(
{
...testBudget,
...BudgetMother.testBudget(),
expenses: { items: [], total: 0 },
stats: {
...testBudget.stats,
...BudgetMother.testBudget().stats,
available: 100,
withGoal: 90,
},
Expand Down Expand Up @@ -126,7 +126,7 @@ describe("ItemFormGroup", () => {
render(
<BrowserRouter>
<ItemFormGroup
itemForm={itemForm1}
itemForm={BudgetItemsMother.itemForm1()}
label="Expenses"
inputRef={ref}
costPercentage={1}
Expand Down
4 changes: 2 additions & 2 deletions src/components/LandingPage/LandingPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import {
budgetContextSpy,
generalContextSpy,
setBudgetMock,
testBudget,
testEmptyBudgetContext,
testGeneralContext,
} from "../../setupTests";
import { LandingPage } from "./LandingPage";
import { BudgetMother } from "../../guitos/domain/budget.mother";

describe("LandingPage", () => {
const comp = (
Expand Down Expand Up @@ -49,7 +49,7 @@ describe("LandingPage", () => {
const uploadEl = screen.getByTestId("import-form-control-landing-page");
await userEvent.upload(
uploadEl,
new File([JSON.stringify(testBudget)], "test"),
new File([JSON.stringify(BudgetMother.testBudget())], "test"),
);
expect((uploadEl as HTMLInputElement).files).toHaveLength(1);
});
Expand Down
12 changes: 7 additions & 5 deletions src/components/NavBar/NavBar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import { describe, expect, it } from "vitest";
import {
budgetContextSpy,
setBudgetMock,
testBudget,
testBudgetClone,
testEmptyBudgetContext,
} from "../../setupTests";
import { NavBar } from "./NavBar";
import { BudgetMother } from "../../guitos/domain/budget.mother";

describe("NavBar", () => {
const comp = (
Expand Down Expand Up @@ -57,7 +56,10 @@ describe("NavBar", () => {
render(comp);
setBudgetMock.mockClear();
await userEvent.click(screen.getByLabelText("clone budget"));
expect(setBudgetMock).toHaveBeenCalledWith(testBudgetClone, true);
expect(setBudgetMock).toHaveBeenCalledWith(
BudgetMother.testBudgetClone(),
true,
);
});

it("triggers event when import button is pressed", async () => {
Expand All @@ -66,7 +68,7 @@ describe("NavBar", () => {
const uploadEl = screen.getByTestId("import-form-control");
await userEvent.upload(
uploadEl,
new File([JSON.stringify(testBudget)], "budget", {
new File([JSON.stringify(BudgetMother.testBudget())], "budget", {
type: "application/json",
}),
);
Expand Down Expand Up @@ -114,7 +116,7 @@ describe("NavBar", () => {

expect(screen.getByDisplayValue("2023-03change name")).toBeInTheDocument();
expect(setBudgetMock).toHaveBeenCalledWith(
{ ...testBudget, name: "2023-03change name" },
{ ...BudgetMother.testBudget(), name: "2023-03change name" },
false,
);
});
Expand Down
26 changes: 20 additions & 6 deletions src/components/StatCard/StatCard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { vi } from "vitest";
import { describe, expect, it } from "vitest";
import { setBudgetMock, testBudget } from "../../setupTests";
import { setBudgetMock } from "../../setupTests";
import { StatCard } from "./StatCard";
import { BudgetMother } from "../../guitos/domain/budget.mother";

describe("StatCard", () => {
const onShowGraphs = vi.fn();
Expand All @@ -29,7 +30,10 @@ describe("StatCard", () => {
await userEvent.type(screen.getByLabelText("reserves"), "2");

expect(setBudgetMock).toHaveBeenCalledWith(
{ ...testBudget, stats: { ...testBudget.stats, reserves: 2 } },
{
...BudgetMother.testBudget(),
stats: { ...BudgetMother.testBudget().stats, reserves: 2 },
},
false,
);
expect(screen.getByDisplayValue("$2")).toBeInTheDocument();
Expand All @@ -38,8 +42,13 @@ describe("StatCard", () => {
await userEvent.type(screen.getByTestId("goal-input"), "95");
expect(setBudgetMock).toHaveBeenCalledWith(
{
...testBudget,
stats: { ...testBudget.stats, goal: 95, saved: 95, withGoal: -5 },
...BudgetMother.testBudget(),
stats: {
...BudgetMother.testBudget().stats,
goal: 95,
saved: 95,
withGoal: -5,
},
},
false,
);
Expand All @@ -55,8 +64,13 @@ describe("StatCard", () => {
);
expect(setBudgetMock).toHaveBeenCalledWith(
{
...testBudget,
stats: { ...testBudget.stats, goal: 90, saved: 90, withGoal: 0 },
...BudgetMother.testBudget(),
stats: {
...BudgetMother.testBudget().stats,
goal: 90,
saved: 90,
withGoal: 0,
},
},
true,
);
Expand Down
Loading

0 comments on commit 9fcdca1

Please sign in to comment.