React Native PayModal 组件 Jest 单元测试用例
import React from 'react';
import { shallow } from 'enzyme';
import PayModal from '../PayModal';
describe('PayModal', () => {
let wrapper;
beforeEach(() => {
wrapper = shallow(<PayModal />);
});
it('should render correctly', () => {
expect(wrapper).toMatchSnapshot();
});
it('should call _handleBack method when onClose is called', () => {
const instance = wrapper.instance();
jest.spyOn(instance, '_handleBack');
instance.onClose();
expect(instance._handleBack).toHaveBeenCalled();
});
it('should call _handleAppStateChange method when AppState change', () => {
const instance = wrapper.instance();
jest.spyOn(instance, '_handleAppStateChange');
instance._handleAppStateChange('active');
expect(instance._handleAppStateChange).toHaveBeenCalledWith('active');
});
it('should call doWhileGetPayResult method when _handleAppStateChange is called with nextAppState as \'active\'', () => {
const instance = wrapper.instance();
jest.spyOn(instance, 'doWhileGetPayResult');
instance._handleAppStateChange('active');
expect(instance.doWhileGetPayResult).toHaveBeenCalled();
});
it('should call _checkPay method when doWhileGetPayResult is called', () => {
const instance = wrapper.instance();
jest.spyOn(instance, '_checkPay');
instance.doWhileGetPayResult();
expect(instance._checkPay).toHaveBeenCalled();
});
it('should call _fetchData method when _checkPay is called', () => {
const instance = wrapper.instance();
jest.spyOn(instance, '_fetchData');
instance._checkPay();
expect(instance._fetchData).toHaveBeenCalled();
});
it('should call _fetchData method when _handleBack is called', () => {
const instance = wrapper.instance();
jest.spyOn(instance, '_fetchData');
instance._handleBack();
expect(instance._fetchData).toHaveBeenCalled();
});
it('should call _handleAppStateChange method when _handleOnClose is called', () => {
const instance = wrapper.instance();
jest.spyOn(instance, '_handleAppStateChange');
instance._handleOnClose();
expect(instance._handleAppStateChange).toHaveBeenCalled();
});
it('should call _handleAppStateChange method when show is called', () => {
const instance = wrapper.instance();
jest.spyOn(instance, '_handleAppStateChange');
instance.show({});
expect(instance._handleAppStateChange).toHaveBeenCalled();
});
it('should call _fetchData method when show is called', () => {
const instance = wrapper.instance();
jest.spyOn(instance, '_fetchData');
instance.show({});
expect(instance._fetchData).toHaveBeenCalled();
});
it('should call _fetchData method when _handleNetResult is called', () => {
const instance = wrapper.instance();
jest.spyOn(instance, '_fetchData');
instance._handleNetResult({});
expect(instance._fetchData).toHaveBeenCalled();
});
it('should call _fetchData method when _handlePayResult is called with code as 6001', () => {
const instance = wrapper.instance();
jest.spyOn(instance, '_fetchData');
instance._handlePayResult({ code: 6001 });
expect(instance._fetchData).toHaveBeenCalled();
});
it('should call _fetchData method when _handlePayResult is called with code as -1', () => {
const instance = wrapper.instance();
jest.spyOn(instance, '_fetchData');
instance._handlePayResult({ code: -1 });
expect(instance._fetchData).toHaveBeenCalled();
});
// ... more test cases
});
原文地址: https://www.cveoy.top/t/topic/f3DW 著作权归作者所有。请勿转载和采集!