ycs77 / headlessui-float

Easily use Headless UI with Floating UI to position floating elements.
https://headlessui-float.vercel.app
MIT License
344 stars 11 forks source link

Should be wrapped in act(...) #60

Closed razzeee closed 10 months ago

razzeee commented 1 year ago

Use Version Use version when bugs appear:

Describe the bug Running tests with the file under test using this component causes:

  console.error
    Warning: An update to Float inside a test was not wrapped in act(...).

    When testing, code that causes React state updates should be wrapped into act(...):

    act(() => {
      /* fire events that update state */
    });
    /* assert on the output */

    This ensures that you're testing the behavior the user would see in the browser. Learn more at https://reactjs.org/link/wrap-tests-with-act
        at /home/klampe/dev/slashskills/node_modules/@headlessui-float/react/dist/headlessui-float.cjs:27:11232
        at div
        at OpenClosedProvider (/home/klampe/dev/slashskills/node_modules/@headlessui/react/dist/headlessui.dev.cjs:1011:31)
        at PopoverFn (/home/klampe/dev/slashskills/node_modules/@headlessui/react/dist/headlessui.dev.cjs:4828:54)

There is also an issue over at https://github.com/testing-library/testing-library-docs/issues/1255

It seemed to only show up after the react 18 update, but I'm unsure. Maybe it's even expected, that we need to do that, as the docs don't have any test examples :)

ycs77 commented 10 months ago

Hi @razzeee, please provide a minimal reproducible example (like github repo, codesandbox, stackblitz...), it is recommended to fork the Headless UI Flaot's Online Demo to reproduce.

razzeee commented 10 months ago

FYI, we currently have to add act (or waitFor in our case) around every component render, that headlessui-float is used in.

it('renders positive', async () => {
    render(<HoursPillRelative hours={-9} />)

    await waitFor(() => expect(screen.getByText('+9h')).toBeInTheDocument())
  })

That's our workaround for now. If it can be done in the library, it would spare quiet some work. I'll look into creating a repro asap.

razzeee commented 10 months ago

I found it quiet hard to work it into the online demo, as there is no testing integrated at all. Creating a new redwoodjs app might just be easier for me.

ycs77 commented 10 months ago

Yes, I think may not need to use the online demo. I just need a minimal reproducible example.

I can only do my best to help you with the act() error, but I can't guarantee that I can solve it. The main reason is that I usually use Vue, not React, and I'm not very familiar with React.

However, I can give you a reference code: Headless UI Float's unit test. I also encountered this problem many times when writing unit tests, but I still couldn't understand why this error occurred. This test is driven by Vitest. I hope it can help you build a minimal example and solve this problem.

ycs77 commented 10 months ago

I found that this issue can only be solved by using await waitFor(), because I also wrote the unit test in the package in the same way:

https://github.com/ycs77/headlessui-float/blob/d2d19f1e7a18d81e2cd59b20511a1b8dcf3d5c42/packages/react/test/unit/components.test.tsx#L6-L38

However, I created a util function based on the usage of Floating UI, which can help your code look more concise:

https://github.com/ycs77/headlessui-float/blob/d2d19f1e7a18d81e2cd59b20511a1b8dcf3d5c42/packages/react/test/unit/utils/testing-library.ts#L18

https://github.com/ycs77/headlessui-float/blob/d2d19f1e7a18d81e2cd59b20511a1b8dcf3d5c42/packages/react/test/unit/utils/testing-library.ts#L25

I hope this can help you.

However, I think this issue cannot be solved in the package, so I will close this issue for now. If you have a better solution, please send a PR.