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

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

it('renders without crashing', () => { expect(wrapper).toBeTruthy(); });

it('initializes state correctly', () => { expect(wrapper.state()).toEqual({ data: null, selPayWayIndex: 0, time: '', isLoading: true, isNetError: false, leftTime: 10000, confirmModalVisible: true, appState: 'active', paymentId: '', }); });

describe('show method', () => { it('updates state correctly when data is null', () => { const instance = wrapper.instance(); instance.show(null); expect(wrapper.state('visible')).toBe(true); expect(wrapper.state('data')).toBe(null); expect(wrapper.state('isLoading')).toBe(true); expect(wrapper.state('paying')).toBe(true); expect(wrapper.state('paymentId')).toBe(''); });

it('updates state correctly when data is not null', () => {
  const instance = wrapper.instance();
  const data = { orderId: 123 };
  instance.show(data);
  expect(wrapper.state('visible')).toBe(true);
  expect(wrapper.state('data')).toBe(data);
  expect(wrapper.state('isLoading')).toBe(true);
  expect(wrapper.state('paying')).toBe(true);
  expect(wrapper.state('paymentId')).toBe('');
});

});

describe('handleAppStateChange method', () => { it('does nothing if appState is inactive or background and nextAppState is not active', () => { const instance = wrapper.instance(); wrapper.setState({ appState: 'inactive' }); instance.handleAppStateChange('inactive'); expect(wrapper.state('appState')).toBe('inactive');

  wrapper.setState({ appState: 'background' });
  instance.handleAppStateChange('background');
  expect(wrapper.state('appState')).toBe('background');
});

it('calls doWhileGetPayResult method if appState is inactive or background and nextAppState is active', () => {
  const instance = wrapper.instance();
  const spy = jest.spyOn(instance, 'doWhileGetPayResult');
  wrapper.setState({ appState: 'inactive' });
  instance.handleAppStateChange('active');
  expect(spy).toHaveBeenCalled();

  wrapper.setState({ appState: 'background' });
  instance.handleAppStateChange('active');
  expect(spy).toHaveBeenCalled();
});

it('updates state correctly', () => {
  const instance = wrapper.instance();
  wrapper.setState({ appState: 'active' });
  instance.handleAppStateChange('inactive');
  expect(wrapper.state('appState')).toBe('inactive');
});

});

describe('doWhileGetPayResult method', () => { it('does nothing if paying is false', () => { const instance = wrapper.instance(); const spy = jest.spyOn(instance, '_checkPay'); wrapper.setState({ paying: false }); instance.doWhileGetPayResult(); expect(spy).not.toHaveBeenCalled(); });

it('does nothing if isChecking is true', () => {
  const instance = wrapper.instance();
  const spy = jest.spyOn(instance, '_checkPay');
  wrapper.setState({ paying: true });
  wrapper.instance().isChecking = true;
  instance.doWhileGetPayResult();
  expect(spy).not.toHaveBeenCalled();
});

it('does nothing if visible is false', () => {
  const instance = wrapper.instance();
  const spy = jest.spyOn(instance, '_checkPay');
  wrapper.setState({ paying: true, visible: false });
  instance.doWhileGetPayResult();
  expect(spy).not.toHaveBeenCalled();
});

it('calls _checkPay method if paying is true, isChecking is false, and visible is true', () => {
  const instance = wrapper.instance();
  const spy = jest.spyOn(instance, '_checkPay');
  wrapper.setState({ paying: true, visible: true });
  instance.doWhileGetPayResult();
  expect(spy).toHaveBeenCalled();
});

it('sets isChecking to true', () => {
  const instance = wrapper.instance();
  wrapper.setState({ paying: true, visible: true });
  instance.doWhileGetPayResult();
  expect(instance.isChecking).toBe(true);
});

it('shows a loading toast', () => {
  const instance = wrapper.instance();
  const spy = jest.spyOn(global.LToast, 'loading');
  wrapper.setState({ paying: true, visible: true });
  instance.doWhileGetPayResult();
  expect(spy).toHaveBeenCalledWith('加载中', 0);
});

it('clears the loading toast after 6 attempts', () => {
  const instance = wrapper.instance();
  const spy = jest.spyOn(global.LToast, 'removeAll');
  wrapper.setState({ paying: true, visible: true });
  instance.doWhileGetPayResult();
  expect(spy).not.toHaveBeenCalled();

  jest.advanceTimersByTime(3000);
  expect(spy).toHaveBeenCalled();

  expect(instance.isChecking).toBe(false);
});

});

