2022-06-09 18:09:43 +08:00
|
|
|
import { CheckOutlined, HighlightOutlined, LikeOutlined, SmileOutlined } from '@ant-design/icons';
|
2022-06-22 14:57:09 +08:00
|
|
|
import copy from 'copy-to-clipboard';
|
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-06-22 14:57:09 +08:00
|
|
|
import React from 'react';
|
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
|
|
|
import rtlTest from '../../../tests/shared/rtlTest';
|
2022-09-28 12:01:20 +08:00
|
|
|
import { fireEvent, render, waitFor, act, waitFakeTimer } from '../../../tests/utils';
|
2022-06-22 14:57:09 +08:00
|
|
|
import Base from '../Base';
|
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';
|
2022-09-05 19:41:32 +08:00
|
|
|
import type { TitleProps } from '../Title';
|
2022-06-22 14:57:09 +08:00
|
|
|
import Title from '../Title';
|
2019-10-01 14:06:09 +08:00
|
|
|
import Typography from '../Typography';
|
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',
|
2022-09-05 19:41:32 +08:00
|
|
|
)?.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);
|
2022-09-05 19:41:32 +08:00
|
|
|
return { height: lines * 16 } as DOMRect;
|
2021-07-20 13:51:03 +08:00
|
|
|
});
|
2019-02-19 11:42:05 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// Mock getComputedStyle
|
|
|
|
const originGetComputedStyle = window.getComputedStyle;
|
2022-11-19 13:47:33 +08:00
|
|
|
window.getComputedStyle = (ele) => {
|
2019-02-19 11:42:05 +08:00
|
|
|
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-09-05 19:41:32 +08:00
|
|
|
render(<Title level={false as unknown as TitleProps['level']} />);
|
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-09-28 12:01:20 +08:00
|
|
|
/**
|
|
|
|
* @param name Test case name
|
|
|
|
* @param text Origin text context
|
|
|
|
* @param target Copy to clipboard text
|
|
|
|
* @param icon Icon
|
|
|
|
* @param tooltips Tooltip config
|
|
|
|
* @param format Copy context format
|
|
|
|
*/
|
2022-09-05 19:41:32 +08:00
|
|
|
function copyTest(
|
|
|
|
name: string,
|
|
|
|
text?: string,
|
|
|
|
target?: string,
|
|
|
|
icon?: React.ReactNode,
|
|
|
|
tooltips?: boolean | string[],
|
|
|
|
format?: 'text/plain' | 'text/html',
|
|
|
|
): void {
|
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-09-28 12:01:20 +08:00
|
|
|
const { container, 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-09-28 12:01:20 +08:00
|
|
|
expect(container.querySelector('.anticon-smile')).toBeTruthy();
|
2020-08-09 12:56:35 +08:00
|
|
|
} else {
|
2022-09-28 12:01:20 +08:00
|
|
|
expect(container.querySelector('.anticon-copy')).toBeTruthy();
|
2020-08-09 12:56:35 +08:00
|
|
|
}
|
|
|
|
|
2022-09-28 12:01:20 +08:00
|
|
|
// Mouse enter to show tooltip
|
|
|
|
fireEvent.mouseEnter(container.querySelector('.ant-typography-copy')!);
|
2022-08-08 13:31:55 +08:00
|
|
|
act(() => {
|
2022-09-28 12:01:20 +08:00
|
|
|
jest.advanceTimersByTime(10000);
|
2022-08-08 13:31:55 +08:00
|
|
|
});
|
2020-08-09 12:56:35 +08:00
|
|
|
|
|
|
|
if (tooltips === undefined || tooltips === true) {
|
2022-09-28 12:01:20 +08:00
|
|
|
expect(container.querySelector('.ant-tooltip-inner')?.textContent).toBe('Copy');
|
2020-08-09 12:56:35 +08:00
|
|
|
} else if (tooltips === false) {
|
2022-09-28 12:01:20 +08:00
|
|
|
expect(container.querySelector('.ant-tooltip-inner')).toBeFalsy();
|
2022-09-05 19:41:32 +08:00
|
|
|
} else if ((tooltips as any)[0] === '' && (tooltips as any)[1] === '') {
|
2022-09-28 12:01:20 +08:00
|
|
|
expect(container.querySelector('.ant-tooltip-inner')).toBeFalsy();
|
2022-09-05 19:41:32 +08:00
|
|
|
} else if ((tooltips as any)[0] === '' && (tooltips as any)[1]) {
|
2022-09-28 12:01:20 +08:00
|
|
|
expect(container.querySelector('.ant-tooltip-inner')).toBeFalsy();
|
2022-09-05 19:41:32 +08:00
|
|
|
} else if ((tooltips as any)[1] === '' && (tooltips as any)[0]) {
|
2022-09-28 12:01:20 +08:00
|
|
|
expect(container.querySelector('.ant-tooltip-inner')?.textContent).toBe(
|
|
|
|
(tooltips as any)[0],
|
|
|
|
);
|
2020-08-09 12:56:35 +08:00
|
|
|
} else {
|
2022-09-28 12:01:20 +08:00
|
|
|
expect(container.querySelector('.ant-tooltip-inner')?.textContent).toBe(
|
|
|
|
(tooltips as any)[0],
|
|
|
|
);
|
2020-08-09 12:56:35 +08:00
|
|
|
}
|
|
|
|
|
2022-09-28 12:01:20 +08:00
|
|
|
// Click to copy
|
|
|
|
fireEvent.click(container.querySelector('.ant-typography-copy')!);
|
|
|
|
await waitFakeTimer(1);
|
2019-02-19 11:42:05 +08:00
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
expect((copy as any).lastStr).toEqual(target);
|
|
|
|
expect((copy as any).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';
|
2022-09-05 19:41:32 +08:00
|
|
|
if (icon && (icon as string).length > 1) {
|
2020-08-09 12:56:35 +08:00
|
|
|
copiedIcon = '.anticon-like';
|
|
|
|
} else {
|
|
|
|
copiedIcon = '.anticon-check';
|
|
|
|
}
|
|
|
|
|
2022-09-28 12:01:20 +08:00
|
|
|
expect(container.querySelector(copiedIcon)).toBeTruthy();
|
|
|
|
|
|
|
|
// Timeout will makes copy tooltip back to origin
|
|
|
|
fireEvent.mouseEnter(container.querySelector('.ant-typography-copy')!);
|
|
|
|
await waitFakeTimer(15, 10);
|
2020-08-09 12:56:35 +08:00
|
|
|
|
|
|
|
if (tooltips === undefined || tooltips === true) {
|
2022-09-28 12:01:20 +08:00
|
|
|
expect(container.querySelector('.ant-tooltip-inner')?.textContent).toBe('Copied');
|
2020-08-09 12:56:35 +08:00
|
|
|
} else if (tooltips === false) {
|
2022-09-28 12:01:20 +08:00
|
|
|
expect(container.querySelector('.ant-tooltip-inner')).toBeFalsy();
|
2020-08-18 16:14:14 +08:00
|
|
|
} else if (tooltips[0] === '' && tooltips[1] === '') {
|
2022-09-28 12:01:20 +08:00
|
|
|
expect(container.querySelector('.ant-tooltip-inner')).toBeFalsy();
|
2020-08-18 16:14:14 +08:00
|
|
|
} else if (tooltips[0] === '' && tooltips[1]) {
|
2022-09-28 12:01:20 +08:00
|
|
|
expect(container.querySelector('.ant-tooltip-inner')?.textContent).toBe(tooltips[1]);
|
2020-08-18 16:14:14 +08:00
|
|
|
} else if (tooltips[1] === '' && tooltips[0]) {
|
2023-04-18 11:35:07 +08:00
|
|
|
// Tooltip will be hidden in this case, with content memoized
|
|
|
|
expect(container.querySelector('.ant-tooltip-inner')?.textContent).toBe(tooltips[0]);
|
2020-08-09 12:56:35 +08:00
|
|
|
} else {
|
2022-09-28 12:01:20 +08:00
|
|
|
expect(container.querySelector('.ant-tooltip-inner')?.textContent).toBe(tooltips[1]);
|
2020-08-09 12:56:35 +08:00
|
|
|
}
|
2019-02-19 11:42:05 +08:00
|
|
|
|
|
|
|
// Will set back when 3 seconds pass
|
2022-09-28 12:01:20 +08:00
|
|
|
await waitFakeTimer();
|
|
|
|
expect(container.querySelector(copiedIcon)).toBeFalsy();
|
|
|
|
|
2022-06-09 18:09:43 +08:00
|
|
|
unmount();
|
2022-09-28 12:01:20 +08:00
|
|
|
jest.clearAllTimers();
|
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(
|
2022-09-28 12:01:20 +08:00
|
|
|
'customize copy with plain text',
|
2022-04-25 09:49:48 +08:00
|
|
|
'bamboo',
|
|
|
|
'bamboo',
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
'text/plain',
|
|
|
|
);
|
|
|
|
copyTest(
|
2022-09-28 12:01:20 +08:00
|
|
|
'customize copy with html text',
|
2022-04-25 09:49:48 +08:00
|
|
|
'bamboo',
|
|
|
|
'bamboo',
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
'text/html',
|
|
|
|
);
|
2022-09-28 12:01:20 +08:00
|
|
|
copyTest('customize copy icon with one', 'bamboo', 'bamboo', <SmileOutlined />);
|
2020-08-09 12:56:35 +08:00
|
|
|
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', () => {
|
2022-09-05 19:41:32 +08:00
|
|
|
interface EditableConfig {
|
|
|
|
name?: string;
|
|
|
|
icon?: React.ReactNode;
|
|
|
|
tooltip?: string | boolean;
|
|
|
|
triggerType?: ('icon' | 'text')[];
|
|
|
|
enterIcon?: React.ReactNode;
|
|
|
|
}
|
2021-09-29 08:43:06 +08:00
|
|
|
function testStep(
|
2022-09-05 19:41:32 +08:00
|
|
|
{ name = '', icon, tooltip, triggerType, enterIcon }: EditableConfig,
|
|
|
|
submitFunc?: (container: ReturnType<typeof render>['container']) => void,
|
2023-02-23 21:56:43 +08:00
|
|
|
expectFunc?: (callback: jest.Mock) => void,
|
2021-09-29 08:43:06 +08:00
|
|
|
) {
|
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
|
|
|
|
2022-10-21 11:45:55 +08:00
|
|
|
if (triggerType === undefined || triggerType.includes('icon')) {
|
2021-09-23 22:14:43 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-10-21 11:45:55 +08:00
|
|
|
if (triggerType === undefined || !triggerType.includes('text')) {
|
2022-09-05 19:41:32 +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]);
|
2022-08-08 13:31:55 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2021-09-23 22:14:43 +08:00
|
|
|
|
|
|
|
if (tooltip === undefined || tooltip === true) {
|
2022-06-09 18:09:43 +08:00
|
|
|
await waitFor(() => {
|
2022-09-05 19:41:32 +08:00
|
|
|
expect(wrapper.querySelector('.ant-tooltip-inner')?.textContent).toBe('Edit');
|
2022-06-09 18:09:43 +08:00
|
|
|
});
|
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(() => {
|
2022-09-05 19:41:32 +08:00
|
|
|
expect(wrapper.querySelector('.ant-tooltip-inner')?.textContent).toBe(tooltip);
|
2022-06-09 18:09:43 +08:00
|
|
|
});
|
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();
|
2022-10-21 11:45:55 +08:00
|
|
|
if (triggerType !== undefined && triggerType.includes('text')) {
|
2022-09-05 19:41:32 +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
|
|
|
}
|
|
|
|
|
2022-10-21 11:45:55 +08:00
|
|
|
if (triggerType !== undefined && triggerType.includes('text')) {
|
|
|
|
if (!triggerType.includes('icon')) {
|
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-09-05 19:41:32 +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-09-05 19:41:32 +08:00
|
|
|
fireEvent.change(wrapper.querySelector('textarea')!, { target: { value: 'Bamboo' } });
|
2019-02-19 11:42:05 +08:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:47:33 +08:00
|
|
|
testStep({ name: 'by key up' }, (wrapper) => {
|
2019-02-19 11:42:05 +08:00
|
|
|
// Not trigger when inComposition
|
2022-09-05 19:41:32 +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-09-05 19:41:32 +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' },
|
2022-11-19 13:47:33 +08:00
|
|
|
(wrapper) => {
|
2022-09-05 19:41:32 +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
|
|
|
},
|
2022-11-19 13:47:33 +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
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-11-19 13:47:33 +08:00
|
|
|
testStep({ name: 'by blur' }, (wrapper) => {
|
2022-09-05 19:41:32 +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]);
|
2022-09-05 19:41:32 +08:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2022-10-17 17:00:33 +08:00
|
|
|
it('should trigger onStart when type Start', () => {
|
|
|
|
const onStart = jest.fn();
|
|
|
|
const { container: wrapper } = render(<Paragraph editable={{ onStart }}>Bamboo</Paragraph>);
|
|
|
|
fireEvent.click(wrapper.querySelectorAll('.ant-typography-edit')[0]);
|
|
|
|
fireEvent.keyDown(wrapper.querySelector('textarea')!, { keyCode: KeyCode.A });
|
|
|
|
fireEvent.keyUp(wrapper.querySelector('textarea')!, { keyCode: KeyCode.A });
|
|
|
|
expect(onStart).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
2021-03-13 23:46:32 +08:00
|
|
|
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]);
|
2022-09-05 19:41:32 +08:00
|
|
|
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-09-05 19:41:32 +08:00
|
|
|
fireEvent.change(wrapper.querySelector('textarea')!, { target: { value: 'good' } });
|
2020-12-26 21:58:35 +08:00
|
|
|
|
|
|
|
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');
|
2022-09-05 19:41:32 +08:00
|
|
|
expect(textareaNode?.selectionStart).toBe(7);
|
|
|
|
expect(textareaNode?.selectionEnd).toBe(7);
|
2020-02-07 13:15:24 +08:00
|
|
|
});
|
2019-02-19 11:42:05 +08:00
|
|
|
});
|
2019-10-01 14:06:09 +08:00
|
|
|
|
|
|
|
it('warning if use setContentRef', () => {
|
2022-09-05 19:41:32 +08:00
|
|
|
const setContentRef = { setContentRef() {} } as any;
|
|
|
|
render(<Typography {...setContentRef} />);
|
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', () => {
|
2022-09-05 19:41:32 +08:00
|
|
|
const ref = React.createRef<HTMLHeadingElement>();
|
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', () => {
|
2022-09-05 19:41:32 +08:00
|
|
|
const ref = React.createRef<HTMLDivElement>();
|
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', () => {
|
2022-09-05 19:41:32 +08:00
|
|
|
const ref = React.createRef<HTMLSpanElement>();
|
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
|
|
|
});
|