React-Native Jest测试函数在componentDidMount中调用
问题描述:
我试图测试一个函数是否在组件的componentDidMount
钩子中调用。React-Native Jest测试函数在componentDidMount中调用
我使用React-Native和Jest来测试我的组件。
组件看起来是这样的:
const tracker = new GoogleAnalyticsTracker('UA-79731-33');
class MyComponent extends Component {
componentDidMount() {
tracker.trackScreenView(this.props.title);
}
}
所以我嘲讽GoogleAnalyticsTracker
,看起来还好。虽然我不知道如何测试它在componentDidMount
挂钩中被调用。
这是我的测试,它无法正常工作:
import 'react-native';
import React from 'react';
import renderer from 'react-test-renderer';
import { GoogleAnalyticsTracker } from 'react-native-google-analytics-bridge';
import MyComponent from '../';
jest.mock('react-native-google-analytics-bridge');
const tracker = new GoogleAnalyticsTracker('ABC-123');
describe('MyComponent',() => {
it('renders',() => {
const tree = renderer.create(
<MyComponent />,
).toJSON();
expect(tree).toMatchSnapshot();
expect(tracker.trackScreenView).toBeCalled();
});
});
的toBeCalled()
返回false。
我该如何测试我的功能是否已在componentDidMount
中调用?
感谢