|
1 | 1 | import * as React from 'react';
|
2 | 2 | import '@testing-library/jest-dom/extend-expect';
|
3 |
| -import { render, cleanup } from '@testing-library/react'; |
| 3 | +import { render, cleanup, fireEvent } from '@testing-library/react'; |
4 | 4 | import EllipsisText from '../index';
|
5 | 5 |
|
| 6 | +(global as any).document.createRange = () => ({ |
| 7 | + selectNodeContents: jest.fn(), |
| 8 | + getBoundingClientRect: jest.fn(() => ({ |
| 9 | + width: 500 |
| 10 | + })) |
| 11 | +}); |
| 12 | + |
6 | 13 | const defaultProps = {
|
7 |
| - value: 'ellipsis-Test' |
| 14 | + value: '我是很长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长的文本' |
8 | 15 | }
|
9 | 16 |
|
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', () => { |
19 | 19 | beforeEach(() => {
|
| 20 | + jest.spyOn(document.documentElement, 'scrollWidth', 'get') |
| 21 | + .mockImplementation(() => 100); |
| 22 | + jest.spyOn(document.documentElement, 'offsetWidth', 'get') |
| 23 | + .mockImplementation(() => 600); |
| 24 | + |
20 | 25 | wrapper = render(
|
21 |
| - <div style={{ width: 500 }}> |
| 26 | + <div> |
22 | 27 | <EllipsisText {...defaultProps} />
|
23 | 28 | </div>
|
24 | 29 | );
|
25 | 30 | })
|
26 | 31 |
|
27 | 32 | afterEach(() => {
|
28 | 33 | 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() |
29 | 116 | })
|
30 | 117 |
|
31 | 118 | test('render correct value in ellipsis', () => {
|
32 | 119 | const { getByText } = wrapper
|
| 120 | + const { value } = defaultProps |
| 121 | + element = getByText(value) |
33 | 122 |
|
34 |
| - expect(getByText('ellipsis-Test')).toBeInTheDocument() |
| 123 | + expect(element).toBeInTheDocument() |
| 124 | + expect(element.style.maxWidth).toBe('0') |
35 | 125 | })
|
36 | 126 | })
|
0 commit comments