我有一个简单的react组件,它使用antd中的Card:
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Card } from 'antd';
export class TBD extends Component {
constructor() {
super();
}
render() {
return (
<Card title={this.props.pathname}>
TODO
</Card>
);
}
}
export let select = (state) => {
return state.route;
};
export default connect(select)(TBD);现在,我编写了一些简单的测试,并想检查一下,我的TBD组件是否使用了卡片
import React from 'react';
import {mount, shallow} from 'enzyme';
import {Provider, connect} from 'react-redux';
import {createMockStore} from 'redux-test-utils';
import {expect} from 'chai';
import chai from 'chai';
import chaiEnzyme from 'chai-enzyme';
chai.use(chaiEnzyme());
import { Card } from 'antd';
import TBDConnected, {TBD, select} from '../src/js/components/TBD';
describe('Fully Connected:', function () {
it('show selected item text', function () {
const expectedState = {route: {pathname: 'Menu1'}};
const store = createMockStore(expectedState);
const ConnectedComponent = connect(select)(TBDConnected);
const component = shallow(<ConnectedComponent store={store} />).shallow().shallow();
console.log(component.debug());
expect(component.equals(<Card/>)).is.equal(true);
});
});但失败了,因为3个浅薄的回报我
<Component title="Menu1">
TODO
</Component>但我希望
<Card title="Menu1">
TODO
</Card>再一次呈现之后,我从呈现卡中获得纯html,我不明白为什么它会将它呈现给组件,而不是Card,以及我如何才能得到我想要的结果。
更新
简化我的问题的例子。下一次测试失败:
describe('TBD', function () {
it('Renders a Card', function () {
const component = shallow(<TBD />);
console.log(component.debug());
expect(component.equals(<Card/>)).is.equal(true);
});
});控制台中的调试输出:
<Component title={[undefined]}>
TODO
</Component>但我希望:
<Card title={[undefined]}>
TODO
</Card>发布于 2016-11-03 14:12:15
Ant组件的问题。这些组件中的一部分是简单的匿名函数,没有扩展React.Component等。结果酶呈现它像<Component />,在浏览器中它看起来像<StatelessComponent />。
发布于 2016-11-01 13:28:54
您不需要测试整个连接组件。我将首先测试表示纯组件(作为单元测试),然后可以隔离地测试连接器。
也就是。
import React from 'react';
import {shallow} from 'enzyme';
import {expect} from 'chai';
import chai from 'chai';
import chaiEnzyme from 'chai-enzyme';
chai.use(chaiEnzyme());
import { Card } from 'antd';
import {TBD} from '../src/js/components/TBD';
describe('TBD', function () {
it('Renders a Card', function () {
const component = shallow(<TBD />);
expect(component.equals(<Card/>)).is.equal(true);
});
it('sets the right title', function () {
const component = shallow(<TBD pathname="example" />);
expect(component.prop('title')).is.equal("example");
});
});正如您所看到的,您的纯组件必须作为纯函数进行测试。你通过一些道具,期待一些渲染。
然后,在测试连接器时,您可以断言它正确地映射了stateToProps和dispatchToProps.
https://stackoverflow.com/questions/40303378
复制相似问题