Jest Mock Features
Layout functions are also known as βspies,β because they allow you to monitor the behavior of a function that is indirectly called some other code, rather than just testing the output. You can create the mock function with jest.fn()
.
jest.fn()
Returns a new unused mock function. Optionally performs a mock implementation.
const mockFn = jest.fn(); mockFn(); expect(mockFn).toHaveBeenCalled();
With mock implementation:
const returnsTrue = jest.fn(() => true); console.log(returnsTrue()) // true;
So you can mock getList
with jest.fn()
as follows:
jest.dontMock('./Countries.jsx'); const React = require('react/addons'); const TestUtils = React.addons.TestUtils; const Countries = require('./Countries.jsx'); describe('Component', function() { it('must call getList on button click', function() { var renderedNode = TestUtils.renderIntoDocument(<Countries />); renderedNode.prototype.getList = jest.fn() var button = TestUtils.findRenderedDOMComponentWithTag(renderedNode, 'button'); TestUtils.Simulate.click(button); expect(renderedNode.prototype.getList).toBeCalled(); }); });
source share