Skip to content

How to test component in testing-library? #41

Description

@dtokarczyk

This is my main component. When I click on the button popover shows notifications. Let's see...

const NotificationDropdown = ({ unread = 0 }) => (
  <PopupState variant="popover">
    {popupState => {
      const { isOpen } = popupState

      return (
        <>
          <Button open={isOpen} unread={unread} {...bindTrigger(popupState)} />
          <Popover
            {...bindPopover(popupState)}
            anchorOrigin={makeOrigin('bottom', 'center')}
            transformOrigin={makeOrigin('top', 'center')}
          >
            <Notifications data-testid="notification-content" />
          </Popover>
        </>
      )
    }}
  </PopupState>
)

I tried write tests for that:

 const Component = ({ unread }) => (
    <>
      <NotificationDropdown unread={unread} />
      <span data-testid="somewhere">For click action</span>
    </>
  )

  it('should be closed after click somewhere', async () => {
    render(Component)

    clickElementByTestId('notification-btn')
    const contentNotification = screen.queryByTestId('notification-content')

    expect(contentNotification).toBeInTheDocument() // Good
    clickElementByTestId('notification-btn')
    expect(contentNotification).not.toBeInTheDocument() // Fail! The document still exits.
  })

Maybe it's an issue with animation (during the assertion component it's in the animation process). But I tried delay assertion for example:

    jest.advanceTimersByTime(2000)

    await waitFor(() => {
      expect(contentNotification).not.toBeInTheDocument()
    })

But I got the same results.

How to fix it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions