2019-02-19 11:42:05 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { mount } from 'enzyme';
|
|
|
|
import KeyCode from 'rc-util/lib/KeyCode';
|
|
|
|
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';
|
|
|
|
import Base from '../Base'; // eslint-disable-line import/no-named-as-default
|
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';
|
2020-03-20 15:07:47 +08:00
|
|
|
import { sleep } 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
|
|
|
|
const originOffsetHeight = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'offsetHeight')
|
|
|
|
.get;
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
});
|
|
|
|
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-05-14 15:57:04 +08:00
|
|
|
'Warning: [antd: Typography.Title] Title only accept `1 | 2 | 3 | 4` as `level` value.',
|
2019-02-19 11:42:05 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Base', () => {
|
|
|
|
describe('trigger ellipsis update', () => {
|
|
|
|
const fullStr =
|
|
|
|
'Bamboo is Little Light Bamboo is Little Light Bamboo is Little Light Bamboo is Little Light Bamboo is Little Light';
|
|
|
|
|
2020-03-20 15:07:47 +08:00
|
|
|
it('should trigger update', async () => {
|
2020-04-21 10:13:57 +08:00
|
|
|
const onEllipsis = jest.fn();
|
2019-02-19 11:42:05 +08:00
|
|
|
const wrapper = mount(
|
2020-04-21 10:13:57 +08:00
|
|
|
<Base ellipsis={{ onEllipsis }} component="p" editable>
|
2019-02-19 11:42:05 +08:00
|
|
|
{fullStr}
|
|
|
|
</Base>,
|
|
|
|
);
|
|
|
|
|
2020-03-20 15:07:47 +08:00
|
|
|
await sleep(20);
|
2019-02-19 11:42:05 +08:00
|
|
|
wrapper.update();
|
2019-08-13 14:07:17 +08:00
|
|
|
expect(wrapper.find('span:not(.anticon)').text()).toEqual('Bamboo is Little ...');
|
2020-04-21 10:13:57 +08:00
|
|
|
expect(onEllipsis).toHaveBeenCalledWith(true);
|
|
|
|
onEllipsis.mockReset();
|
2019-02-19 11:42:05 +08:00
|
|
|
|
2020-04-21 10:13:57 +08:00
|
|
|
wrapper.setProps({ ellipsis: { rows: 2, onEllipsis } });
|
2020-03-20 15:07:47 +08:00
|
|
|
await sleep(20);
|
2019-02-19 11:42:05 +08:00
|
|
|
wrapper.update();
|
2019-08-29 23:41:46 +08:00
|
|
|
expect(wrapper.find('span:not(.anticon)').text()).toEqual(
|
|
|
|
'Bamboo is Little Light Bamboo is Litt...',
|
|
|
|
);
|
2020-04-21 10:13:57 +08:00
|
|
|
expect(onEllipsis).not.toHaveBeenCalled();
|
2019-02-19 11:42:05 +08:00
|
|
|
|
2020-04-21 10:13:57 +08:00
|
|
|
wrapper.setProps({ ellipsis: { rows: 99, onEllipsis } });
|
2020-03-20 15:07:47 +08:00
|
|
|
await sleep(20);
|
2019-02-19 11:42:05 +08:00
|
|
|
wrapper.update();
|
|
|
|
expect(wrapper.find('p').text()).toEqual(fullStr);
|
2020-04-21 10:13:57 +08:00
|
|
|
expect(onEllipsis).toHaveBeenCalledWith(false);
|
2019-02-19 11:42:05 +08:00
|
|
|
|
|
|
|
wrapper.unmount();
|
|
|
|
});
|
|
|
|
|
2020-03-20 15:07:47 +08:00
|
|
|
it('should middle ellipsis', async () => {
|
2019-12-24 11:59:17 +08:00
|
|
|
const suffix = '--suffix';
|
|
|
|
const wrapper = mount(
|
|
|
|
<Base ellipsis={{ rows: 1, suffix }} component="p">
|
|
|
|
{fullStr}
|
|
|
|
</Base>,
|
|
|
|
);
|
|
|
|
|
2020-03-20 15:07:47 +08:00
|
|
|
await sleep(20);
|
2019-12-24 11:59:17 +08:00
|
|
|
wrapper.update();
|
|
|
|
expect(wrapper.find('p').text()).toEqual('Bamboo is...--suffix');
|
|
|
|
wrapper.unmount();
|
|
|
|
});
|
|
|
|
|
2020-03-20 15:07:47 +08:00
|
|
|
it('should front or middle ellipsis', async () => {
|
2019-12-24 11:59:17 +08:00
|
|
|
const suffix = '--The information is very important';
|
|
|
|
const wrapper = mount(
|
|
|
|
<Base ellipsis={{ rows: 1, suffix }} component="p">
|
|
|
|
{fullStr}
|
|
|
|
</Base>,
|
|
|
|
);
|
|
|
|
|
2020-03-20 15:07:47 +08:00
|
|
|
await sleep(20);
|
2019-12-24 11:59:17 +08:00
|
|
|
wrapper.update();
|
|
|
|
expect(wrapper.find('p').text()).toEqual('...--The information is very important');
|
|
|
|
|
|
|
|
wrapper.setProps({ ellipsis: { rows: 2, suffix } });
|
2020-03-20 15:07:47 +08:00
|
|
|
await sleep(20);
|
2019-12-24 11:59:17 +08:00
|
|
|
wrapper.update();
|
|
|
|
expect(wrapper.find('p').text()).toEqual('Ba...--The information is very important');
|
|
|
|
|
|
|
|
wrapper.setProps({ ellipsis: { rows: 99, suffix } });
|
2020-03-20 15:07:47 +08:00
|
|
|
await sleep(20);
|
2019-12-24 11:59:17 +08:00
|
|
|
wrapper.update();
|
|
|
|
expect(wrapper.find('p').text()).toEqual(fullStr + suffix);
|
|
|
|
|
|
|
|
wrapper.unmount();
|
|
|
|
});
|
|
|
|
|
2020-03-20 15:07:47 +08:00
|
|
|
it('connect children', async () => {
|
2019-10-01 14:06:09 +08:00
|
|
|
const bamboo = 'Bamboo';
|
|
|
|
const is = ' is ';
|
|
|
|
|
2019-02-19 11:42:05 +08:00
|
|
|
const wrapper = mount(
|
|
|
|
<Base ellipsis component="p" editable>
|
2019-10-01 14:06:09 +08:00
|
|
|
{bamboo}
|
|
|
|
{is}
|
2019-02-19 11:42:05 +08:00
|
|
|
<code>Little</code>
|
|
|
|
<code>Light</code>
|
|
|
|
</Base>,
|
|
|
|
);
|
|
|
|
|
2020-03-20 15:07:47 +08:00
|
|
|
await sleep(20);
|
2019-02-19 11:42:05 +08:00
|
|
|
wrapper.update();
|
|
|
|
|
2019-08-13 14:07:17 +08:00
|
|
|
expect(wrapper.find('span:not(.anticon)').text()).toEqual('Bamboo is Little...');
|
2019-02-19 11:42:05 +08:00
|
|
|
});
|
|
|
|
|
2020-03-20 15:07:47 +08:00
|
|
|
it('should expandable work', async () => {
|
2019-02-19 11:42:05 +08:00
|
|
|
const onExpand = jest.fn();
|
|
|
|
const wrapper = mount(
|
|
|
|
<Base ellipsis={{ expandable: true, onExpand }} component="p" copyable editable>
|
|
|
|
{fullStr}
|
|
|
|
</Base>,
|
|
|
|
);
|
|
|
|
|
2020-03-20 15:07:47 +08:00
|
|
|
await sleep(20);
|
2019-02-19 11:42:05 +08:00
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
wrapper.find('.ant-typography-expand').simulate('click');
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onExpand).toHaveBeenCalled();
|
2020-03-20 15:07:47 +08:00
|
|
|
await sleep(20);
|
2019-02-19 11:42:05 +08:00
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
expect(wrapper.find('p').text()).toEqual(fullStr);
|
|
|
|
});
|
|
|
|
|
2020-05-26 11:54:52 +08:00
|
|
|
it('should have custom expand style', async () => {
|
|
|
|
const symbol = 'more';
|
|
|
|
const wrapper = mount(
|
|
|
|
<Base ellipsis={{ expandable: true, symbol }} component="p">
|
|
|
|
{fullStr}
|
|
|
|
</Base>,
|
|
|
|
);
|
|
|
|
|
|
|
|
await sleep(20);
|
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
expect(wrapper.find('.ant-typography-expand').text()).toEqual('more');
|
|
|
|
});
|
|
|
|
|
2019-02-19 11:42:05 +08:00
|
|
|
it('can use css ellipsis', () => {
|
|
|
|
const wrapper = mount(<Base ellipsis component="p" />);
|
|
|
|
expect(wrapper.find('.ant-typography-ellipsis-single-line').length).toBeTruthy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('copyable', () => {
|
|
|
|
function copyTest(name, text, target) {
|
|
|
|
it(name, () => {
|
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(
|
|
|
|
<Base component="p" copyable={{ text, onCopy }}>
|
|
|
|
test copy
|
|
|
|
</Base>,
|
|
|
|
);
|
|
|
|
|
2020-04-21 10:13:57 +08:00
|
|
|
wrapper.find('.ant-typography-copy').first().simulate('click');
|
2019-02-19 11:42:05 +08:00
|
|
|
expect(copy.lastStr).toEqual(target);
|
|
|
|
|
|
|
|
wrapper.update();
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onCopy).toHaveBeenCalled();
|
2019-02-19 11:42:05 +08:00
|
|
|
|
|
|
|
expect(wrapper.find('.anticon-check').length).toBeTruthy();
|
|
|
|
|
|
|
|
jest.runAllTimers();
|
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
// Will set back when 3 seconds pass
|
|
|
|
expect(wrapper.find('.anticon-check').length).toBeFalsy();
|
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');
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('editable', () => {
|
|
|
|
function testStep(name, submitFunc, expectFunc) {
|
|
|
|
it(name, () => {
|
|
|
|
const onStart = jest.fn();
|
|
|
|
const onChange = jest.fn();
|
|
|
|
|
2019-04-25 17:42:59 +08:00
|
|
|
const className = 'test';
|
|
|
|
const style = {};
|
|
|
|
|
|
|
|
const wrapper = mount(
|
|
|
|
<Paragraph editable={{ onChange, onStart }} className={className} style={style}>
|
|
|
|
Bamboo
|
|
|
|
</Paragraph>,
|
|
|
|
);
|
2019-02-19 11:42:05 +08:00
|
|
|
|
2020-04-21 10:13:57 +08:00
|
|
|
wrapper.find('.ant-typography-edit').first().simulate('click');
|
2019-02-19 11:42:05 +08:00
|
|
|
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onStart).toHaveBeenCalled();
|
2019-02-19 11:42:05 +08:00
|
|
|
|
2019-04-25 17:42:59 +08:00
|
|
|
// Should have className
|
|
|
|
const props = wrapper.find('div').props();
|
|
|
|
expect(props.style).toEqual(style);
|
|
|
|
expect(props.className.includes(className)).toBeTruthy();
|
|
|
|
|
2019-02-19 11:42:05 +08:00
|
|
|
wrapper.find('TextArea').simulate('change', {
|
|
|
|
target: { value: 'Bamboo' },
|
|
|
|
});
|
|
|
|
|
|
|
|
submitFunc(wrapper);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
testStep('by key up', wrapper => {
|
|
|
|
// Not trigger when inComposition
|
|
|
|
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 });
|
|
|
|
|
|
|
|
// Now trigger
|
|
|
|
wrapper.find('TextArea').simulate('keyDown', { keyCode: KeyCode.ENTER });
|
|
|
|
wrapper.find('TextArea').simulate('keyUp', { keyCode: KeyCode.ENTER });
|
|
|
|
});
|
|
|
|
|
|
|
|
testStep(
|
|
|
|
'by esc key',
|
|
|
|
wrapper => {
|
|
|
|
wrapper.find('TextArea').simulate('keyDown', { keyCode: KeyCode.ESC });
|
|
|
|
wrapper.find('TextArea').simulate('keyUp', { keyCode: KeyCode.ESC });
|
|
|
|
},
|
|
|
|
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
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
testStep('by blur', wrapper => {
|
|
|
|
wrapper.find('TextArea').simulate('blur');
|
|
|
|
});
|
|
|
|
});
|
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', () => {
|
|
|
|
function refFunc() {}
|
|
|
|
mount(<Typography setContentRef={refFunc} />);
|
|
|
|
|
|
|
|
expect(errorSpy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [antd: Typography] `setContentRef` is deprecated. Please use `ref` instead.',
|
|
|
|
);
|
|
|
|
});
|
2019-02-19 11:42:05 +08:00
|
|
|
});
|