gpt4 book ai didi

reactjs - 使用 MemoryRouter 时如何知道当前 url

转载 作者:行者123 更新时间:2023-11-28 20:10:34 25 4
gpt4 key购买 nike

我正在尝试弄清楚如何使用测试来断言当前位置路径名是什么。

作为实验and based on some docs我写了下面的测试

  test(`Can navigate MemoryRouter`, (done) => {
const logAll = (p, ...args) => {
console.log(`SOME OUTPUT`,p, ...args)
return p.location.pathname
}

const div = document.createElement('div')

const Test = () => (
<MemoryRouter>
<React.Fragment>
<Link to="/foo/bar" id="click-me" />
<Route render={logAll} />
</React.Fragment>
</MemoryRouter>
)
ReactDOM.render(React.createElement(Test), div)
console.log('click')
Simulate.click(div.querySelector('#click-me'))
})

我希望因为 logAll 路由匹配所有更改,所以它会在 / 中记录一次(它确实如此),然后在 /foo/bar 中再次记录。但后者在测试超时(大约 5 秒)之前从不登录。

我错过了什么?

最佳答案

您的测试没问题(您甚至不需要 done),但是缺少 Simulate.click 的第二个参数。如果您添加它,它将按您预期的那样工作:

Simulate.click(
div.querySelector('#click-me'),
{ button: 0 }
);

the codebase显示 Link 不会进行事件中没有 button=0 的点击。

Blame of this line , 显示 Link tracks only left button click .

关于reactjs - 使用 MemoryRouter 时如何知道当前 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52521384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com