2020-09-18 16:53:18 +08:00
|
|
|
import React from 'react';
|
|
|
|
import Button from '..';
|
2022-06-06 17:33:42 +08:00
|
|
|
import { sleep, render, fireEvent } from '../../../tests/utils';
|
|
|
|
|
|
|
|
// Mock Wave ref
|
|
|
|
let waveInstanceMock: any;
|
|
|
|
jest.mock('../../_util/wave', () => {
|
|
|
|
const Wave = jest.requireActual('../../_util/wave');
|
|
|
|
const WaveComponent = Wave.default;
|
|
|
|
return {
|
|
|
|
...Wave,
|
|
|
|
__esModule: true,
|
|
|
|
default: (props: any) => (
|
|
|
|
<WaveComponent
|
|
|
|
ref={(node: any) => {
|
|
|
|
waveInstanceMock = node;
|
|
|
|
}}
|
|
|
|
{...props}
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
};
|
|
|
|
});
|
2020-09-18 16:53:18 +08:00
|
|
|
|
|
|
|
describe('click wave effect', () => {
|
|
|
|
async function clickButton(wrapper: any) {
|
2022-06-06 17:33:42 +08:00
|
|
|
const element = wrapper.container.firstChild;
|
|
|
|
fireEvent.click(element);
|
|
|
|
fireEvent(element, new Event('transitionstart'));
|
2020-09-18 16:53:18 +08:00
|
|
|
await sleep(20);
|
2022-06-06 17:33:42 +08:00
|
|
|
fireEvent(element, new Event('animationend'));
|
2020-09-18 16:53:18 +08:00
|
|
|
await sleep(20);
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should have click wave effect for primary button', async () => {
|
2022-06-06 17:33:42 +08:00
|
|
|
const wrapper = render(<Button type="primary">button</Button>);
|
2020-09-18 17:53:37 +08:00
|
|
|
await clickButton(wrapper);
|
2022-06-06 17:33:42 +08:00
|
|
|
expect(wrapper.container.querySelector('.ant-btn')).toHaveAttribute(
|
|
|
|
'ant-click-animating-without-extra-node',
|
|
|
|
);
|
2020-09-18 16:53:18 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should have click wave effect for default button', async () => {
|
2022-06-06 17:33:42 +08:00
|
|
|
const wrapper = render(<Button>button</Button>);
|
2020-09-18 16:53:18 +08:00
|
|
|
await clickButton(wrapper);
|
2022-06-06 17:33:42 +08:00
|
|
|
expect(wrapper.container.querySelector('.ant-btn')).toHaveAttribute(
|
|
|
|
'ant-click-animating-without-extra-node',
|
|
|
|
);
|
2020-09-18 16:53:18 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not have click wave effect for link type button', async () => {
|
2022-06-06 17:33:42 +08:00
|
|
|
const wrapper = render(<Button type="link">button</Button>);
|
2020-09-18 16:53:18 +08:00
|
|
|
await clickButton(wrapper);
|
2022-06-06 17:33:42 +08:00
|
|
|
expect(wrapper.container.querySelector('.ant-btn')).not.toHaveAttribute(
|
|
|
|
'ant-click-animating-without-extra-node',
|
|
|
|
);
|
2020-09-18 16:53:18 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not have click wave effect for text type button', async () => {
|
2022-06-06 17:33:42 +08:00
|
|
|
const wrapper = render(<Button type="text">button</Button>);
|
2020-09-18 16:53:18 +08:00
|
|
|
await clickButton(wrapper);
|
2022-06-06 17:33:42 +08:00
|
|
|
expect(wrapper.container.querySelector('.ant-btn')).not.toHaveAttribute(
|
|
|
|
'ant-click-animating-without-extra-node',
|
|
|
|
);
|
2020-09-18 16:53:18 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle transitionstart', async () => {
|
2022-06-06 17:33:42 +08:00
|
|
|
const wrapper = render(<Button type="primary">button</Button>);
|
2020-09-18 16:53:18 +08:00
|
|
|
await clickButton(wrapper);
|
2022-06-06 17:33:42 +08:00
|
|
|
const buttonNode = wrapper.container.querySelector('.ant-btn')!;
|
|
|
|
fireEvent(buttonNode, new Event('transitionstart'));
|
|
|
|
expect(wrapper.container.querySelector('.ant-btn')).toHaveAttribute(
|
|
|
|
'ant-click-animating-without-extra-node',
|
|
|
|
);
|
2020-09-18 16:53:18 +08:00
|
|
|
wrapper.unmount();
|
2022-06-06 17:33:42 +08:00
|
|
|
fireEvent(buttonNode, new Event('transitionstart'));
|
2020-09-18 16:53:18 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should run resetEffect in transitionstart', async () => {
|
2022-06-06 17:33:42 +08:00
|
|
|
const wrapper = render(<Button type="primary">button</Button>);
|
|
|
|
const resetEffect = jest.spyOn(waveInstanceMock, 'resetEffect');
|
2020-09-18 16:53:18 +08:00
|
|
|
await clickButton(wrapper);
|
|
|
|
expect(resetEffect).toHaveBeenCalledTimes(1);
|
2022-06-06 17:33:42 +08:00
|
|
|
fireEvent.click(wrapper.container.querySelector('.ant-btn')!);
|
2020-09-18 16:53:18 +08:00
|
|
|
await sleep(10);
|
|
|
|
expect(resetEffect).toHaveBeenCalledTimes(2);
|
2022-06-06 17:33:42 +08:00
|
|
|
waveInstanceMock.animationStart = false;
|
|
|
|
fireEvent(wrapper.container.querySelector('.ant-btn')!, new Event('transitionstart'));
|
2020-09-18 16:53:18 +08:00
|
|
|
expect(resetEffect).toHaveBeenCalledTimes(3);
|
|
|
|
resetEffect.mockRestore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle transitionend', async () => {
|
2022-06-06 17:33:42 +08:00
|
|
|
const wrapper = render(<Button type="primary">button</Button>);
|
|
|
|
const resetEffect = jest.spyOn(waveInstanceMock, 'resetEffect');
|
2020-09-18 16:53:18 +08:00
|
|
|
await clickButton(wrapper);
|
|
|
|
expect(resetEffect).toHaveBeenCalledTimes(1);
|
|
|
|
const event = new Event('animationend');
|
|
|
|
Object.assign(event, { animationName: 'fadeEffect' });
|
2022-06-06 17:33:42 +08:00
|
|
|
fireEvent(wrapper.container.querySelector('.ant-btn')!, event);
|
2020-09-18 16:53:18 +08:00
|
|
|
expect(resetEffect).toHaveBeenCalledTimes(2);
|
2020-09-18 17:53:37 +08:00
|
|
|
resetEffect.mockRestore();
|
2020-09-18 16:53:18 +08:00
|
|
|
});
|
2020-09-21 18:32:24 +08:00
|
|
|
|
|
|
|
it('Wave on falsy element', async () => {
|
2022-06-06 17:33:42 +08:00
|
|
|
const { default: Wave } = jest.requireActual('../../_util/wave');
|
|
|
|
let waveInstance: any;
|
|
|
|
render(
|
|
|
|
<Wave
|
|
|
|
ref={(node: any) => {
|
|
|
|
waveInstance = node;
|
|
|
|
}}
|
|
|
|
/>,
|
|
|
|
);
|
2020-09-21 18:32:24 +08:00
|
|
|
waveInstance.resetEffect();
|
|
|
|
});
|
2020-09-18 16:53:18 +08:00
|
|
|
});
|