import React from 'react';
import { mount } from 'enzyme';
import RcTextArea from 'rc-textarea';
import Input from '..';
import focusTest from '../../../tests/shared/focusTest';
import { sleep } from '../../../tests/utils';
const { TextArea } = Input;
focusTest(TextArea, { refFocus: true });
describe('TextArea', () => {
const originalGetComputedStyle = window.getComputedStyle;
beforeAll(() => {
Object.defineProperty(window, 'getComputedStyle', {
value: node => ({
getPropertyValue: prop => {
if (prop === 'box-sizing') {
return originalGetComputedStyle(node)[prop] || 'border-box';
}
return originalGetComputedStyle(node)[prop];
},
}),
});
});
afterAll(() => {
Object.defineProperty(window, 'getComputedStyle', {
value: originalGetComputedStyle,
});
});
it('should auto calculate height according to content length', async () => {
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
const ref = React.createRef();
const wrapper = mount(
,
);
const mockFunc = jest.spyOn(ref.current.resizableTextArea, 'resizeTextarea');
wrapper.setProps({ value: '1111\n2222\n3333' });
await sleep(0);
expect(mockFunc).toHaveBeenCalledTimes(1);
wrapper.setProps({ value: '1111' });
await sleep(0);
expect(mockFunc).toHaveBeenCalledTimes(2);
wrapper.update();
expect(wrapper.find('textarea').props().style.overflow).toBeFalsy();
expect(errorSpy).not.toHaveBeenCalled();
errorSpy.mockRestore();
});
it('should support onPressEnter and onKeyDown', () => {
const fakeHandleKeyDown = jest.fn();
const fakeHandlePressEnter = jest.fn();
const wrapper = mount(
,
);
/** KeyCode 65 is A */
wrapper.find('textarea').simulate('keydown', { keyCode: 65 });
expect(fakeHandleKeyDown).toHaveBeenCalledTimes(1);
expect(fakeHandlePressEnter).toHaveBeenCalledTimes(0);
/** KeyCode 13 is Enter */
wrapper.find('textarea').simulate('keydown', { keyCode: 13 });
expect(fakeHandleKeyDown).toHaveBeenCalledTimes(2);
expect(fakeHandlePressEnter).toHaveBeenCalledTimes(1);
});
it('should support disabled', () => {
const wrapper = mount();
expect(wrapper.render()).toMatchSnapshot();
});
describe('maxLength', () => {
it('should support maxLength', () => {
const wrapper = mount();
expect(wrapper.render()).toMatchSnapshot();
});
it('maxLength should not block control', () => {
const wrapper = mount();
expect(wrapper.find('textarea').props().value).toEqual('light');
});
it('should exceed maxLength when use IME', () => {
const onChange = jest.fn();
const wrapper = mount();
wrapper.find('textarea').simulate('compositionStart');
wrapper.find('textarea').simulate('change', { target: { value: 'zhu' } });
wrapper.find('textarea').simulate('compositionEnd', { currentTarget: { value: '竹' } });
wrapper.find('textarea').simulate('change', { target: { value: '竹' } });
expect(onChange).toHaveBeenLastCalledWith(
expect.objectContaining({ target: expect.objectContaining({ value: '竹' }) }),
);
});
});
it('when prop value not in this.props, resizeTextarea should be called', async () => {
const ref = React.createRef();
const wrapper = mount();
const resizeTextarea = jest.spyOn(ref.current.resizableTextArea, 'resizeTextarea');
wrapper.find('textarea').simulate('change', {
target: {
value: 'test',
},
});
expect(resizeTextarea).toHaveBeenCalled();
});
it('handleKeyDown', () => {
const onPressEnter = jest.fn();
const onKeyDown = jest.fn();
const wrapper = mount(
,
);
wrapper.find(RcTextArea).instance().handleKeyDown({ keyCode: 13 });
expect(onPressEnter).toHaveBeenCalled();
expect(onKeyDown).toHaveBeenCalled();
});
it('should trigger onResize', async () => {
const onResize = jest.fn();
const wrapper = mount();
await sleep(100);
wrapper
.find('ResizeObserver')
.instance()
.onResize([
{
target: {
getBoundingClientRect() {
return {};
},
},
},
]);
await Promise.resolve();
expect(onResize).toHaveBeenCalledWith(
expect.objectContaining({
width: expect.any(Number),
height: expect.any(Number),
}),
);
});
it('should works same as Input', async () => {
const input = mount();
const textarea = mount();
input.setProps({ value: undefined });
textarea.setProps({ value: undefined });
expect(textarea.find('textarea').at(0).getDOMNode().value).toBe(input.getDOMNode().value);
});
describe('should support showCount', () => {
it('maxLength', () => {
const wrapper = mount();
const textarea = wrapper.find('.ant-input-textarea');
expect(wrapper.find('textarea').prop('value')).toBe('12345');
expect(textarea.prop('data-count')).toBe('5 / 5');
});
it('control exceed maxLength', () => {
const wrapper = mount();
const textarea = wrapper.find('.ant-input-textarea');
expect(wrapper.find('textarea').prop('value')).toBe('12345678');
expect(textarea.prop('data-count')).toBe('8 / 5');
});
describe('emoji', () => {
it('should minimize value between emoji length and maxLength', () => {
const wrapper = mount();
const textarea = wrapper.find('.ant-input-textarea');
expect(wrapper.find('textarea').prop('value')).toBe('👀');
expect(textarea.prop('data-count')).toBe('1 / 1');
// fix: 当 maxLength 长度为 2 的时候,输入 emoji 之后 showCount 会显示 1/2,但是不能再输入了
// zombieJ: 逻辑统一了,emoji 现在也可以正确计数了
const wrapper1 = mount();
const textarea1 = wrapper1.find('.ant-input-textarea');
expect(textarea1.prop('data-count')).toBe('1 / 2');
});
it('defaultValue should slice', () => {
const wrapper = mount();
expect(wrapper.find('textarea').prop('value')).toBe('🧐');
});
// 修改TextArea value截取规则后新增单测
it('slice emoji', () => {
const wrapper = mount();
const textarea = wrapper.find('.ant-input-textarea');
expect(wrapper.find('textarea').prop('value')).toBe('1234😂');
expect(textarea.prop('data-count')).toBe('5 / 5');
});
});
it('className & style patch to outer', () => {
const wrapper = mount(
,
);
// Outer
expect(wrapper.find('div').first().hasClass('bamboo')).toBeTruthy();
expect(wrapper.find('div').first().props().style.background).toEqual('red');
// Inner
expect(wrapper.find('.ant-input').hasClass('bamboo')).toBeFalsy();
expect(wrapper.find('.ant-input').props().style.background).toBeFalsy();
});
it('count formatter', () => {
const wrapper = mount(