redux-rtl.tsx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import { AnyAction, configureStore } from '@reduxjs/toolkit';
  2. import { ThunkMiddlewareFor } from '@reduxjs/toolkit/dist/getDefaultMiddleware';
  3. import { render as rtlRender } from '@testing-library/react';
  4. import React from 'react';
  5. import { Provider } from 'react-redux';
  6. import { createRootReducer } from 'app/core/reducers/root';
  7. import { StoreState } from 'app/types';
  8. import { mockNavModel } from './mocks/navModel';
  9. function render(
  10. ui: React.ReactElement,
  11. {
  12. preloadedState = { navIndex: mockNavModel },
  13. store = configureStore<
  14. StoreState,
  15. AnyAction,
  16. ReadonlyArray<ThunkMiddlewareFor<StoreState, { thunk: true; serializableCheck: false; immutableCheck: false }>>
  17. >({
  18. reducer: createRootReducer(),
  19. preloadedState,
  20. middleware: (getDefaultMiddleware) =>
  21. getDefaultMiddleware({ thunk: true, serializableCheck: false, immutableCheck: false }),
  22. }),
  23. ...renderOptions
  24. }: { preloadedState?: Partial<StoreState>; store?: ReturnType<typeof configureStore> } = {}
  25. ) {
  26. function Wrapper({ children }: { children: React.ReactNode }) {
  27. return <Provider store={store}>{children}</Provider>;
  28. }
  29. return rtlRender(ui, { wrapper: Wrapper, ...renderOptions });
  30. }
  31. export { render };