Skip to content

Commit ee4beff

Browse files
author
kongshan
committed
test: add ellipsisText unit test
1 parent 906459e commit ee4beff

File tree

2 files changed

+103
-14
lines changed

2 files changed

+103
-14
lines changed
Lines changed: 103 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,126 @@
11
import * as React from 'react';
22
import '@testing-library/jest-dom/extend-expect';
3-
import { render, cleanup } from '@testing-library/react';
3+
import { render, cleanup, fireEvent } from '@testing-library/react';
44
import EllipsisText from '../index';
55

6+
(global as any).document.createRange = () => ({
7+
selectNodeContents: jest.fn(),
8+
getBoundingClientRect: jest.fn(() => ({
9+
width: 500
10+
}))
11+
});
12+
613
const defaultProps = {
7-
value: 'ellipsis-Test'
14+
value: '我是很长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长的文本'
815
}
916

10-
let wrapper
11-
describe('test ellipsis text if not full display', () => {
12-
(global as any).document.createRange = () => ({
13-
selectNodeContents: jest.fn(),
14-
getBoundingClientRect: jest.fn(() => ({
15-
width: 1000
16-
}))
17-
});
18-
17+
let wrapper, element
18+
describe('test ellipsis text if not set max width', () => {
1919
beforeEach(() => {
20+
jest.spyOn(document.documentElement, 'scrollWidth', 'get')
21+
.mockImplementation(() => 100);
22+
jest.spyOn(document.documentElement, 'offsetWidth', 'get')
23+
.mockImplementation(() => 600);
24+
2025
wrapper = render(
21-
<div style={{ width: 500 }}>
26+
<div>
2227
<EllipsisText {...defaultProps} />
2328
</div>
2429
);
2530
})
2631

2732
afterEach(() => {
2833
cleanup()
34+
jest.restoreAllMocks()
35+
})
36+
37+
test('render correct value in ellipsis', () => {
38+
const { getByText } = wrapper
39+
const { value } = defaultProps
40+
element = getByText(value)
41+
42+
expect(element).toBeInTheDocument()
43+
expect(element.style.maxWidth).toBe('100px')
44+
})
45+
46+
test('render correct value in ellipsis', () => {
47+
const { getByText } = wrapper
48+
const { value } = defaultProps
49+
element = getByText(value)
50+
51+
expect(element).toBeInTheDocument()
52+
expect(element.style.maxWidth).toBe('100px')
53+
})
54+
})
55+
56+
describe('test ellipsis text if set max width', () => {
57+
beforeEach(() => {
58+
wrapper = render(
59+
<div>
60+
<EllipsisText {...defaultProps} maxWidth={100}/>
61+
</div>
62+
);
63+
})
64+
65+
afterEach(() => {
66+
cleanup()
67+
jest.restoreAllMocks()
68+
})
69+
70+
test('render correct value in ellipsis', () => {
71+
const { getByText } = wrapper
72+
const { value } = defaultProps
73+
element = getByText(value)
74+
75+
expect(element).toBeInTheDocument()
76+
expect(element.style.maxWidth).toBe('100px')
77+
})
78+
79+
test('render correct prompt info if mouse hover the text ', () => {
80+
const { getByText, getAllByText, baseElement } = wrapper
81+
const { value } = defaultProps
82+
element = getByText(value)
83+
84+
jest.useFakeTimers()
85+
fireEvent.mouseOver(element)
86+
jest.runAllTimers()
87+
88+
expect(baseElement.querySelector('.ant-tooltip-open')).toBeInTheDocument()
89+
expect(getAllByText(value).length).toBe(2)
90+
})
91+
})
92+
93+
describe('test ellipsis text if in IE8', () => {
94+
beforeEach(() => {
95+
jest.spyOn(document.documentElement, 'scrollWidth', 'get')
96+
.mockImplementation(() => 100);
97+
jest.spyOn(document.documentElement, 'offsetWidth', 'get')
98+
.mockImplementation(() => 100);
99+
Object.defineProperty(document.documentElement, 'currentStyle', {
100+
value: {
101+
paddingLeft: '0px',
102+
paddingRight: '0px'
103+
}
104+
});
105+
106+
wrapper = render(
107+
<div>
108+
<EllipsisText {...defaultProps}/>
109+
</div>
110+
);
111+
})
112+
113+
afterEach(() => {
114+
cleanup()
115+
jest.restoreAllMocks()
29116
})
30117

31118
test('render correct value in ellipsis', () => {
32119
const { getByText } = wrapper
120+
const { value } = defaultProps
121+
element = getByText(value)
33122

34-
expect(getByText('ellipsis-Test')).toBeInTheDocument()
123+
expect(element).toBeInTheDocument()
124+
expect(element.style.maxWidth).toBe('0')
35125
})
36126
})

src/components/ellipsisText/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ class EllipsisText extends PureComponent<Props, State> {
7575
};
7676

7777
onResize = () => {
78-
if (!this.ellipsisRef) return;
7978
const { maxWidth } = this.props;
8079
const ellipsisNode = this.ellipsisRef;
8180
const rangeWidth = this.getRangeWidth(ellipsisNode);

0 commit comments

Comments
 (0)