2019-02-19 11:42:05 +08:00
|
|
|
import React from 'react';
|
2022-06-09 18:09:43 +08:00
|
|
|
import { CheckOutlined, HighlightOutlined, LikeOutlined, SmileOutlined } 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';
|
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-06-09 18:09:43 +08:00
|
|
|
import { fireEvent, render, sleep, waitFor } 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', () => {
|
2022-06-09 18:09:43 +08:00
|
|
|
render(<Title level={false} />);
|
2019-02-19 11:42:05 +08:00
|
|
|
|
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();
|
2022-06-09 18:09:43 +08:00
|
|
|
const { container: wrapper, unmount } = render(
|
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) {
|
2022-06-09 18:09:43 +08:00
|
|
|
expect(wrapper.querySelectorAll('.anticon-smile').length).toBeGreaterThan(0);
|
2020-08-09 12:56:35 +08:00
|
|
|
} else {
|
2022-06-09 18:09:43 +08:00
|
|
|
expect(wrapper.querySelectorAll('.anticon-copy').length).toBeGreaterThan(0);
|
2020-08-09 12:56:35 +08:00
|
|
|
}
|
|
|
|
|
2022-06-09 18:09:43 +08:00
|
|
|
fireEvent.mouseEnter(wrapper.querySelector('.ant-typography-copy'));
|
2020-08-09 12:56:35 +08:00
|
|
|
jest.runAllTimers();
|
|
|
|
|
|
|
|
if (tooltips === undefined || tooltips === true) {
|
2022-06-09 18:09:43 +08:00
|
|
|
await waitFor(() => {
|
|
|
|
expect(wrapper.querySelector('.ant-tooltip-inner').textContent).toBe('Copy');
|
|
|
|
});
|
2020-08-09 12:56:35 +08:00
|
|
|
} else if (tooltips === false) {
|
2022-06-09 18:09:43 +08:00
|
|
|
await waitFor(() => {
|
|
|
|
expect(wrapper.querySelectorAll('.ant-tooltip-inner').length).toBe(0);
|
|
|
|
});
|
2020-08-18 16:14:14 +08:00
|
|
|
} else if (tooltips[0] === '' && tooltips[1] === '') {
|
2022-06-09 18:09:43 +08:00
|
|
|
await waitFor(() => {
|
|
|
|
expect(wrapper.querySelectorAll('.ant-tooltip-inner').length).toBe(0);
|
|
|
|
});
|
2020-08-18 16:14:14 +08:00
|
|
|
} else if (tooltips[0] === '' && tooltips[1]) {
|
2022-06-09 18:09:43 +08:00
|
|
|
await waitFor(() => {
|
|
|
|
expect(wrapper.querySelectorAll('.ant-tooltip-inner').length).toBe(0);
|
|
|
|
});
|
2020-08-18 16:14:14 +08:00
|
|
|
} else if (tooltips[1] === '' && tooltips[0]) {
|
2022-06-09 18:09:43 +08:00
|
|
|
await waitFor(() => {
|
|
|
|
expect(wrapper.querySelector('.ant-tooltip-inner').textContent).toBe(tooltips[0]);
|
|
|
|
});
|
2020-08-09 12:56:35 +08:00
|
|
|
} else {
|
2022-06-09 18:09:43 +08:00
|
|
|
await waitFor(() => {
|
|
|
|
expect(wrapper.querySelector('.ant-tooltip-inner').textContent).toBe(tooltips[0]);
|
|
|
|
});
|
2020-08-09 12:56:35 +08:00
|
|
|
}
|
|
|
|
|
2022-06-09 18:09:43 +08:00
|
|
|
fireEvent.click(wrapper.querySelector('.ant-typography-copy'));
|
2020-08-18 16:14:14 +08:00
|
|
|
jest.useRealTimers();
|
2022-06-09 18:09:43 +08:00
|
|
|
fireEvent.mouseEnter(wrapper.querySelectorAll('.ant-typography-copy')[0]);
|
|
|
|
// tooltips 为 ['', 'xxx'] 时,切换时需要延时 mouseEnterDelay 的时长
|
2020-08-18 16:14:14 +08:00
|
|
|
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-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';
|
|
|
|
}
|
|
|
|
|
2022-06-09 18:09:43 +08:00
|
|
|
expect(wrapper.querySelectorAll(copiedIcon).length).toBeGreaterThan(0);
|
|
|
|
fireEvent.mouseEnter(wrapper.querySelectorAll('.ant-typography-copy')[0]);
|
2020-08-09 12:56:35 +08:00
|
|
|
|
|
|
|
if (tooltips === undefined || tooltips === true) {
|
2022-06-09 18:09:43 +08:00
|
|
|
await waitFor(() => {
|
|
|
|
expect(wrapper.querySelector('.ant-tooltip-inner').textContent).toBe('Copied');
|
|
|
|
});
|
2020-08-09 12:56:35 +08:00
|
|
|
} else if (tooltips === false) {
|
2022-06-09 18:09:43 +08:00
|
|
|
await waitFor(() => {
|
|
|
|
expect(wrapper.querySelectorAll('.ant-tooltip-inner').length).toBe(0);
|
|
|
|
});
|
2020-08-18 16:14:14 +08:00
|
|
|
} else if (tooltips[0] === '' && tooltips[1] === '') {
|
2022-06-09 18:09:43 +08:00
|
|
|
await waitFor(() => {
|
|
|
|
expect(wrapper.querySelectorAll('.ant-tooltip-inner').length).toBe(0);
|
|
|
|
});
|
2020-08-18 16:14:14 +08:00
|
|
|
} else if (tooltips[0] === '' && tooltips[1]) {
|
2022-06-09 18:09:43 +08:00
|
|
|
await waitFor(() => {
|
|
|
|
expect(wrapper.querySelector('.ant-tooltip-inner').textContent).toBe(tooltips[1]);
|
|
|
|
});
|
2020-08-18 16:14:14 +08:00
|
|
|
} else if (tooltips[1] === '' && tooltips[0]) {
|
2022-06-09 18:09:43 +08:00
|
|
|
await waitFor(() => {
|
|
|
|
expect(wrapper.querySelector('.ant-tooltip-inner').textContent).toBe('');
|
|
|
|
});
|
2020-08-09 12:56:35 +08:00
|
|
|
} else {
|
2022-06-09 18:09:43 +08:00
|
|
|
await waitFor(() => {
|
|
|
|
expect(wrapper.querySelector('.ant-tooltip-inner').textContent).toBe(tooltips[1]);
|
|
|
|
});
|
2020-08-09 12:56:35 +08:00
|
|
|
}
|
2019-02-19 11:42:05 +08:00
|
|
|
|
2020-08-18 16:14:14 +08:00
|
|
|
jest.useFakeTimers();
|
2022-06-09 18:09:43 +08:00
|
|
|
fireEvent.click(wrapper.querySelectorAll('.ant-typography-copy')[0]);
|
2019-02-19 11:42:05 +08:00
|
|
|
jest.runAllTimers();
|
|
|
|
|
|
|
|
// Will set back when 3 seconds pass
|
2022-06-09 18:09:43 +08:00
|
|
|
await sleep(3000);
|
|
|
|
expect(wrapper.querySelectorAll(copiedIcon).length).toBe(0);
|
|
|
|
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,
|
|
|
|
) {
|
2022-06-09 18:09:43 +08:00
|
|
|
it(name, async () => {
|
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';
|
2022-06-09 18:09:43 +08:00
|
|
|
const style = { padding: 'unset' };
|
2019-04-25 17:42:59 +08:00
|
|
|
|
2022-06-09 18:09:43 +08:00
|
|
|
const { container: wrapper } = render(
|
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) {
|
2022-06-09 18:09:43 +08:00
|
|
|
expect(wrapper.querySelectorAll('.anticon-highlight').length).toBeGreaterThan(0);
|
2021-09-23 22:14:43 +08:00
|
|
|
} else {
|
2022-06-09 18:09:43 +08:00
|
|
|
expect(wrapper.querySelectorAll('.anticon-edit').length).toBeGreaterThan(0);
|
2021-09-23 22:14:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (triggerType === undefined || triggerType.indexOf('text') === -1) {
|
2022-06-09 18:09:43 +08:00
|
|
|
fireEvent.click(wrapper.firstChild);
|
2021-09-23 22:14:43 +08:00
|
|
|
expect(onStart).not.toHaveBeenCalled();
|
|
|
|
}
|
2022-06-09 18:09:43 +08:00
|
|
|
fireEvent.mouseEnter(wrapper.querySelectorAll('.ant-typography-edit')[0]);
|
2021-09-23 22:14:43 +08:00
|
|
|
jest.runAllTimers();
|
|
|
|
|
|
|
|
if (tooltip === undefined || tooltip === true) {
|
2022-06-09 18:09:43 +08:00
|
|
|
await waitFor(() => {
|
|
|
|
expect(wrapper.querySelector('.ant-tooltip-inner').textContent).toBe('Edit');
|
|
|
|
});
|
2021-09-23 22:14:43 +08:00
|
|
|
} else if (tooltip === false) {
|
2022-06-09 18:09:43 +08:00
|
|
|
await waitFor(() => {
|
|
|
|
expect(wrapper.querySelectorAll('.ant-tooltip-inner').length).toBe(0);
|
|
|
|
});
|
2021-09-23 22:14:43 +08:00
|
|
|
} else {
|
2022-06-09 18:09:43 +08:00
|
|
|
await waitFor(() => {
|
|
|
|
expect(wrapper.querySelector('.ant-tooltip-inner').textContent).toBe(tooltip);
|
|
|
|
});
|
2021-09-23 22:14:43 +08:00
|
|
|
}
|
|
|
|
|
2022-06-09 18:09:43 +08:00
|
|
|
fireEvent.click(wrapper.querySelectorAll('.ant-typography-edit')[0]);
|
2021-09-23 22:14:43 +08:00
|
|
|
|
|
|
|
expect(onStart).toHaveBeenCalled();
|
|
|
|
if (triggerType !== undefined && triggerType.indexOf('text') !== -1) {
|
2022-06-09 18:09:43 +08:00
|
|
|
fireEvent.keyDown(wrapper.querySelector('textarea'), { keyCode: KeyCode.ESC });
|
|
|
|
fireEvent.keyUp(wrapper.querySelector('textarea'), { keyCode: KeyCode.ESC });
|
2021-09-23 22:14:43 +08:00
|
|
|
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) {
|
2022-06-09 18:09:43 +08:00
|
|
|
expect(wrapper.querySelectorAll('.anticon-highlight').length).toBe(0);
|
|
|
|
expect(wrapper.querySelectorAll('.anticon-edit').length).toBe(0);
|
2021-09-23 22:14:43 +08:00
|
|
|
}
|
2022-06-09 18:09:43 +08:00
|
|
|
fireEvent.click(wrapper.firstChild);
|
2021-09-23 22:14:43 +08:00
|
|
|
expect(onStart).toHaveBeenCalled();
|
2020-08-09 12:56:35 +08:00
|
|
|
}
|
|
|
|
|
2019-04-25 17:42:59 +08:00
|
|
|
// Should have className
|
2022-06-09 18:09:43 +08:00
|
|
|
const props = wrapper.querySelectorAll('div')[0];
|
|
|
|
expect(props.getAttribute('style')).toContain('padding: unset');
|
2019-04-25 17:42:59 +08:00
|
|
|
expect(props.className.includes(className)).toBeTruthy();
|
|
|
|
|
2022-06-09 18:09:43 +08:00
|
|
|
fireEvent.change(wrapper.querySelector('textarea'), {
|
2019-02-19 11:42:05 +08:00
|
|
|
target: { value: 'Bamboo' },
|
|
|
|
});
|
|
|
|
|
2021-09-22 21:54:27 +08:00
|
|
|
if (enterIcon === undefined) {
|
|
|
|
expect(
|
2022-06-09 18:09:43 +08:00
|
|
|
wrapper.querySelectorAll('span.ant-typography-edit-content-confirm')[0].className,
|
2021-09-22 21:54:27 +08:00
|
|
|
).toContain('anticon-enter');
|
|
|
|
} else if (enterIcon === null) {
|
2022-06-09 18:09:43 +08:00
|
|
|
expect(
|
|
|
|
wrapper.querySelectorAll('span.ant-typography-edit-content-confirm').length,
|
|
|
|
).toBe(0);
|
2021-09-22 21:54:27 +08:00
|
|
|
} else {
|
|
|
|
expect(
|
2022-06-09 18:09:43 +08:00
|
|
|
wrapper.querySelectorAll('span.ant-typography-edit-content-confirm')[0].className,
|
2021-09-22 21:54:27 +08:00
|
|
|
).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
|
2022-06-09 18:09:43 +08:00
|
|
|
fireEvent.compositionStart(wrapper.querySelector('textarea'));
|
|
|
|
fireEvent.keyDown(wrapper.querySelector('textarea'), { keyCode: KeyCode.ENTER });
|
|
|
|
fireEvent.compositionEnd(wrapper.querySelector('textarea'));
|
|
|
|
fireEvent.keyUp(wrapper.querySelector('textarea'), { keyCode: KeyCode.ENTER });
|
2019-02-19 11:42:05 +08:00
|
|
|
|
|
|
|
// Now trigger
|
2022-06-09 18:09:43 +08:00
|
|
|
fireEvent.keyDown(wrapper.querySelector('textarea'), { keyCode: KeyCode.ENTER });
|
|
|
|
fireEvent.keyUp(wrapper.querySelector('textarea'), { 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 => {
|
2022-06-09 18:09:43 +08:00
|
|
|
fireEvent.keyDown(wrapper.querySelector('textarea'), { keyCode: KeyCode.ESC });
|
|
|
|
fireEvent.keyUp(wrapper.querySelector('textarea'), { 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 => {
|
2022-06-09 18:09:43 +08:00
|
|
|
fireEvent.blur(wrapper.querySelector('textarea'));
|
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();
|
2022-06-09 18:09:43 +08:00
|
|
|
const { container: wrapper } = render(<Paragraph editable={{ onEnd }}>Bamboo</Paragraph>);
|
|
|
|
fireEvent.click(wrapper.querySelectorAll('.ant-typography-edit')[0]);
|
|
|
|
fireEvent.keyDown(wrapper.querySelector('textarea'), { keyCode: KeyCode.ENTER });
|
|
|
|
fireEvent.keyUp(wrapper.querySelector('textarea'), { keyCode: KeyCode.ENTER });
|
2021-03-13 23:46:32 +08:00
|
|
|
expect(onEnd).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should trigger onCancel when type ESC', () => {
|
|
|
|
const onCancel = jest.fn();
|
2022-06-09 18:09:43 +08:00
|
|
|
const { container: wrapper } = render(
|
|
|
|
<Paragraph editable={{ onCancel }}>Bamboo</Paragraph>,
|
|
|
|
);
|
|
|
|
fireEvent.click(wrapper.querySelectorAll('.ant-typography-edit')[0]);
|
|
|
|
fireEvent.keyDown(wrapper.querySelector('textarea'), { keyCode: KeyCode.ESC });
|
|
|
|
fireEvent.keyUp(wrapper.querySelector('textarea'), { keyCode: KeyCode.ESC });
|
2021-03-13 23:46:32 +08:00
|
|
|
expect(onCancel).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
2020-12-26 21:58:35 +08:00
|
|
|
it('should only trigger focus on the first time', () => {
|
|
|
|
let triggerTimes = 0;
|
2022-06-09 18:09:43 +08:00
|
|
|
const { container: wrapper } = render(<Paragraph editable>Bamboo</Paragraph>);
|
|
|
|
const editIcon = wrapper.querySelectorAll('.ant-typography-edit')[0];
|
|
|
|
|
|
|
|
editIcon.addEventListener('focus', () => {
|
2020-12-26 21:58:35 +08:00
|
|
|
triggerTimes += 1;
|
|
|
|
});
|
|
|
|
|
2022-06-09 18:09:43 +08:00
|
|
|
fireEvent.focus(editIcon);
|
|
|
|
expect(triggerTimes).toEqual(1);
|
2020-12-26 21:58:35 +08:00
|
|
|
|
2022-06-09 18:09:43 +08:00
|
|
|
fireEvent.click(editIcon);
|
2020-12-26 21:58:35 +08:00
|
|
|
expect(triggerTimes).toEqual(1);
|
|
|
|
|
2022-06-09 18:09:43 +08:00
|
|
|
fireEvent.change(wrapper.querySelector('textarea'), {
|
2020-12-26 21:58:35 +08:00
|
|
|
target: { value: 'good' },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(triggerTimes).toEqual(1);
|
|
|
|
});
|
2019-02-19 11:42:05 +08:00
|
|
|
});
|
2020-02-07 13:15:24 +08:00
|
|
|
|
|
|
|
it('should focus at the end of textarea', () => {
|
2022-06-09 18:09:43 +08:00
|
|
|
const { container: wrapper } = render(<Paragraph editable>content</Paragraph>);
|
|
|
|
fireEvent.click(wrapper.querySelectorAll('.ant-typography-edit')[0]);
|
|
|
|
const textareaNode = wrapper.querySelector('textarea');
|
2020-02-07 13:15:24 +08:00
|
|
|
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 = () => {};
|
2022-06-09 18:09:43 +08:00
|
|
|
render(<Typography setContentRef={refFunc} />);
|
2019-10-01 14:06:09 +08:00
|
|
|
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();
|
|
|
|
|
2022-06-09 18:09:43 +08:00
|
|
|
render(<Title level={1} ref={ref} />);
|
2022-04-05 13:02:32 +08:00
|
|
|
expect(ref.current instanceof HTMLHeadingElement).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should get HTMLDivElement ref from Paragraph', () => {
|
|
|
|
const ref = React.createRef();
|
|
|
|
|
2022-06-09 18:09:43 +08:00
|
|
|
render(<Paragraph ref={ref} />);
|
2022-04-05 13:02:32 +08:00
|
|
|
expect(ref.current instanceof HTMLDivElement).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should get HTMLSpanElement ref from Text', () => {
|
|
|
|
const ref = React.createRef();
|
|
|
|
|
2022-06-09 18:09:43 +08:00
|
|
|
render(<Text ref={ref} />);
|
2022-04-05 13:02:32 +08:00
|
|
|
expect(ref.current instanceof HTMLSpanElement).toBe(true);
|
|
|
|
});
|
2019-02-19 11:42:05 +08:00
|
|
|
});
|