Giter VIP home page Giter VIP logo

Comments (5)

ycs77 avatar ycs77 commented on August 23, 2024

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.

from headlessui-float.

razzeee avatar razzeee commented on August 23, 2024

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.

from headlessui-float.

razzeee avatar razzeee commented on August 23, 2024

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.

from headlessui-float.

ycs77 avatar ycs77 commented on August 23, 2024

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.

from headlessui-float.

ycs77 avatar ycs77 commented on August 23, 2024

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:

it('should to render <Float> with <Menu>', async () => {
render(
<Menu>
<Float placement="bottom-start" offset={4}>
<Menu.Button>Options</Menu.Button>
<Menu.Items>
<Menu.Item><button type="button">Account settings</button></Menu.Item>
<Menu.Item><button type="button">Documentation</button></Menu.Item>
<Menu.Item disabled><span>Invite a friend (coming soon!)</span></Menu.Item>
</Menu.Items>
</Float>
</Menu>
)
await waitFor()
const button = screen.getByText('Options')
await waitFor()
expect(button).toBeInTheDocument()
expect(screen.queryByRole('menu')).toBeNull()
await userEvent.click(button)
await waitFor()
expect(screen.queryByRole('menu')).toHaveAttribute('data-headlessui-state', 'open')
const menuItems = screen.queryAllByRole('menuitem')
expect(menuItems).toHaveLength(3)
await userEvent.click(button)
await waitFor()
expect(screen.queryByRole('menu')).toBeNull()
})

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

const promisedWaitFor = () => new Promise<void>(resolve => waitFor(resolve))

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.

from headlessui-float.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.