From 75eeb0e4e9c70d03d63048d752606c09ce69d85f Mon Sep 17 00:00:00 2001 From: jwu-fisker Date: Wed, 6 Jan 2021 21:54:09 -0800 Subject: [PATCH] Update app routes test --- src/components/App/App.test.js | 84 +- .../App/__snapshots__/App.test.js.snap | 732 ++++++++++++++++++ .../__snapshots__/FileUploadForm.test.js.snap | 2 +- src/components/FileUploadForm/index.jsx | 2 +- .../contexts/__mocks__/UserContext.jsx | 22 +- 5 files changed, 830 insertions(+), 12 deletions(-) create mode 100644 src/components/App/__snapshots__/App.test.js.snap diff --git a/src/components/App/App.test.js b/src/components/App/App.test.js index 7892e7b..a9e2c6a 100644 --- a/src/components/App/App.test.js +++ b/src/components/App/App.test.js @@ -1,8 +1,78 @@ -import { render, screen } from '@testing-library/react'; -import App from '.'; +jest.mock("../Contexts/UserContext"); -test('renders learn react link', () => { - render(); - const linkElement = screen.getByText(/learn react/i); - expect(linkElement).toBeInTheDocument(); -}); +import { render, screen, cleanup, waitFor } from "@testing-library/react" +import { setToken } from "../Contexts/UserContext"; +import App from "."; + +const TEST_TOKEN = { accessToken: { jwtToken: "TEST" }}; + +describe.only("App", () => { + + it ("Route / unauthenticated", async () => { + setToken(null); + window.history.pushState({}, "", "/"); + const { container } = render(); + // need 2 cycles to render due to routes + await waitFor(() => {}); + await waitFor(() => {}); + expect(container).toMatchSnapshot(); + expect(container.querySelector("h1").innerHTML).toEqual("Sign in"); + cleanup(); + }); + + it ("Route /signup unauthenticated", async () => { + setToken(null); + window.history.pushState({}, "", "/signup"); + const { container } = render(); + await waitFor(() => {}); + await waitFor(() => {}); + expect(container).toMatchSnapshot(); + expect(container.querySelector("h1").innerHTML).toEqual("Sign up"); + cleanup(); + }); + + it ("Route /home unauthenticated", async () => { + setToken(null); + window.history.pushState({}, "", "/home"); + const { container } = render(); + await waitFor(() => {}); + await waitFor(() => {}); + expect(container).toMatchSnapshot(); + expect(container.querySelector("h1").innerHTML).toEqual("Sign in"); + cleanup(); + }); + + it ("Route / authenticated", async () => { + setToken(TEST_TOKEN); + window.history.pushState({}, "", "/"); + const { container } = render(); + await waitFor(() => {}); + await waitFor(() => {}); + expect(container).toMatchSnapshot(); + expect(container.querySelector("h1").innerHTML).toEqual("Upload file"); + cleanup(); + }); + + it ("Route /signup authenticated", async () => { + setToken(TEST_TOKEN); + window.history.pushState({}, "", "/signup"); + const { container } = render(); + await waitFor(() => {}); + await waitFor(() => {}); + expect(container).toMatchSnapshot(); + expect(container.querySelector("h1").innerHTML).toEqual("Upload file"); + cleanup(); + }); + + it ("Route /home authenticated", async () => { + setToken(TEST_TOKEN); + window.history.pushState({}, "", "/home"); + const { container } = render(); + await waitFor(() => {}); + await waitFor(() => {}); + expect(container).toMatchSnapshot(); + expect(container.querySelector("h1").innerHTML).toEqual("Upload file"); + cleanup(); + }); + +}) \ No newline at end of file diff --git a/src/components/App/__snapshots__/App.test.js.snap b/src/components/App/__snapshots__/App.test.js.snap new file mode 100644 index 0000000..a8a7e6f --- /dev/null +++ b/src/components/App/__snapshots__/App.test.js.snap @@ -0,0 +1,732 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`App Route / authenticated 1`] = ` +
+
+
+
+

+ Upload file +

+
+
+ +
+

+ Drag and drop a file here or click +

+ +
+
+
+
+ +
+
+
+
+
+
+
+`; + +exports[`App Route / unauthenticated 1`] = ` +
+
+
+
+

+ Sign in +

+
+
+ +
+ + +
+
+
+ +
+ + +
+
+ + +
+
+
+
+
+`; + +exports[`App Route /home authenticated 1`] = ` +
+
+
+
+

+ Upload file +

+
+
+ +
+

+ Drag and drop a file here or click +

+ +
+
+
+
+ +
+
+
+
+
+
+
+`; + +exports[`App Route /home unauthenticated 1`] = ` +
+
+
+
+

+ Sign in +

+
+
+ +
+ + +
+
+
+ +
+ + +
+
+ + +
+
+
+
+
+`; + +exports[`App Route /signup authenticated 1`] = ` +
+
+
+
+

+ Upload file +

+
+
+ +
+

+ Drag and drop a file here or click +

+ +
+
+
+
+ +
+
+
+
+
+
+
+`; + +exports[`App Route /signup unauthenticated 1`] = ` +
+
+
+
+

+ Sign up +

+
+
+ +
+ + +
+
+
+ +
+ + +
+
+
+ +
+ + +
+
+ + +
+
+
+
+
+`; diff --git a/src/components/FileUploadForm/__snapshots__/FileUploadForm.test.js.snap b/src/components/FileUploadForm/__snapshots__/FileUploadForm.test.js.snap index 70916a3..16ca995 100644 --- a/src/components/FileUploadForm/__snapshots__/FileUploadForm.test.js.snap +++ b/src/components/FileUploadForm/__snapshots__/FileUploadForm.test.js.snap @@ -11,7 +11,7 @@ exports[`File Upload Form Should render 1`] = `

- Upload File + Upload file

- Upload File + Upload file diff --git a/src/components/contexts/__mocks__/UserContext.jsx b/src/components/contexts/__mocks__/UserContext.jsx index 1968079..73e7b45 100644 --- a/src/components/contexts/__mocks__/UserContext.jsx +++ b/src/components/contexts/__mocks__/UserContext.jsx @@ -1,5 +1,9 @@ import React from 'react'; +let token = null; +let fetching = false; +let error = null; + export const UserProvider = ({ children }) => { return (
@@ -9,11 +13,23 @@ export const UserProvider = ({ children }) => { }; export const useUserContext = () => ({ - fetching: false, - token: null, - error: null, + token, + fetching, + error, setError: jest.fn(), signIn: jest.fn(), signUp: jest.fn(), signOut: jest.fn(), }); + +export const setToken = (val) => { + token = val; +}; + +export const setFetching = (val) => { + fetching = val; +}; + +export const setError = (val) => { + error = val; +};