testing(utils): add test for auth and scanner
This commit is contained in:
parent
118ca1ed82
commit
fac5e7b66c
9 changed files with 238 additions and 88 deletions
frontend/src/shared/utils/__tests__
|
@ -0,0 +1,66 @@
|
|||
import React from "react";
|
||||
import { render, waitFor, cleanup } from "@testing-library/react";
|
||||
import { AuthContext } from "../../../App";
|
||||
import { useAuth } from "../useAuthentication";
|
||||
|
||||
const MockedComponent: React.FC = () => {
|
||||
const { authenticated } = useAuth();
|
||||
return <div>{authenticated}</div>;
|
||||
};
|
||||
|
||||
let authenticated = false;
|
||||
const setAuthenticated = (args: boolean) => (authenticated = args);
|
||||
|
||||
describe("useAuth", () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
cleanup();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
cleanup();
|
||||
});
|
||||
|
||||
it("should set authenticated to true when fetch returns ok", async () => {
|
||||
global.fetch = jest.fn(() => Promise.resolve({ ok: true })) as jest.Mock;
|
||||
|
||||
render(
|
||||
<AuthContext.Provider value={{ authenticated, setAuthenticated }}>
|
||||
<MockedComponent />
|
||||
</AuthContext.Provider>
|
||||
);
|
||||
|
||||
await waitFor(() => expect(fetch).toHaveBeenCalledTimes(1));
|
||||
|
||||
expect(authenticated).toBe(true);
|
||||
});
|
||||
|
||||
it("should set authenticated to false when fetch returns not ok", async () => {
|
||||
global.fetch = jest.fn(() => Promise.resolve({ ok: false })) as jest.Mock;
|
||||
|
||||
render(
|
||||
<AuthContext.Provider value={{ authenticated, setAuthenticated }}>
|
||||
<MockedComponent />
|
||||
</AuthContext.Provider>
|
||||
);
|
||||
|
||||
await waitFor(() => expect(fetch).toHaveBeenCalledTimes(1));
|
||||
|
||||
expect(authenticated).toBe(false);
|
||||
});
|
||||
|
||||
it("should call fetch once", async () => {
|
||||
global.fetch = jest.fn(() => Promise.resolve({ ok: false })) as jest.Mock;
|
||||
|
||||
render(
|
||||
<AuthContext.Provider value={{ authenticated, setAuthenticated }}>
|
||||
<MockedComponent />
|
||||
</AuthContext.Provider>
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(fetch).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue