2020-09-18 16:53:18 +08:00
|
|
|
import React from 'react';
|
|
|
|
import Button from '..';
|
2022-10-19 11:50:18 +08:00
|
|
|
import { fireEvent, render, sleep, assertsExist } from '../../../tests/utils';
|
2022-06-06 17:33:42 +08:00
|
|
|
|
|
|
|
// Mock Wave ref
|
2022-10-19 20:24:00 +08:00
|
|
|
let waveInstanceMock: any;
|
2022-06-06 17:33:42 +08:00
|
|
|
jest.mock('../../_util/wave', () => {
|
2022-10-19 11:50:18 +08:00
|
|
|
const Wave: typeof import('../../_util/wave') = jest.requireActual('../../_util/wave');
|
2022-06-06 17:33:42 +08:00
|
|
|
const WaveComponent = Wave.default;
|
2022-10-19 11:50:18 +08:00
|
|
|
|
2022-06-06 17:33:42 +08:00
|
|
|
return {
|
|
|
|
...Wave,
|
|
|
|
__esModule: true,
|
2022-10-19 11:50:18 +08:00
|
|
|
default: (props: import('../../_util/wave').WaveProps) => (
|
2022-06-06 17:33:42 +08:00
|
|
|
<WaveComponent
|
2022-11-19 13:47:33 +08:00
|
|
|
ref={(node) => {
|
2022-06-06 17:33:42 +08:00
|
|
|
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>);
|
2022-10-19 11:50:18 +08:00
|
|
|
assertsExist(waveInstanceMock);
|
2022-06-06 17:33:42 +08:00
|
|
|
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>);
|
2022-10-19 11:50:18 +08:00
|
|
|
assertsExist(waveInstanceMock);
|
2022-06-06 17:33:42 +08:00
|
|
|
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
|
|
|
});
|