Skip to content

Commit 4b864ff

Browse files
author
kongshan
committed
feat: unit test fix and change getStyle methods
1 parent 938f3fa commit 4b864ff

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

src/components/editCell/__tests__/editCell.test.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ import EditCell from '../index';
33
import { render, fireEvent, cleanup } from '@testing-library/react';
44
import '@testing-library/jest-dom/extend-expect';
55

6+
(global as any).document.createRange = () => ({
7+
selectNodeContents: jest.fn(),
8+
getBoundingClientRect: jest.fn(() => ({
9+
width: 500
10+
}))
11+
});
12+
613
const defaultProps = {
714
value: 'test editCell',
815
keyField: 'name',

src/components/ellipsisText/__tests__/ellipsisText.test.tsx

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
2-
import '@testing-library/jest-dom/extend-expect';
32
import { render, cleanup, fireEvent } from '@testing-library/react';
3+
import '@testing-library/jest-dom/extend-expect';
44
import EllipsisText from '../index';
55

66
(global as any).document.createRange = () => ({
@@ -21,6 +21,13 @@ describe('test ellipsis text if not set max width', () => {
2121
.mockImplementation(() => 100);
2222
jest.spyOn(document.documentElement, 'offsetWidth', 'get')
2323
.mockImplementation(() => 600);
24+
Object.defineProperty(window, 'getComputedStyle', {
25+
value: jest.fn(() => ({
26+
paddingLeft: '0px',
27+
paddingRight: '0px'
28+
})
29+
)
30+
});
2431

2532
wrapper = render(
2633
<div>
@@ -42,19 +49,18 @@ describe('test ellipsis text if not set max width', () => {
4249
expect(element).toBeInTheDocument()
4350
expect(element.style.maxWidth).toBe('100px')
4451
})
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-
})
5452
})
5553

5654
describe('test ellipsis text if set max width', () => {
5755
beforeEach(() => {
56+
Object.defineProperty(window, 'getComputedStyle', {
57+
value: () => ({
58+
getPropertyValue: (prop) => {
59+
return '';
60+
}
61+
})
62+
});
63+
5864
wrapper = render(
5965
<div>
6066
<EllipsisText {...defaultProps} maxWidth={100}/>

src/components/ellipsisText/index.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export default class EllipsisText extends PureComponent<Props, State> {
4444
const stylePadding =
4545
window?.getComputedStyle(dom)[attr] || dom.currentStyle[attr];
4646

47-
return stylePadding.slice(0, -2);
47+
return parseInt(stylePadding.replace('px', ''));
4848
};
4949

5050
// 最近块级父元素-除省略文本元素外其余元素的宽
@@ -72,7 +72,6 @@ export default class EllipsisText extends PureComponent<Props, State> {
7272
const rangeWidth = this.getRangeWidth(ellipsisNode);
7373
const ellipsisWidth = this.getMaxWidth(ellipsisNode.parentElement);
7474
ellipsisNode.style.display = "inline-block";
75-
7675
this.setState({
7776
actMaxWidth: ellipsisWidth,
7877
isEllipsis: rangeWidth > (maxWidth || ellipsisWidth)

0 commit comments

Comments
 (0)