这个问题偶尔出现在我的单元测试(jest)中。以下输出是this.props.location
的样子:
this.props.location: Object {
pathname: '/logs',
search: '?hello=kitty',
hash: '',
state: null,
action: 'POP',
key: 'zn54se',
query: undefined } <<< NOTE that i's undefined <<<<<
我不确定是什么造成的。以下是组件的设置方式:
// Logs.jsx:
...
renderToolbar() {
var generateClassName = (destObj) =>
{
console.log("this.props.location:", this.props.location);
}
...
return <Button to="/logs" className={generateClassName({kind: 'all'})}>All</Button>;
}
render() {
return <Grid toolbar={this.renderToolbar} />;
}
// Grid.jsx:
...
renderToolbar() {
return this.props.toolbar ? this.props.toolbar() : '';
},
render() {
return <div> { this.renderToolbar(); } </div>;
}
这是路线设置(在我的测试中):
describe('Logs', function() {
var ts = (new Date()).valueOf();
var node;
beforeEach(function(){
jest.setMock('myapp/lib/api/request', {});
node = document.createElement('div');
path = "/logs?hello=kitty";
history = createHistory(path);
routes = <Router history={history}><Route path="/logs" component={Logs} /></Router>;
});
it('should render one log entry', function() {
LogStore.getLogs = jest.genMockFunction().mockReturnValue(
[ {key: 0, kind: 'transfer', timestamp: ts, conn1name: 'Connector', conn2name: 'Connector2'} ]
);
var handler = ReactDOM.render(routes, node);
expect(TestUtils.scryRenderedDOMComponentsWithClass(handler, 'log-entry').length).toBe(1);
});
});
答案 0 :(得分:0)
事实证明,jest
模仿qs
模块,它是react-router
用于生成查询对象的依赖项。这就是它最终未定义的原因。
要解决此问题,我已将/qs
添加到jest
中的package.json
设置中:
"jest": {
"unmockedModulePathPatterns": [
"/qs",
...
]
}
答案 1 :(得分:0)
以下在单元测试中为我解决了
<Router basename="">
<Switch>
<Route history={history} path="/">
<MyApp/>
</Route>
</Switch>
</Router>
历史定义如下
import createBrowserHistory from 'history/createBrowserHistory';
const history = createBrowserHistory({
basename: '/'
});