Skip to content

Commit

Permalink
test: replace some slepp with jest.runAllTimers
Browse files Browse the repository at this point in the history
  • Loading branch information
zpc7 committed May 7, 2022
1 parent 22b89ee commit ad886d1
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions components/tooltip/__tests__/tooltip.test.js
Expand Up @@ -18,6 +18,11 @@ describe('Tooltip', () => {
spyElementPrototype(HTMLElement, 'offsetParent', {
get: () => ({}),
});
jest.useFakeTimers();
});

afterAll(() => {
jest.useRealTimers();
});

it('check `onVisibleChange` arguments', () => {
Expand Down Expand Up @@ -234,6 +239,7 @@ describe('Tooltip', () => {
});

it('should works for date picker', async () => {
jest.useRealTimers();
const onVisibleChange = jest.fn();
const ref = React.createRef();

Expand All @@ -257,9 +263,11 @@ describe('Tooltip', () => {
expect(onVisibleChange).toHaveBeenCalledWith(false);
expect(ref.current.props.visible).toBe(false);
expect(container.querySelector('.ant-tooltip-open')).toBeNull();
jest.useFakeTimers();
});

it('should works for input group', async () => {
jest.useRealTimers();
const onVisibleChange = jest.fn();
const ref = React.createRef();
const { container } = render(
Expand All @@ -284,6 +292,7 @@ describe('Tooltip', () => {
expect(onVisibleChange).toHaveBeenCalledWith(false);
expect(ref.current.props.visible).toBe(false);
expect(container.querySelector('.ant-tooltip-open')).toBeNull();
jest.useFakeTimers();
});

// https://github.com/ant-design/ant-design/issues/20891
Expand Down Expand Up @@ -346,7 +355,7 @@ describe('Tooltip', () => {
expect(container.getElementsByTagName('span')).toHaveLength(1);
const element = container.getElementsByTagName('span')[0];
fireEvent.mouseEnter(element);
await sleep(500);
jest.runAllTimers();

await waitFor(() => {
expect(document.querySelector(`.ant-tooltip-placement-${placement}`)).not.toBeNull();
Expand All @@ -357,7 +366,7 @@ describe('Tooltip', () => {
placementList.forEach(placement => testPlacement(`Placement ${placement}`, placement));
});

it('should works for mismatch placement', async () => {
it('should works for mismatch placement', () => {
const { container } = render(
<Tooltip
title="xxxxx"
Expand All @@ -371,7 +380,7 @@ describe('Tooltip', () => {
);
const button = container.getElementsByTagName('span')[0];
fireEvent.mouseEnter(button);
await sleep(600);
jest.runAllTimers();
expect(document.querySelector('.ant-tooltip')).not.toBeNull();
});

Expand Down

0 comments on commit ad886d1

Please sign in to comment.