describe('_checkPay method', () => { it('does nothing if visible is false', () => { const instance = wrapper.instance(); const spy = jest.spyOn(instance, '_fetchData'); wrapper.setState({ visible: false }); instance._checkPay(); expect(spy).not.toHaveBeenCalled(); });

it('calls _fetchData if paymentId is not set', () => {
  const instance = wrapper.instance();
  const spy = jest.spyOn(instance, '_fetchData');
  wrapper.setState({ visible: true });
  instance._checkPay();
  expect(spy).toHaveBeenCalled();
});

it('calls checkPay if paymentId is set', () => {
  const instance = wrapper.instance();
  const spy = jest.spyOn(global.checkPay, 'then');
  wrapper.setState({ visible: true, paymentId: '123' });
  instance._checkPay();
  expect(spy).toHaveBeenCalled();
});

it('handles successful checkPay response', async () => {
  const instance = wrapper.instance();
  const spy = jest.spyOn(instance, '_handleNetResult');
  const mockResponse = {
    code: 10000,
    data: {
      payStatus: '1',
    },
  };
  jest.spyOn(global.checkPay, 'then').mockResolvedValue(mockResponse);
  wrapper.setState({ visible: true, paymentId: '123' });
  await instance._checkPay();
  expect(spy).toHaveBeenCalledWith(mockResponse);
});

it('handles unsuccessful checkPay response', async () => {
  const instance = wrapper.instance();
  const spy = jest.spyOn(global.LToast, 'removeAll');
  const mockResponse = {
    code: 40000,
    desc: '服务器错误',
  };
  jest.spyOn(global.checkPay, 'then').mockResolvedValue(mockResponse);
  wrapper.setState({ visible: true, paymentId: '123' });
  await instance._checkPay();
  expect(spy).toHaveBeenCalled();
  expect(global.toast).toHaveBeenCalledWith('服务器开小差啦~');
});

it('handles checkPay error', async () => {
  const instance = wrapper.instance();
  const spy = jest.spyOn(global.LToast, 'removeAll');
  jest.spyOn(global.checkPay, 'then').mockRejectedValue(new Error('网络错误'));
  wrapper.setState({ visible: true, paymentId: '123' });
  await instance._checkPay();
  expect(spy).toHaveBeenCalled();
  expect(global.toast).toHaveBeenCalledWith('服务器开小差啦~');
});

});

describe('_handleBack method', () => { it('calls _handleOnClose', () => { const instance = wrapper.instance(); const spy = jest.spyOn(instance, '_handleOnClose'); instance._handleBack(); expect(spy).toHaveBeenCalled(); });

it('navigates to OrderDtlScreen if from is confirm', () => {
  const instance = wrapper.instance();
  const spy = jest.spyOn(instance.props.navigation, 'replace');
  wrapper.setProps({ from: 'confirm', data: { orderId: 123 } });
  instance._handleBack();
  expect(spy).toHaveBeenCalledWith('OrderDtlScreen', { orderID: 123 });
});

it('calls onRefresh if from is orderDtl', () => {
  const instance = wrapper.instance();
  const spy = jest.spyOn(instance.props, 'onRefresh');
  wrapper.setProps({ from: 'orderDtl' });
  instance._handleBack();
  expect(spy).toHaveBeenCalled();
});

it('calls track', () => {
  const instance = wrapper.instance();
  const spy = jest.spyOn(global.track, 'track');
  instance._handleBack();
  expect(spy).toHaveBeenCalledWith('$AppClick', 'PayScreen', { $element_id: 'click_cancel_pay' });
});

});

