最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to mock a function in jest or prevent default? - Stack Overflow

programmeradmin0浏览0评论

I am trying to use react-testing-lib to do some integration testing

I want to mock a function like inside my react class called handleSubmit

handleSubmit(){
 // does some stuff
 // calls an action creator

}

I basically want to stub this method, so that it either returns null/undefined or anything. But I don't want it to actually call the action creator

The reason being I wanted to assert some UI is present and calling the action creator is giving me the error:

Actions must be plain objects. Use custom middleware for async actions.

I have tried to jest.mock(thismethod) and jest.spyOn()` too but neither seem to work. I just want it to do something like

myFunc() {

}

as if it were an empty function and does nothing. How can I stub this?

I am trying to use react-testing-lib to do some integration testing

I want to mock a function like inside my react class called handleSubmit

handleSubmit(){
 // does some stuff
 // calls an action creator

}

I basically want to stub this method, so that it either returns null/undefined or anything. But I don't want it to actually call the action creator

The reason being I wanted to assert some UI is present and calling the action creator is giving me the error:

Actions must be plain objects. Use custom middleware for async actions.

I have tried to jest.mock(thismethod) and jest.spyOn()` too but neither seem to work. I just want it to do something like

myFunc() {

}

as if it were an empty function and does nothing. How can I stub this?

Share Improve this question edited May 8, 2019 at 14:55 skyboyer 23.8k7 gold badges62 silver badges71 bronze badges asked May 8, 2019 at 8:39 Red BaronRed Baron 7,70112 gold badges47 silver badges112 bronze badges 6
  • Can you post code that you have tried to mock and is not working along-with class containing function? – BeautifulWorld Commented May 8, 2019 at 8:46
  • Mock store is prepared ? – Codesingh Commented May 8, 2019 at 9:05
  • @Shruti not really as it's sensitive code. is there not a generic way to do this ? – Red Baron Commented May 8, 2019 at 13:16
  • @RedBaron could you please help me confirm my understanding: class has function which calls action. You want to test function(though mock) but don't want that action to be called(may be mock action), is that right? – BeautifulWorld Commented May 9, 2019 at 7:57
  • @Shruti yes that is correct – Red Baron Commented May 13, 2019 at 6:28
 |  Show 1 more ment

2 Answers 2

Reset to default 4

It looks like handleSubmit is a prototype method...if it is then you can mock it like this:

import * as React from 'react';
import { render, fireEvent } from 'react-testing-library';

class MyComponent extends React.Component {
  handleSubmit() {  // <= prototype method
    throw new Error('should not get here');
  }
  render() {
    return (<button onClick={this.handleSubmit}>the button</button>);
  }
}

test('MyComponent', () => {
  const mock = jest.spyOn(MyComponent.prototype, 'handleSubmit');
  mock.mockImplementation(() => {});  // <= replace the implementation

  const { getByText } = render(<MyComponent/>);
  fireEvent.click(getByText('the button'));

  expect(mock).toHaveBeenCalled();  // Success!
});

Just make sure to implement the mock on the prototype before rendering the ponent.

Try below mocking of function and action:

//Mock function
 var mockPromise = new Promise((resolve, reject) => {
    resolve(<mock response similar to actual promise response>);
});
functionName = jest.fn().mockReturnValueOnce(mockPromise)

//Mock action
const actions = [];
const dispatchAction = jest.fn((dispatchCall) => {
      actions.push(dispatchCall);
});

functionName()(dispatchAction);
expect(dispatchAction).toBeCalledTimes(1)
发布评论

评论列表(0)

  1. 暂无评论