import { configure, mount, shallow } from 'enzyme'; import Adapter from 'enzyme-adapter-react-16'; import * as React from 'react'; import { Text, TouchableWithoutFeedback, View } from 'react-native'; import renderer from 'react-test-renderer'; import MDButton from '../../button'; import MDCaptcha from '../../captcha'; import MDIcon from '../../icon'; import MDNoticeBar from '../../notice-bar'; import MDCashierChannel from '../cashier-channel'; import MDCashier from '../index'; configure({ adapter: new Adapter() }); const channels = [ { icon: , text: '招商银行(0056)', value: '001', }, { icon: , text: '支付宝支付', value: '002', }, { icon: , text: '微信支付', value: '003', }, { icon: , text: '工商银行', value: '004', }, { icon: , text: '农业银行', value: '005', }, ]; function renderChannel (channel: any, index: number) { return ( {channel.icon} {channel.text} ); } it('renders correctly with defaults', () => { const component = renderer .create( { return ; }} /> ) .toJSON(); expect(component).toMatchSnapshot(); }); it('renders correctly with default cashier', () => { const component = renderer.create().toJSON(); expect(component).toMatchSnapshot(); }); it('render correctly with visiable cashier', () => { const component = renderer.create().toJSON(); expect(component).toMatchSnapshot(); }); it('renders correctly with channels', () => { const component = renderer .create() .toJSON(); expect(component).toMatchSnapshot(); }); it('renders correctly with custom renderChannels', () => { const component = renderer .create() .toJSON(); expect(component).toMatchSnapshot(); }); it('renders correctly with paymentAmount', () => { const component1 = renderer .create() .toJSON(); expect(component1).toMatchSnapshot(); const component2 = renderer.create().toJSON(); expect(component2).toMatchSnapshot(); }); it('render cashier correctly with channelLimit', () => { const component = renderer.create().toJSON(); expect(component).toMatchSnapshot(); }); it('render cashier correctly with header slot', () => { const component = renderer .create( 该银行3:00-12:00系统维护,请更换其他银行卡 } /> ) .toJSON(); expect(component).toMatchSnapshot(); }); it('test onShow and on dismiss function', (done) => { const mockOnShow = jest.fn(); const mockOnDismiss = jest.fn(); const component = shallow( ); component.setProps({ visible: true }); setTimeout(() => { expect(mockOnShow.mock.calls.length).toEqual(1); done(); }, 300); component.setProps({ visible: false }); setTimeout(() => { expect(mockOnDismiss.mock.calls.length).toEqual(1); done(); }, 300); }); it('test onPay and onSelect function', () => { const mockOnPay = jest.fn(); const mockOnSelect = jest.fn(); const component = shallow( 测试scene} /> ); const cashier = component.find('MDCashierChannel').shallow(); cashier .find('TouchableWithoutFeedback') .at(0) .simulate('press'); expect(mockOnSelect.mock.calls.length).toEqual(1); component.find('MDButton').simulate('press'); expect(mockOnPay.mock.calls.length).toEqual(1); expect(component.state('loading')).toBe(true); }); it('test next function', () => { const component = shallow(); const instance: any = component.instance(); instance.next('loading'); expect(component.state('scene')).toEqual('loading'); // todo expect 1 // expect(component.find('MDActivityIndicator')).toHaveLength(0); instance.next('captcha', { text: 'Verification code sent to 156 **** 8965', brief: 'The latest verification code is still valid', autoCountdown: false, countNormalText: 'Send Verification code', countActiveText: 'Retransmission after {$1}s', }); expect(component.find('MDCaptcha')).toHaveLength(1); const mockSuccessHandler = jest.fn(); instance.next('success', { buttonText: '成功', handler: mockSuccessHandler }); expect(component.find('MDIcon')).toHaveLength(1); const successButton = component.findWhere((w) => { return w.type() === MDButton && w.props().children === '成功'; }); expect(successButton).toHaveLength(1); successButton.simulate('press'); expect(mockSuccessHandler.mock.calls.length).toEqual(1); expect(component.state('isPopupShow')).toBe(false); instance.next('fail', { buttonText: '失败' }); expect(component.find('MDIcon')).toHaveLength(1); expect( component.findWhere((w) => { return w.type() === MDButton && w.props().children === '失败'; }) ).toHaveLength(1); instance.next('choose'); expect(component.find('MDCashierChannel')).toHaveLength(1); instance.next('custom'); // todo expect 1 expect(component.findWhere((w) => w.text() === '测试scene')).toHaveLength(0); instance.next('test'); expect(component.find('MDCashierChannel')).toHaveLength(1); }); it('test title bar action', () => { const component = shallow(); component .find('MDPopupTitleBar') .shallow() .find('TouchableWithoutFeedback') .simulate('press'); expect(component.state('isPopupShow')).toBe(false); }); it('test null function', () => { const component = shallow( ); component.setProps({ visible: true }); expect(component.state('isPopupShow')).toBe(true); component.setProps({ visible: false }); expect(component.state('isPopupShow')).toBe(false); const cashier = component.find('MDCashierChannel').shallow(); cashier .find('TouchableWithoutFeedback') .at(0) .simulate('press'); component.find('MDButton').simulate('press'); }); it('test public property', () => { const component = mount(); const instance: any = component.instance(); expect(instance.captcha).toBeNull(); instance.next('captcha', { text: 'Verification code sent to 156 **** 8965', brief: 'The latest verification code is still valid', autoCountdown: false, countNormalText: 'Send Verification code', countActiveText: 'Retransmission after {$1}s', }); expect(instance.captcha).toBeInstanceOf(MDCaptcha); });