diff --git a/frontend/__tests__/unit/components/HomeLoggedIn.test.jsx b/frontend/__tests__/unit/components/HomeLoggedIn.test.jsx index 2877952..5a8734d 100644 --- a/frontend/__tests__/unit/components/HomeLoggedIn.test.jsx +++ b/frontend/__tests__/unit/components/HomeLoggedIn.test.jsx @@ -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( + + + + + { + testLocation = location; + return null; + }} + /> + + ); + }); + + const createANetworkButton = screen.getByRole("button", { + name: "Create A Network", + }); + + await user.click(createANetworkButton); + expect(testLocation.pathname).toBe("/network/testid"); + }); });