Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

waitForNextUpdate times out with async function #4

Open
mischnic opened this issue Sep 9, 2020 · 1 comment
Open

waitForNextUpdate times out with async function #4

mischnic opened this issue Sep 9, 2020 · 1 comment

Comments

@mischnic
Copy link

mischnic commented Sep 9, 2020

import React from "preact/compat";
import { act, renderHook } from "@testing-library/preact-hooks";

// works:
// import React from "react";
// import { act, renderHook } from "@testing-library/react-hooks";

function useTest() {
  let [state, setState] = React.useState(1);

  return {
    value: state,
    async increment() {
      await new Promise((res) => setTimeout(res, 100));
      setState((i) => i + 1);
    },
  };
}

test("test", async () => {
  let { result, waitForNextUpdate } = renderHook(() => useTest());

  expect(result.current.value).toBe(1);
  await act(async () => {
    result.current.increment();
    await waitForNextUpdate();
  });
  expect(result.current.value).toBe(2);
});

Without await waitForNextUpdate(), the second assertion fails.
With that call, Jest times out (this works as expected when using React).

@brunoti
Copy link

brunoti commented Dec 15, 2020

Same problem here, but with events:

import { useEffect, useState } from 'preact/hooks';

const useWindowFocus = () => {
  const [visible, setVisible] = useState('visible');
  useEffect(() => {
    const handleVisibilityChange = () => {
      setVisible(() => {
        console.log('CALLED');
        return document.visibilityState;
      });
    };
    document.addEventListener('visibilitychange', handleVisibilityChange);
    return () => document.removeEventListener('visibilitychange', handleVisibilityChange);
  }, []);

  return visible === 'visible';
};

export { useWindowFocus };

import { renderHook, act } from '@testing-library/preact-hooks';
import { useWindowFocus } from '../use-window-focus';

Object.defineProperty(document, 'visibilityState', { value: 'visible', configurable: true, writable: true });

describe('useWindowFocus', () => {
  test('given that the visibilitychange event was triggered: should change its value accordingly', async () => {
    const { result, waitForNextUpdate } = renderHook(() => useWindowFocus());

    expect(result.current.isFocused).toBe(true);

    document.visibilityState = 'hidden';
    act(() => { document.dispatchEvent(new Event('visibilitychange')) });
    await waitForNextUpdate();
    expect(result.current.isFocused).toBe(false);

    //     document.visibilityState = 'visible';
    //     act(() => { document.dispatchEvent(new Event('visibilitychange')) });
    //     expect(result.current).toBe(true);
  });
});
EDIT: If the hook returns an object instead of a value it works. Here's the new code:
import { useEffect, useState } from 'preact/hooks';

const useWindowFocus = () => {
  const [visible, setVisible] = useState('visible');
  useEffect(() => {
    const handleVisibilityChange = () => setVisible(document.visibilityState);
    document.addEventListener('visibilitychange', handleVisibilityChange);
    return () => document.removeEventListener('visibilitychange', handleVisibilityChange);
  }, []);

  return { isFocused: visible === 'visible' };
};

describe('useWindowFocus', () => {
  test('given that the visibilitychange event was triggered: should change its value accordingly', async () => {
    const { result } = renderHook(() => useWindowFocus());

    expect(result.current.isFocused).toBe(true);

    document.visibilityState = 'hidden';
    act(() => { document.dispatchEvent(new Event('visibilitychange')) });
    expect(result.current.isFocused).toBe(false);

    document.visibilityState = 'visible';
    act(() => { document.dispatchEvent(new Event('visibilitychange')) });
    expect(result.current.isFocused).toBe(true);
  });
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants