oblador / react-native-collapsible

Animated collapsible component for React Native, good for accordions, toggles etc
MIT License
2.42k stars 455 forks source link

How to test react-native-collapsible/Accordion using testing-libray/react-native? #467

Open Shail-Patel-1 opened 1 year ago

Shail-Patel-1 commented 1 year ago
 const { getByTestId } = testingHelpers.renderWithProviders(
      <ProductDetailScreen {...testProps} />,
      {
        preloadedState: preloadedState,
      }
    );
    const productSpecificationsTitleView = getByTestId(
      TestIds.PRODUCT_DETAIL.PRODUCT_SPECIFICATION_TITLE
    );

    expect(productSpecificationsTitleView).toBeDefined();

    await act(async () => {
      await fireEvent.press(productSpecificationsTitleView);
    });

    const productSpecificationsContentView = getByTestId(
      TestIds.PRODUCT_DETAIL.PRODUCT_SPECIFICATION_CONTENT
    );

    expect(productSpecificationsContentView).toBeDefined();
  });

Whether or not i press the title the content is still present in the dom

Used & tested:

toBeDefined toBeOnScreen toBeNull toBeVisible

but above all fails i used queryByTestId, getByTestId, queryByText

KonstantinKostianytsia commented 9 months ago

I tested on this way

const firstItemCollapsable = screen.getByTestId(
      `${LongformContentNavigationTestIDs.outlineListItemContainer}.${mockedRoutes[0].id}`,
    ).children[1];
    /// is hidden
    expect(firstItemCollapsable.instance.props.collapsed).toBe(true);
    fireEvent.press(firstItemIcon);
    /// is collapsed
    expect(firstItemCollapsable.instance.props.collapsed).toBe(false);
    fireEvent.press(firstItemIcon);
    /// is hidden again
    expect(firstItemCollapsable.instance.props.collapsed).toBe(true);