[英]Testing-library custom query Typescript error
In a current project using react
with jest
and @testing-library/react
, I'm implementing custom queries to query the text content of components.在当前使用
react
with jest
和@testing-library/react
,我正在实施自定义查询来查询组件的文本内容。
The custom query (built with testing-library's buildQueries
method) I want to use receives two parameters (one is optional).我想使用的自定义查询(使用 testing-library 的
buildQueries
方法buildQueries
)接收两个参数(一个是可选的)。 When I want to add a second parameter, Typescript tells me that only one argument can be passed.当我想添加第二个参数时,Typescript 告诉我只能传递一个参数。
const queryAllByTextContent = (
container: HTMLElement,
text: string | RegExp,
options?: QueryParameterOptions
): HTMLElement[] => {
const { exact = false } = options ?? { };
return queryAllByText(container, (content, node): boolean => {
if (!node) {
return false;
}
const nodeHasSearchText = nodeContainsSearchText({ searchText: text, exact, node });
// eslint-disable-next-line unicorn/prefer-spread
const childNodesDontHaveSearchText = Array.from(node.children).every(
(childNode): boolean => !nodeContainsSearchText({ searchText: text, exact, node: childNode })
);
return nodeHasSearchText && childNodesDontHaveSearchText;
});
};
const getMultipleError = (
container: Element | null,
text: string | RegExp
): string => `Found multiple elements with the text: ${text}`;
const getMissingError = (
container: Element | null,
text: string | RegExp
): string => `Unable to find an element with the text: ${text}`;
const [
queryByTextContent,
getAllByTextContent,
getByTextContent,
findAllByTextContent,
findByTextContent
] = buildQueries(queryAllByTextContent, getMultipleError, getMissingError);
I have created a Github repository with a working example:我用一个工作示例创建了一个 Github 存储库:
https://github.com/desudo/example-jest-with-react-testing-library https://github.com/desudo/example-jest-with-react-testing-library
Most important files are:最重要的文件是:
/test/helpers/textContentQueries.ts
-> queryAllByTextContent
/test/component/Price.test.tsx
/test/helpers/textContentQueries.ts
-> queryAllByTextContent
/test/component/Price.test.tsx
What I found out :我发现了什么:
The first parameter of the custom query has to be of type string | RegExp
自定义查询的第一个参数的类型必须是
string | RegExp
string | RegExp
. string | RegExp
。 When I set the type to just a string, everything works as intended.当我将类型设置为只是一个字符串,一切都会按计划。
IMPORTANT重要的
The example tests are just dummy tests to show the Typescript error.示例测试只是用于显示 Typescript 错误的虚拟测试。
Thank you for any hint or solution!感谢您的任何提示或解决方案!
One way of fixing this issue is to add the parameter options?: QueryParameterOptions
to both getMultipleError
and getMissingError
.解决此问题的一种方法是将参数
options?: QueryParameterOptions
添加到getMultipleError
和getMissingError
。
This is due to the type that is being defined under the hood of buildQueries
.这是由于在
buildQueries
引擎盖下定义的类型。
I certainly do not like this solution because we are not using the options parameter in either of these two methods.我当然不喜欢这个解决方案,因为我们没有在这两种方法中使用 options 参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.