test: Update swicth test case (#38284)

This commit is contained in:
hms181231 2022-10-31 10:22:38 +08:00 committed by GitHub
parent a927ef62a3
commit 9352d24bc4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 5 deletions

View File

@ -3,7 +3,7 @@ import Switch from '..';
import focusTest from '../../../tests/shared/focusTest'; import focusTest from '../../../tests/shared/focusTest';
import mountTest from '../../../tests/shared/mountTest'; import mountTest from '../../../tests/shared/mountTest';
import rtlTest from '../../../tests/shared/rtlTest'; import rtlTest from '../../../tests/shared/rtlTest';
import { sleep, fireEvent, render } from '../../../tests/utils'; import { waitFakeTimer, fireEvent, render } from '../../../tests/utils';
import { resetWarned } from '../../_util/warning'; import { resetWarned } from '../../_util/warning';
describe('Switch', () => { describe('Switch', () => {
@ -12,10 +12,13 @@ describe('Switch', () => {
rtlTest(Switch); rtlTest(Switch);
it('should has click wave effect', async () => { it('should has click wave effect', async () => {
jest.useFakeTimers();
const { container } = render(<Switch />); const { container } = render(<Switch />);
fireEvent.click(container.querySelector('.ant-switch')!); fireEvent.click(container.querySelector('.ant-switch')!);
await sleep(0); await waitFakeTimer();
expect(container.querySelector('button')!.getAttribute('ant-click-animating')).toBe('true'); expect(container.querySelector('button')!.getAttribute('ant-click-animating')).toBe('true');
jest.clearAllTimers();
jest.useRealTimers();
}); });
it('warning if set `value`', () => { it('warning if set `value`', () => {

View File

@ -1,17 +1,18 @@
import React from 'react'; import React from 'react';
import Switch from '..'; import Switch from '..';
import { sleep, render, fireEvent } from '../../../tests/utils'; import { waitFakeTimer, render, fireEvent } from '../../../tests/utils';
describe('click wave effect', () => { describe('click wave effect', () => {
async function click(container: HTMLElement) { async function click(container: HTMLElement) {
fireEvent.click(container.querySelector('.ant-switch')!); fireEvent.click(container.querySelector('.ant-switch')!);
container.querySelector('.ant-switch')!.dispatchEvent(new Event('transitionstart')); container.querySelector('.ant-switch')!.dispatchEvent(new Event('transitionstart'));
await sleep(20); await waitFakeTimer();
container.querySelector('.ant-switch')!.dispatchEvent(new Event('animationend')); container.querySelector('.ant-switch')!.dispatchEvent(new Event('animationend'));
await sleep(20); await waitFakeTimer();
} }
it('should have click wave effect', async () => { it('should have click wave effect', async () => {
jest.useFakeTimers();
const { container } = render(<Switch />); const { container } = render(<Switch />);
await click(container); await click(container);
await click(container); await click(container);
@ -23,5 +24,7 @@ describe('click wave effect', () => {
const event = new Event('animationend'); const event = new Event('animationend');
Object.assign(event, { animationName: 'fadeEffect' }); Object.assign(event, { animationName: 'fadeEffect' });
container.querySelector('.ant-switch')!.dispatchEvent(event); container.querySelector('.ant-switch')!.dispatchEvent(event);
jest.clearAllTimers();
jest.useRealTimers();
}); });
}); });