Skip to content

Instantly share code, notes, and snippets.

@pablospaniard
Last active November 20, 2023 09:36
Show Gist options
  • Select an option

  • Save pablospaniard/95ad493186b6f201a661e6c030929c24 to your computer and use it in GitHub Desktop.

Select an option

Save pablospaniard/95ad493186b6f201a661e6c030929c24 to your computer and use it in GitHub Desktop.
rtl render method extended with Store and Context
import React from 'react';
import { createStore } from 'redux';
import { Provider as RRProvider } from 'react-redux';
import { render as rtlRender } from '@testing-library/react-native';
import { rootReducer } from 'store';
// you can provide initialState or the entire store that the ui is rendered with
export const renderWithStore = (
ui,
{
initialState,
store = createStore(rootReducer, initialState),
Provider,
providerProps = {},
...renderOptions
} = {}
) => {
const Wrapper = ({ children }) => (
<RRProvider store={store}>
{Provider ? <Provider {...providerProps}>{children}</Provider> : children}
</RRProvider>
);
return {
...rtlRender(ui, {
wrapper: Wrapper,
...renderOptions
}),
// adding `store` to the returned utilities to allow us
// to reference it in our tests (just try to avoid using
// this to test implementation details).
store
};
};
import { render } from '@testing-library/react'
export const renderWithContext = (
ui,
Provider,
{ providerProps = {}, ...renderOptions } = {}
) => render(<Provider {...providerProps}>{ui}</Provider>, renderOptions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment