gpt4 book ai didi

javascript - getByText 用于通过动态生成的字符串将文本拆分为单独的行

转载 作者:行者123 更新时间:2023-12-02 18:23:08 26 4
gpt4 key购买 nike

我有一个元素在同一行上组合了静态和动态文本。

当通过包含一些静态和动态文本的子字符串测试这行文本时,它失败了,显示输出被拆分为多行,动态部分前后都有拆分。

这是怎么回事?为什么我不能按照我期望的方式测试它?我将如何正确测试它?

代码.jsx

const dynamicText = 'all be on one line';

return <div>This should {dynamicText} like I expect.</div>

代码.spec.js

it('Should have dynamic text on one line', () => {
const {getByText} = render(<Code />);

expect(getByText('should all be on one line ')).toBeTruthy();
});

测试输出

Unable to find an element with the text: ... This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.

...


<div>
This should
all be on one line
like I expect
</div>

最佳答案

我找到了一种可以轻松完成此操作的方法。

在我的代码中,我试图匹配完整字符串的子字符串,因为我只对检查动态部分是否正确呈现感兴趣。在引号(单引号或双引号)内指定我要查找的字符串似乎使 RTL 将其视为确定的字符串。

相反,我更改了单引号以使其更像正则表达式样式,因此将 'should all be on one line' 更改为 /should all be on one line/.

测试现在通过了:)

关于javascript - getByText 用于通过动态生成的字符串将文本拆分为单独的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70591547/

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