You can mock React.useState
to return a different initial state in your tests:
// Cache original functionality
const realUseState = React.useState
// Stub the initial state
const stubInitialState = ['stub data']
// Mock useState before rendering your component
jest
.spyOn(React, 'useState')
.mockImplementationOnce(() => realUseState(stubInitialState))
Reference: https://dev.to/theactualgivens/testing-react-hook-state-changes-2oga
Related Contents:
- The useState set method is not reflecting a change immediately
- State not updating when using React state hook within setInterval
- How can I use multiple refs for an array of elements with hooks?
- React useEffect in depth / use of useEffect?
- Push method in React Hooks (useState)?
- Is useState synchronous? [duplicate]
- Invalid hook call. Hooks can only be called inside of the body of a function component
- Wrong React hooks behaviour with event listener
- React Hooks useState() with Object
- How to call loading function with React useEffect only once
- How can I use React hooks in React classic `class` component?
- React hooks – right way to clear timeouts and intervals
- Can I set state inside a useEffect hook
- How to use componentWillMount() in React Hooks?
- Updating and merging state object using React useState() hook
- Why is useState not triggering re-render?
- componentDidMount equivalent on a React function/Hooks component?
- react useEffect comparing objects
- How to register event with useEffect hooks?
- Is there a generic way to set state in React Hooks? How to manage multiple states?
- What is useState() in React?
- How do I update states `onChange` in an array of object in React Hooks
- How can I force a component to re-render with hooks in React?
- How to call an async function inside a UseEffect() in React?
- Where can I make API call with hooks in react?
- React useState hook event handler using initial state
- React Function Components with hooks vs Class Components
- Why need useRef and not mutable variable?
- Multiple calls to state updater from useState in component causes multiple re-renders
- React hooks: accessing up-to-date state from within a callback
- React – useState – why setTimeout function does not have latest state value?
- React useEffect causing: Can’t perform a React state update on an unmounted component
- Why React Hook useState uses const and not let
- React Hooks – What’s happening under the hood?
- Next.js context provider wrapping App component with page specific layout component giving undefined data
- How to prevent re-rendering of components that have not changed?
- Determine which dependency array variable caused useEffect hook to fire
- How to change the value of a Context with useContext?
- Simulate a button click in Jest
- useEffect does not listen for localStorage
- How to programmatically clear/reset React-Select?
- What does useCallback/useMemo do in React?
- React Hooks useState+useEffect+event gives stale state
- React Hooks render twice
- Do we still need functional setState way in react hooks?
- How does JavaScript mechanism behind react hooks work?
- useMemo vs. useEffect + useState
- Equivalent to componentDidUpdate using React hooks
- How can I use Jest to spy on a method call?
- Why can’t React Hooks be called inside loops or nested function?