From bd7d15721d9ce57b9f1492b7fc7bf49d05867f48 Mon Sep 17 00:00:00 2001 From: jwu-fisker Date: Thu, 7 Jan 2021 08:10:09 -0800 Subject: [PATCH] Refactor rendering --- src/components/App/App.test.js | 57 +++++++++++++++------------------- 1 file changed, 25 insertions(+), 32 deletions(-) diff --git a/src/components/App/App.test.js b/src/components/App/App.test.js index a9e2c6a..629f5c7 100644 --- a/src/components/App/App.test.js +++ b/src/components/App/App.test.js @@ -1,75 +1,68 @@ jest.mock("../Contexts/UserContext"); -import { render, screen, cleanup, waitFor } from "@testing-library/react" +import { render, screen, cleanup, waitForElementToBeRemoved, waitFor } from "@testing-library/react" import { setToken } from "../Contexts/UserContext"; import App from "."; const TEST_TOKEN = { accessToken: { jwtToken: "TEST" }}; +const LOADING_STATUS = "Loading..."; + +const renderRoute = async (route) => { + window.history.pushState({}, "", route); + const { container } = render(); + if (screen.queryByText(LOADING_STATUS)) { + await waitForElementToBeRemoved(() => screen.getByText(LOADING_STATUS)); + } else { + await waitFor(() => {}); + } + return container; +}; describe.only("App", () => { - it ("Route / unauthenticated", async () => { + it("Route / unauthenticated", async () => { setToken(null); - window.history.pushState({}, "", "/"); - const { container } = render(); - // need 2 cycles to render due to routes - await waitFor(() => {}); - await waitFor(() => {}); + const container = await renderRoute("/"); expect(container).toMatchSnapshot(); expect(container.querySelector("h1").innerHTML).toEqual("Sign in"); cleanup(); }); - it ("Route /signup unauthenticated", async () => { + it("Route /signup unauthenticated", async () => { setToken(null); - window.history.pushState({}, "", "/signup"); - const { container } = render(); - await waitFor(() => {}); - await waitFor(() => {}); + const container = await renderRoute("/signup"); expect(container).toMatchSnapshot(); expect(container.querySelector("h1").innerHTML).toEqual("Sign up"); cleanup(); }); - it ("Route /home unauthenticated", async () => { + it("Route /home unauthenticated", async () => { setToken(null); - window.history.pushState({}, "", "/home"); - const { container } = render(); - await waitFor(() => {}); - await waitFor(() => {}); + const container = await renderRoute("/home"); expect(container).toMatchSnapshot(); expect(container.querySelector("h1").innerHTML).toEqual("Sign in"); cleanup(); }); - it ("Route / authenticated", async () => { + it("Route / authenticated", async () => { setToken(TEST_TOKEN); - window.history.pushState({}, "", "/"); - const { container } = render(); - await waitFor(() => {}); - await waitFor(() => {}); + const container = await renderRoute("/"); expect(container).toMatchSnapshot(); expect(container.querySelector("h1").innerHTML).toEqual("Upload file"); cleanup(); }); - it ("Route /signup authenticated", async () => { + it("Route /signup authenticated", async () => { setToken(TEST_TOKEN); - window.history.pushState({}, "", "/signup"); - const { container } = render(); - await waitFor(() => {}); - await waitFor(() => {}); + const container = await renderRoute("/signup"); expect(container).toMatchSnapshot(); expect(container.querySelector("h1").innerHTML).toEqual("Upload file"); cleanup(); }); - it ("Route /home authenticated", async () => { + it("Route /home authenticated", async () => { setToken(TEST_TOKEN); - window.history.pushState({}, "", "/home"); - const { container } = render(); - await waitFor(() => {}); - await waitFor(() => {}); + const container = await renderRoute("/home"); expect(container).toMatchSnapshot(); expect(container.querySelector("h1").innerHTML).toEqual("Upload file"); cleanup();