在React Native中开玩笑地发现线-抽屉导航-反应导航-toggleDrawer

时间:2020-06-22 07:08:37

标签: react-native jestjs react-navigation-v5

我在测试以下React Native组件的第30行和第37行时遇到问题。运行测试时,测试未涵盖包含“()=> navigation.toggleDrawer()”的行。


const MyCars = ({ navigation }: DrawerNavProps<'MyCars', 'MyCars'>) => {
  return (
    <SafeAreaView style={styles.safeAreaStyle}>
      <StatusBar hidden={true} />
      <View style={styles.content}>
        <View style={styles.headerBox}>
          <View style={styles.headerBoxLeft}>
            <Text style={styles.headerSmall}>myVehicle</Text>
            <Text style={styles.headerBig}>Dream Car</Text>
          </View>
          <View style={styles.headerBoxRight}>
            <Button
              title="thebutton"
              testID="MenuItemMyCars1"
              onPress={() => navigation.toggleDrawer()}
            >
              <Text>Test</Text>
            </Button>
            <CustomHeaderButton
              testId="MenuItemMyCars"
              action={() => {
                navigation.toggleDrawer();
              }}
              style={styles.menuButton}
              imageSource={Images.menu.hamburgerMenu}
            />
          </View>
        </View>
        <Image
          style={styles.imageCentered}
          source={Images.cars.tesla.blue}
        />
      </View>
    </SafeAreaView>
  );
};

export default MyCars;

这是我的测试代码:


const controlOptions: any = {
  route: jest.fn(),
  navigation: {
    navigate: jest.fn(),
    setOptions: jest.fn(),
    toggleDrawer: jest.fn(),
  },
};

describe('MyCars', () => {

  it('Button Opens Drawer Menu', () => {
    const { getByTestId } = render(<MyCars {...controlOptions} />);
    const MenuItemMyCars = getByTestId('MenuItemMyCars1');
    expect(MenuItemMyCars).toBeDefined();
    fireEvent.press(MenuItemMyCars);
    //The following line does not work, I get:  Expected number of calls: >= 1
    //Received number of calls:    0 
    //expect(controlOptions.navigation.toggleDrawer).toHaveBeenCalled();
  });

});

我正在使用Jest运行测试,并且在覆盖率报告中,发现这两行是未发现的。我尝试了不同的方法,但似乎无济于事。 enter image description here

有人可以帮忙吗?谢谢

0 个答案:

没有答案