test: Update button test case (#38768)

* test: Update button test case

* test: Update button test case
This commit is contained in:
hms181231 2022-11-21 10:00:13 +08:00 committed by GitHub
parent f9ef37e9be
commit 153f232243
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 9 deletions

View File

@ -5,7 +5,7 @@ import { act } from 'react-dom/test-utils';
import Button from '..';
import mountTest from '../../../tests/shared/mountTest';
import rtlTest from '../../../tests/shared/rtlTest';
import { fireEvent, render, sleep } from '../../../tests/utils';
import { fireEvent, render, waitFakeTimer } from '../../../tests/utils';
import ConfigProvider from '../../config-provider';
describe('Button', () => {
@ -242,6 +242,7 @@ describe('Button', () => {
});
it('should support to change loading', async () => {
jest.useFakeTimers();
const { container, rerender, unmount } = render(<Button>Button</Button>);
rerender(<Button loading />);
expect(container.querySelectorAll('.ant-btn-loading').length).toBe(1);
@ -249,12 +250,13 @@ describe('Button', () => {
expect(container.querySelectorAll('.ant-btn-loading').length).toBe(0);
rerender(<Button loading={{ delay: 50 }} />);
expect(container.querySelectorAll('.ant-btn-loading').length).toBe(0);
await sleep(50);
await waitFakeTimer();
expect(container.querySelectorAll('.ant-btn-loading').length).toBe(1);
rerender(<Button loading={false} />);
await sleep(50);
await waitFakeTimer();
expect(container.querySelectorAll('.ant-btn-loading').length).toBe(0);
expect(unmount).not.toThrow();
jest.useRealTimers();
});
it('should warning when pass a string as icon props', () => {

View File

@ -1,6 +1,7 @@
import userEvent from '@testing-library/user-event';
import React from 'react';
import Button from '..';
import { fireEvent, render, sleep, assertsExist } from '../../../tests/utils';
import { fireEvent, render, assertsExist } from '../../../tests/utils';
// Mock Wave ref
let waveInstanceMock: any;
@ -23,13 +24,21 @@ jest.mock('../../_util/wave', () => {
});
describe('click wave effect', () => {
beforeEach(() => {
jest.useFakeTimers();
});
afterEach(() => {
jest.clearAllTimers();
jest.useRealTimers();
});
async function clickButton(wrapper: any) {
const element = wrapper.container.firstChild;
fireEvent.click(element);
// https://github.com/testing-library/user-event/issues/833
await userEvent.setup({ advanceTimers: jest.advanceTimersByTime }).click(element);
fireEvent(element, new Event('transitionstart'));
await sleep(20);
fireEvent(element, new Event('animationend'));
await sleep(20);
}
it('should have click wave effect for primary button', async () => {
@ -82,8 +91,9 @@ describe('click wave effect', () => {
const resetEffect = jest.spyOn(waveInstanceMock, 'resetEffect');
await clickButton(wrapper);
expect(resetEffect).toHaveBeenCalledTimes(1);
fireEvent.click(wrapper.container.querySelector('.ant-btn')!);
await sleep(10);
await userEvent
.setup({ advanceTimers: jest.advanceTimersByTime })
.click(wrapper.container.querySelector('.ant-btn')!);
expect(resetEffect).toHaveBeenCalledTimes(2);
waveInstanceMock.animationStart = false;
fireEvent(wrapper.container.querySelector('.ant-btn')!, new Event('transitionstart'));