n4kz / react-native-pages

Easy to use page view component
Other
376 stars 83 forks source link

Add the possibility to set testID to create unit tests #55

Open ManuViola77 opened 2 years ago

ManuViola77 commented 2 years ago

In my project, we are writing unit tests for our components and screens and we got stuck for a while because this library doesn't accept the testID property for the main View.

I suggest to add the testID here:

return (
    <View style={[styles.container, containerStyle]} onLayout={this.onLayout} testID={this.props.testID}>
        {this.renderPages(props)}

        <Pager />
      </View>
    );

I think it would be great if you could add the ability to set a testID. If you want I can make the PR, I even tested it locally but I decided to go with a workaround, in case anyone else wants it, here is my current code:

<View testID={'pagesViewWrapperTestId'}>
   <Pages
    ...        
   </Pages>
</View>

And in my test I do:

const pagesViewWrapper = queryByTestId('pagesViewWrapperTestId');
expect(pagesViewWrapper).toBeTruthy();
const pagesView = pagesViewWrapper.children[0].children[0]; // this gets the view from Pages I want to add the testID to access directly to it
fireEvent(pagesView, 'layout', {
      nativeEvent: { layout: { height: 100 } },
});

The last part is to trigger the onLayout function from the View, otherwise, it doesn't render the pages content.