2019-08-24 00:25:45 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { mount } from 'enzyme';
|
|
|
|
// eslint-disable-next-line import/no-unresolved
|
|
|
|
import Input from '..';
|
|
|
|
import focusTest from '../../../tests/shared/focusTest';
|
2019-08-26 22:53:20 +08:00
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
2020-01-02 19:10:16 +08:00
|
|
|
import rtlTest from '../../../tests/shared/rtlTest';
|
2019-12-31 14:41:09 +08:00
|
|
|
import { sleep } from '../../../tests/utils';
|
2020-06-16 19:49:08 +08:00
|
|
|
import Password from '../Password';
|
2019-08-24 00:25:45 +08:00
|
|
|
|
|
|
|
describe('Input.Password', () => {
|
2020-06-16 19:49:08 +08:00
|
|
|
focusTest(Input.Password, { refFocus: true });
|
2019-08-26 22:53:20 +08:00
|
|
|
mountTest(Input.Password);
|
2020-01-02 19:10:16 +08:00
|
|
|
rtlTest(Input.Password);
|
2019-08-24 00:25:45 +08:00
|
|
|
|
|
|
|
it('should get input element from ref', () => {
|
2020-06-16 19:49:08 +08:00
|
|
|
const ref = React.createRef();
|
|
|
|
const onSelect = jest.fn();
|
|
|
|
|
|
|
|
const wrapper = mount(<Input.Password onSelect={onSelect} ref={ref} />);
|
|
|
|
expect(ref.current.input instanceof HTMLInputElement).toBe(true);
|
|
|
|
wrapper.find('input').simulate('select');
|
|
|
|
expect(onSelect).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should support size', () => {
|
|
|
|
const wrapper = mount(<Password size="large" />);
|
2022-03-01 14:17:48 +08:00
|
|
|
expect(wrapper.find('.ant-input-affix-wrapper-lg')).toBeTruthy();
|
2020-06-16 19:49:08 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
2019-08-24 00:25:45 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should change type when click', () => {
|
|
|
|
const wrapper = mount(<Input.Password />);
|
|
|
|
wrapper.find('input').simulate('change', { target: { value: '111' } });
|
2020-02-17 19:12:42 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
2020-04-28 11:16:01 +08:00
|
|
|
wrapper.find('.ant-input-password-icon').at(0).simulate('click');
|
2020-02-17 19:12:42 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
2020-04-28 11:16:01 +08:00
|
|
|
wrapper.find('.ant-input-password-icon').at(0).simulate('click');
|
2020-02-17 19:12:42 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
2019-08-24 00:25:45 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('visibilityToggle should work', () => {
|
|
|
|
const wrapper = mount(<Input.Password visibilityToggle={false} />);
|
|
|
|
expect(wrapper.find('.anticon-eye').length).toBe(0);
|
|
|
|
wrapper.setProps({ visibilityToggle: true });
|
|
|
|
expect(wrapper.find('.anticon-eye-invisible').length).toBe(1);
|
|
|
|
});
|
|
|
|
|
2019-09-19 17:19:22 +08:00
|
|
|
it('should not toggle visibility when disabled prop is true', () => {
|
|
|
|
const wrapper = mount(<Input.Password disabled />);
|
|
|
|
expect(wrapper.find('.anticon-eye-invisible').length).toBe(1);
|
|
|
|
wrapper.find('.anticon-eye-invisible').simulate('click');
|
|
|
|
expect(wrapper.find('.anticon-eye').length).toBe(0);
|
|
|
|
});
|
|
|
|
|
2019-08-24 00:25:45 +08:00
|
|
|
it('should keep focus state', () => {
|
2020-02-17 19:12:42 +08:00
|
|
|
const wrapper = mount(<Input.Password defaultValue="111" autoFocus />, {
|
|
|
|
attachTo: document.body,
|
|
|
|
});
|
2020-04-28 11:16:01 +08:00
|
|
|
expect(document.activeElement).toBe(wrapper.find('input').at(0).getDOMNode());
|
2020-04-27 18:02:51 +08:00
|
|
|
document.activeElement.setSelectionRange(2, 2);
|
|
|
|
expect(document.activeElement.selectionStart).toBe(2);
|
2020-04-28 11:16:01 +08:00
|
|
|
wrapper.find('.ant-input-password-icon').at(0).simulate('mousedown');
|
|
|
|
wrapper.find('.ant-input-password-icon').at(0).simulate('mouseup');
|
|
|
|
wrapper.find('.ant-input-password-icon').at(0).simulate('click');
|
|
|
|
expect(document.activeElement).toBe(wrapper.find('input').at(0).getDOMNode());
|
2020-04-27 18:02:51 +08:00
|
|
|
expect(document.activeElement.selectionStart).toBe(2);
|
2020-02-04 17:57:34 +08:00
|
|
|
wrapper.unmount();
|
2019-08-24 00:25:45 +08:00
|
|
|
});
|
2019-12-31 14:41:09 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/20541
|
|
|
|
it('should not show value attribute in input element', async () => {
|
|
|
|
const wrapper = mount(<Input.Password />);
|
|
|
|
wrapper
|
|
|
|
.find('input')
|
|
|
|
.at('0')
|
|
|
|
.simulate('change', { target: { value: 'value' } });
|
|
|
|
await sleep();
|
2020-04-28 11:16:01 +08:00
|
|
|
expect(wrapper.find('input').at('0').getDOMNode().getAttribute('value')).toBeFalsy();
|
2019-12-31 14:41:09 +08:00
|
|
|
});
|
|
|
|
|
2020-05-28 14:49:03 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/24526
|
|
|
|
it('should not show value attribute in input element after blur it', async () => {
|
|
|
|
const wrapper = mount(<Input.Password />);
|
|
|
|
wrapper
|
|
|
|
.find('input')
|
|
|
|
.at('0')
|
|
|
|
.simulate('change', { target: { value: 'value' } });
|
|
|
|
await sleep();
|
|
|
|
expect(wrapper.find('input').at('0').getDOMNode().getAttribute('value')).toBeFalsy();
|
|
|
|
wrapper.find('input').at('0').simulate('blur');
|
|
|
|
await sleep();
|
|
|
|
expect(wrapper.find('input').at('0').getDOMNode().getAttribute('value')).toBeFalsy();
|
|
|
|
wrapper.find('input').at('0').simulate('focus');
|
|
|
|
await sleep();
|
|
|
|
expect(wrapper.find('input').at('0').getDOMNode().getAttribute('value')).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
2019-12-31 14:41:09 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/20541
|
|
|
|
it('could be unmount without errors', () => {
|
|
|
|
expect(() => {
|
|
|
|
const wrapper = mount(<Input.Password />);
|
|
|
|
wrapper
|
|
|
|
.find('input')
|
|
|
|
.at('0')
|
|
|
|
.simulate('change', { target: { value: 'value' } });
|
|
|
|
wrapper.unmount();
|
|
|
|
}).not.toThrow();
|
|
|
|
});
|
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/pull/20544#issuecomment-569861679
|
2020-06-17 10:58:24 +08:00
|
|
|
it('should not contain value attribute in input element with defaultValue', async () => {
|
2019-12-31 14:41:09 +08:00
|
|
|
const wrapper = mount(<Input.Password defaultValue="value" />);
|
|
|
|
await sleep();
|
2020-04-28 11:16:01 +08:00
|
|
|
expect(wrapper.find('input').at('0').getDOMNode().getAttribute('value')).toBeFalsy();
|
2019-12-31 14:41:09 +08:00
|
|
|
});
|
2019-08-24 00:25:45 +08:00
|
|
|
});
|