我有一个自定义钩子,它可以将可选的 ref 作为对象的属性传递给它,该钩子将其作为参数:
export const useShortcuts = ({ ref }) => {
useEffect(() => {
const trapper = new mousetrap(ref.current);
该代码有效,但我现在正在尝试使用 @testing-library/react-hooks 库为此react-testing-library
编写测试。
我正在使用renderHook
@ testing-library/react-hooks,但我不知道如何在组件外部创建引用或模拟引用。
it('should create shortcuts with no ref', () => {
const ref = ????? // how do I do this
const { result } = renderHook(() => useShortcuts({ ref }), {
initialProps: true
});
});