|  | 
|  | 1 | +import * as React from 'react' | 
|  | 2 | +import {render, fireEvent, screen} from '../' | 
|  | 3 | +import {actIfEnabled} from '../act-compat' | 
|  | 4 | + | 
|  | 5 | +beforeEach(() => { | 
|  | 6 | +  global.IS_REACT_ACT_ENVIRONMENT = true | 
|  | 7 | +}) | 
|  | 8 | + | 
|  | 9 | +test('render calls useEffect immediately', async () => { | 
|  | 10 | +  const effectCb = jest.fn() | 
|  | 11 | +  function MyUselessComponent() { | 
|  | 12 | +    React.useEffect(effectCb) | 
|  | 13 | +    return null | 
|  | 14 | +  } | 
|  | 15 | +  await render(<MyUselessComponent />) | 
|  | 16 | +  expect(effectCb).toHaveBeenCalledTimes(1) | 
|  | 17 | +}) | 
|  | 18 | + | 
|  | 19 | +test('findByTestId returns the element', async () => { | 
|  | 20 | +  const ref = React.createRef() | 
|  | 21 | +  await render(<div ref={ref} data-testid="foo" />) | 
|  | 22 | +  expect(await screen.findByTestId('foo')).toBe(ref.current) | 
|  | 23 | +}) | 
|  | 24 | + | 
|  | 25 | +test('fireEvent triggers useEffect calls', async () => { | 
|  | 26 | +  const effectCb = jest.fn() | 
|  | 27 | +  function Counter() { | 
|  | 28 | +    React.useEffect(effectCb) | 
|  | 29 | +    const [count, setCount] = React.useState(0) | 
|  | 30 | +    return <button onClick={() => setCount(count + 1)}>{count}</button> | 
|  | 31 | +  } | 
|  | 32 | +  const { | 
|  | 33 | +    container: {firstChild: buttonNode}, | 
|  | 34 | +  } = await render(<Counter />) | 
|  | 35 | + | 
|  | 36 | +  effectCb.mockClear() | 
|  | 37 | +  // eslint-disable-next-line testing-library/no-await-sync-events --  TODO: Remove lint rule. | 
|  | 38 | +  await fireEvent.click(buttonNode) | 
|  | 39 | +  expect(buttonNode).toHaveTextContent('1') | 
|  | 40 | +  expect(effectCb).toHaveBeenCalledTimes(1) | 
|  | 41 | +}) | 
|  | 42 | + | 
|  | 43 | +test('calls to hydrate will run useEffects', async () => { | 
|  | 44 | +  const effectCb = jest.fn() | 
|  | 45 | +  function MyUselessComponent() { | 
|  | 46 | +    React.useEffect(effectCb) | 
|  | 47 | +    return null | 
|  | 48 | +  } | 
|  | 49 | +  await render(<MyUselessComponent />, {hydrate: true}) | 
|  | 50 | +  expect(effectCb).toHaveBeenCalledTimes(1) | 
|  | 51 | +}) | 
|  | 52 | + | 
|  | 53 | +test('cleans up IS_REACT_ACT_ENVIRONMENT if its callback throws', async () => { | 
|  | 54 | +  global.IS_REACT_ACT_ENVIRONMENT = false | 
|  | 55 | + | 
|  | 56 | +  await expect(() => | 
|  | 57 | +    actIfEnabled(() => { | 
|  | 58 | +      throw new Error('threw') | 
|  | 59 | +    }), | 
|  | 60 | +  ).rejects.toThrow('threw') | 
|  | 61 | + | 
|  | 62 | +  expect(global.IS_REACT_ACT_ENVIRONMENT).toEqual(false) | 
|  | 63 | +}) | 
|  | 64 | + | 
|  | 65 | +test('cleans up IS_REACT_ACT_ENVIRONMENT if its async callback throws', async () => { | 
|  | 66 | +  global.IS_REACT_ACT_ENVIRONMENT = false | 
|  | 67 | + | 
|  | 68 | +  await expect(() => | 
|  | 69 | +    actIfEnabled(async () => { | 
|  | 70 | +      throw new Error('thenable threw') | 
|  | 71 | +    }), | 
|  | 72 | +  ).rejects.toThrow('thenable threw') | 
|  | 73 | + | 
|  | 74 | +  expect(global.IS_REACT_ACT_ENVIRONMENT).toEqual(false) | 
|  | 75 | +}) | 
|  | 76 | + | 
|  | 77 | +test('state update from microtask does not trigger "missing act" warning', async () => { | 
|  | 78 | +  let triggerStateUpdateFromMicrotask | 
|  | 79 | +  function App() { | 
|  | 80 | +    const [state, setState] = React.useState(0) | 
|  | 81 | +    triggerStateUpdateFromMicrotask = () => setState(1) | 
|  | 82 | +    React.useEffect(() => { | 
|  | 83 | +      // eslint-disable-next-line jest/no-conditional-in-test | 
|  | 84 | +      if (state === 1) { | 
|  | 85 | +        Promise.resolve().then(() => { | 
|  | 86 | +          setState(2) | 
|  | 87 | +        }) | 
|  | 88 | +      } | 
|  | 89 | +    }, [state]) | 
|  | 90 | +    return state | 
|  | 91 | +  } | 
|  | 92 | +  const {container} = await render(<App />) | 
|  | 93 | + | 
|  | 94 | +  await actIfEnabled(() => { | 
|  | 95 | +    triggerStateUpdateFromMicrotask() | 
|  | 96 | +  }) | 
|  | 97 | + | 
|  | 98 | +  expect(container).toHaveTextContent('2') | 
|  | 99 | +}) | 
0 commit comments