describe('_handleOnClose method', () => { it('calls onClose', () => { const instance = wrapper.instance(); const spy = jest.spyOn(instance.props, 'onClose'); instance._handleOnClose(); expect(spy).toHaveBeenCalled(); });

it('calls updateCallback if from is orderList', () => {
  const instance = wrapper.instance();
  const spy = jest.spyOn(instance.props, 'updateCallback');
  wrapper.setProps({ from: 'orderList', updateCallback: jest.fn() });
  instance._handleOnClose();
  expect(spy).toHaveBeenCalled();
});

it('stops timerText', () => {
  const instance = wrapper.instance();
  const spy = jest.spyOn(instance.timerText, 'stop');
  instance._handleOnClose();
  expect(spy).toHaveBeenCalled();
});

it('updates state correctly', () => {
  const instance = wrapper.instance();
  instance._handleOnClose();
  expect(wrapper.state('visible')).toBe(false);
  expect(wrapper.state('isLoading')).toBe(false);
  expect(wrapper.state('paying')).toBe(false);
});

});

describe('_fetchData method', () => { it('does nothing if data is null', () => { const instance = wrapper.instance(); const spy = jest.spyOn(global.getList, 'then'); instance._fetchData(); expect(spy).not.toHaveBeenCalled(); });

it('calls getList if data is not null', () => {
  const instance = wrapper.instance();
  const spy = jest.spyOn(global.getList, 'then');
  wrapper.setState({ data: { orderId: 123 } });
  instance._fetchData();
  expect(spy).toHaveBeenCalled();
});

it('handles successful getList response', async () => {
  const instance = wrapper.instance();
  const spy = jest.spyOn(instance, '_handleNetResult');
  const mockResponse = {
    code: 10000,
    data: { payStatus: '1' },
  };
  jest.spyOn(global.getList, 'then').mockResolvedValue(mockResponse);
  wrapper.setState({ data: { orderId: 123 } });
  await instance._fetchData();
  expect(spy).toHaveBeenCalledWith(mockResponse);
});

it('handles unsuccessful getList response', async () => {
  const instance = wrapper.instance();
  const spy = jest.spyOn(global.toast, 'toast');
  const mockResponse = {
    code: 40000,
    desc: '服务器错误',
  };
  jest.spyOn(global.getList, 'then').mockResolvedValue(mockResponse);
  wrapper.setState({ data: { orderId: 123 } });
  await instance._fetchData();
  expect(spy).toHaveBeenCalledWith('服务器开小差啦~');
});

it('handles getList error', async () => {
  const instance = wrapper.instance();
  const spy = jest.spyOn(global.toast, 'toast');
  jest.spyOn(global.getList, 'then').mockRejectedValue(new Error('网络错误'));
  wrapper.setState({ data: { orderId: 123 } });
  await instance._fetchData();
  expect(spy).toHaveBeenCalledWith('服务器开小差啦~');
});

});

describe('_handleNetResult method', () => { it('does nothing if isGoToSuccess is true', () => { const instance = wrapper.instance(); instance.isGoToSuccess = true; instance._handleNetResult({}); expect(wrapper.state('isLoading')).toBe(false); expect(wrapper.state('isNetError')).toBe(false); });

it('updates state correctly if data.payStatus is 1', () => {
  const instance = wrapper.instance();
  const data = { payStatus: '1' };
  instance._handleNetResult({ data: data });
  expect(wrapper.state('visible')).toBe(false);
  expect(wrapper.state('isLoading')).toBe(false);
  expect(wrapper.state('isNetError')).toBe(false);
  expect(wrapper.state('data')).toBe(data);
});

it('updates state correctly if data.payStatus is not 1', () => {
  const instance = wrapper.instance();
  const data = { payStatus: '2' };
  instance._handleNetResult({ data: data });
  expect(wrapper.state('isLoading')).toBe(false);
  expect(wrapper.state('isNetError')).toBe(false);
  expect(wrapper.state('data')).toBe(data);
  expect(wrapper.state('leftTime')).toBe(data.leftTime);
});

});

describe('_handlePayResult method', () => { it('calls doWhileGetPayResult if result is empty', () => { const instance = wrapper.instance(); const spy = jest.spyOn(instance, 'doWhileGetPayResult'); instance._handlePayResult({}); expect(spy).toHaveBeenCalled(); });

it('handles code 6001', () => {
  const instance = wrapper.instance();
  const spy = jest.spyOn(global.toast, 'toast');
  instance._handlePayResult({ code: 6001 });
  expect(spy).toHaveBeenCalledWith('取消支付');
});

it('handles code -1', () => {
  const instance = wrapper.instance();
  const spy = jest.spyOn(global.toast, 'toast');
  instance._handlePayResult({ code: -1 });
  expect(spy).toHaveBeenCalledWith('其他错误');
});

it('handles code 8000', () => {
  const instance = wrapper.instance();
  const spy = jest.spyOn(global.toast, 'toast');
  instance._handlePayResult({ code: 8000 });
  expect(spy).not.toHaveBeenCalled();
});

});

