kandimat/redaktions-app/src/integration-tests/app-routing.integration.test.tsx
Christoph Lienhard 7370a7c493
#11 Implement AddQuestion
Also introduce notistack/snackbar to handle async error/success messages
2020-12-29 13:02:50 +01:00

74 lines
2.3 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", "asdfasdfasdf")
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", "asdfasdfasdf")
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", "asdfasdfasdf")
renderAppAtUrl("/signup");
expect(() => screen.getByLabelText(/current user/)).not.toThrow()
});
});