【问题标题】:how can i mock createBottomTabNavigator from @react-navigation/bottom-tabs in Jest我如何在 Jest 中从 @react-navigation/bottom-tabs 模拟 createBottomTabNavigator
【发布时间】:2021-04-27 01:39:01
【问题描述】:

我似乎无法用 Jest 测试我的底部导航器。

这是我的导航器:

import * as React from 'react';
import 'react-native-gesture-handler';
import Icon from 'react-native-vector-icons/FontAwesome';
import IonIcon from 'react-native-vector-icons/Octicons';
import {NavigationContainer} from '@react-navigation/native';
import {createBottomTabNavigator} from '@react-navigation/bottom-tabs';

import HomeNavigator from './routes/home';
import SearchNavigator from './routes/search';

export default function Navigator() {
  const Tab = createBottomTabNavigator();

  return (
    <NavigationContainer>
      <Tab.Navigator
        tabBarOptions={{
          keyboardHidesTabBar: true,
        }}>
        <Tab.Screen
          name="Home"
          component={HomeNavigator}
          options={{
            tabBarLabel: 'Home',
            tabBarIcon: ({color, size}) => (
              <Icon name="home" color={color} size={size} />
            ),
          }}
        />
        <Tab.Screen
          name="Search"
          component={SearchNavigator}
          options={{
            tabBarLabel: 'Search',
            tabBarIcon: ({color, size}) => (
              <IonIcon
                name="search"
                color={color}
                size={size}
                style={{transform: [{scaleX: -1}]}}
              />
            ),
          }}
        />
      </Tab.Navigator>
    </NavigationContainer>
  );
}

导航/index.js

我的 redux 包装组件的渲染函数:

<Provider store={global.store}>
  <PersistGate loading={null} persistor={global.persistor}>
    {this.props.children}
  </PersistGate>
</Provider>

redux_wrapper.js

我的 navigator.test.js

import * as React from 'react';
import {NavigationContainer} from '@react-navigation/native';
import {render, fireEvent} from '@testing-library/react-native';
import ReduxWrapper from '../src/redux/redux_wrapper';

import HomeNavigator from '../src/navigation/routes/home';

jest.mock('react-native/Libraries/Animated/NativeAnimatedHelper');

describe('Testing react navigation', () => {
  test('page contains the header and 10 items', async () => {
    const component = (
      <ReduxWrapper>
        <NavigationContainer>
          <HomeNavigator />
        </NavigationContainer>
      </ReduxWrapper>
    );

    const {getAllByTestId} = render(component);

    const homeScene = getAllByTestId('homeScene');

    expect(homeScene).toBeTruthy();
  });
});

navigator.test.js

当我尝试运行它时,我从 Jest 收到以下错误:

知道我的主要目的是稍后测试到详细信息屏幕的导航,我该如何正确测试?

【问题讨论】:

    标签: react-native jestjs react-navigation-bottom-tab


    【解决方案1】:

    getByTestID 函数查看渲染组件的testID= 属性。

    您尚未共享 HomeNavigator 的代码,但如果您将其更改为:

    // './routes/home'
        <View testID="homeScene">
          { /* rest of the HomeNavigator code */ }
        </View>
    

    您可能还需要在 &lt;Tab.Navigator JSX 中设置 initialRouteName 属性。

          <Tab.Navigator
            initialRouteName="Home"
            tabBarOptions={{
              keyboardHidesTabBar: true,
            }}>
            <Tab.Screen
            ...
          </Tab.Navigator>
    

    【讨论】:

    • 谢谢,成功了。实际上,我通过的 testID 中有错字。