reactjs - 测试 Material UI Radio Checked Value

标签 reactjs redux react-redux material-ui react-hooks

  • 我有一个使用 React 和 Redux 编写的功能组件 Hook 。
  • 我正在使用 Jest 和 Enzyme 进行测试。
  • 该组件呈现 Material UI 单选按钮(代码示例如下):
<RadioGroup>
  <FormControlLabel
    value="batchName"
    label="Batch Name"
    name="batchName"
    control={
      <Radio
        disableRipple
        name="batchName"
        color="primary"
        checked={searchBy === 'batchName'}
        onClick={() => {dispatch(actions.setBatchSearchBy('batchName'))}}
      />
    }
  />
  <FormControlLabel
    value="firstPaymentDate"
    label="First Payment Date"
    name="firstPaymentDate"
    control={
      <Radio:
        disableRipple
        name="firstPaymentDate"
        color="primary"
        checked={searchBy === 'firstPaymentDate'}
        onClick={() => {dispatch(actions.setBatchSearchBy('firstPaymentDate'))}}
      />
    }
  />
</RadioGroup>

测试文件:

import React from 'react';
import { BatchHeaderComponent } from '../../../components/batchManagement/BatchHeaderComponent';
import configureStore from '../../../store';
import {Provider} from "react-redux";
import Enzyme, { mount } from "enzyme";
import Adapter from "enzyme-adapter-react-16";
import Radio from "@material-ui/core/Radio";

Enzyme.configure({ adapter: new Adapter() });

describe('BatchHeaderComponent', () => {
  it('mounts to the DOM with its sub-components', () => {
    const wrapper = mount(<Provider store={configureStore()}>
      <BatchHeaderComponent/>
    </Provider>);
    expect(wrapper.find(BatchHeaderComponent)).toBeDefined();
  });

  it('changes searchBy when a new option has been selected', () => {
    const wrapper = mount(<Provider store={configureStore()}>
      <BatchHeaderComponent />
    </Provider>);
    const radio = wrapper.find(Radio).last();
    console.log(radio.debug());
    // radio.simulate('change', {target: {name: 'firstPaymentDate', checked: true}});
    // radio.prop('onChange', {target: { name: 'firstPaymentDate', checked: true}});
    radio.simulate('click');
    console.log(radio.debug());
    expect(radio.props().checked).toEqual(true);
  });
});

在模拟“点击”或“更改”事件时,我无法更改“选中”值。

无论我采取哪条路径,检查的值都保持为 false。

如有任何建议,我们将不胜感激。 谢谢!

最佳答案

我明白了。我需要再次运行wrapper.find才能看到更新后的更改。

  it('changes searchBy when a new option has been selected', () => {
    const wrapper = mount(<Provider store={configureStore()}>
      <BatchHeaderComponent />
    </Provider>);
    const radio = wrapper.find(Radio).last();
    radio.simulate('click');
    expect(wrapper.find(Radio).last().props().checked).toEqual(true);
  });

关于reactjs - 测试 Material UI Radio Checked Value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59274812/

相关文章:

javascript - ReactJS JSX 的 toString()

javascript - 在 native react 中重置组件的状态?

reactjs - Redux TS 通用类型 'Dispatch<S>' 需要 1 个类型参数

reactjs - react-router-dom 匹配对象 isExact false

javascript - React 中动态生成的按钮不会重新渲染

reactjs - 如何使用CustomRequest和antd上传功能将图片上传到firebase?

javascript - 如何在 React 应用程序中传递授权 token

javascript - 理解 Redux 和状态

javascript - React - Axios 调用发出太多请求

reactjs - React context vs redux vs hooks,应该考虑哪一个以及每一个有何不同