import { RenderResult } from '@testing-library/react';
import { LOGIN_PAGE_TITLE } from '../../components/login';
import { history } from './react-router-dom';
import { HOME_PAGE_TITLE } from '../../components/home';

export function expectPageTitle(title: string) {
  expect(document.title.endsWith(title)).toBeTruthy();
}

export async function expectPageTestIds(renderResult: RenderResult) {
  expect(await renderResult.findByTestId('header')).toBeInTheDocument();
  expect(await renderResult.findByTestId('header-nav')).toBeInTheDocument();
  expect(await renderResult.findByTestId('main')).toBeInTheDocument();
  expect(await renderResult.findByTestId('footer')).toBeInTheDocument();
}

export async function expectRedirectedToLoginPage(renderResult: RenderResult) {
  expect(await renderResult.findByTestId('login')).toBeInTheDocument();
  await expectPageTitle(LOGIN_PAGE_TITLE);
  await expect(history.location.pathname).toBe('/login');
}

export async function expectRedirectedToHomePage(renderResult: RenderResult) {
  expect(await renderResult.findByTestId('home')).toBeInTheDocument();
  await expectPageTitle(HOME_PAGE_TITLE);
  await expect(history.location.pathname).toBe('/home');
}
