Skip to content

Instantly share code, notes, and snippets.

@martinhj
Last active September 19, 2023 15:33
Show Gist options
  • Save martinhj/3b64cdb1670232b71add0b89cad5070d to your computer and use it in GitHub Desktop.
Save martinhj/3b64cdb1670232b71add0b89cad5070d to your computer and use it in GitHub Desktop.
Mock implementation of useDispatch with possibility of dynamic response
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');
})
})
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