Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Automatically clear mocks between tests #1073

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions private/test/src/matrix-react/cerbos-provider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,36 +80,41 @@ function App({ children }: PropsWithChildren<object>): ReactElement {

describe("<CerbosProvider>", () => {
const container = document.createElement("div");
let rerender: () => void;

const { rerender } = renderHook(useCerbos, { wrapper: App, container });
it("correctly initializes ClientWithPrincipal", () => {
expect(client.withPrincipal).toHaveBeenLastCalledWith(
({ rerender } = renderHook(useCerbos, { wrapper: App, container }));

expect(client.withPrincipal).toHaveBeenCalledOnce();
expect(client.withPrincipal).toHaveBeenCalledWith(
getPrincipal(false),
getAuxData(false),
);
expect(client.withPrincipal).toHaveBeenCalledTimes(1);
});

it("re-initializes ClientWithPrincipal when principal changes", async () => {
await userEvent.click(getByText(container, "switch user"));
expect(client.withPrincipal).toHaveBeenLastCalledWith(

expect(client.withPrincipal).toHaveBeenCalledOnce();
expect(client.withPrincipal).toHaveBeenCalledWith(
getPrincipal(true),
getAuxData(false),
);
expect(client.withPrincipal).toHaveBeenCalledTimes(2);
});

it("re-initializes ClientWithPrincipal when auxData changes", async () => {
await userEvent.click(getByText(container, "switch aux data"));
expect(client.withPrincipal).toHaveBeenLastCalledWith(

expect(client.withPrincipal).toHaveBeenCalledOnce();
expect(client.withPrincipal).toHaveBeenCalledWith(
getPrincipal(true),
getAuxData(true),
);
expect(client.withPrincipal).toHaveBeenCalledTimes(3);
});

it("re-renders without parameter values does not cause re-initialization of ClientWithPrincipal", () => {
rerender();
expect(client.withPrincipal).toHaveBeenCalledTimes(3);

expect(client.withPrincipal).not.toHaveBeenCalled();
});
});
5 changes: 0 additions & 5 deletions private/test/src/matrix-react/hooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
afterAll,
afterEach,
beforeAll,
beforeEach,
describe,
expect,
it,
Expand Down Expand Up @@ -140,10 +139,6 @@ function testCerbosHook<TParams>(
cleanup();
});

beforeEach(() => {
vi.clearAllMocks();
});

function render(
client: Pick<Client, "withPrincipal">,
): ReturnType<typeof renderHook<AsyncResult<unknown>, TParams>> {
Expand Down
1 change: 1 addition & 0 deletions private/test/vitest.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
clearMocks: true,
setupFiles: "src/setup.ts",
testTimeout: 20000,
},
Expand Down
Loading