我想知道为什么我的模拟似乎没有覆盖Component的onclick方法。实际上它确实如此,除非我模拟点击。
/** @jsx React.DOM */
//tests/app/modules/autocompletesearchinput-test.js
jest.dontMock('../../../app/modules/AutocompleteSearchInput.jsx');
var React, TestUtils, FluxxorTestUtils, FluxConstructor, realFlux, fakeFlux, MyComponent, Component;
describe('Testing Autocomplete Search Input', function() {
beforeEach(function() {
React = require('react/addons');
TestUtils = React.addons.TestUtils;
FluxxorTestUtils = require('fluxxor-test-utils');
FluxConstructor = require('../../../app/FluxConstructor.js');
realFlux = FluxConstructor();
fakeFlux = FluxxorTestUtils.fakeFlux(realFlux);
fakeFlux.genMocksForStoresAndActions();
// now all store and action methods are mocked for testing
MyComponent = require('../../../app/modules/AutocompleteSearchInput.jsx');
Component = TestUtils.renderIntoDocument(<MyComponent flux={fakeFlux} />);
});
it('when created, component should have AutocompleteSearchInput class', function() {
var div = TestUtils.findRenderedDOMComponentWithClass(
Component, 'AutocompleteSearchInput');
});
it('when mounted and clicked, should call searchBtnClick', function() {
Component.searchBtnClick = jest.genMockFunction();
TestUtils.Simulate.click(Component.refs.searchButton.getDOMNode());
Component.searchBtnClick();
expect(Component.searchBtnClick).toBeCalled();
});
});
作为参考,我一直在使用它:Test a React Component function with Jest