Testing Custom Hooks — Concept Check
Questions on Testing Custom Hooks. renderHook runs a hook without a component. Use it for hooks with real logic, and test the rest through the components that use them.
Questions in this quiz
8 questions on Testing Custom Hooks — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.You have a custom hook that calculates pagination logic. How should you test it?
- AUse renderHook to test the hook directly.
- BTest it through the component that uses it.
- CTest the hook by simulating user events.
- DUse a mock component to test the hook.
2.A hook is used to debounce input values. What is the correct way to test it?
- AUse renderHook without advancing timers.
- BTest it through the component that uses the hook.
- CUse renderHook and advance timers with act.
- DTest it using real user events.
3.When testing a hook with renderHook, what happens if you don't use act for state updates?
- AState updates are automatically handled.
- BThe test runs without any issues.
- CThe hook doesn't get tested at all.
- DYou get a warning and stale assertions.
4.A hook is a thin wrapper around a query and a permission check. How should it be tested?
- AUse renderHook to test the hook directly.
- BMock the hook and test it in isolation.
- CTest through the component that uses it.
- DTest the hook with fake timers.
5.Why might you choose not to test a hook directly with renderHook?
- AThe hook is used across multiple components.
- BThe hook is a thin wrapper used by one component.
- CThe hook uses timers and needs act.
- DThe hook contains complex logic.
6.What is the purpose of using a wrapper in renderHook for context-dependent hooks?
- ATo test the hook in isolation.
- BTo provide necessary context for the hook.
- CTo avoid using act for updates.
- DTo simulate user interactions.
7.A hook uses a reducer-backed workflow. How should it be tested?
- AMock the reducer logic and test separately.
- BTest it directly using renderHook.
- CTest using user events and async queries.
- DTest through the component using the hook.
8.What does result.current represent in renderHook tests?
- AThe initial props passed to the hook.
- BThe previous state before updates.
- CA snapshot of the initial state.
- DThe latest return value of the hook.