开玩笑/酶可以在输入复选框上查找检查属性

发布于 2025-01-23 11:14:17 字数 1967 浏览 2 评论 0原文

我正在为此组成部分编写测试。

    <div>
      <label className='switch me-4' htmlFor={`checkbox-${props.idx}`}>
        <input id={`checkbox-${props.idx}`} type="checkbox" defaultChecked={props.defaultChecked} onChange={props.onChange} />
        <span className='slider round'></span>
      </label>
      {props.label}
    </div>

我想断言检查的属性是在Prop defaultChecked = {true}中传递时的属性,因为在实际浏览器中是这种情况。


let onChange: jest.Mock<any, any>;

beforeEach(() => {
  jest.clearAllMocks();

  onChange = jest.fn();
});

test('Renders true.', () => {
  const component = shallow(<ToggleSwitch
    onChange={onChange}
    idx={1}
    defaultChecked={true}
  />);

  let toggled = component.find({ type: 'checkbox' });
  expect(toggled).toBeChecked()
})

我还尝试了期望(toggled.props()。检查)。

问题似乎是检查属性不存在道具中,但我阅读的所有内容都表明它应该存在。请参阅下面的道具。

    {
      id: 'checkbox-1',
      type: 'checkbox',
      defaultChecked: true,
      onChange: [Function: mockConstructor] {
        _isMockFunction: true,
        getMockImplementation: [Function (anonymous)],
        mock: [Getter/Setter],
        mockClear: [Function (anonymous)],
        mockReset: [Function (anonymous)],
        mockRestore: [Function (anonymous)],
        mockReturnValueOnce: [Function (anonymous)],
        mockResolvedValueOnce: [Function (anonymous)],
        mockRejectedValueOnce: [Function (anonymous)],
        mockReturnValue: [Function (anonymous)],
        mockResolvedValue: [Function (anonymous)],
        mockRejectedValue: [Function (anonymous)],
        mockImplementationOnce: [Function (anonymous)],
        mockImplementation: [Function (anonymous)],
        mockReturnThis: [Function (anonymous)],
        mockName: [Function (anonymous)],
        getMockName: [Function (anonymous)]
      }
    }

谢谢!

I'm writing a test for this component.

    <div>
      <label className='switch me-4' htmlFor={`checkbox-${props.idx}`}>
        <input id={`checkbox-${props.idx}`} type="checkbox" defaultChecked={props.defaultChecked} onChange={props.onChange} />
        <span className='slider round'></span>
      </label>
      {props.label}
    </div>

I want to assert that the checked attribute is present when passing in the prop defaultChecked={true} as this is the case in the actual browser.


let onChange: jest.Mock<any, any>;

beforeEach(() => {
  jest.clearAllMocks();

  onChange = jest.fn();
});

test('Renders true.', () => {
  const component = shallow(<ToggleSwitch
    onChange={onChange}
    idx={1}
    defaultChecked={true}
  />);

  let toggled = component.find({ type: 'checkbox' });
  expect(toggled).toBeChecked()
})

I've also tried expect(toggled.props().checked).toEqual(true).

The problem seems to be that the checked attribute does not exist in the props but everything I've read indicates that it should be there. See the props below.

    {
      id: 'checkbox-1',
      type: 'checkbox',
      defaultChecked: true,
      onChange: [Function: mockConstructor] {
        _isMockFunction: true,
        getMockImplementation: [Function (anonymous)],
        mock: [Getter/Setter],
        mockClear: [Function (anonymous)],
        mockReset: [Function (anonymous)],
        mockRestore: [Function (anonymous)],
        mockReturnValueOnce: [Function (anonymous)],
        mockResolvedValueOnce: [Function (anonymous)],
        mockRejectedValueOnce: [Function (anonymous)],
        mockReturnValue: [Function (anonymous)],
        mockResolvedValue: [Function (anonymous)],
        mockRejectedValue: [Function (anonymous)],
        mockImplementationOnce: [Function (anonymous)],
        mockImplementation: [Function (anonymous)],
        mockReturnThis: [Function (anonymous)],
        mockName: [Function (anonymous)],
        getMockName: [Function (anonymous)]
      }
    }

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文