mercredi 7 octobre 2015

Mock Action functions in Jest test

How would one go about calling a function from an imported action module? Here is my component

class Task extends Component {
 handleClick(e) {
  e.preventDefault();
  Actions.returnTask(this.props.id);
 }
 render() {...}
}

and tests that look like this:

jest.dontMock('./Task');
jest.dontMock('./Actions');

describe('Tasks', function() {
 let renderedComponent;
 let mockReturnTask;
 let TaskActions;
 beforeEach(function() {
  renderedComponent = TestUtils.renderIntoDocument(
   <Task task={testTask} key={1} />
    );
   Actions = require('./Actions');
   mockReturnTask = jest.genMockFn();
   Actions.returnTask = mockReturnTask;
  });
  it('should call a returnTask action on Actions', function() {
   renderedComponent.handleClick(event);
   expect(mockReturnTask).toBeCalled();
  });
});

When running my test it tells me that the function was not called. If I do expect(Actions.returnTask).toBeCalled(); I get an error message that toBeCalledWith() should be used on a mock function error. How do I mock a function on the external Actions and check that it is called in my tests? I feel like my method above should be working.

Aucun commentaire:

Enregistrer un commentaire