// test-utils.js
// source : https://redux.js.org/recipes/writing-tests
import React from 'react';
import { render as rtlRender } from '@testing-library/react';
import { Provider } from 'react-redux';
import { ApolloProvider } from '@apollo/client';
import { ConnectedRouter } from 'connected-react-router';
import { DndProvider } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';

import { IconContext } from 'react-icons';
import configBrowserStore from 'store/setup/client';
import client from 'store/rootApollo';

const render = (ui, { initialState, ...renderOptions } = {}) => {
  const storeSetup = configBrowserStore({
    initialState,
  });

  const Wrapper = ({ children }) => {
    return (
      <ApolloProvider client={client.getInstance()}>
        <Provider store={storeSetup.store}>
          <DndProvider backend={HTML5Backend}>
            <ConnectedRouter history={storeSetup.history}>
              <IconContext.Provider value={{ style: { verticalAlign: 'middle' } }}>{children}</IconContext.Provider>
            </ConnectedRouter>
          </DndProvider>
        </Provider>
      </ApolloProvider>
    );
  };
  return rtlRender(ui, { wrapper: Wrapper, ...renderOptions });
};

// re-export everything
export * from '@testing-library/react';
// override render method
export { render };
