Update app routes test

This commit is contained in:
jwu-fisker
2021-01-06 21:54:09 -08:00
parent 85ec4d5728
commit 75eeb0e4e9
5 changed files with 830 additions and 12 deletions

View File

@@ -1,8 +1,78 @@
import { render, screen } from '@testing-library/react';
import App from '.';
jest.mock("../Contexts/UserContext");
test('renders learn react link', () => {
render(<App />);
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(<App />);
// 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(<App />);
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(<App />);
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(<App />);
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(<App />);
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(<App />);
await waitFor(() => {});
await waitFor(() => {});
expect(container).toMatchSnapshot();
expect(container.querySelector("h1").innerHTML).toEqual("Upload file");
cleanup();
});
})

View File

@@ -0,0 +1,732 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`App Route / authenticated 1`] = `
<div>
<div
data-testid="mocked-userprovider"
>
<main
class="MuiContainer-root MuiContainer-maxWidthXs"
>
<div
class="makeStyles-paper-25"
>
<h1
class="MuiTypography-root MuiTypography-h5"
>
Upload file
</h1>
<form
class="makeStyles-form-27"
novalidate=""
>
<div
class="MuiDropzoneArea-root"
tabindex="0"
>
<input
accept=""
autocomplete="off"
multiple=""
style="display: none;"
tabindex="-1"
type="file"
/>
<div
class="MuiDropzoneArea-textContainer"
>
<p
class="MuiTypography-root MuiDropzoneArea-text MuiTypography-h5"
>
Drag and drop a file here or click
</p>
<svg
aria-hidden="true"
class="MuiSvgIcon-root MuiDropzoneArea-icon"
focusable="false"
viewBox="0 0 24 24"
>
<path
d="M19.35 10.04C18.67 6.59 15.64 4 12 4 9.11 4 6.6 5.64 5.35 8.04 2.34 8.36 0 10.91 0 14c0 3.31 2.69 6 6 6h13c2.76 0 5-2.24 5-5 0-2.64-2.05-4.78-4.65-4.96zM14 13v4h-4v-4H7l5-5 5 5h-3z"
/>
</svg>
</div>
</div>
<div
class="MuiGrid-root MuiGrid-container"
>
<div
class="MuiGrid-root MuiGrid-item"
>
<button
class="MuiButtonBase-root MuiButton-root MuiButton-text"
tabindex="0"
type="button"
>
<span
class="MuiButton-label"
>
Sign Out
</span>
<span
class="MuiTouchRipple-root"
/>
</button>
</div>
</div>
</form>
</div>
</main>
</div>
</div>
`;
exports[`App Route / unauthenticated 1`] = `
<div>
<div
data-testid="mocked-userprovider"
>
<main
class="MuiContainer-root MuiContainer-maxWidthXs"
>
<div
class="makeStyles-paper-1"
>
<h1
class="MuiTypography-root MuiTypography-h5"
>
Sign in
</h1>
<form
action="{onSubmit}"
class="makeStyles-form-3"
novalidate=""
>
<div
class="MuiFormControl-root MuiTextField-root MuiFormControl-marginNormal MuiFormControl-fullWidth"
>
<label
class="MuiFormLabel-root MuiInputLabel-root MuiInputLabel-formControl MuiInputLabel-animated MuiInputLabel-shrink MuiInputLabel-outlined Mui-focused Mui-focused Mui-required Mui-required"
data-shrink="true"
for="email"
id="email-label"
>
Email Address
<span
aria-hidden="true"
class="MuiFormLabel-asterisk MuiInputLabel-asterisk"
>
*
</span>
</label>
<div
class="MuiInputBase-root MuiOutlinedInput-root MuiInputBase-fullWidth Mui-focused Mui-focused MuiInputBase-formControl"
>
<input
aria-invalid="false"
autocomplete="email"
class="MuiInputBase-input MuiOutlinedInput-input"
id="email"
name="email"
required=""
type="text"
value=""
/>
<fieldset
aria-hidden="true"
class="PrivateNotchedOutline-root-5 MuiOutlinedInput-notchedOutline"
>
<legend
class="PrivateNotchedOutline-legendLabelled-7 PrivateNotchedOutline-legendNotched-8"
>
<span>
Email Address
 *
</span>
</legend>
</fieldset>
</div>
</div>
<div
class="MuiFormControl-root MuiTextField-root MuiFormControl-marginNormal MuiFormControl-fullWidth"
>
<label
class="MuiFormLabel-root MuiInputLabel-root MuiInputLabel-formControl MuiInputLabel-animated MuiInputLabel-outlined Mui-required Mui-required"
data-shrink="false"
for="password"
id="password-label"
>
Password
<span
aria-hidden="true"
class="MuiFormLabel-asterisk MuiInputLabel-asterisk"
>
*
</span>
</label>
<div
class="MuiInputBase-root MuiOutlinedInput-root MuiInputBase-fullWidth MuiInputBase-formControl"
>
<input
aria-invalid="false"
autocomplete="current-password"
class="MuiInputBase-input MuiOutlinedInput-input"
id="password"
name="password"
required=""
type="password"
value=""
/>
<fieldset
aria-hidden="true"
class="PrivateNotchedOutline-root-5 MuiOutlinedInput-notchedOutline"
>
<legend
class="PrivateNotchedOutline-legendLabelled-7"
>
<span>
Password
 *
</span>
</legend>
</fieldset>
</div>
</div>
<button
class="MuiButtonBase-root MuiButton-root MuiButton-contained makeStyles-submit-4 MuiButton-containedPrimary MuiButton-fullWidth"
tabindex="0"
type="submit"
>
<span
class="MuiButton-label"
>
Sign In
</span>
<span
class="MuiTouchRipple-root"
/>
</button>
<div
class="MuiGrid-root MuiGrid-container"
>
<div
class="MuiGrid-root MuiGrid-item"
>
<a
class="MuiTypography-root MuiLink-root MuiLink-underlineHover MuiTypography-body2 MuiTypography-colorPrimary"
href="/signup"
>
Don't have an account? Sign Up
</a>
</div>
</div>
</form>
</div>
</main>
</div>
</div>
`;
exports[`App Route /home authenticated 1`] = `
<div>
<div
data-testid="mocked-userprovider"
>
<main
class="MuiContainer-root MuiContainer-maxWidthXs"
>
<div
class="makeStyles-paper-33"
>
<h1
class="MuiTypography-root MuiTypography-h5"
>
Upload file
</h1>
<form
class="makeStyles-form-35"
novalidate=""
>
<div
class="MuiDropzoneArea-root"
tabindex="0"
>
<input
accept=""
autocomplete="off"
multiple=""
style="display: none;"
tabindex="-1"
type="file"
/>
<div
class="MuiDropzoneArea-textContainer"
>
<p
class="MuiTypography-root MuiDropzoneArea-text MuiTypography-h5"
>
Drag and drop a file here or click
</p>
<svg
aria-hidden="true"
class="MuiSvgIcon-root MuiDropzoneArea-icon"
focusable="false"
viewBox="0 0 24 24"
>
<path
d="M19.35 10.04C18.67 6.59 15.64 4 12 4 9.11 4 6.6 5.64 5.35 8.04 2.34 8.36 0 10.91 0 14c0 3.31 2.69 6 6 6h13c2.76 0 5-2.24 5-5 0-2.64-2.05-4.78-4.65-4.96zM14 13v4h-4v-4H7l5-5 5 5h-3z"
/>
</svg>
</div>
</div>
<div
class="MuiGrid-root MuiGrid-container"
>
<div
class="MuiGrid-root MuiGrid-item"
>
<button
class="MuiButtonBase-root MuiButton-root MuiButton-text"
tabindex="0"
type="button"
>
<span
class="MuiButton-label"
>
Sign Out
</span>
<span
class="MuiTouchRipple-root"
/>
</button>
</div>
</div>
</form>
</div>
</main>
</div>
</div>
`;
exports[`App Route /home unauthenticated 1`] = `
<div>
<div
data-testid="mocked-userprovider"
>
<main
class="MuiContainer-root MuiContainer-maxWidthXs"
>
<div
class="makeStyles-paper-17"
>
<h1
class="MuiTypography-root MuiTypography-h5"
>
Sign in
</h1>
<form
action="{onSubmit}"
class="makeStyles-form-19"
novalidate=""
>
<div
class="MuiFormControl-root MuiTextField-root MuiFormControl-marginNormal MuiFormControl-fullWidth"
>
<label
class="MuiFormLabel-root MuiInputLabel-root MuiInputLabel-formControl MuiInputLabel-animated MuiInputLabel-shrink MuiInputLabel-outlined Mui-focused Mui-focused Mui-required Mui-required"
data-shrink="true"
for="email"
id="email-label"
>
Email Address
<span
aria-hidden="true"
class="MuiFormLabel-asterisk MuiInputLabel-asterisk"
>
*
</span>
</label>
<div
class="MuiInputBase-root MuiOutlinedInput-root MuiInputBase-fullWidth Mui-focused Mui-focused MuiInputBase-formControl"
>
<input
aria-invalid="false"
autocomplete="email"
class="MuiInputBase-input MuiOutlinedInput-input"
id="email"
name="email"
required=""
type="text"
value=""
/>
<fieldset
aria-hidden="true"
class="PrivateNotchedOutline-root-21 MuiOutlinedInput-notchedOutline"
>
<legend
class="PrivateNotchedOutline-legendLabelled-23 PrivateNotchedOutline-legendNotched-24"
>
<span>
Email Address
 *
</span>
</legend>
</fieldset>
</div>
</div>
<div
class="MuiFormControl-root MuiTextField-root MuiFormControl-marginNormal MuiFormControl-fullWidth"
>
<label
class="MuiFormLabel-root MuiInputLabel-root MuiInputLabel-formControl MuiInputLabel-animated MuiInputLabel-outlined Mui-required Mui-required"
data-shrink="false"
for="password"
id="password-label"
>
Password
<span
aria-hidden="true"
class="MuiFormLabel-asterisk MuiInputLabel-asterisk"
>
*
</span>
</label>
<div
class="MuiInputBase-root MuiOutlinedInput-root MuiInputBase-fullWidth MuiInputBase-formControl"
>
<input
aria-invalid="false"
autocomplete="current-password"
class="MuiInputBase-input MuiOutlinedInput-input"
id="password"
name="password"
required=""
type="password"
value=""
/>
<fieldset
aria-hidden="true"
class="PrivateNotchedOutline-root-21 MuiOutlinedInput-notchedOutline"
>
<legend
class="PrivateNotchedOutline-legendLabelled-23"
>
<span>
Password
 *
</span>
</legend>
</fieldset>
</div>
</div>
<button
class="MuiButtonBase-root MuiButton-root MuiButton-contained makeStyles-submit-20 MuiButton-containedPrimary MuiButton-fullWidth"
tabindex="0"
type="submit"
>
<span
class="MuiButton-label"
>
Sign In
</span>
<span
class="MuiTouchRipple-root"
/>
</button>
<div
class="MuiGrid-root MuiGrid-container"
>
<div
class="MuiGrid-root MuiGrid-item"
>
<a
class="MuiTypography-root MuiLink-root MuiLink-underlineHover MuiTypography-body2 MuiTypography-colorPrimary"
href="/signup"
>
Don't have an account? Sign Up
</a>
</div>
</div>
</form>
</div>
</main>
</div>
</div>
`;
exports[`App Route /signup authenticated 1`] = `
<div>
<div
data-testid="mocked-userprovider"
>
<main
class="MuiContainer-root MuiContainer-maxWidthXs"
>
<div
class="makeStyles-paper-29"
>
<h1
class="MuiTypography-root MuiTypography-h5"
>
Upload file
</h1>
<form
class="makeStyles-form-31"
novalidate=""
>
<div
class="MuiDropzoneArea-root"
tabindex="0"
>
<input
accept=""
autocomplete="off"
multiple=""
style="display: none;"
tabindex="-1"
type="file"
/>
<div
class="MuiDropzoneArea-textContainer"
>
<p
class="MuiTypography-root MuiDropzoneArea-text MuiTypography-h5"
>
Drag and drop a file here or click
</p>
<svg
aria-hidden="true"
class="MuiSvgIcon-root MuiDropzoneArea-icon"
focusable="false"
viewBox="0 0 24 24"
>
<path
d="M19.35 10.04C18.67 6.59 15.64 4 12 4 9.11 4 6.6 5.64 5.35 8.04 2.34 8.36 0 10.91 0 14c0 3.31 2.69 6 6 6h13c2.76 0 5-2.24 5-5 0-2.64-2.05-4.78-4.65-4.96zM14 13v4h-4v-4H7l5-5 5 5h-3z"
/>
</svg>
</div>
</div>
<div
class="MuiGrid-root MuiGrid-container"
>
<div
class="MuiGrid-root MuiGrid-item"
>
<button
class="MuiButtonBase-root MuiButton-root MuiButton-text"
tabindex="0"
type="button"
>
<span
class="MuiButton-label"
>
Sign Out
</span>
<span
class="MuiTouchRipple-root"
/>
</button>
</div>
</div>
</form>
</div>
</main>
</div>
</div>
`;
exports[`App Route /signup unauthenticated 1`] = `
<div>
<div
data-testid="mocked-userprovider"
>
<main
class="MuiContainer-root MuiContainer-maxWidthXs"
>
<div
class="makeStyles-paper-9"
>
<h1
class="MuiTypography-root MuiTypography-h5"
>
Sign up
</h1>
<form
class="makeStyles-form-11"
novalidate=""
>
<div
class="MuiFormControl-root MuiTextField-root MuiFormControl-marginNormal MuiFormControl-fullWidth"
>
<label
class="MuiFormLabel-root MuiInputLabel-root MuiInputLabel-formControl MuiInputLabel-animated MuiInputLabel-shrink MuiInputLabel-outlined Mui-focused Mui-focused Mui-required Mui-required"
data-shrink="true"
for="email"
id="email-label"
>
Email Address
<span
aria-hidden="true"
class="MuiFormLabel-asterisk MuiInputLabel-asterisk"
>
*
</span>
</label>
<div
class="MuiInputBase-root MuiOutlinedInput-root MuiInputBase-fullWidth Mui-focused Mui-focused MuiInputBase-formControl"
>
<input
aria-invalid="false"
autocomplete="email"
class="MuiInputBase-input MuiOutlinedInput-input"
id="email"
name="email"
required=""
type="text"
value=""
/>
<fieldset
aria-hidden="true"
class="PrivateNotchedOutline-root-13 MuiOutlinedInput-notchedOutline"
>
<legend
class="PrivateNotchedOutline-legendLabelled-15 PrivateNotchedOutline-legendNotched-16"
>
<span>
Email Address
 *
</span>
</legend>
</fieldset>
</div>
</div>
<div
class="MuiFormControl-root MuiTextField-root MuiFormControl-marginNormal MuiFormControl-fullWidth"
>
<label
class="MuiFormLabel-root MuiInputLabel-root MuiInputLabel-formControl MuiInputLabel-animated MuiInputLabel-outlined Mui-required Mui-required"
data-shrink="false"
for="password"
id="password-label"
>
Password
<span
aria-hidden="true"
class="MuiFormLabel-asterisk MuiInputLabel-asterisk"
>
*
</span>
</label>
<div
class="MuiInputBase-root MuiOutlinedInput-root MuiInputBase-fullWidth MuiInputBase-formControl"
>
<input
aria-invalid="false"
autocomplete="new-password"
class="MuiInputBase-input MuiOutlinedInput-input"
id="password"
name="password"
required=""
type="password"
value=""
/>
<fieldset
aria-hidden="true"
class="PrivateNotchedOutline-root-13 MuiOutlinedInput-notchedOutline"
>
<legend
class="PrivateNotchedOutline-legendLabelled-15"
>
<span>
Password
 *
</span>
</legend>
</fieldset>
</div>
</div>
<div
class="MuiFormControl-root MuiTextField-root MuiFormControl-marginNormal MuiFormControl-fullWidth"
>
<label
class="MuiFormLabel-root MuiInputLabel-root MuiInputLabel-formControl MuiInputLabel-animated MuiInputLabel-outlined Mui-required Mui-required"
data-shrink="false"
for="passwordConfirm"
id="passwordConfirm-label"
>
Confirm Password
<span
aria-hidden="true"
class="MuiFormLabel-asterisk MuiInputLabel-asterisk"
>
*
</span>
</label>
<div
class="MuiInputBase-root MuiOutlinedInput-root MuiInputBase-fullWidth MuiInputBase-formControl"
>
<input
aria-invalid="false"
class="MuiInputBase-input MuiOutlinedInput-input"
id="passwordConfirm"
name="password"
required=""
type="password"
value=""
/>
<fieldset
aria-hidden="true"
class="PrivateNotchedOutline-root-13 MuiOutlinedInput-notchedOutline"
>
<legend
class="PrivateNotchedOutline-legendLabelled-15"
>
<span>
Confirm Password
 *
</span>
</legend>
</fieldset>
</div>
</div>
<button
class="MuiButtonBase-root MuiButton-root MuiButton-contained makeStyles-submit-12 MuiButton-containedPrimary MuiButton-fullWidth"
tabindex="0"
type="submit"
>
<span
class="MuiButton-label"
>
Sign Up
</span>
<span
class="MuiTouchRipple-root"
/>
</button>
<div
class="MuiGrid-root MuiGrid-container"
>
<div
class="MuiGrid-root MuiGrid-item"
>
<a
class="MuiTypography-root MuiLink-root MuiLink-underlineHover MuiTypography-body2 MuiTypography-colorPrimary"
href="/"
>
Already have an account? Sign In
</a>
</div>
</div>
</form>
</div>
</main>
</div>
</div>
`;

View File

@@ -11,7 +11,7 @@ exports[`File Upload Form Should render 1`] = `
<h1
class="MuiTypography-root MuiTypography-h5"
>
Upload File
Upload file
</h1>
<form
class="makeStyles-form-3"

View File

@@ -13,7 +13,7 @@ export default function FileUploadForm() {
<CssBaseline />
<div className={classes.paper}>
<Typography component="h1" variant="h5">
Upload File
Upload file
</Typography>
<form className={classes.form} noValidate>
<DropzoneAreaBase />

View File

@@ -1,5 +1,9 @@
import React from 'react';
let token = null;
let fetching = false;
let error = null;
export const UserProvider = ({ children }) => {
return (
<div data-testid="mocked-userprovider">
@@ -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;
};