queries returned by render are not scoped
还没有人认领这个 Issue。
评估
- 难度
- 5/5
- 预计耗时
- 一周以上
- 新手友好度
- 35/100
- Issue 类型
- 功能
- 描述清晰度
- 描述清楚
- 活跃度
- 停滞
- 技术栈
- javascript, react
- 领域
- frontend, testing-qa
调研方向
首先检查 render 如何返回其查询,以及 within(container) 如何绑定查询。然后复现所提供的示例,并验证 render 的查询仅在返回的容器中进行搜索,而 screen 仍然可以搜索更广泛的文档。breaking-change 和发布的影响需要在实现之前解决。
由索引模型根据 Issue 内容生成。
描述
@testing-library/reactversion: 14.1.2- Testing Framework and version: Jest 29.7
- DOM Environment: jsdom 20.0.3
Relevant code or config:
it('uses a consistent scope', () => {
const MyComponent = () => {
useEffect(() => {
const separateElement = document.createElement('div');
separateElement.textContent = 'hello';
document.body.append(separateElement);
return () => separateElement.remove();
}, []);
return (
<div>
<div>hello</div>
</div>
);
};
const { container, getByText } = render(<MyComponent />);
within(container).getByText('hello'); // passes
getByText('hello'); // fails (finds 2 elements)
});
What happened:
The first test (within(container).getByText('hello')) passes, and the second (getByText('hello')) fails:
Found multiple elements with the text: hello
Here are the matching elements:
Ignored nodes: comments, script, style
<div>
hello
</div>
Ignored nodes: comments, script, style
<div>
hello
</div>
Problem description:
The container and queries returned by render should be consistent with each other: the queries should search within the returned container by default, to avoid pollution from other tests and libraries which attach elements to other parts of the DOM. For users who need to check the wider document, they can continue to use screen.
It is also worth noting that the documentation claims that the queries are "bound", which does not match the current behaviour (since they apply globally across the document).
Suggested solution:
It is possible to make a wrapper function in user-space which works around this, which should be easy to integrate into the core library:
export const renderScoped = (ui: ReactElement, options?: RenderOptions) => {
const rendered = render(ui, options);
return {
...rendered,
...within(rendered.container),
};
};
This will be a potentially breaking change for users who currently rely on the queries not being scoped, so probably needs to be a "4.2" release.
- 主要语言
- JavaScript
- 星标
- 19.7k
- 派生
- 1.2k
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
testing-library/react-testing-library 的其他 Issue
-
fireEvent.select does not wrap its automatic native focus in act可能已有人在做 @sergioperezcheco 于 4 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 88/100
-
bug: calling configure() without reactStrictMode resets it to undefined, silently disabling strict mode可能重新可做 @suhailopensource 于 74 天前认领,目前没有进行中的 PR。 未关闭
难度 2/5 1-3 小时 新手友好度 35/100
testing-library/react-testing-library#1466 · 1 条评论 ·
-
难度 4/5 3-5 天 新手友好度 30/100
testing-library/react-testing-library#1459 · 2 条评论 ·
-
perf: optimize container lookup with early exit可能重新可做 @Ch-Abhinav-Chowdary 于 300 天前认领,目前没有进行中的 PR。 未关闭
难度 1/5 1 小时以内 新手友好度 35/100
testing-library/react-testing-library#1430 · 1 条评论 ·
-
`fireEvent.mouseEnter` does not forward `relatedTarget` (relatedTarget is the window instead)可能已有人在做 @swarnim02 于 318 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 55/100
查看 testing-library/react-testing-library 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
XRPLF/xrpl-dev-portal#4000 ·
维护者通常 1 天内回复
-
accessibility good first issue
难度 1/5 1-3 小时 新手友好度 92/100
维护者通常 1 天内回复
-
[BUG] Bubble chart series name is not XML-escaped in the embedded workbook (xl/tables/table1.xml)未关闭
难度 1/5 1 小时以内 新手友好度 94/100
-
Content:Learn needs triage
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 1 天内回复
-
enhancement good first issue
难度 2/5 1-3 小时 新手友好度 88/100
anoopcodehack/DevBoard#609 ·
维护者通常 1 天内回复