0
我試圖測試一個函數是否在組件的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
中調用?
感謝