【发布时间】:2019-09-24 20:44:56
【问题描述】:
我有一个样式组件,它用一些样式包装了一个输入复选框元素。在我的应用程序中,默认情况下可能已经选中了此复选框。这是部分组件代码:
const InputCheckbox = styled.input.attrs((props) => ({
id: props.id,
type: 'checkbox',
checked: props.checked
}))`
visibility: hidden;
&:checked + label {
background-color: ${(props) => props.theme.mainColor};
border-color: ${(props) => props.theme.mainColor};
&:after {
border-left: 2px solid #fff;
border-bottom: 2px solid #fff;
}
}
`;
function Checkbox(props) {
return (
<CheckboxContainer>
<InputCheckbox
id={props.id}
checked={props.checked}
onChange={(event) => {
props.onChange(event.target.checked);
}}
/>
<CheckboxLabel id={props.id} />
</CheckboxContainer>
);
}
我正在使用 Jest 和 Enzyme 进行测试,但我找不到任何有关如何深入 Enzyme 浅层包装器以检查 InputCheckbox 中的输入是否已将 checked 属性设置为 true 的信息。例如:
describe('Checkbox', () => {
const mockProps = {
id: 'settings-user',
checked: true,
onComplete: (id) => jest.fn(id)
};
const component = shallow(<Checkbox {...mockProps}/>);
describe('on initialization', () => {
it('Input should be checked', () => {
const inputCheckbox = component.find('InputCheckbox');
expect(inputCheckbox.props().checked).toBe(true);
});
});
});
此测试失败,因为.find() 找不到任何节点。
【问题讨论】:
标签: javascript reactjs jestjs enzyme styled-components