import React from 'react'; import { shallow } from 'enzyme'; import PayModal from '../PayModal';

describe('PayModal', () => { let wrapper; beforeEach(() => { wrapper = shallow(); });

it('should render correctly', () => { expect(wrapper).toMatchSnapshot(); });

it('should set the initial state correctly', () => { expect(wrapper.state()).toEqual({ data: null, selPayWayIndex: 0, time: '', isLoading: true, isNetError: false, leftTime: 10000, confirmModalVisible: true, appState: AppState.currentState, paymentId: '', }); });

it('should handle _handleAppStateChange correctly', () => { const nextAppState = 'active'; wrapper.instance()._handleAppStateChange(nextAppState); expect(wrapper.state().appState).toEqual(nextAppState); expect(wrapper.instance().isChecking).toBe(false); });

it('should handle doWhileGetPayResult correctly', () => { const paying = true; const visible = true; wrapper.setState({ paying, visible }); wrapper.instance().isChecking = false; wrapper.instance().isGoToSuccess = false; wrapper.instance()._checkPay = jest.fn(); wrapper.instance()._fetchData = jest.fn(); wrapper.instance().timerText = { stop: jest.fn() }; wrapper.instance().updateCallback = 'updateCallback'; wrapper.instance().props = { from: 'orderList', navigation: { navigate: jest.fn(), }, };

wrapper.instance().doWhileGetPayResult();
expect(wrapper.instance().isChecking).toBe(true);
expect(wrapper.instance()._checkPay).toHaveBeenCalledTimes(1);
expect(wrapper.instance()._fetchData).toHaveBeenCalledTimes(0);

wrapper.setState({ paymentId: 'paymentId' });
wrapper.instance().doWhileGetPayResult();
expect(wrapper.instance().isChecking).toBe(true);
expect(wrapper.instance()._checkPay).toHaveBeenCalledTimes(2);
expect(wrapper.instance()._fetchData).toHaveBeenCalledTimes(0);

wrapper.setState({ visible: false });
wrapper.instance().doWhileGetPayResult();
expect(wrapper.instance().isChecking).toBe(false);
expect(wrapper.instance()._checkPay).toHaveBeenCalledTimes(2);
expect(wrapper.instance()._fetchData).toHaveBeenCalledTimes(0);

wrapper.setState({ paying: false });
wrapper.instance().doWhileGetPayResult();
expect(wrapper.instance().isChecking).toBe(false);
expect(wrapper.instance()._checkPay).toHaveBeenCalledTimes(2);
expect(wrapper.instance()._fetchData).toHaveBeenCalledTimes(0);

wrapper.setState({ visible: true, paying: true });
wrapper.instance().doWhileGetPayResult();
expect(wrapper.instance().isChecking).toBe(true);
expect(wrapper.instance()._checkPay).toHaveBeenCalledTimes(3);
expect(wrapper.instance()._fetchData).toHaveBeenCalledTimes(0);

});

it('should handle _checkPay correctly', () => { const interval = 'interval'; const paymentId = 'paymentId'; const visible = true; const res = { code: 10000, data: { payStatus: '1', promotionType: 'prepare', preparePayType: 'deposit', }, }; const checkPay = jest.fn(() => Promise.resolve(res)); wrapper.setState({ paymentId, visible }); wrapper.instance()._checkPay(interval); expect(checkPay).toHaveBeenCalledTimes(1); expect(wrapper.instance().isChecking).toBe(false); expect(wrapper.instance().isGoToSuccess).toBe(true); expect(wrapper.instance().timerText.stop).toHaveBeenCalledTimes(1); expect(wrapper.instance().updateCallback).toBe(null); expect(wrapper.instance().props.navigation.navigate).toHaveBeenCalledTimes(1);

wrappe

原文地址: http://www.cveoy.top/t/topic/hAiA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录