我正在寻找一个解决方案,以便仍然可以使用来自react-router的链接,而不是在测试href属性值时.实际上,我有一些根据上下文改变路线的组件.但是,当我测试href属性值时,返回的唯一内容是null.但是,当我使用a时,它会返回预期值.
这是一个失败的测试:
import React from 'react';
import {Link} from 'react-router';
import TestUtils from 'react-addons-test-utils';
import expect from 'must';
const LINK_LOCATION = '/my_route';
class TestComponent extends React.Component {
render() {
return (
<div>
<Link className='link' to={LINK_LOCATION}/>
<a className='a' href={LINK_LOCATION}/>
</div>
);
}
}
describe('Url things', () => {
it('should return me the same href value for both link and a node', () => {
const test_component = TestUtils.renderIntoDocument(<TestComponent/>);
const link = TestUtils.findRenderedDOMComponentWithClass(test_component, 'link');
const a = TestUtils.findRenderedDOMComponentWithClass(test_component, 'a');
expect(link.getAttribute('href')).to.eql(a.getAttribute('href'));
});
}); …Run Code Online (Sandbox Code Playgroud) react-router ×1