Skip to content

Commit

Permalink
test: add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
bbb169 committed Aug 28, 2023
1 parent 04b923c commit 6fe22c5
Showing 1 changed file with 89 additions and 2 deletions.
91 changes: 89 additions & 2 deletions components/typography/__tests__/ellipsis.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
import React from 'react';
import { act } from 'react-dom/test-utils';
import { fireEvent, render, triggerResize, waitFakeTimer, waitFor } from '../../../tests/utils';
import {
fireEvent,
render,
sleep,

Check warning on line 7 in components/typography/__tests__/ellipsis.test.tsx

View check run for this annotation

codefactor.io / CodeFactor

components/typography/__tests__/ellipsis.test.tsx#L7

'sleep' is defined but never used. (no-unused-vars)

Check warning on line 7 in components/typography/__tests__/ellipsis.test.tsx

View check run for this annotation

codefactor.io / CodeFactor

components/typography/__tests__/ellipsis.test.tsx#L7

'sleep' is defined but never used. (@typescript-eslint/no-unused-vars)

Check failure on line 7 in components/typography/__tests__/ellipsis.test.tsx

View workflow job for this annotation

GitHub Actions / lint

'sleep' is declared but its value is never read.
triggerResize,
waitFakeTimer,
waitFor,
} from '../../../tests/utils';
import type { EllipsisConfig } from '../Base';
import Base from '../Base';

Expand Down Expand Up @@ -45,7 +52,7 @@ describe('Typography.Ellipsis', () => {

computeSpy = jest
.spyOn(window, 'getComputedStyle')
.mockImplementation(() => ({ fontSize: 12 } as unknown as CSSStyleDeclaration));
.mockImplementation(() => ({ fontSize: 12 }) as unknown as CSSStyleDeclaration);
});

afterEach(() => {
Expand Down Expand Up @@ -433,4 +440,84 @@ describe('Typography.Ellipsis', () => {
});
mockRectSpy.mockRestore();
});

it('should not resize many times', async () => {
let resizeTimes = 0;
let currentWidth = 100;
// string conut is different with different width
const getLineStrCount = (width: number) => {
const res = width === 100 ? 20 : 17;
return res;
};

const ref = React.createRef<HTMLElement>();
const resize = async (width: number) => {
if (resizeTimes <= 10 && width !== currentWidth) {
resizeTimes++;
currentWidth = width;
if (ref.current) triggerResize(ref.current);
await waitFakeTimer(4, 1);

// wait for a while and trigger it resize again
return ref.current?.offsetWidth;
}
};
mockRectSpy = spyElementPrototypes(HTMLElement, {
offsetHeight: {
get() {
let html = this.innerHTML;
html = html.replace(/<[^>]*>/g, '');
const lines = Math.ceil(html.length / getLineStrCount(currentWidth));

return lines * 16;
},
},
offsetWidth: {
get() {
let html = this.innerHTML;
html = html.replace(/<[^>]*>/g, '');
const lines = Math.ceil(html.length / getLineStrCount(currentWidth));

// if height is over, set width smaller, like scrollbar
if (lines > 2) {
// makes value return first
setTimeout(() => {
resize(88.6);
}, 0);
return 88.6;
}
setTimeout(() => {
resize(100);
}, 0);
return 100;
},
},
getBoundingClientRect() {
let html = this.innerHTML;
html = html.replace(/<[^>]*>/g, '');
const lines = Math.ceil(html.length / getLineStrCount(currentWidth));
return { height: lines * 16 };
},
});

render(
<Base
ellipsis={{
rows: 2,
}}
title={`${fullStr}`}

Check warning on line 508 in components/typography/__tests__/ellipsis.test.tsx

View check run for this annotation

codefactor.io / CodeFactor

components/typography/__tests__/ellipsis.test.tsx#L508

Curly braces are unnecessary here. (react/jsx-curly-brace-presence)
ref={ref}
editable
component="p"
>
{fullStr}
</Base>,
);

triggerResize(ref.current!);
await waitFakeTimer();

expect(resizeTimes).not.toBeGreaterThan(2);
mockRectSpy.mockRestore();
});
});

0 comments on commit 6fe22c5

Please sign in to comment.