describe('_renderPayWayRow method', () => { it('renders correctly for Alipay', () => { const instance = wrapper.instance(); const result = instance._renderPayWayRow({ title: '支付宝', appId: 'alipayapp', index: 0 }); expect(result).toMatchSnapshot(); });

it('renders correctly for WeChat Pay', () => {
  const instance = wrapper.instance();
  const result = instance._renderPayWayRow({ title: '微信支付', appId: 'wxpayapp', index: 1 });
  expect(result).toMatchSnapshot();
});

});

describe('_doPayment method', () => { it('shows an alert if WeChat is not installed', async () => { const instance = wrapper.instance(); const spy = jest.spyOn(instance.alertModal, 'show'); jest.spyOn(global.Linking, 'canOpenURL').mockResolvedValue(false); wrapper.setState({ data: { orderId: 123, payMoney: 100 } }); await instance._doPayment({ appId: 'wxpayapp' }); expect(spy).toHaveBeenCalled(); });

it('calls doPayment if WeChat is installed', async () => {
  const instance = wrapper.instance();
  const spy = jest.spyOn(global.doPayment, 'then');
  jest.spyOn(global.Linking, 'canOpenURL').mockResolvedValue(true);
  wrapper.setState({ data: { orderId: 123, payMoney: 100 } });
  await instance._doPayment({ appId: 'wxpayapp' });
  expect(spy).toHaveBeenCalled();
});

it('handles successful doPayment response for Alipay', async () => {
  const instance = wrapper.instance();
  const spy = jest.spyOn(global.aliPay, 'then');
  jest.spyOn(global.Linking, 'canOpenURL').mockResolvedValue(true);
  const mockResponse = {
    code: 10000,
    data: {
      html: 'html content',
      paymentId: '123',
    },
  };
  jest.spyOn(global.doPayment, 'then').mockResolvedValue(mockResponse);
  wrapper.setState({ data: { orderId: 123, payMoney: 100 } });
  await instance._doPayment({ appId: 'alipayapp' });
  expect(spy).toHaveBeenCalled();
  expect(wrapper.state('paymentId')).toBe('123');
});

it('handles successful doPayment response for WeChat Pay', async () => {
  const instance = wrapper.instance();
  const spy = jest.spyOn(global.wxPay, 'then');
  jest.spyOn(global.Linking, 'canOpenURL').mockResolvedValue(true);
  const mockResponse = {
    code: 10000,
    data: {
      html: 'html content',
      paymentId: '123',
    },
  };
  jest.spyOn(global.doPayment, 'then').mockResolvedValue(mockResponse);
  wrapper.setState({ data: { orderId: 123, payMoney: 100 } });
  await instance._doPayment({ appId: 'wxpayapp' });
  expect(spy).toHaveBeenCalled();
  expect(wrapper.state('paymentId')).toBe('123');
});

it('handles unsuccessful doPayment response', async () => {
  const instance = wrapper.instance();
  const spy = jest.spyOn(global.toast, 'toast');
  const mockResponse = {
    code: 40000,
    desc: '服务器错误',
  };
  jest.spyOn(global.doPayment, 'then').mockResolvedValue(mockResponse);
  wrapper.setState({ data: { orderId: 123, payMoney: 100 } });
  await instance._doPayment({ appId: 'wxpayapp' });
  expect(spy).toHaveBeenCalledWith('服务器开小差啦~');
});

it('handles doPayment error', async () => {
  const instance = wrapper.instance();
  const spy = jest.spyOn(global.toast, 'toast');
  jest.spyOn(global.doPayment, 'then').mockRejectedValue(new Error('网络错误'));
  wrapper.setState({ data: { orderId: 123, payMoney: 100 } });
  await instance._doPayment({ appId: 'wxpayapp' });
  expect(spy).toHaveBeenCalledWith('网络错误');
});

}); });


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

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