笑话错误:“您的焦点陷阱必须始终具有至少一个包含一个tabbable节点的容器。
我正在用玩笑来编写React组件的测试。我无法在组件测试中修复错误。我试图将重点放在测试代码中:
await waitFor(() => expect(screen.getByTestId('continue')).toHaveFocus());
但是它不起作用。如何解决此错误?
这是组件代码:
function SuccessCommentAddPopup({onSuccessComment}: SuccessCommentAddPopupProps): JSX.Element {
return (
<AriaModal
titleText='add comment success'
initialFocus="#continue"
>
<div style={{position: 'relative', width: '550px', height: '410px', marginBottom: '50px'}}>
<div className={style.modal}>
<div className={style.modalWrapper}>
<div className={style.modalOverlay} data-close-modal></div>
<div className={style.modalContent}>
<svg className={style.modalIcon} width="26" height="20" aria-hidden="true">
<use xlinkHref="#icon-success"></use>
</svg>
<p className={style.modalMessage}>Thank's a lot!</p>
<div className={style.modalButtonContainer}>
<button
id='continue'
data-testid='continue'
className={style.modalButton}
onClick={()=>{
onSuccessComment(false);
}}
>
Close
</button>
</div>
<button
className={style.modalCloseBtn}
type="button"
aria-label="Close"
onClick={()=>{
onSuccessComment(false);
}}
>
<span className={style.buttonCrossIcon}>
</span><span className={style.modalCloseBtninteractiveArea}></span>
</button>
</div>
</div>
</div>
</div>
</AriaModal>
);
}
export default SuccessCommentAddPopup;
这是测试代码:
const history = createMemoryHistory();
history.push('/guitars/1');
describe('Component: SuccessCommentAddPopup', () => {
it('Компонент отрисовывается корректно', async () => {
render(
<HistoryRouter history={history}>
<SuccessCommentAddPopup
onSuccessComment={jest.fn()}
/>
</HistoryRouter>,
);
await waitFor(() => expect(screen.getByTestId('continue')).toHaveFocus());
expect(screen.getByText('Спасибо за ваш отзыв!')).toBeInTheDocument();
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
@Ararat是正确的。我通过嘲笑tabbable: https://github.com/focus-trap /tabbable#testing-in-jsdom
添加了:./< strong> mocks/tabable.js,以下内容:
我花了很多时间来弄清楚这一点,这就是为什么我分享!我在VUE 3项目中使用了@Vueus的Usefocustrap,由于缺少此缺少,我的Vue-testing-library组件测试破裂了!
@Ararat is correct. I solved this by mocking out tabbable: https://github.com/focus-trap/tabbable#testing-in-jsdom
added: ./mocks/tabable.js with the following:
I spent a lot of time figuring this out, thats why im sharing! I was using useFocusTrap from @vueUs in a vue 3 project and my vue-testing-library component tests broke because of this missing!
您需要模拟
Tabbable
模块。检查此 linkYou need to mock
tabbable
module. Check this link您需要在
__模拟__
文件夹中创建tabbanle.js文件,现在,如果您没有文件夹,则iDe inde inde in for__ test test __ test __ test __
在我的存储库中,我会在我的存储库中添加了该文件夹src/server/models/__模拟__/tabbable.js
是的,它类似于其他答案,但是在哪里丢失了文件夹信息,我花了很长时间才能弄清楚它,因此
在此处添加以下代码。
只需保存文件并再次运行测试,错误就会神奇地消失。
更新后,我还写了有关所有其他笑话错误的帖子,您可以在此处查看 https:> https:> https:> https:> https: //medium.com/@arianallami/jest-update-466f08181d06
You need to create tabbanle.js file inside your
__mocks__
folder, now if you do not have the folder in you IDE look for__test__
folder in my repository I added the folder in thesrc/server/models/__mocks__/tabbable.js
yes it is similar to the other answer but where do create the folder information was missing and it took me long time to figure it out hence adding here
And add the below code in that file.
just save the file and run your tests again and the error would have magically disappeared.
I have also written a post on all other jest errors after the update you can check here here https://medium.com/@arianallami/jest-update-466f08181d06
我们应该找到此功能的哪个分支返回true
在我的情况下
,我们需要像这样嘲笑它
We should find which branch of this function returns true https://github.com/focus-trap/tabbable/blob/master/src/index.js#L367
In my case it was
So, we need to mock it like so
对于Vite,您可以在直接安装组件时指定,按此评论:
不过,这对Vite的依赖关系无济于事。
For vite, you can specify when mounting the component directly, as per this comment:
This won't help with dependencies of dependencies in vite though.