describe('testing inner component', function() {
beforeEach(function() {
this.sandbox = sinon.createSandbox();
this.getEstimatedCost = this.sandbox.stub().returns(Promise.resolve({ estimatedCost: 10 }));
this.renderComponent = (props = {}) => {
const store = createMainReduxStore({
// some store.
});
return (
<Router>
<Provider store={store}>
<Layout>
<FooPage getEstimatedCost={this.getEstimatedCost} />
</Layout>
</Provider>
</Router>
);
};
});
afterEach(function() {
this.sandbox.restore();
});
describe('when rendered', function() {
beforeEach(function() {
this.wrapper = enzyme.mount(this.renderComponent());
return Promise.resolve().then(() => this.wrapper.update());
});
it('calls getEstimatedCost', function() {
expect(this.getEstimatedCost).to.have.been.calledOnce;
expect(
this.wrapper
.find('Router')
.find('Layout')
.find('FooPage')
.state('estimatedCost')
).to.equal(10);
});
});
});
我想测试“ getEstimatedCost”方法是否设置了FooPage的“ estimatedCost”状态。
在这里我得到一个错误:
错误:方法“ instance”应在1个节点上运行。找到0个。 在ReactWrapper.single(node_modules / enzyme / build / ReactWrapper.js:1718:17) 在ReactWrapper.instance(node_modules / enzyme / build / ReactWrapper.js:321:21) 在ReactWrapper.state(node_modules / enzyme / build / ReactWrapper.js:1035:18) 在Context.state(test / organization / components / Settings / FooPage.spec.js:94:12) 在process._tickCallback(internal / process / next_tick.js:68:7)