开玩笑/酶可以在输入复选框上查找检查属性
我正在为此组成部分编写测试。
<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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论