用Jest测试React Component函数
原版的
首先,我遵循Flux架构。
我有一个显示秒数的指示器,例如:30秒。每显示一秒,它就会少显示一秒钟,因此29、28、27直到0。当到达0时,我清除间隔,使其不再重复。而且,我触发一个动作。派遣此操作后,我的商店会通知我。因此,发生这种情况时,我将间隔重置为30秒,依此类推。组件看起来像:
var Indicator = React.createClass({ mixins: [SetIntervalMixin],
getInitialState: function(){
return{
elapsed: this.props.rate
};
},
getDefaultProps: function() {
return {
rate: 30
};
},
propTypes: {
rate: React.PropTypes.number.isRequired
},
componentDidMount: function() {
MyStore.addChangeListener(this._onChange);
},
componentWillUnmount: function() {
MyStore.removeChangeListener(this._onChange);
},
refresh: function(){
this.setState({elapsed: this.state.elapsed-1})
if(this.state.elapsed == 0){
this.clearInterval();
TriggerAnAction();
}
},
render: function() {
return (
<p>{this.state.elapsed}s</p>
);
},
/**
* Event handler for 'change' events coming from MyStore
*/
_onChange: function() {
this.setState({elapsed: this.props.rate}
this.setInterval(this.refresh, 1000);
}
});
module.exports = Indicator;
组件工作正常。现在,我想用Jest测试它。我知道我可以使用renderIntoDocument,然后可以将setTimeout设置为30s并检查我的component.state.elapsed是否等于0(例如)。
但是,我要在这里测试的是不同的东西。我想测试 。此外,我想测试一下,当我的经过状态为0时,
。好的,我尝试做的第一件事是:
jest.dontMock('../Indicator');describe('Indicator', function() {
it('waits 1 second foreach tick', function() {
var React = require('react/addons');
var Indicator = require('../Indicator.js');
var TestUtils = React.addons.TestUtils;
var Indicator = TestUtils.renderIntoDocument(
<Indicator />
);
expect(Indicator.refresh).toBeCalled();
});
});
但是在编写npm test时收到以下错误:
Throws: Error: toBeCalled() should be used on a mock function
我从ReactTestUtils看到了一个模拟组件函数,但是给出了解释,我不确定这是否是我需要的。
好吧,在这一点上,我被困住了。谁能给我一些有关如何测试上面提到的两件事的信息?
更新1,基于Ian的答案
那是我要运行的测试(请参见某些行中的注释):
jest.dontMock('../Indicator');describe('Indicator', function() {
it('waits 1 second foreach tick', function() {
var React = require('react/addons');
var Indicator = require('../Indicator.js');
var TestUtils = React.addons.TestUtils;
var refresh = jest.genMockFunction();
Indicator.refresh = refresh;
var onChange = jest.genMockFunction();
Indicator._onChange = onChange;
onChange(); //Is that the way to call it?
expect(refresh).toBeCalled(); //Fails
expect(setInterval.mock.calls.length).toBe(1); //Fails
// I am trying to execute the 1 second timer till finishes (would be 60 seconds)
jest.runAllTimers();
expect(Indicator.state.elapsed).toBe(0); //Fails (I know is wrong but this is the idea)
expect(clearInterval.mock.calls.length).toBe(1); //Fails (should call this function when time elapsed is 0)
});
});
我还是误会了…
回答:
看来您步入正轨。为了确保每个人都在同一页面上找到此答案,让我们弄清楚一些术语。
:具有由单元测试控制的行为的功能。通常,您可以使用模拟函数交换某些对象上的实函数,以确保正确调用模拟函数。Jest会自动为模块上的每个函数提供模拟,除非您调用jest.dontMock
该模块的名称。
:这是React.createClass
。您可以使用它来创建组件实例(这比这要复杂得多,但这足以满足我们的目的)。
:组件类的实际渲染实例。这是您在调用TestUtils.renderIntoDocument
其他TestUtils
功能或许多其他功能之后所得到的。
在问题的更新示例中,您正在生成模拟并将其附加到组件 类 而不是组件的 实例
。此外,您只想模拟要监视或以其他方式更改的功能。例如,您嘲笑了_onChange
,但您实际上并不想这么做,因为您希望它正常运行-
只是refresh
您想嘲笑而已。
这是我为此组件编写的一组建议的测试;评论是内联的,因此,如果有任何疑问,请发表评论。有关此示例和测试套件的完整且有效的源代码位于https://github.com/BinaryMuse/so-
jest-react-mock-example/tree/master;您应该能够克隆它并毫无问题地运行它。请注意,由于并非所有引用的模块都在您的原始问题中,因此我不得不对组件进行一些小的猜测和更改。
/** @jsx React.DOM */jest.dontMock('../indicator');
// any other modules `../indicator` uses that shouldn't
// be mocked should also be passed to `jest.dontMock`
var React, IndicatorComponent, Indicator, TestUtils;
describe('Indicator', function() {
beforeEach(function() {
React = require('react/addons');
TestUtils = React.addons.TestUtils;
// Notice this is the Indicator *class*...
IndicatorComponent = require('../indicator.js');
// ...and this is an Indicator *instance* (rendered into the DOM).
Indicator = TestUtils.renderIntoDocument(<IndicatorComponent />);
// Jest will mock the functions on this module automatically for us.
TriggerAnAction = require('../action');
});
it('waits 1 second foreach tick', function() {
// Replace the `refresh` method on our component instance
// with a mock that we can use to make sure it was called.
// The mock function will not actually do anything by default.
Indicator.refresh = jest.genMockFunction();
// Manually call the real `_onChange`, which is supposed to set some
// state and start the interval for `refresh` on a 1000ms interval.
Indicator._onChange();
expect(Indicator.state.elapsed).toBe(30);
expect(setInterval.mock.calls.length).toBe(1);
expect(setInterval.mock.calls[0][1]).toBe(1000);
// Now we make sure `refresh` hasn't been called yet.
expect(Indicator.refresh).not.toBeCalled();
// However, we do expect it to be called on the next interval tick.
jest.runOnlyPendingTimers();
expect(Indicator.refresh).toBeCalled();
});
it('decrements elapsed by one each time refresh is called', function() {
// We've already determined that `refresh` gets called correctly; now
// let's make sure it does the right thing.
Indicator._onChange();
expect(Indicator.state.elapsed).toBe(30);
Indicator.refresh();
expect(Indicator.state.elapsed).toBe(29);
Indicator.refresh();
expect(Indicator.state.elapsed).toBe(28);
});
it('calls TriggerAnAction when elapsed reaches zero', function() {
Indicator.setState({elapsed: 1});
Indicator.refresh();
// We can use `toBeCalled` here because Jest automatically mocks any
// modules you don't call `dontMock` on.
expect(TriggerAnAction).toBeCalled();
});
});
以上是 用Jest测试React Component函数 的全部内容, 来源链接: utcz.com/qa/430562.html