[英]How to solve ts2339? Using typescript with testing-library/react
I got an error message " Property 'toggled' does not exist on type 'HTMLElement'.ts(2339) "我收到一条错误消息“属性 'toggled' 在类型 'HTMLElement'.ts(2339) 上不存在”
Strangely, My test case all have succeeded.奇怪的是,我的测试用例都成功了。 But, In testing file, There have occured error message about ts.2339.但是,在测试文件中,出现了关于 ts.2339 的错误消息。
What shall I do to solve this problem?我该怎么做才能解决这个问题?
component...
<Container data-testid={"list-element-toggle"} toggled={state[data.id - 1]}>. * style.div``
<Content data-testid={"list-element-content"} toggled={state[data.id - 1]}>. * style.div``
...
testing file...
expect(getAllByTestId("list-element-toggle")[0].toggled).toEqual(true);
expect(getAllByTestId("list-element-content")[0].toggled).toEqual(true);
testing-library/react is not checking the props of the component.测试库/反应不检查组件的道具。 Probably you should have a logic inside Container
and Content
that is based on toggled
prop.可能你应该在Container
和Content
中有一个基于toggled
道具的逻辑。
For example inside Content
:例如在Content
:
export function Content({ toggled }) {
return (
<div>
{toggled ? : <p>I am toggled</p> : null}
</div>
);
}
Then in the test:然后在测试中:
import { render, screen } from '@testing-library/react';
render(<App />);
expect(screen.getByText("I am toggled")).toBeInTheDocument();
I solved-self my problem the 'toggle' has a html dom element.我解决了我的问题,“切换”有一个 html dom 元素。 but, I want to fire for specific button was in other place.但是,我想为特定按钮触发在其他地方。
I had selected the button for test, it solved it我选择了按钮进行测试,它解决了它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.