import * as React from 'react';
import userEvent from '@testing-library/user-event';
import { MemoryRouter, Router } from 'react-router-dom';
import { render } from '@testing-library/react';
import { createMemoryHistory } from 'history';

export function renderWithRouter(ui: React.ReactElement, { route = '/' } = {}) {
  window.history.pushState({}, 'Test page', route);

  return {
    user: userEvent,
    ...render(ui, { wrapper: MemoryRouter })
  };
}

export const history = createMemoryHistory();

export function createRouterWrapper(route = '/') {
  history.push(route);

  // eslint-disable-next-line react/display-name
  return ({ children }: { children: React.ReactNode }) => (
    <Router history={history}> {children}</Router>
  );
}
