kandimat/redaktions-app/src/integration-tests/app-routing.integration.test.tsx
Christoph Lienhard 9944f8a38b
#20 add drawer menu
Also:
* Extract User Menu into own Component
2021-02-07 23:06:58 +01:00

85 lines
3.1 KiB
TypeScript

import React from "react";
import { render, screen } from "@testing-library/react";
import { MockedProvider } from "@apollo/client/testing";
import { MemoryRouter } from "react-router-dom";
import App from "../App";
import { SnackbarProvider } from "notistack";
const renderAppAtUrl = (path: string) =>
render(
<MockedProvider>
<MemoryRouter initialEntries={[path]}>
<SnackbarProvider>
<App />
</SnackbarProvider>
</MemoryRouter>
</MockedProvider>
);
beforeEach(() => localStorage.clear());
describe("The root path /", () => {
test("renders user's home page if they are logged in", () => {
localStorage.setItem(
"token",
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiY2FuZHltYXRfZWRpdG9yIiwicGVyc29uX3Jvd19pZCI6MSwiZXhwIjoxNjEyMjEyODQyLCJpYXQiOjE2MTIwNDAwNDIsImF1ZCI6InBvc3RncmFwaGlsZSIsImlzcyI6InBvc3RncmFwaGlsZSJ9.8Z8iCKq-WHOCyKz4rdrjwcVy7sR5_9dHQZR-lJDLEg4"
);
renderAppAtUrl("/");
expect(() => screen.getByLabelText(/current user/)).not.toThrow();
});
test("redirects to login page if user not logged in", () => {
renderAppAtUrl("/");
const emailField = screen.getByRole("textbox", { name: "Email Address" });
const passwordField = screen.getByLabelText(/Password/);
expect(emailField).toHaveValue("");
expect(passwordField).toHaveValue("");
});
});
describe("The /login path", () => {
test("renders the signin page if the user is not logged in", () => {
renderAppAtUrl("/login");
const emailField = screen.getByRole("textbox", { name: "Email Address" });
const passwordField = screen.getByLabelText(/Password/);
expect(emailField).toHaveValue("");
expect(passwordField).toHaveValue("");
});
test("redirects to root / and the user's home page if the user is logged in", () => {
localStorage.setItem(
"token",
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiY2FuZHltYXRfZWRpdG9yIiwicGVyc29uX3Jvd19pZCI6MSwiZXhwIjoxNjEyMjEyODQyLCJpYXQiOjE2MTIwNDAwNDIsImF1ZCI6InBvc3RncmFwaGlsZSIsImlzcyI6InBvc3RncmFwaGlsZSJ9.8Z8iCKq-WHOCyKz4rdrjwcVy7sR5_9dHQZR-lJDLEg4"
);
renderAppAtUrl("/login");
expect(() => screen.getByLabelText(/current user/)).not.toThrow();
});
});
describe("The /signup path", () => {
test("renders the signup page if the user is not logged in", () => {
renderAppAtUrl("/signup");
expect(() =>
screen.getByRole("textbox", { name: "Email Address" })
).not.toThrow();
expect(() => screen.getByLabelText(/Password/)).not.toThrow();
expect(() => screen.getByLabelText(/First Name/)).not.toThrow();
expect(() => screen.getByLabelText(/Last Name/)).not.toThrow();
});
test("redirects to root / and the user's home page if the user is logged in", () => {
localStorage.setItem(
"token",
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiY2FuZHltYXRfZWRpdG9yIiwicGVyc29uX3Jvd19pZCI6MSwiZXhwIjoxNjEyMjEyODQyLCJpYXQiOjE2MTIwNDAwNDIsImF1ZCI6InBvc3RncmFwaGlsZSIsImlzcyI6InBvc3RncmFwaGlsZSJ9.8Z8iCKq-WHOCyKz4rdrjwcVy7sR5_9dHQZR-lJDLEg4"
);
renderAppAtUrl("/signup");
expect(() => screen.getByLabelText(/current user/)).not.toThrow();
});
});