Skip to content

PortalHost is not working in jest ReactNative Expo ~53.0.5 #91

@SSHristow

Description

@SSHristow

PortalHost doesn't work in jest render

Description

I have a Dropdown in my app that I want to test, but I'm stuck on an issue: When I click the dropdown button, the expanded is true, but I don't see the menu items in the tests. So I created this basic example to verify the issue. Usually, I have it in the test-utils where I define all of my providers, it's a replica of my root _layout.

Reproduction

import { render, fireEvent, waitFor } from '@testing-library/react-native'
import { PortalHost } from '@rn-primitives/portal'
import { Button } from '~/components/ui/button'
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger,
} from '~/components/ui/dropdown-menu'
import { Text } from '~/components/ui/text'

const TestDropdownComponent = () => {
  return (
    <>
      <DropdownMenu>
        <DropdownMenuTrigger asChild>
          <Button>
            <Text>Open Menu</Text>
          </Button>
        </DropdownMenuTrigger>
        <DropdownMenuContent>
          <DropdownMenuItem>
            <Text>Item 1</Text>
          </DropdownMenuItem>
          <DropdownMenuItem>
            <Text>Item 2</Text>
          </DropdownMenuItem>
        </DropdownMenuContent>
      </DropdownMenu>
      <PortalHost />
    </>
  )
}

describe('Portal Dropdown Test', () => {
  it('should render dropdown content', async () => {
    const screen = render(<TestDropdownComponent />)
    
    const trigger = screen.getByText('Open Menu')
    fireEvent.press(trigger)

    screen.debug()

    await waitFor(() => {
      expect(screen.getByText('Item 1')).toBeTruthy()
      expect(screen.getByText('Item 2')).toBeTruthy()
    })
  })
})

Screen debug result

 ● Portal Dropdown Test › should render dropdown content

    Unable to find an element with text: Item 1

    <View>
      <View
        accessibilityState={
          {
            "disabled": false,
            "expanded": true,  ----> //this is `true`
          }
        }
        accessible={true}
        role="button"
      >
        <Text>
          Open Menu
        </Text>
      </View>
    </View>

      48 |     screen.debug()

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