import useFunctionState from '../src'; import React, { createContext, useContext, useEffect } from 'react'; import { create, act, ReactTestRenderer } from 'react-test-renderer'; // A React context containing a handler function and a setter for it type Handler = (value: string) => void; interface HandlerContextValue { handler?: Handler; setHandler: (handler?: Handler) => void; } const HandlerContext = createContext({ handler: undefined, setHandler: () => {}, }); // A parent component that provides the context and renders // * Child1 consuming it to set the handler, and // * Child2 consuming it to call the handler const Parent: React.FC<{}> = () => { // changing useFunctionState to useState in the next line breaks the test! const [handler, setHandler] = useFunctionState(); return ( ); }; // The test handler set by Child1 const mockHandler = jest.fn(); // The child component setting the handler const Child1: React.FC<{}> = () => { const { setHandler } = useContext(HandlerContext); useEffect(() => { setHandler(mockHandler); return () => setHandler(); }, [setHandler]); return null; }; // The child component calling the handler const Child2: React.FC<{}> = () => { const { handler } = useContext(HandlerContext); return