Add example test to readme
See original GitHub issueSummary
Testing screens with ViewPager that uses setPage
function fails with the following error:
TypeError: Cannot read property ‘Commands’ of undefined
Tests like below will fail:
import React from 'react';
import {render, act} from '@testing-library/react-native';
import ViewPager from '@react-native-community/viewpager';
import {View} from 'react-native';
describe('ViewPager', () => {
test('view pager should move to next view', () => {
const viewPagerRef = React.createRef();
const {UNSAFE_getByType} = render(
<ViewPager ref={viewPagerRef}>
<View testID="page1" />
<View testID="page2" />
</ViewPager>,
);
expect(UNSAFE_getByType(ViewPager)._fiber.index).toBe(0);
act(() => {
viewPagerRef.current.setPage && viewPagerRef.current.setPage(1);
});
expect(UNSAFE_getByType(ViewPager)._fiber.index).toBe(1);
});
});
Environment info
react-native info
output:
System:
OS: macOS 10.15.6
CPU: (4) x64 Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz
Memory: 21.24 MB / 8.00 GB
Shell: 5.7.1 - /bin/zsh
Binaries:
Node: 12.18.3 - /var/folders/vl/7sf6krl17mz491vg6rqrlkr00000gn/T/yarn--1598293332713-0.5982209185240344/node
Yarn: 1.22.4 - /var/folders/vl/7sf6krl17mz491vg6rqrlkr00000gn/T/yarn--1598293332713-0.5982209185240344/yarn
npm: 6.14.6 - ~/.asdf/installs/nodejs/12.18.3/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
Managers:
CocoaPods: 1.9.3 - /usr/local/bin/pod
SDKs:
iOS SDK:
Platforms: iOS 13.6, DriverKit 19.0, macOS 10.15, tvOS 13.4, watchOS 6.2
Android SDK:
API Levels: 28
Build Tools: 28.0.3, 30.0.1
System Images: android-28 | Intel x86 Atom
Android NDK: Not Found
IDEs:
Android Studio: 4.0 AI-193.6911.18.40.6626763
Xcode: 11.6/11E708 - /usr/bin/xcodebuild
Languages:
Java: 1.8.0_261 - /usr/bin/javac
Python: 2.7.16 - /Users/brunocrpontes/.asdf/shims/python
npmPackages:
@react-native-community/cli: Not Found
react: 16.11.0 => 16.11.0
react-native: 0.62.2 => 0.62.2
npmGlobalPackages:
*react-native*: Not Found
Library version: 4.1.6
Steps to reproduce
- Create a test with
<ViewPager />
- Take its ref like
<ViewPager ref={ref => viewPager = ref}
- Call
viewPager.setPage(/* page value */)
Describe what you expected to happen:
- Call
setPage
doesn’t throw any errors. - Update value of index in component reference.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:6
- Comments:18 (5 by maintainers)
Top Results From Across the Web
GitHub - aswitalski/readme-to-test
The readme-to-test libary allows to easily validate code examples defined in the README file. It finds all JavaScript blocks of code defined in...
Read more >Make a README
Learn how to make a great README for your programming project, and use the editable template to get started.
Read more >Running tests against code examples in a README.md?
Is it possible to include README.md code snippets in my test suite? For example, the usage of say.nancat with sample params: # $...
Read more >README.md
Tests can be grouped together using the group() function. Each group's description is added to the beginning of its test's descriptions.
Read more >README.md template | Documenting your project - Drupal
README documentation can include more Markdown-features than those used in the example snippets in this template. For instance, Markdown let you create ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Any updates on this? Still not sure how to test it
Took me so long to get to the bottom of this - if anyone needs to test the tab change event and the
onIndexChange
function, you can add this mock file or usejest.mock
as others have suggested: