我有一个自定义钩子,我正在尝试编写使用反应钩子测试库包的测试,我想知道如何测试自定义钩子中未返回但在其他函数中使用的内部函数。
const customHook = () => {
const [count, setCount] = React.useState(0);
const doSomeThing = () => {
..code
}
const increment = () => {
doSomeThing(); //Would like to make assertations on this
setCount((x) => x + 1 );
}
return { count, increment }
}
export default customHook;
测试
it('Should call increment', () => {
const { result } = renderHook(() => useCustomHook())
act(() => {
result.current.increment();
});
expect(doSomeThing).toHaveBeenCalled(); //end result of what I would like help on
});
如何编写测试以查看 doSomething 是否已被调用/使用?