gpt4 book ai didi

react-native - Jest 模拟和监视导入的异步函数

转载 作者:行者123 更新时间:2023-12-05 05:11:42 24 4
gpt4 key购买 nike

我想弄清楚如何在 Jest 中模拟导入的属性函数

这是我的组件 AboutScreen.js

import React from 'react';
import { Constants, WebBrowser } from 'expo';
import { View, Text } from 'react-native';

const AboutScreen = () => {
return (
<View>
<Text testId={"t-and-c"} onPress={() => WebBrowser.openBrowserAsync('tcUrl')}>
Terms & conditions
</Text>
</View>
);
};


export default AboutScreen;

我在 AboutScreen.test.js 中的测试如下所示

import React from 'react';
import { shallow } from 'enzyme';
import config from '../../config';
import AboutScreen from '../AboutScreen';
import { Constants, WebBrowser } from 'expo';
const { termsAndConditionsUrl, privacyPolicyUrl } = config;

jest.mock('expo', () => ({
Constants: {
manifest: {
version: '0.0.1',
releaseChannel: 'PROD',
},
WebBrowser: {
openBrowserAsync: jest.fn()
}
},
}));

it('click on terms and conditions link', () => {
const mock = jest.spyOn(WebBrowser, 'openBrowserAsync');
mock.mockImplementation(() => Promise.resolve());
// above statement return 'Cannot spyOn on a primitive value; undefined given'
// WebBrowser.openBrowserAsync = jest.fn(); --> This returns `WebBroser undefined
const wrapper = shallow(<AboutScreen />);
wrapper.find({ testId: 't-and-c' }).simulate('click');
expect(mock).lastCalledWith('abc');
// expect(WebBrowser.openBrowserAsync).toHaveBeenCalledWith('tcUrl);
});

我能够模拟 Constants.manifest.version 但无法弄清楚如何模拟“Browser”对象中的函数。

最佳答案

你很接近。


您当前正在将 WebBrowser 模拟为 Constantsinside 属性,因此需要像这样将其移出:

jest.mock('expo', () => ({
Constants: {
manifest: {
version: '0.0.1',
releaseChannel: 'PROD',
}
},
WebBrowser: {
openBrowserAsync: jest.fn()
}
}));

另一个问题是在使用 shallowsimulate 是如何工作的。来自Common Gotchas文档部分:

Even though the name would imply this simulates an actual event, .simulate() will in fact target the component's prop based on the event you give it. For example, .simulate('click') will actually get the onClick prop and call it.

...并且由于您的组件没有 onClick 属性调用 .simulate('click') 最终什么都不做。

This post来自 Airbnb 开发者的建议直接调用 props 并避免 simulate

您可以像这样直接调用 prop 来调用 onPress:

wrapper.find({ testId: 't-and-c' }).props().onPress();

所以所有的工作测试看起来像这样:

import React from 'react';
import { shallow } from 'enzyme';
import config from '../../config';
import AboutScreen from '../AboutScreen';
import { Constants, WebBrowser } from 'expo';
const { termsAndConditionsUrl, privacyPolicyUrl } = config;

jest.mock('expo', () => ({
Constants: {
manifest: {
version: '0.0.1',
releaseChannel: 'PROD',
}
},
WebBrowser: {
openBrowserAsync: jest.fn()
}
}));

it('click on terms and conditions link', () => {
const mock = jest.spyOn(WebBrowser, 'openBrowserAsync');
mock.mockImplementation(() => Promise.resolve());

const wrapper = shallow(<AboutScreen />);

wrapper.find({ testId: 't-and-c' }).props().onPress();
expect(mock).toHaveBeenCalledWith('tcUrl'); // Success!
});

关于react-native - Jest 模拟和监视导入的异步函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55267277/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com