[英]How to solve ts2339? Using typescript with testing-library/react
我收到一條錯誤消息“屬性 'toggled' 在類型 'HTMLElement'.ts(2339) 上不存在”
奇怪的是,我的測試用例都成功了。 但是,在測試文件中,出現了關於 ts.2339 的錯誤消息。
我該怎么做才能解決這個問題?
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);
測試庫/反應不檢查組件的道具。 可能你應該在Container
和Content
中有一個基於toggled
道具的邏輯。
例如在Content
:
export function Content({ toggled }) {
return (
<div>
{toggled ? : <p>I am toggled</p> : null}
</div>
);
}
然后在測試中:
import { render, screen } from '@testing-library/react';
render(<App />);
expect(screen.getByText("I am toggled")).toBeInTheDocument();
我解決了我的問題,“切換”有一個 html dom 元素。 但是,我想為特定按鈕觸發在其他地方。
我選擇了按鈕進行測試,它解決了它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.