Last active
September 19, 2023 15:33
-
-
Save martinhj/3b64cdb1670232b71add0b89cad5070d to your computer and use it in GitHub Desktop.
Mock implementation of useDispatch with possibility of dynamic response
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as ReactRedux from 'react-redux'; | |
describe('nothing', () => { | |
let loginFn = jest.fn(() => null); | |
jest.spyOn(ReactRedux, 'useDispatch').mockImplementation(() => { | |
return loginFn; | |
}); | |
}) | |
it('does something', () => { | |
const actual = render(<LoginComponent />); // component doing let dispatch = useDispatch(loginAction) | |
fireEvent.press(actual.getByText(/Log in$/)); | |
expect(loginFn).toHaveBeenCalledTimes(1); | |
expect(loginFn).toHaveBeenCalledWith('something'); | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as ReactRedux from 'react-redux'; | |
describe('nothing', () => { | |
jest.spyOn(ReactRedux, 'useSelector').mockImplementation(state => { | |
return {loginStatus: UNPERFORMED}; | |
}); | |
}) | |
it('does something', () => { | |
const actual = render(<LoginComponent />); // component having let loginStatus = useSelector(useSelector(state => state.user.loginStatus)) | |
fireEvent.press(actual.getByText(/Log in$/)); | |
expect(loginFn).toHaveBeenCalledTimes(1); | |
expect(loginFn).toHaveBeenCalledWith('something'); | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment