Skip to content

Instantly share code, notes, and snippets.

@kisildev
Created November 18, 2019 09:40
Show Gist options
  • Save kisildev/2cd5b8613bb460e0087c7a1b91a23bc1 to your computer and use it in GitHub Desktop.
Save kisildev/2cd5b8613bb460e0087c7a1b91a23bc1 to your computer and use it in GitHub Desktop.
Jest test mock class method
it('should redirect to another page', () => {
const user = {
id: 1,
token: 'J9gj9fj90.Tgf84h8Ufds.jfiosdjfi',
};
// static method
const spy = jest.spyOn(Login, 'redirectUser');
mount(<Login user={user} />, global.contextData);
expect(spy).toBeCalled();
// Method by Spy on
const mockRedirectUser = jest.spyOn(wrapper.instance(), 'componentDidMount');
expect(mockRedirectUser).toHaveBeenCalled();
// Method by jest.fn()
wrapper.instance().redirectUser = jest.fn();
expect(wrapper.instance().redirectUser).toBeCalled();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment