我有相当简单的反应组件(链接包装器,如果路由处于活动状态,则添加'活动'类):
import React, { PropTypes } from 'react';
import { Link } from 'react-router';
const NavLink = (props, context) => {
const isActive = context.router.isActive(props.to, true);
const activeClass = isActive ? 'active' : '';
return (
<li className={activeClass}>
<Link {...props}>{props.children}</Link>
</li>
);
}
NavLink.contextTypes = {
router: PropTypes.object,
};
NavLink.propTypes = {
children: PropTypes.node,
to: PropTypes.string,
};
export default NavLink;
我该怎么测试呢?我唯一的尝试是:
import NavLink from '../index';
import expect from 'expect';
import { mount } from 'enzyme';
import React from 'react';
describe('<NavLink />', () => {
it('should add active class', () => {
const renderedComponent = mount(<NavLink to="/home" />, { router: { pathname: '/home' } });
expect(renderedComponent.hasClass('active')).toEqual(true);
});
});
它不起作用并返回TypeError: Cannot read property 'isActive' of undefined
。它肯定需要一些路由器模拟,但我不知道如何编写它。
答案 0 :(得分:19)
感谢@Elon Szopos的答案,但我设法写了一些更简单的内容(https://github.com/airbnb/enzyme/pull/62之后):
import NavLink from '../index';
import expect from 'expect';
import { shallow } from 'enzyme';
import React from 'react';
describe('<NavLink />', () => {
it('should add active class', () => {
const context = { router: { isActive: (a, b) => true } };
const renderedComponent = shallow(<NavLink to="/home" />, { context });
expect(renderedComponent.hasClass('active')).toEqual(true);
});
});
我必须将mount
更改为shallow
,以便不评估Link
,这会给我一个与react-router TypeError: router.createHref is not a function
相关的错误。
我宁愿拥有&#34;真实&#34; react-router不仅仅是一个对象,但我不知道如何创建它。
答案 1 :(得分:13)
对于react router v4,您可以使用<MemoryRouter>
。 AVA和酶的例子:
import React from 'react';
import PropTypes from 'prop-types';
import test from 'ava';
import { mount } from 'enzyme';
import sinon from 'sinon';
import { MemoryRouter as Router } from 'react-router-dom';
const mountWithRouter = node => mount(<Router>{node}</Router>);
test('submits form directly', t => {
const onSubmit = sinon.spy();
const wrapper = mountWithRouter(<LogInForm onSubmit={onSubmit} />);
const form = wrapper.find('form');
form.simulate('submit');
t.true(onSubmit.calledOnce);
});
答案 2 :(得分:5)
测试依赖于上下文的组件可能有点棘手。我所做的是写一个我在测试中使用的包装器。
您可以在下面找到包装器:
import React, { PropTypes } from 'react'
export default class WithContext extends React.Component {
static propTypes = {
children: PropTypes.any,
context: PropTypes.object
}
validateChildren () {
if (this.props.children === undefined) {
throw new Error('No child components were passed into WithContext')
}
if (this.props.children.length > 1) {
throw new Error('You can only pass one child component into WithContext')
}
}
render () {
class WithContext extends React.Component {
getChildContext () {
return this.props.context
}
render () {
return this.props.children
}
}
const context = this.props.context
WithContext.childContextTypes = {}
for (let propertyName in context) {
WithContext.childContextTypes[propertyName] = PropTypes.any
}
this.validateChildren()
return (
<WithContext context={this.props.context}>
{this.props.children}
</WithContext>
)
}
}
您可以在此处查看示例用法:
<WithContext context={{ location: {pathname: '/Michael/Jackson/lives' }}}>
<MoonwalkComponent />
</WithContext>
<WithContext context={{ router: { isActive: true }}}>
<YourTestComponent />
</WithContext>
它应该像你期望的那样工作。
答案 3 :(得分:3)
您可以将https://github.com/pshrmn/react-router-test-context用于此目的
“创建一个复制React Router的context.router结构的伪上下文对象。这对于使用Enzyme的浅单元测试非常有用。”
安装后,您将能够执行类似
的操作describe('my test', () => {
it('renders', () => {
const context = createRouterContext()
const wrapper = shallow(<MyComponent />, { context })
})
})
答案 4 :(得分:-1)
您需要了解mount
&amp; shallow
。
official documentation解释了 Shallow Render :
在为React编写单元测试时,浅渲染可能会有所帮助。 浅渲染使您可以渲染一个组件&#34;一个深度&#34;和 断言其渲染方法返回的事实,而不用担心 关于未实例化的子组件的行为 渲染。这不需要DOM。
然后,没有复杂性,也没有上下文:
const renderedComponent = shallow(<NavLink to="/home" />);
而不是
const renderedComponent = mount(<NavLink to="/home" />, { router: { pathname: '/home' } });
它会起作用!