【发布时间】: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
知道我的主要目的是稍后测试到详细信息屏幕的导航,我该如何正确测试?
【问题讨论】:
标签: react-native jestjs react-navigation-bottom-tab