jma*_*eli 28 reactjs react-router enzyme
我有相当简单的反应组件(Link包装器,如果路由处于活动状态,它会添加'active'类):
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;
Run Code Online (Sandbox Code Playgroud)
我该怎么测试呢?我唯一的尝试是:
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);
});
});
Run Code Online (Sandbox Code Playgroud)
它不起作用并返回TypeError: Cannot read property 'isActive' of undefined
.它肯定需要一些路由器模拟,但我不知道如何编写它.
jma*_*eli 22
感谢@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);
});
});
Run Code Online (Sandbox Code Playgroud)
我不得不改变mount
到shallow
为了不评价Link
这给了我与反应路由器连接的错误TypeError: router.createHref is not a function
.
我宁愿拥有"真正的"反应路由器,而不仅仅是一个对象,但我不知道如何创建它.
asd*_*sdf 19
对于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);
});
Run Code Online (Sandbox Code Playgroud)
测试依赖于上下文的组件可能有点棘手.我做的是写一个我在测试中使用的包装器.
你可以在下面找到包装器:
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>
)
}
}
Run Code Online (Sandbox Code Playgroud)
在这里您可以看到示例用法:
<WithContext context={{ location: {pathname: '/Michael/Jackson/lives' }}}>
<MoonwalkComponent />
</WithContext>
<WithContext context={{ router: { isActive: true }}}>
<YourTestComponent />
</WithContext>
Run Code Online (Sandbox Code Playgroud)
它应该像你期望的那样工作.
归档时间: |
|
查看次数: |
18376 次 |
最近记录: |