Feature: Easy Mocking For Tests?
JackHowa opened this issue · 2 comments
JackHowa commented
feature request
- Easy mocking for snapshot and behavioral testing
What is the current behavior?
- I'm having trouble setting this up for testing using middleware. stack post.
What is the expected behavior?
- That the library would work similarly as in development in test. Not sure if I'm missing something.
What's your environment?
"react-redux-promise-listener": "^1.0.0",
"redux-promise-listener": "^1.1.1",
Sandbox
- Will work on this (edit)
Other information
- See stackoverflow post.
JackHowa commented
So figured out one issue testing:
// in the tested component
// import { promiseListener } from '~/store/createStore';
import { promiseListener } from '~/__mocks__/TestReduxProvider';
When I import the promise listener from the non-test store, the mocked store and the imported promise listener are incongruous.
// test store
import React from 'react';
import { createStore, applyMiddleware, compose as reduxCompose } from 'redux';
import { Provider as ReduxProvider } from 'react-redux';
import { compose, setDisplayName, defaultProps } from 'recompose';
import rootReducer from '../store/rootReducer';
import createReduxPromiseListener from 'redux-promise-listener';
const displayName = 'TestReduxProvider';
const reduxPromiseListener = createReduxPromiseListener();
const enhance = compose(
setDisplayName(displayName),
defaultProps({ state: {} }),
);
function TestReduxProvider(props) {
const { state, children } = props;
const store = createStore(
rootReducer,
state,
applyMiddleware(reduxPromiseListener.middleware),
);
return <ReduxProvider store={store}>{children}</ReduxProvider>;
}
export const promiseListener = reduxPromiseListener;
export default enhance(TestReduxProvider);
Let me know if you have any features or any other experience mocking this? I haven't been able to find any features or examples of doing this.
JackHowa commented
See solution: https://stackoverflow.com/a/57714553/7491536
Sorted this out. I was using the wrong promiseListener in the test environment. If you need to dynamically import the promiseListener, I'd recommend using context!
Thanks @erikras !