// @ts-ignore import { configure, render, shallow } from 'enzyme'; // @ts-ignore import Adapter from 'enzyme-adapter-react-16'; import * as React from 'react'; import { Text } from 'react-native'; import renderer from 'react-test-renderer'; import MDIcon from '../../icon/index'; import MDInputItem from '../index'; configure({ adapter: new Adapter() }); it('renders correctly with defaults', () => { const component = renderer.create().toJSON(); expect(component).toMatchSnapshot(); }); it('renders correctly normal input', () => { const component = renderer .create( ) .toJSON(); expect(component).toMatchSnapshot(); }); it('renders correctly with disabled input item', () => { const component = renderer .create() .toJSON(); expect(component).toMatchSnapshot(); }); it('renders correctly with disabled input item focus function', () => { const component = shallow( ); expect(component.find('MDTextInput')).toHaveLength(0); expect(component.state('value')).toEqual('禁用表单'); }); it('renders correctly with readonly input item', () => { const component = renderer .create() .toJSON(); expect(component).toMatchSnapshot(); }); it('renders correctly with readonly input item focus function', () => { const component = shallow( ); expect(component.find('MDTextInput')).toHaveLength(0); expect(component.state('value')).toEqual('只读表单'); }); it('renders correctly with highlight input item', () => { const component = renderer .create( ) .toJSON(); expect(component).toMatchSnapshot(); }); it('renders correctly with input item highlight function', () => { const component = shallow( ); const textInput = component.find('MDTextInput'); expect(textInput).toHaveLength(1); textInput.simulate('focus'); expect(component.state('highlight')).toBe(true); }); it('renders correctly with input item align center', () => { const component = renderer .create( ) .toJSON(); expect(component).toMatchSnapshot(); }); it('renders correctly with input item align center', () => { const component = shallow( ); const style = component .find('MDTextInput') .shallow() .props().style as any; expect(style[1]).toHaveProperty('textAlign', 'center'); }); it('renders correctly with input item align right', () => { const component = shallow( ); const style = component .find('MDTextInput') .shallow() .props().style as any; expect(style[1]).toHaveProperty('textAlign', 'right'); }); it('renders correctly with input item default align', () => { const component = shallow( ); const style = component .find('MDTextInput') .shallow() .props().style as any; expect(style[1]).toHaveProperty('textAlign', 'left'); }); it('renders correctly with different input item type', () => { const types = ['bank-card', 'password', 'phone', 'money', 'digit', 'text']; for (const type of types) { const component = renderer .create( ) .toJSON(); expect(component).toMatchSnapshot(); } }); it('renders correctly with text input item', () => { const component = shallow( ); component.find('MDTextInput').simulate('changeText', '清泉石上流'); expect(component.state('value')).toEqual('清泉石上流'); }); it('renders bank-card correctly', () => { const component = shallow( ); component.find('MDTextInput').simulate('changeText', '6225230137797347'); expect(component.state('value')).toEqual('6225 2301 3779 7347'); component.find('MDTextInput').simulate('changeText', ''); expect(component.state('value')).toEqual(''); }); it('renders correctly with phone input item', () => { const component = shallow( ); component.find('MDTextInput').simulate('changeText', '13512345678'); expect(component.state('value')).toEqual('135 1234 5678'); }); it('renders correctly with money input item', () => { const component = shallow( ); component.find('MDTextInput').simulate('changeText', '12345678.1234'); expect(component.state('value')).toEqual('12,345,678.1234'); }); it('renders correctly with password input item', () => { const component = shallow( ); component.find('MDTextInput').simulate('changeText', '12345678'); expect(component.find('MDTextInput').prop('secureTextEntry')).toBe(true); }); it('renders correctly with password digit item', () => { const component = shallow( ); component.find('MDTextInput').simulate('changeText', '12345678'); expect(component.state('value')).toEqual('12345678'); }); it('render correcttly with left and right slot', () => { const component = renderer .create( } right={} /> ) .toJSON(); expect(component).toMatchSnapshot(); }); it('render with financial number keyboard', () => { const component = renderer .create( ) .toJSON(); expect(component).toMatchSnapshot(); }); it('render with clear button', () => { const component = renderer .create( ) .toJSON(); expect(component).toMatchSnapshot(); }); it('render with Material input', () => { const component = renderer.create( ); expect(component.toJSON()).toMatchSnapshot(); }); it('render Material input with text', () => { const component = renderer .create( ) .toJSON(); expect(component).toMatchSnapshot(); }); it('render correctly with error message', () => { const component = renderer .create( ) .toJSON(); expect(component).toMatchSnapshot(); }); it('render correctly with error slot', () => { const component = shallow( 查看支持银行列表} /> ); component.setProps({ error: '不支持当前银行' }); const errorSlot = component .find('Text') .findWhere((w) => w.text() === '查看支持银行列表') .first(); expect(errorSlot).toHaveLength(1); }); it('render correctly with large size', () => { const component = shallow( ); const input = component.find('MDTextInput'); input.simulate('changeText', '123123217392187389'); component.setProps({ size: 'large' }); const style = input.shallow().props().style as any; expect(style[2]).toHaveProperty('fontSize'); }); it('render correctly with android large size', () => { jest.mock('Platform', () => { return { OS: 'android' }; }); const component = shallow( ); const input = component.find('MDTextInput'); input.simulate('changeText', '123123217392187389'); component.setProps({ size: 'large' }); const style = input.shallow().props().style as any; expect(style[2]).toHaveProperty('fontSize'); }); it('test callback', () => { const onFocusCallback = jest.fn(); const onBlurCallback = jest.fn(); const onChangeTextCallback = jest.fn(); const component = shallow( ); component.find('MDTextInput').simulate('focus'); expect(onFocusCallback.mock.calls.length).toBe(1); component.find('MDTextInput').simulate('blur'); expect(onBlurCallback.mock.calls.length).toBe(1); component.find('MDTextInput').simulate('changeText', '12321'); expect(onChangeTextCallback.mock.calls[0][0]).toEqual('12321'); }); it('test null callback', () => { const component = shallow( ); component.find('MDTextInput').simulate('changeText', '12321'); expect(component.state('value')).toEqual('12321'); }); it('render correctly with max length', () => { const component = shallow( ); component.find('MDTextInput').simulate('changeText', '123211'); expect(component.state('value')).toEqual('12321'); });