如何模拟和测试 mailto 按钮

问题描述 投票:0回答:0

我正在尝试测试 React Native 中的链接按钮。该链接打开电子邮件服务器的 url。

在名为 SupportScreen 的父组件中使用自定义组件,如下所示:


     <LinkButton
       title={email}
       accessibilityLabel={supportEmail}
       onPress={() => Linking.openURL(`mailto:${supportEmail}`)}
     />

在模拟中:


    const mockOpenURL = jest.fn();
    jest.spyOn(ReactNative, 'Linking').mockImplementation(() => {
      return {
        openURL: mockOpenURL,
      };
    });

我收到一条错误消息:

 Cannot spy the Linking property because it is not a function; object given instead

然后我如何实际为模拟链接编写测试?

 

    it('should navigate to email client when pressed', () => {
        const { getByLabelText } = render(<SupportScreen />);
      });
    });

react-native jestjs mocking react-testing-library mailto
© www.soinside.com 2019 - 2024. All rights reserved.