2019-02-19 11:42:05 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { mount } from 'enzyme';
|
2021-09-22 21:54:27 +08:00
|
|
|
import { SmileOutlined, LikeOutlined, HighlightOutlined, CheckOutlined } from '@ant-design/icons';
|
2019-02-19 11:42:05 +08:00
|
|
|
import KeyCode from 'rc-util/lib/KeyCode';
|
2021-06-16 15:52:05 +08:00
|
|
|
import { resetWarned } from 'rc-util/lib/warning';
|
2022-01-18 14:13:41 +08:00
|
|
|
import { spyElementPrototype } from 'rc-util/lib/test/domHook';
|
2019-02-19 11:42:05 +08:00
|
|
|
import copy from 'copy-to-clipboard';
|
|
|
|
import Title from '../Title';
|
2020-05-11 14:28:57 +08:00
|
|
|
import Link from '../Link';
|
2019-02-19 11:42:05 +08:00
|
|
|
import Paragraph from '../Paragraph';
|
2021-06-16 15:52:05 +08:00
|
|
|
import Text from '../Text';
|
2020-10-10 11:30:58 +08:00
|
|
|
import Base from '../Base';
|
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-10-01 14:06:09 +08:00
|
|
|
import Typography from '../Typography';
|
2022-04-18 21:02:11 +08:00
|
|
|
import { sleep, render } from '../../../tests/utils';
|
2019-02-19 11:42:05 +08:00
|
|
|
|
|
|
|
jest.mock('copy-to-clipboard');
|
|
|
|
|
|
|
|
describe('Typography', () => {
|
2019-08-26 22:53:20 +08:00
|
|
|
mountTest(Paragraph);
|
|
|
|
mountTest(Base);
|
|
|
|
mountTest(Title);
|
2020-05-11 14:28:57 +08:00
|
|
|
mountTest(Link);
|
2019-08-26 22:53:20 +08:00
|
|
|
|
2020-01-02 19:10:16 +08:00
|
|
|
rtlTest(Paragraph);
|
|
|
|
rtlTest(Base);
|
|
|
|
rtlTest(Title);
|
2020-05-11 14:28:57 +08:00
|
|
|
rtlTest(Link);
|
2020-01-02 19:10:16 +08:00
|
|
|
|
2019-02-19 11:42:05 +08:00
|
|
|
const LINE_STR_COUNT = 20;
|
|
|
|
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
|
|
|
|
|
|
// Mock offsetHeight
|
2021-06-01 10:11:24 +08:00
|
|
|
const originOffsetHeight = Object.getOwnPropertyDescriptor(
|
|
|
|
HTMLElement.prototype,
|
|
|
|
'offsetHeight',
|
|
|
|
).get;
|
2021-07-20 13:51:03 +08:00
|
|
|
|
|
|
|
const mockGetBoundingClientRect = jest.spyOn(HTMLElement.prototype, 'getBoundingClientRect');
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
Object.defineProperty(HTMLElement.prototype, 'offsetHeight', {
|
|
|
|
get() {
|
|
|
|
let html = this.innerHTML;
|
|
|
|
html = html.replace(/<[^>]*>/g, '');
|
|
|
|
const lines = Math.ceil(html.length / LINE_STR_COUNT);
|
|
|
|
return lines * 16;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
mockGetBoundingClientRect.mockImplementation(function fn() {
|
2019-02-19 11:42:05 +08:00
|
|
|
let html = this.innerHTML;
|
|
|
|
html = html.replace(/<[^>]*>/g, '');
|
|
|
|
const lines = Math.ceil(html.length / LINE_STR_COUNT);
|
2021-07-20 13:51:03 +08:00
|
|
|
return { height: lines * 16 };
|
|
|
|
});
|
2019-02-19 11:42:05 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// Mock getComputedStyle
|
|
|
|
const originGetComputedStyle = window.getComputedStyle;
|
|
|
|
window.getComputedStyle = ele => {
|
|
|
|
const style = originGetComputedStyle(ele);
|
|
|
|
style.lineHeight = '16px';
|
|
|
|
return style;
|
|
|
|
};
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
errorSpy.mockReset();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
errorSpy.mockRestore();
|
|
|
|
Object.defineProperty(HTMLElement.prototype, 'offsetHeight', {
|
|
|
|
get: originOffsetHeight,
|
|
|
|
});
|
2021-07-20 13:51:03 +08:00
|
|
|
mockGetBoundingClientRect.mockRestore();
|
2019-02-19 11:42:05 +08:00
|
|
|
window.getComputedStyle = originGetComputedStyle;
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Title', () => {
|
|
|
|
it('warning if `level` not correct', () => {
|
|
|
|
mount(<Title level={false} />);
|
|
|
|
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(errorSpy).toHaveBeenCalledWith(
|
2020-07-28 20:39:43 +08:00
|
|
|
'Warning: [antd: Typography.Title] Title only accept `1 | 2 | 3 | 4 | 5` as `level` value. And `5` need 4.6.0+ version.',
|
2019-02-19 11:42:05 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Base', () => {
|
|
|
|
describe('copyable', () => {
|
2022-04-25 09:49:48 +08:00
|
|
|
function copyTest(name, text, target, icon, tooltips, format) {
|
2020-08-18 16:14:14 +08:00
|
|
|
it(name, async () => {
|
2020-03-20 15:07:47 +08:00
|
|
|
jest.useFakeTimers();
|
2019-02-19 11:42:05 +08:00
|
|
|
const onCopy = jest.fn();
|
|
|
|
const wrapper = mount(
|
2022-04-25 09:49:48 +08:00
|
|
|
<Base component="p" copyable={{ text, onCopy, icon, tooltips, format }}>
|
2019-02-19 11:42:05 +08:00
|
|
|
test copy
|
|
|
|
</Base>,
|
|
|
|
);
|
|
|
|
|
2020-08-09 12:56:35 +08:00
|
|
|
if (icon) {
|
|
|
|
expect(wrapper.find('.anticon-smile').length).toBeTruthy();
|
|
|
|
} else {
|
|
|
|
expect(wrapper.find('.anticon-copy').length).toBeTruthy();
|
|
|
|
}
|
|
|
|
|
|
|
|
wrapper.find('.ant-typography-copy').first().simulate('mouseenter');
|
|
|
|
jest.runAllTimers();
|
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
if (tooltips === undefined || tooltips === true) {
|
|
|
|
expect(wrapper.find('.ant-tooltip-inner').text()).toBe('Copy');
|
|
|
|
} else if (tooltips === false) {
|
|
|
|
expect(wrapper.find('.ant-tooltip-inner').length).toBeFalsy();
|
2020-08-18 16:14:14 +08:00
|
|
|
} else if (tooltips[0] === '' && tooltips[1] === '') {
|
|
|
|
expect(wrapper.find('.ant-tooltip-inner').length).toBeFalsy();
|
|
|
|
} else if (tooltips[0] === '' && tooltips[1]) {
|
|
|
|
expect(wrapper.find('.ant-tooltip-inner').length).toBeFalsy();
|
|
|
|
} else if (tooltips[1] === '' && tooltips[0]) {
|
|
|
|
expect(wrapper.find('.ant-tooltip-inner').text()).toBe(tooltips[0]);
|
2020-08-09 12:56:35 +08:00
|
|
|
} else {
|
|
|
|
expect(wrapper.find('.ant-tooltip-inner').text()).toBe(tooltips[0]);
|
|
|
|
}
|
|
|
|
|
2020-04-21 10:13:57 +08:00
|
|
|
wrapper.find('.ant-typography-copy').first().simulate('click');
|
2020-08-18 16:14:14 +08:00
|
|
|
jest.useRealTimers();
|
|
|
|
wrapper.find('.ant-typography-copy').first().simulate('mouseenter');
|
|
|
|
// tooltips 为 ['', 'xxx'] 时,切换时需要延时 mousenEnterDelay 的时长
|
|
|
|
if (tooltips && tooltips[0] === '' && tooltips[1]) {
|
|
|
|
await sleep(150);
|
|
|
|
}
|
2019-02-19 11:42:05 +08:00
|
|
|
|
2020-08-18 16:14:14 +08:00
|
|
|
expect(copy.lastStr).toEqual(target);
|
2022-04-25 09:49:48 +08:00
|
|
|
expect(copy.lastOptions.format).toEqual(format);
|
2019-02-19 11:42:05 +08:00
|
|
|
wrapper.update();
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onCopy).toHaveBeenCalled();
|
2019-02-19 11:42:05 +08:00
|
|
|
|
2020-08-09 12:56:35 +08:00
|
|
|
let copiedIcon = '.anticon-check';
|
|
|
|
if (icon && icon.length > 1) {
|
|
|
|
copiedIcon = '.anticon-like';
|
|
|
|
} else {
|
|
|
|
copiedIcon = '.anticon-check';
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(wrapper.find(copiedIcon).length).toBeTruthy();
|
2020-08-18 16:14:14 +08:00
|
|
|
wrapper.find('.ant-typography-copy').first().simulate('mouseenter');
|
2020-08-09 12:56:35 +08:00
|
|
|
|
|
|
|
if (tooltips === undefined || tooltips === true) {
|
|
|
|
expect(wrapper.find('.ant-tooltip-inner').text()).toBe('Copied');
|
|
|
|
} else if (tooltips === false) {
|
|
|
|
expect(wrapper.find('.ant-tooltip-inner').length).toBeFalsy();
|
2020-08-18 16:14:14 +08:00
|
|
|
} else if (tooltips[0] === '' && tooltips[1] === '') {
|
|
|
|
expect(wrapper.find('.ant-tooltip-inner').length).toBeFalsy();
|
|
|
|
} else if (tooltips[0] === '' && tooltips[1]) {
|
|
|
|
expect(wrapper.find('.ant-tooltip-inner').text()).toBe(tooltips[1]);
|
|
|
|
} else if (tooltips[1] === '' && tooltips[0]) {
|
|
|
|
expect(wrapper.find('.ant-tooltip-inner').text()).toBe('');
|
2020-08-09 12:56:35 +08:00
|
|
|
} else {
|
|
|
|
expect(wrapper.find('.ant-tooltip-inner').text()).toBe(tooltips[1]);
|
|
|
|
}
|
2019-02-19 11:42:05 +08:00
|
|
|
|
2020-08-18 16:14:14 +08:00
|
|
|
jest.useFakeTimers();
|
2021-06-01 10:11:24 +08:00
|
|
|
wrapper.find('.ant-typography-copy').first().simulate('click');
|
2019-02-19 11:42:05 +08:00
|
|
|
jest.runAllTimers();
|
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
// Will set back when 3 seconds pass
|
2020-08-09 12:56:35 +08:00
|
|
|
expect(wrapper.find(copiedIcon).length).toBeFalsy();
|
2020-08-18 16:14:14 +08:00
|
|
|
wrapper.unmount();
|
2020-03-20 15:07:47 +08:00
|
|
|
jest.useRealTimers();
|
2019-02-19 11:42:05 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
copyTest('basic copy', undefined, 'test copy');
|
|
|
|
copyTest('customize copy', 'bamboo', 'bamboo');
|
2022-04-25 09:49:48 +08:00
|
|
|
copyTest(
|
|
|
|
'costomize copy with plain text',
|
|
|
|
'bamboo',
|
|
|
|
'bamboo',
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
'text/plain',
|
|
|
|
);
|
|
|
|
copyTest(
|
|
|
|
'costomize copy with html text',
|
|
|
|
'bamboo',
|
|
|
|
'bamboo',
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
'text/html',
|
|
|
|
);
|
2020-08-09 12:56:35 +08:00
|
|
|
copyTest('customize copy icon', 'bamboo', 'bamboo', <SmileOutlined />);
|
|
|
|
copyTest('customize copy icon by pass array', 'bamboo', 'bamboo', [
|
|
|
|
<SmileOutlined key="copy-icon" />,
|
|
|
|
]);
|
|
|
|
copyTest('customize copy icon and copied icon ', 'bamboo', 'bamboo', [
|
|
|
|
<SmileOutlined key="copy-icon" />,
|
|
|
|
<LikeOutlined key="copied-icon" />,
|
|
|
|
]);
|
|
|
|
copyTest('customize copy show tooltips', 'bamboo', 'bamboo', undefined, true);
|
|
|
|
copyTest('customize copy hide tooltips', 'bamboo', 'bamboo', undefined, false);
|
|
|
|
copyTest('customize copy tooltips text', 'bamboo', 'bamboo', undefined, [
|
|
|
|
'click here',
|
|
|
|
'you clicked!!',
|
|
|
|
]);
|
2020-08-18 16:14:14 +08:00
|
|
|
copyTest('tooltips contains two empty text', 'bamboo', 'bamboo', undefined, ['', '']);
|
|
|
|
copyTest('tooltips contains one empty text', 'bamboo', 'bamboo', undefined, [
|
|
|
|
'',
|
|
|
|
'you clicked!!',
|
|
|
|
]);
|
|
|
|
copyTest('tooltips contains one empty text 2', 'bamboo', 'bamboo', undefined, [
|
|
|
|
'click here',
|
|
|
|
'',
|
|
|
|
]);
|
2019-02-19 11:42:05 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('editable', () => {
|
2021-09-29 08:43:06 +08:00
|
|
|
function testStep(
|
2021-11-26 15:59:17 +08:00
|
|
|
{ name = '', icon, tooltip, triggerType, enterIcon },
|
2021-09-29 08:43:06 +08:00
|
|
|
submitFunc,
|
|
|
|
expectFunc,
|
|
|
|
) {
|
2019-02-19 11:42:05 +08:00
|
|
|
it(name, () => {
|
2020-08-09 12:56:35 +08:00
|
|
|
jest.useFakeTimers();
|
2019-02-19 11:42:05 +08:00
|
|
|
const onStart = jest.fn();
|
|
|
|
const onChange = jest.fn();
|
|
|
|
|
2019-04-25 17:42:59 +08:00
|
|
|
const className = 'test';
|
|
|
|
const style = {};
|
|
|
|
|
|
|
|
const wrapper = mount(
|
2020-08-09 12:56:35 +08:00
|
|
|
<Paragraph
|
2021-09-23 22:14:43 +08:00
|
|
|
editable={{ onChange, onStart, icon, tooltip, triggerType, enterIcon }}
|
2020-08-09 12:56:35 +08:00
|
|
|
className={className}
|
|
|
|
style={style}
|
|
|
|
>
|
2019-04-25 17:42:59 +08:00
|
|
|
Bamboo
|
|
|
|
</Paragraph>,
|
|
|
|
);
|
2019-02-19 11:42:05 +08:00
|
|
|
|
2021-09-23 22:14:43 +08:00
|
|
|
if (triggerType === undefined || triggerType.indexOf('icon') !== -1) {
|
|
|
|
if (icon) {
|
|
|
|
expect(wrapper.find('.anticon-highlight').length).toBeTruthy();
|
|
|
|
} else {
|
|
|
|
expect(wrapper.find('.anticon-edit').length).toBeTruthy();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (triggerType === undefined || triggerType.indexOf('text') === -1) {
|
|
|
|
wrapper.simulate('click');
|
|
|
|
expect(onStart).not.toHaveBeenCalled();
|
|
|
|
}
|
|
|
|
wrapper.find('.ant-typography-edit').first().simulate('mouseenter');
|
|
|
|
jest.runAllTimers();
|
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
if (tooltip === undefined || tooltip === true) {
|
|
|
|
expect(wrapper.find('.ant-tooltip-inner').text()).toBe('Edit');
|
|
|
|
} else if (tooltip === false) {
|
|
|
|
expect(wrapper.find('.ant-tooltip-inner').length).toBeFalsy();
|
|
|
|
} else {
|
|
|
|
expect(wrapper.find('.ant-tooltip-inner').text()).toBe(tooltip);
|
|
|
|
}
|
|
|
|
|
|
|
|
wrapper.find('.ant-typography-edit').first().simulate('click');
|
|
|
|
|
|
|
|
expect(onStart).toHaveBeenCalled();
|
|
|
|
if (triggerType !== undefined && triggerType.indexOf('text') !== -1) {
|
|
|
|
wrapper.find('textarea').simulate('keyDown', { keyCode: KeyCode.ESC });
|
|
|
|
wrapper.find('textarea').simulate('keyUp', { keyCode: KeyCode.ESC });
|
|
|
|
expect(onChange).not.toHaveBeenCalled();
|
|
|
|
}
|
2020-08-09 12:56:35 +08:00
|
|
|
}
|
|
|
|
|
2021-09-23 22:14:43 +08:00
|
|
|
if (triggerType !== undefined && triggerType.indexOf('text') !== -1) {
|
|
|
|
if (triggerType.indexOf('icon') === -1) {
|
|
|
|
expect(wrapper.find('.anticon-highlight').length).toBeFalsy();
|
|
|
|
expect(wrapper.find('.anticon-edit').length).toBeFalsy();
|
|
|
|
}
|
|
|
|
wrapper.simulate('click');
|
|
|
|
expect(onStart).toHaveBeenCalled();
|
2020-08-09 12:56:35 +08:00
|
|
|
}
|
|
|
|
|
2019-04-25 17:42:59 +08:00
|
|
|
// Should have className
|
2020-10-10 11:30:58 +08:00
|
|
|
const props = wrapper.find('div').first().props();
|
2019-04-25 17:42:59 +08:00
|
|
|
expect(props.style).toEqual(style);
|
|
|
|
expect(props.className.includes(className)).toBeTruthy();
|
|
|
|
|
2020-10-10 11:30:58 +08:00
|
|
|
wrapper.find('textarea').simulate('change', {
|
2019-02-19 11:42:05 +08:00
|
|
|
target: { value: 'Bamboo' },
|
|
|
|
});
|
|
|
|
|
2021-09-22 21:54:27 +08:00
|
|
|
if (enterIcon === undefined) {
|
|
|
|
expect(
|
|
|
|
wrapper.find('span.ant-typography-edit-content-confirm').first().props().className,
|
|
|
|
).toContain('anticon-enter');
|
|
|
|
} else if (enterIcon === null) {
|
|
|
|
expect(wrapper.find('span.ant-typography-edit-content-confirm').length).toBe(0);
|
|
|
|
} else {
|
|
|
|
expect(
|
|
|
|
wrapper.find('span.ant-typography-edit-content-confirm').first().props().className,
|
|
|
|
).not.toContain('anticon-enter');
|
|
|
|
}
|
|
|
|
|
2020-08-09 12:56:35 +08:00
|
|
|
if (submitFunc) {
|
|
|
|
submitFunc(wrapper);
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2019-02-19 11:42:05 +08:00
|
|
|
|
|
|
|
if (expectFunc) {
|
|
|
|
expectFunc(onChange);
|
|
|
|
} else {
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith('Bamboo');
|
2019-02-19 11:42:05 +08:00
|
|
|
expect(onChange).toHaveBeenCalledTimes(1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-09 12:56:35 +08:00
|
|
|
testStep({ name: 'by key up' }, wrapper => {
|
2019-02-19 11:42:05 +08:00
|
|
|
// Not trigger when inComposition
|
2020-10-10 11:30:58 +08:00
|
|
|
wrapper.find('textarea').simulate('compositionStart');
|
|
|
|
wrapper.find('textarea').simulate('keyDown', { keyCode: KeyCode.ENTER });
|
|
|
|
wrapper.find('textarea').simulate('compositionEnd');
|
|
|
|
wrapper.find('textarea').simulate('keyUp', { keyCode: KeyCode.ENTER });
|
2019-02-19 11:42:05 +08:00
|
|
|
|
|
|
|
// Now trigger
|
2020-10-10 11:30:58 +08:00
|
|
|
wrapper.find('textarea').simulate('keyDown', { keyCode: KeyCode.ENTER });
|
|
|
|
wrapper.find('textarea').simulate('keyUp', { keyCode: KeyCode.ENTER });
|
2019-02-19 11:42:05 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
testStep(
|
2020-08-09 12:56:35 +08:00
|
|
|
{ name: 'by esc key' },
|
2019-02-19 11:42:05 +08:00
|
|
|
wrapper => {
|
2020-10-10 11:30:58 +08:00
|
|
|
wrapper.find('textarea').simulate('keyDown', { keyCode: KeyCode.ESC });
|
|
|
|
wrapper.find('textarea').simulate('keyUp', { keyCode: KeyCode.ESC });
|
2019-02-19 11:42:05 +08:00
|
|
|
},
|
|
|
|
onChange => {
|
2020-04-03 15:02:11 +08:00
|
|
|
// eslint-disable-next-line jest/no-standalone-expect
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onChange).not.toHaveBeenCalled();
|
2019-02-19 11:42:05 +08:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2020-08-09 12:56:35 +08:00
|
|
|
testStep({ name: 'by blur' }, wrapper => {
|
2020-10-10 11:30:58 +08:00
|
|
|
wrapper.find('textarea').simulate('blur');
|
2019-02-19 11:42:05 +08:00
|
|
|
});
|
2020-08-09 12:56:35 +08:00
|
|
|
|
|
|
|
testStep({ name: 'customize edit icon', icon: <HighlightOutlined /> });
|
|
|
|
testStep({ name: 'customize edit show tooltip', tooltip: true });
|
|
|
|
testStep({ name: 'customize edit hide tooltip', tooltip: false });
|
|
|
|
testStep({ name: 'customize edit tooltip text', tooltip: 'click to edit text' });
|
2021-09-22 21:54:27 +08:00
|
|
|
testStep({ name: 'enter icon - default', enterIcon: undefined });
|
|
|
|
testStep({ name: 'enter icon - null', enterIcon: null });
|
|
|
|
testStep({ name: 'enter icon - custom', enterIcon: <CheckOutlined /> });
|
2020-12-26 21:58:35 +08:00
|
|
|
|
2021-09-23 22:14:43 +08:00
|
|
|
testStep({ name: 'trigger by icon', triggerType: ['icon'] });
|
|
|
|
testStep({ name: 'trigger by text', triggerType: ['text'] });
|
|
|
|
testStep({ name: 'trigger by both icon and text', triggerType: ['icon', 'text'] });
|
2020-12-26 21:58:35 +08:00
|
|
|
|
2021-03-13 23:46:32 +08:00
|
|
|
it('should trigger onEnd when type Enter', () => {
|
|
|
|
const onEnd = jest.fn();
|
|
|
|
const wrapper = mount(<Paragraph editable={{ onEnd }}>Bamboo</Paragraph>);
|
|
|
|
wrapper.find('.ant-typography-edit').first().simulate('click');
|
|
|
|
wrapper.find('textarea').simulate('keyDown', { keyCode: KeyCode.ENTER });
|
|
|
|
wrapper.find('textarea').simulate('keyUp', { keyCode: KeyCode.ENTER });
|
|
|
|
expect(onEnd).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should trigger onCancel when type ESC', () => {
|
|
|
|
const onCancel = jest.fn();
|
|
|
|
const wrapper = mount(<Paragraph editable={{ onCancel }}>Bamboo</Paragraph>);
|
|
|
|
wrapper.find('.ant-typography-edit').first().simulate('click');
|
|
|
|
wrapper.find('textarea').simulate('keyDown', { keyCode: KeyCode.ESC });
|
|
|
|
wrapper.find('textarea').simulate('keyUp', { keyCode: KeyCode.ESC });
|
|
|
|
expect(onCancel).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
2020-12-26 21:58:35 +08:00
|
|
|
it('should only trigger focus on the first time', () => {
|
|
|
|
let triggerTimes = 0;
|
|
|
|
const mockFocus = spyElementPrototype(HTMLElement, 'focus', () => {
|
|
|
|
triggerTimes += 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
const wrapper = mount(<Paragraph editable>Bamboo</Paragraph>);
|
|
|
|
|
|
|
|
wrapper.find('.ant-typography-edit').first().simulate('click');
|
|
|
|
expect(triggerTimes).toEqual(1);
|
|
|
|
|
|
|
|
wrapper.find('textarea').simulate('change', {
|
|
|
|
target: { value: 'good' },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(triggerTimes).toEqual(1);
|
|
|
|
|
|
|
|
mockFocus.mockRestore();
|
|
|
|
});
|
2019-02-19 11:42:05 +08:00
|
|
|
});
|
2020-02-07 13:15:24 +08:00
|
|
|
|
|
|
|
it('should focus at the end of textarea', () => {
|
|
|
|
const wrapper = mount(<Paragraph editable>content</Paragraph>);
|
2020-04-21 10:13:57 +08:00
|
|
|
wrapper.find('.ant-typography-edit').first().simulate('click');
|
2020-02-07 13:15:24 +08:00
|
|
|
const textareaNode = wrapper.find('textarea').getDOMNode();
|
|
|
|
expect(textareaNode.selectionStart).toBe(7);
|
|
|
|
expect(textareaNode.selectionEnd).toBe(7);
|
|
|
|
});
|
2019-02-19 11:42:05 +08:00
|
|
|
});
|
2019-10-01 14:06:09 +08:00
|
|
|
|
|
|
|
it('warning if use setContentRef', () => {
|
2021-08-30 13:04:21 +08:00
|
|
|
const refFunc = () => {};
|
2019-10-01 14:06:09 +08:00
|
|
|
mount(<Typography setContentRef={refFunc} />);
|
|
|
|
expect(errorSpy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [antd: Typography] `setContentRef` is deprecated. Please use `ref` instead.',
|
|
|
|
);
|
|
|
|
});
|
2021-06-16 15:52:05 +08:00
|
|
|
|
|
|
|
it('no italic warning', () => {
|
|
|
|
resetWarned();
|
2022-04-06 11:07:15 +08:00
|
|
|
render(<Text italic>Little</Text>);
|
2021-06-16 15:52:05 +08:00
|
|
|
|
|
|
|
expect(errorSpy).not.toHaveBeenCalled();
|
|
|
|
});
|
2022-04-05 13:02:32 +08:00
|
|
|
|
|
|
|
it('should get HTMLHeadingElement ref from Title', () => {
|
|
|
|
const ref = React.createRef();
|
|
|
|
|
|
|
|
mount(<Title level={1} ref={ref} />);
|
|
|
|
expect(ref.current instanceof HTMLHeadingElement).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should get HTMLDivElement ref from Paragraph', () => {
|
|
|
|
const ref = React.createRef();
|
|
|
|
|
|
|
|
mount(<Paragraph ref={ref} />);
|
|
|
|
expect(ref.current instanceof HTMLDivElement).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should get HTMLSpanElement ref from Text', () => {
|
|
|
|
const ref = React.createRef();
|
|
|
|
|
|
|
|
mount(<Text ref={ref} />);
|
|
|
|
expect(ref.current instanceof HTMLSpanElement).toBe(true);
|
|
|
|
});
|
2019-02-19 11:42:05 +08:00
|
|
|
});
|