小编Dal*_*ace的帖子

如何使用React-Testing-Library选中Material-UI Checkbox?

似乎Material UI的工作方式是单击复选框时呈现了一个不同的SVG,而不更改属性或实际输入元素上的任何内容。那么,我该如何实际测试该元素是否按照react-testing-library哲学进行了检查?

这是一个粗略的例子

// Checkbox component usage
export const CheckBoxContainer = () => 
<Checkbox
  inputProps={{ 'data-testid': `clickable-checkbox-1234` }}
  data-testid={`checkbox-1234`}
/>

// test
test('check the box', async () => {
  const { getByTestId } = render(<CheckBoxContainer />);
  await waitForElement(() => getByTestId(`checkbox-1234`));
  const checkbox = getByTestId(`checkbox-1234`);
  fireEvent.click(getByTestId(`clickable-checkbox-1234`));
  expect(checkbox).toHaveAttribute('checked');
});

// Generated HTML by Material UI
<span class="MuiButtonBase-root-54 MuiIconButton-root-48 MuiSwitchBase-root-231 MuiCheckbox-root-225 MuiCheckbox-colorSecondary-230 MuiSwitchBase-checked-232 MuiCheckbox-checked-226" data-testid="checkbox-1234">
  <span class="MuiIconButton-label-53">
    <svg class="MuiSvgIcon-root-57" focusable="false" viewBox="0 0 24 24" aria-hidden="true" role="presentation">
      <path d="M19 3H5c-1.11 0-2 .9-2 …
Run Code Online (Sandbox Code Playgroud)

material-ui react-testing-library

5
推荐指数
2
解决办法
2233
查看次数

标签 统计

material-ui ×1

react-testing-library ×1