我想測驗一個沒有標簽的復選框。關鍵是還有其他復選框,所以我不能使用 getByRole。
我曾嘗試使用 data-testid,但顯然它不起作用。
我應該如何檢查該復選框是否被選中(toBeChecked())?
<Checkbox
data-testid={item?.id}
key={item?.id}
id={item?.id.toString()}
color="secondary"
checked={item?.checked}
disabled={hasAll}
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
if (item) {
item.checked = e.target.checked;
setFinalData(updateItemInArray(finalData, {
item,
index,
}));
}
}}
/>
uj5u.com熱心網友回復:
執行此操作的正確方法是aria-label在您的復選框中添加一個,以便螢屏閱讀器正確宣布:
<Checkbox inputProps={{ 'aria-label': 'Select row 1' }} />
這樣,您就可以在測驗中使用getByLabelText.
如果你想使用data-testid,你可以把它放在復選框上,類似于aria-label:
<Checkbox inputProps={{ 'data-testid': 'checkbox1' }} />
請注意,如果您使用的是 Typescript,則必須將 inputProps 轉換React.InputHTMLAttributes<HTMLInputElement>為資料屬性不是 InputHTMLAttributes 的一部分:
<Checkbox inputProps={{ 'data-testid': 'checkbox1' } as React.InputHTMLAttributes<HTMLInputElement>} />
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/457508.html
