小编Seb*_*ian的帖子

如何使用 Jest 测试 React 中功能组件的状态(无 Enzyme)

大家好,我如何从 useState 挂钩测试功能组件的状态?这是我的组件:

const Container = styled.div
    display: flex;
    flex: 1;
    flex-direction: row;
    align-items: ${props => props.hasSubtitle ? 'flex-start' : 'center'};
    opacity: ${props => props.disabled ? 0.5 : 1.0};
    ${props => props.style}

const Button = styled.button
    padding: 0;
    border: none;
    background: none;
    cursor: pointer;
    outline: none;

const TextContainer = styled.div
    display: flex;
    flex: 1;
    flex-direction: column;
    margin-left: 12px;

const CheckBox = props => {
    const [selected, setSelected] = useState(false);

    useEffect(() => {
        setSelected(props.selected);
    }, [props.selected]);

    return (
        <Container data-testid={'checkbox'} style={props.style} …
Run Code Online (Sandbox Code Playgroud)

javascript testing unit-testing reactjs jestjs

13
推荐指数
1
解决办法
2万
查看次数

标签 统计

javascript ×1

jestjs ×1

reactjs ×1

testing ×1

unit-testing ×1