import { renderWithClient } from './utils/test/react-query';
import App from './App';
import { createRouterWrapper } from './utils/test/react-router-dom';
import {
  expectPageTestIds,
  expectPageTitle,
  expectRedirectedToHomePage,
  expectRedirectedToLoginPage
} from './utils/test/expects';
import { ROOT_PAGE_TITLE } from './components/root';
import { REGISTER_PAGE_TITLE } from './components/register';
import { TERMS_AND_CONDITIONS_PAGE_TITLE } from './components/termsAndConditions';
import { LOGIN_PAGE_TITLE } from './components/login';
import { mockFailureResponse } from './mocks/server';
import { HOME_PAGE_TITLE } from './components/home';
import { FINANCIAL_PAGE_TITLE } from './components/financial';

describe('App component', () => {
  const routes = [
    { name: 'root', route: '/', title: ROOT_PAGE_TITLE },
    { name: 'home', route: '/home', title: HOME_PAGE_TITLE },
    { name: 'financial', route: '/financial', title: FINANCIAL_PAGE_TITLE },
    { name: 'login', route: '/login', title: LOGIN_PAGE_TITLE },
    { name: 'register', route: '/register', title: REGISTER_PAGE_TITLE },
    {
      name: 'terms-and-conditions',
      route: '/terms-and-conditions',
      title: TERMS_AND_CONDITIONS_PAGE_TITLE
    }
  ];

  for (let i = 0; i < routes.length; i++) {
    const { name, route, title } = routes[i];

    test(`successful render ${name} page`, async () => {
      const result = renderWithClient(<App />, createRouterWrapper(route));
      await expectPageTitle(title);
      await expectPageTestIds(result);
    });
  }

  test('successful redirect loginRequired page to login page when user is not authenticated', async () => {
    mockFailureResponse();
    const result = renderWithClient(<App />, createRouterWrapper('/home'));
    await expectRedirectedToLoginPage(result);
  });

  test('successful redirect register page to root page when user is authenticated', async () => {
    const result = renderWithClient(<App />, createRouterWrapper('/register'));
    await expectRedirectedToHomePage(result);
  });
});
