test(HomeLoggedIn): add test for 100% coverage

This commit is contained in:
David Laganiere 2023-08-13 14:21:21 -04:00
commit 514a86831f
No known key found for this signature in database
GPG key ID: 18C4B80581C28747

View file

@ -1,9 +1,11 @@
import { render, screen } from "@testing-library/react";
import HomeLoggedIn from "components/HomeLoggedIn";
import { Router } from "react-router-dom";
import { MemoryRouter, Route, Router } from "react-router-dom";
import { createMemoryHistory } from "history";
import { testNetwork } from "../../data/network";
import userEvent from "@testing-library/user-event";
import API from "utils/API";
import { act } from "react-dom/test-utils";
import MockAdapter from "axios-mock-adapter";
describe("HomeLoggedIn", () => {
@ -43,4 +45,44 @@ describe("HomeLoggedIn", () => {
expect(container).toMatchSnapshot();
});
it("creates a new network when the 'Create A Network' button is clicked", async () => {
jest.spyOn(Math, "random").mockReturnValue(0.5);
jest.spyOn(Date, "now").mockImplementation(() => 1487076708000);
let mock = new MockAdapter(API);
const user = userEvent.setup();
let testLocation;
const networkId = "testid";
// For the API call with the initial render
mock.onGet("network").reply(200, []);
// For the API call after the 'Create A Network' button is clicked
mock.onPost("/network").reply(200, { config: { id: "testid" } });
await act(async () => {
render(
<MemoryRouter initialEntries={["/"]}>
<Route path="/">
<HomeLoggedIn />
</Route>
<Route
path="*"
render={({ location }) => {
testLocation = location;
return null;
}}
/>
</MemoryRouter>
);
});
const createANetworkButton = screen.getByRole("button", {
name: "Create A Network",
});
await user.click(createANetworkButton);
expect(testLocation.pathname).toBe("/network/testid");
});
});