Skip to content

Commit

Permalink
changes to toHaveBeenCalledWith()
Browse files Browse the repository at this point in the history
  • Loading branch information
devinvillarosa committed Dec 11, 2024
1 parent 3d5bd60 commit 4c9e55d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import { Toaster } from "@/components/ui/toaster";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { createWrapper } from "@tests/utils";
import { describe, expect, it, vi } from "vitest";
import { Table } from "./data-table";

const MOCK_DATA = [
{
id: "0",
created: "2021-01-01T00:00:00Z",
updated: "2021-01-01T00:00:00Z",
active: true,
name: "global concurrency limit 0",
limit: 0,
active_slots: 0,
slot_decay_per_second: 0,
},
];
const MOCK_ROW = {
id: "0",
created: "2021-01-01T00:00:00Z",
updated: "2021-01-01T00:00:00Z",
active: true,
name: "global concurrency limit 0",
limit: 0,
active_slots: 0,
slot_decay_per_second: 0,
};

describe("GlobalConcurrencyLimitTable -- table", () => {
it("renders row data", () => {
render(
<Table
data={MOCK_DATA}
data={[MOCK_ROW]}
onDeleteRow={vi.fn()}
onEditRow={vi.fn()}
searchValue=""
Expand All @@ -44,7 +43,7 @@ describe("GlobalConcurrencyLimitTable -- table", () => {

render(
<Table
data={MOCK_DATA}
data={[MOCK_ROW]}
onDeleteRow={mockFn}
onEditRow={vi.fn()}
searchValue=""
Expand All @@ -56,15 +55,15 @@ describe("GlobalConcurrencyLimitTable -- table", () => {
screen.getByRole("button", { name: /open menu/i, hidden: true }),
);
await user.click(screen.getByRole("menuitem", { name: /delete/i }));
expect(mockFn).toHaveBeenCalledOnce();
expect(mockFn).toBeCalledWith(MOCK_ROW);
});
it("calls onEdit upon clicking rest action menu item", async () => {
const user = userEvent.setup();
const mockFn = vi.fn();

render(
<Table
data={MOCK_DATA}
data={[MOCK_ROW]}
onDeleteRow={vi.fn()}
onEditRow={mockFn}
searchValue=""
Expand All @@ -76,6 +75,47 @@ describe("GlobalConcurrencyLimitTable -- table", () => {
screen.getByRole("button", { name: /open menu/i, hidden: true }),
);
await user.click(screen.getByRole("menuitem", { name: /edit/i }));
expect(mockFn).toHaveBeenCalledOnce();
expect(mockFn).toHaveBeenCalledWith(MOCK_ROW);
});

it("toggles active switch", async () => {
const user = userEvent.setup();

const { rerender } = render(
<>
<Toaster />
<Table
data={[MOCK_ROW]}
onDeleteRow={vi.fn()}
onEditRow={vi.fn()}
searchValue=""
onSearchChange={vi.fn()}
/>
</>,
{ wrapper: createWrapper() },
);
expect(
screen.getByRole("switch", { name: /toggle active/i }),
).toBeChecked();

await user.click(
screen.getByRole("switch", {
name: /toggle active/i,
}),
);
expect(screen.getByText("Concurrency limit updated")).toBeVisible();
rerender(
<Table
data={[{ ...MOCK_ROW, active: false }]}
onDeleteRow={vi.fn()}
onEditRow={vi.fn()}
searchValue=""
onSearchChange={vi.fn()}
/>,
);

expect(
screen.getByRole("switch", { name: /toggle active/i }),
).not.toBeChecked();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@ import userEvent from "@testing-library/user-event";
import { describe, expect, it, vi } from "vitest";
import { Table } from "./data-table";

const MOCK_DATA = [
{
id: "0",
created: "2021-01-01T00:00:00Z",
updated: "2021-01-01T00:00:00Z",
tag: "my tag 0",
concurrency_limit: 1,
active_slots: [] as Array<string>,
},
];
const MOCK_ROW = {
id: "0",
created: "2021-01-01T00:00:00Z",
updated: "2021-01-01T00:00:00Z",
tag: "my tag 0",
concurrency_limit: 1,
active_slots: [] as Array<string>,
};

describe("TaskRunDataTable -- table", () => {
it("renders row data", () => {
render(
<Table
data={MOCK_DATA}
data={[MOCK_ROW]}
onDeleteRow={vi.fn()}
onResetRow={vi.fn()}
searchValue=""
Expand All @@ -35,7 +33,7 @@ describe("TaskRunDataTable -- table", () => {

render(
<Table
data={MOCK_DATA}
data={[MOCK_ROW]}
onDeleteRow={mockFn}
onResetRow={vi.fn()}
searchValue=""
Expand All @@ -46,15 +44,15 @@ describe("TaskRunDataTable -- table", () => {
screen.getByRole("button", { name: /open menu/i, hidden: true }),
);
await user.click(screen.getByRole("menuitem", { name: /delete/i }));
expect(mockFn).toHaveBeenCalledOnce();
expect(mockFn).toHaveBeenCalledWith(MOCK_ROW);
});
it("calls onReset upon clicking rest action menu item", async () => {
const user = userEvent.setup();
const mockFn = vi.fn();

render(
<Table
data={MOCK_DATA}
data={[MOCK_ROW]}
onDeleteRow={vi.fn()}
onResetRow={mockFn}
searchValue=""
Expand All @@ -65,6 +63,6 @@ describe("TaskRunDataTable -- table", () => {
screen.getByRole("button", { name: /open menu/i, hidden: true }),
);
await user.click(screen.getByRole("menuitem", { name: /reset/i }));
expect(mockFn).toHaveBeenCalledOnce();
expect(mockFn).toHaveBeenCalledWith(MOCK_ROW);
});
});

0 comments on commit 4c9e55d

Please sign in to comment.