简体   繁体   English

ts2339怎么解决? 使用带有测试库/反应的打字稿

[英]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);
  • I think of it had been occured why the toggle has other HTML DOM Element.我想到了为什么切换具有其他 HTML DOM 元素的情况。 So, I had this trouble.所以,我遇到了这个麻烦。

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.可能你应该在ContainerContent中有一个基于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.

相关问题 错误TS2339:JavaScript到Typescript错误 - error TS2339: JavaScript to Typescript error React + TypeScript:将React组件作为道具传递,使用道具进行渲染[错误:TS2339] - React + TypeScript: Passing React Component as Props, rendering with props [Error: TS2339] 使用 Jest 和 @testing-library/react-hooks 在 TypeScript 中单元测试自定义 React Hook - Unit Testing Custom React Hooks in TypeScript using Jest and @testing-library/react-hooks TypeScript 类型与 JSX.Element 反应将不接受.map() 并给出错误 TS2339 - TypeScript types with JSX.Element on react will not accept .map()and gives error TS2339 如何在Typescript中为其子项设置功能道具类型? TS2339类型上不存在“孩子”属性 - How to set function props type for its child in Typescript? Property 'children' does not exist on type TS2339 如何解决打字稿错误 TS2339:“IntrinsicAttributes &amp; …”类型上不存在“XXX”属性? - How to resolve typescript error TS2339: Property 'XXX' does not exist on type 'IntrinsicAttributes & …? Typescript 无法编译类型不存在的错误 TS2339 - Typescript can't compile type not existing Error TS2339 有没有办法放松 TypeScript 编译器警告 TS2339? - Is there a way to relax TypeScript compiler warning TS2339? 在事件处理程序中使用另一个类中的方法并绑定当前类上下文时,打字稿错误TS2339 - typescript error TS2339 when using method from another class in event handler and binding current class context 测试库自定义查询打字稿错误 - Testing-library custom query Typescript error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM