Your mock myFunction
didn't invoke when you click the button. Because the returned value of mapDispatchToProps
will become the props of the component.
You can get the mock myFunction
via the second parameter of mapDispatchToProps
function named ownProps
. You may want to call the mock myFunction
when dispatching the action. If so, expect(mockUpdate).toHaveBeenCalled();
assertion will work.
component.tsx
:
import React from 'react';
import { connect } from 'react-redux';
import { myAction } from './actions';
const mapDispatchToProps = (dispatch, ownProps) => {
console.log('Your mock myFunction is here:', ownProps.myFunction);
// But below myFunction is not a mock function and is passed into your component finally.
return {
myFunction: (data) => dispatch(myAction(data)),
};
};
const Component = ({ buttonText, myFunction }) => (
<button data-testid="test" onClick={() => myFunction(123)}>
{buttonText}
</button>
);
export default connect(null, mapDispatchToProps)(Component);
import React from 'react';
import { render, screen } from '@testing-library/react';
import { Provider } from 'react-redux';
import createMockStore from 'redux-mock-store';
import userEvent from '@testing-library/user-event';
import Component from './component';
it('Should pass', async () => {
const mockStore = createMockStore();
const store = mockStore();
render(
<Provider store={store}>
<Component buttonText="Click me" />
</Provider>
);
await userEvent.click(screen.getByTestId('test'));
expect(store.getActions()).toEqual([{ type: 'MY_ACTION', agentData: 123 }]);
});
Test result:
PASS stackoverflow/75173986/component.test.tsx (8.625 s)
✓ Should pass (92 ms)
console.log
Your mock myFunction is here: [Function: mockConstructor] {
_isMockFunction: true,
getMockImplementation: [Function (anonymous)],
mock: [Getter/Setter],
mockClear: [Function (anonymous)],
mockReset: [Function (anonymous)],
mockRestore: [Function (anonymous)],
mockReturnValueOnce: [Function (anonymous)],
mockResolvedValueOnce: [Function (anonymous)],
mockRejectedValueOnce: [Function (anonymous)],
mockReturnValue: [Function (anonymous)],
mockResolvedValue: [Function (anonymous)],
mockRejectedValue: [Function (anonymous)],
mockImplementationOnce: [Function (anonymous)],
mockImplementation: [Function (anonymous)],
mockReturnThis: [Function (anonymous)],
mockName: [Function (anonymous)],
getMockName: [Function (anonymous)]
}
at Function.mapDispatchToProps [as mapToProps] (stackoverflow/75173986/component.tsx:6:11)
---------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
---------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
actions.ts | 100 | 100 | 100 | 100 |
component.tsx | 100 | 100 | 100 | 100 |
---------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 9.103 s, estimated 10 s
Ran all test suites related to changed files.