Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update testing section #1374

Open
wants to merge 25 commits into
base: main
Choose a base branch
from

Conversation

AnCichocka
Copy link

@AnCichocka AnCichocka commented Oct 7, 2024

update testing with jest section:

  • add mocking react-native-screens
  • add example tests

Copy link

netlify bot commented Oct 7, 2024

Deploy Preview for react-navigation-docs ready!

Name Link
🔨 Latest commit b3ff6b6
🔍 Latest deploy log https://app.netlify.com/sites/react-navigation-docs/deploys/671137eb4af28200089841be
😎 Deploy Preview https://deploy-preview-1374--react-navigation-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

@kkafar kkafar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, I think the examples look nice, we might want to get some insights from @satya164 here also

Comment on lines 62 to 67
screens = Object.create(
Object.getPrototypeOf(screens),
Object.getOwnPropertyDescriptors(screens)
);

return screens;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are missing here example of actual mock of some component from react-native-screens. Try to mock Screen as a View for example.

@kkafar kkafar requested a review from satya164 October 8, 2024 13:06
@AnCichocka AnCichocka requested a review from kkafar October 10, 2024 07:44
Copy link
Member

@satya164 satya164 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

I think the current examples showcasing different navigation methods aren't the best examples to use here. The important thing to note that this documentation is for users of the library. These tests showing how different navigation methods work isn't what the user will need. Our docs should be more focused on what users need.

Similarly, the fake timers examples are generic fake timer example code. The idea is to demonstrate why it maybe necessary in some cases when writing tests for navigation code (e.g. it's necessary in case of JS stack navigator because navigating triggers an animation, so with fake timers, we can instantly skip the animation for testing).

Here are some examples:

  1. Navigating to a screen and asserting that the new screen content is rendered.
    • By pressing a button
    • By pressing tabs on a tab bar
    • By pressing drawer item
  2. Testing app's functionality when using various hooks from React Navigation
    • Check if correct content is rendered when screen is focused and unfocused (using useIsFocused hook)
    • Check if data is fetched/a side effect is performed when screen comes to focus (using useFocusEffect hook)
  3. Testing app's functionality with code in various events (e.g. tabPress)

You can also think of more cases like this - specifically, what kind of tests you may write when building an app that involves navigation.

We should structure the docs in this way:

  • Problem statement - example of a real-world case that the user may want to test
  • Example code
  • Explanation for the example code
  • Explanation for any special code we may have done, e.g. we use fake timers to skip animations in JS stack navigators

Reanimated.default.call = () => {};

return Reanimated;
require('react-native-reanimated/mock');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems we're missing a return statement

@AnCichocka AnCichocka marked this pull request as ready for review October 17, 2024 16:16
@AnCichocka AnCichocka requested a review from satya164 October 17, 2024 16:16
const StackNavigation = createStaticNavigation(StackNavigator);
render(<StackNavigation />);

fireEvent.press(screen.queryByText('Go to Settings'));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use getBy… instead of queryBy… when we expect that an element will be present on the screen, and use queryBy… only when we expect that an element won’t be available?

  • This way, it will be clearer which elements we should expect to be visible or not.
  • In case of an error, getBy… will fail earlier than queryBy….

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants