mohebifar / react-native-copilot

Step-by-step walkthrough tooltip for your react native app
MIT License
2.25k stars 409 forks source link

Testing with @testing-library/react-native #211

Open mtoninelli opened 3 years ago

mtoninelli commented 3 years ago

Current Behavior Testing with @testing-library/react-native the CopilotText element is not found

Unable to find an element with testID: test-schedule-name
...
const CopilotText = walkthroughable(Text);
                                  ^

Input Code

import { walkthroughable, CopilotStep } from 'react-native-copilot';
...
const CopilotText = walkthroughable(Text);
...
<View>
  <CopilotStep order={2} name='name'>
    <CopilotText testID={'test-schedule-name'}>
      {this.props.scheduleName}
    </CopilotText>
  </CopilotStep>
</View>
...

The test

jest.mock('react-native-copilot', () => ({
    walkthroughable: WrappedComponent => ({ copilot, ...props }) => <WrappedComponent {...copilot} {...props} />,
}));

it('displays name', () => {
    const { getByTestId } = render(<MyComponent />);
    const scheduleName = getByTestId('test-schedule-name');
    ...
});

Expected behavior/code Be able to select the CopilotText as the Text do

Environment

Possible Solution Another way of mocking the module (?)

jakequade commented 1 year ago

This works if you wrap the component in a copilot before using in the test:

import { render } from '@testing-library/react-native';
import React from 'react';
import { View, Text } from 'react-native';
import { walkthroughable, CopilotStep, copilot } from 'react-native-copilot';

const CopilotText = walkthroughable(Text);

function Thing() {
  return (
    <View>
      <CopilotStep order={2} name='name' text='hello there'>
        <CopilotText testID={'test-schedule-name'}>Some name</CopilotText>
      </CopilotStep>
    </View>
  );
}

export const WrappedThing = copilot({})(Thing);

it('should do the thing', () => {
  const { getByText } = render(<WrappedThing />);
  const text = getByText('Some name');
  expect(text).toBeTruthy();
});
jakequade commented 1 year ago

It also sees both the test ID and the text, so I think it's just an issue with how your test is set up.

image