I am attempting to test that I am calling one of my mapDispatchToProps
functions with args, however I can't seem to get it to work...
I attempted to follow this previous question, but it didn't seem to work for me.
Component.jsx
const mapDispatchToProps = dispatch => ({
myFunction: (data) => dispatch(myAction(data))
});
const Component = ({buttonText, myFunction}) => (
<button data-testid="test" onClick={() => myFunction(123)}>{buttonText}</button>
)
export default connect(null, mapDispatchToProps)(Component);
Actions.js
export const myAction = agentData => ({
type: `MY_ACTION`,
agentData
});
Test.js
import createMockStore from "redux-mock-store";
it('Should pass', () => {
const mockStore = createMockStore();
const store = mockStore({});
const mockUpdate = jest.fn(data => console.log('HIT FUNCTION with ' + data));
const props = {buttonText: 'Click me', myFunction: mockUpdate};
render(
<Provider store={store}>
<Component {...props}/>
</Provider>
);
userEvent.click(screen.queryByTestId('test'));
expect(mockUpdate).toHaveBeenCalled();
expect(mockUpdate).toHaveBeenCalledWith(123);
});
I have also tried moving the myFunction: mockUpdate
from the props
object into the mockStore({})
initial object, but still no luck...
I found a solution by combining something similar to @Lin Du and also some other resources I found online (that I can't seem to find in my search history to link to at the moment).
This solution doesn't require any additional args such as testProps to be added to the
mapDispatchToProps
function.