react-native-tab-view icon indicating copy to clipboard operation
react-native-tab-view copied to clipboard

Testing TabView with Jest doensn't working

Open fuggerstadt1907 opened this issue 4 years ago • 6 comments

Current behaviour

I want to test interactions on my different TabView scenes, e.g. click on a selectable Card and press a next Button to navigate to the next scene. But that isn't possible with Jest. All Scenes and Components (Buttons, Texts, ...) are returned as promsise in my jest / react-native-testing-library setup. I can expect them, e.g. expect(getByTestId(SOME_TEST_ID_IN_TABVIEW)) but when I access a Button and try to fire an event (fireEvent.press(SOME_BUTTON_IN_TABVIEW)) I get the TypeError onPress of undefined

Expected behaviour

I want to test interactions on the different scenes, so it's necessary, that I can access all components in the rendered scenes.

Code sample

const ShuffleFilter = ({ navigateToHome }) => {
  const filterRoutes = ['STEP_1', 'STEP_2', 'STEP_3', 'STEP_4', 'STEP_5']

  const [tabState, setTabState] = useState({
    index: 0,
    routes: filterRoutes.map((key) => ({ key })),
  })

  const renderTabBar = (props) => {
    return (
      <FilterStepTabBarButton
        tabProps={props}
        tabState={tabState}
        navigateToHome={navigateToHome}
      />
    )
  }

  const renderScene = ({ route }) => {
    return <FilterStep onPressClose={navigateToHome} step={route.key} />
  }

  return (
    <SafeAreaView style={styles.body} testID={A11y.SHUFFLE_FILTER_VIEW}>
      <TabView
        swipeEnabled={false}
        navigationState={tabState}
        renderScene={renderScene}
        renderTabBar={renderTabBar}
        tabBarPosition="bottom"
        onIndexChange={(index) => setTabState((state) => ({ ...state, index }))}
      />
    </SafeAreaView>
  )
}

What have you tried

I've tried to access them in different ways, await them etc.... nothing helped me. All inside my <TabView />is handled as a promise.

Your Environment

software version
ios or android ios
react 16.13.1
react-native 0.62.2
react-native-tab-view ^2.14.4
react-native-gesture-handler ^1.6.1
react-native-reanimated ^1.9.0
node 12.16.1
npm or yarn yarn 1.22.0

fuggerstadt1907 avatar Jun 05 '20 06:06 fuggerstadt1907

I am also having trouble testing with jest. Has anyone found a guide or solution for testing Tabview?

jorgeibarra01 avatar Nov 17 '20 06:11 jorgeibarra01

same here

jackyleehong avatar Dec 08 '20 09:12 jackyleehong

What have you tried

I've tried to access them in different ways, await them etc.... nothing helped me. All inside my <TabView />is handled as a promise.

@fuggerstadt1907 Did you find any solution to this? Any insight that may be helpful for people with same issue?

jorgeibarra01 avatar Dec 13 '20 20:12 jorgeibarra01

have anyone here found this fix or a workaround?

jorgearuv avatar Aug 25 '21 18:08 jorgearuv

Did anyone found any solution for this?

prayaslashkari avatar Feb 08 '22 06:02 prayaslashkari

No, unfortunately not so far 😞

fuggerstadt1907 avatar Feb 08 '22 07:02 fuggerstadt1907