小编Jor*_*dez的帖子

如何使用 jest/enzyme 中的“当前”道具测试 useRef

我希望有人能指出我在useRef下面的组件中进行测试的正确方向。

我有一个结构如下的组件。我正在尝试测试其中的功能,otherFunction()但我不确定如何模拟来自组件引用的当前属性。有没有人做过这样的事情?

const Component = (props) => {
    const thisComponent = useRef(null);
    const otherFunction = ({ current, previousSibling  }) => {
        if (previousSibling) return previousSibling.focus();
        if (!previousSibling && current) return current.focus();
    } 
    const handleFocus = () => {
        const {current} = thisComponent;
        otherFunction(current);
    }
     return (
        <div ref={thisComponent} onFocus={handleFocus}>Stuff In here</div>
    );
};
Run Code Online (Sandbox Code Playgroud)

reactjs jestjs enzyme

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

标签 统计

enzyme ×1

jestjs ×1

reactjs ×1