Skip to content

Commit

Permalink
chore: Automatically clear mocks between tests
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Haines <[email protected]>
  • Loading branch information
haines committed Jan 13, 2025
1 parent 73c47ce commit 00e4631
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
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

0 comments on commit 00e4631

Please sign in to comment.