2022-06-22 14:57:09 +08:00
|
|
|
import React from 'react';
|
2020-03-19 19:27:02 +08:00
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
2022-12-28 23:20:22 +08:00
|
|
|
import { render, fireEvent, getByText, waitFakeTimer } from '../../../tests/utils';
|
2022-06-22 14:57:09 +08:00
|
|
|
import Wave from '../wave';
|
2022-12-28 23:20:22 +08:00
|
|
|
|
|
|
|
(global as any).isVisible = true;
|
|
|
|
|
|
|
|
jest.mock('rc-util/lib/Dom/isVisible', () => {
|
|
|
|
const mockFn = () => (global as any).isVisible;
|
|
|
|
return mockFn;
|
|
|
|
});
|
2020-03-19 19:27:02 +08:00
|
|
|
|
|
|
|
describe('Wave component', () => {
|
|
|
|
mountTest(Wave);
|
|
|
|
|
2022-10-12 22:46:16 +08:00
|
|
|
beforeAll(() => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
2022-08-23 23:50:33 +08:00
|
|
|
beforeEach(() => {
|
2022-12-28 23:20:22 +08:00
|
|
|
(global as any).isVisible = true;
|
2022-08-23 23:50:33 +08:00
|
|
|
document.body.innerHTML = '';
|
|
|
|
});
|
|
|
|
|
2020-03-19 19:27:02 +08:00
|
|
|
afterEach(() => {
|
2022-10-12 22:46:16 +08:00
|
|
|
jest.clearAllTimers();
|
2020-03-19 19:27:02 +08:00
|
|
|
const styles = document.getElementsByTagName('style');
|
|
|
|
for (let i = 0; i < styles.length; i += 1) {
|
|
|
|
styles[i].remove();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-12-28 23:20:22 +08:00
|
|
|
function getWaveStyle() {
|
|
|
|
const styleObj: Record<string, string> = {};
|
|
|
|
const { style } = document.querySelector<HTMLElement>('.ant-wave')!;
|
|
|
|
style.cssText.split(';').forEach((kv) => {
|
|
|
|
if (kv.trim()) {
|
|
|
|
const cells = kv.split(':');
|
|
|
|
styleObj[cells[0].trim()] = cells[1].trim();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return styleObj;
|
2022-08-23 23:50:33 +08:00
|
|
|
}
|
|
|
|
|
2022-12-28 23:20:22 +08:00
|
|
|
it('work', async () => {
|
2022-08-21 23:25:00 +08:00
|
|
|
const { container, unmount } = render(
|
2020-05-12 12:44:49 +08:00
|
|
|
<Wave>
|
|
|
|
<button type="button">button</button>
|
|
|
|
</Wave>,
|
|
|
|
);
|
2022-08-21 23:25:00 +08:00
|
|
|
|
2022-12-28 23:20:22 +08:00
|
|
|
fireEvent.click(container.querySelector('button')!);
|
|
|
|
expect(document.querySelector('.ant-wave')).toBeTruthy();
|
|
|
|
|
|
|
|
// Match deadline
|
|
|
|
await waitFakeTimer();
|
|
|
|
|
|
|
|
expect(document.querySelector('.ant-wave')).toBeFalsy();
|
2022-08-21 23:25:00 +08:00
|
|
|
|
|
|
|
unmount();
|
2020-03-19 19:27:02 +08:00
|
|
|
});
|
|
|
|
|
2022-12-28 23:20:22 +08:00
|
|
|
it('invisible in screen', () => {
|
|
|
|
(global as any).isVisible = false;
|
2022-08-21 23:25:00 +08:00
|
|
|
const { container, unmount } = render(
|
2020-05-12 12:44:49 +08:00
|
|
|
<Wave>
|
|
|
|
<button type="button">button</button>
|
|
|
|
</Wave>,
|
|
|
|
);
|
2022-12-28 23:20:22 +08:00
|
|
|
|
|
|
|
fireEvent.click(container.querySelector('button')!);
|
|
|
|
expect(document.querySelector('.ant-wave')).toBeFalsy();
|
|
|
|
|
2022-08-21 23:25:00 +08:00
|
|
|
unmount();
|
2020-03-19 19:27:02 +08:00
|
|
|
});
|
|
|
|
|
2022-12-28 23:20:22 +08:00
|
|
|
it('wave color is grey', () => {
|
2022-08-21 23:25:00 +08:00
|
|
|
const { container, unmount } = render(
|
2020-05-12 12:44:49 +08:00
|
|
|
<Wave>
|
2022-06-28 15:20:09 +08:00
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
style={{ borderColor: 'rgb(0, 0, 0)', backgroundColor: 'transparent' }}
|
|
|
|
>
|
2020-05-12 12:44:49 +08:00
|
|
|
button
|
|
|
|
</button>
|
|
|
|
</Wave>,
|
|
|
|
);
|
2022-12-28 23:20:22 +08:00
|
|
|
|
|
|
|
fireEvent.click(container.querySelector('button')!);
|
|
|
|
|
|
|
|
const style = getWaveStyle();
|
|
|
|
|
|
|
|
expect(style['--wave-scale']).toBeTruthy();
|
|
|
|
expect(style['--wave-color']).toBeFalsy();
|
|
|
|
|
2022-08-21 23:25:00 +08:00
|
|
|
unmount();
|
2020-03-19 19:27:02 +08:00
|
|
|
});
|
|
|
|
|
2022-12-28 23:20:22 +08:00
|
|
|
it('wave color is not grey', () => {
|
2022-08-21 23:25:00 +08:00
|
|
|
const { container, unmount } = render(
|
2020-05-12 12:44:49 +08:00
|
|
|
<Wave>
|
|
|
|
<button type="button" style={{ borderColor: 'red' }}>
|
|
|
|
button
|
|
|
|
</button>
|
|
|
|
</Wave>,
|
|
|
|
);
|
2022-12-28 23:20:22 +08:00
|
|
|
|
|
|
|
fireEvent.click(container.querySelector('button')!);
|
|
|
|
|
|
|
|
const style = getWaveStyle();
|
|
|
|
expect(style['--wave-color']).toEqual('red');
|
|
|
|
|
2022-08-21 23:25:00 +08:00
|
|
|
unmount();
|
2020-03-19 19:27:02 +08:00
|
|
|
});
|
|
|
|
|
2022-12-28 23:20:22 +08:00
|
|
|
it('read wave color from border-top-color', () => {
|
2022-08-21 23:25:00 +08:00
|
|
|
const { container, unmount } = render(
|
2020-05-12 12:44:49 +08:00
|
|
|
<Wave>
|
|
|
|
<div style={{ borderTopColor: 'blue' }}>button</div>
|
|
|
|
</Wave>,
|
|
|
|
);
|
2022-12-28 23:20:22 +08:00
|
|
|
|
|
|
|
fireEvent.click(getByText(container, 'button')!);
|
|
|
|
|
|
|
|
const style = getWaveStyle();
|
|
|
|
expect(style['--wave-color']).toEqual('blue');
|
|
|
|
|
2022-08-21 23:25:00 +08:00
|
|
|
unmount();
|
2020-03-19 19:27:02 +08:00
|
|
|
});
|
|
|
|
|
2022-12-28 23:20:22 +08:00
|
|
|
it('read wave color from background color', () => {
|
2022-08-21 23:25:00 +08:00
|
|
|
const { container, unmount } = render(
|
2020-05-12 12:44:49 +08:00
|
|
|
<Wave>
|
|
|
|
<div style={{ backgroundColor: 'green' }}>button</div>
|
|
|
|
</Wave>,
|
|
|
|
);
|
2022-12-28 23:20:22 +08:00
|
|
|
|
|
|
|
fireEvent.click(getByText(container, 'button')!);
|
|
|
|
|
|
|
|
const style = getWaveStyle();
|
|
|
|
expect(style['--wave-color']).toEqual('green');
|
|
|
|
|
2022-08-21 23:25:00 +08:00
|
|
|
unmount();
|
2020-03-19 19:27:02 +08:00
|
|
|
});
|
|
|
|
|
2022-12-28 23:20:22 +08:00
|
|
|
it('read wave color from border firstly', () => {
|
2022-08-21 23:25:00 +08:00
|
|
|
const { container, unmount } = render(
|
2020-05-12 12:44:49 +08:00
|
|
|
<Wave>
|
|
|
|
<div style={{ borderColor: 'yellow', backgroundColor: 'green' }}>button</div>
|
|
|
|
</Wave>,
|
|
|
|
);
|
2022-12-28 23:20:22 +08:00
|
|
|
|
|
|
|
fireEvent.click(getByText(container, 'button')!);
|
|
|
|
|
|
|
|
const style = getWaveStyle();
|
|
|
|
expect(style['--wave-color']).toEqual('yellow');
|
|
|
|
|
2022-08-21 23:25:00 +08:00
|
|
|
unmount();
|
2020-03-19 19:27:02 +08:00
|
|
|
});
|
|
|
|
|
2022-12-28 23:20:22 +08:00
|
|
|
it('hidden element with -leave className', () => {
|
2022-08-21 23:25:00 +08:00
|
|
|
const { container, unmount } = render(
|
2020-05-12 12:44:49 +08:00
|
|
|
<Wave>
|
|
|
|
<button type="button" className="xx-leave">
|
|
|
|
button
|
|
|
|
</button>
|
|
|
|
</Wave>,
|
|
|
|
);
|
2020-03-19 19:27:02 +08:00
|
|
|
|
2022-12-28 23:20:22 +08:00
|
|
|
fireEvent.click(container.querySelector('button')!);
|
|
|
|
expect(document.querySelector('.ant-wave')).toBeFalsy();
|
|
|
|
|
2022-08-21 23:25:00 +08:00
|
|
|
unmount();
|
2020-03-19 19:27:02 +08:00
|
|
|
});
|
2022-06-27 16:57:34 +08:00
|
|
|
|
2022-12-28 23:20:22 +08:00
|
|
|
it('not show when disabled', () => {
|
|
|
|
const { container } = render(
|
|
|
|
<Wave>
|
2022-06-27 16:57:34 +08:00
|
|
|
<button type="button" disabled>
|
|
|
|
button
|
|
|
|
</button>
|
|
|
|
</Wave>,
|
|
|
|
);
|
2022-12-28 23:20:22 +08:00
|
|
|
|
|
|
|
fireEvent.click(container.querySelector('button')!);
|
|
|
|
expect(document.querySelector('.ant-wave')).toBeFalsy();
|
2022-06-27 16:57:34 +08:00
|
|
|
});
|
|
|
|
|
2022-12-28 23:20:22 +08:00
|
|
|
it('not show when hidden', () => {
|
|
|
|
(global as any).isVisible = false;
|
|
|
|
|
|
|
|
const { container } = render(
|
|
|
|
<Wave>
|
2022-06-27 16:57:34 +08:00
|
|
|
<button type="button" style={{ display: 'none' }}>
|
|
|
|
button
|
|
|
|
</button>
|
|
|
|
</Wave>,
|
|
|
|
);
|
2022-12-28 23:20:22 +08:00
|
|
|
|
|
|
|
fireEvent.click(container.querySelector('button')!);
|
|
|
|
expect(document.querySelector('.ant-wave')).toBeFalsy();
|
2022-06-27 16:57:34 +08:00
|
|
|
});
|
|
|
|
|
2022-12-28 23:20:22 +08:00
|
|
|
it('not show when is input', () => {
|
|
|
|
const { container } = render(
|
|
|
|
<Wave>
|
2022-06-27 16:57:34 +08:00
|
|
|
<input />
|
|
|
|
</Wave>,
|
|
|
|
);
|
2022-12-28 23:20:22 +08:00
|
|
|
|
|
|
|
fireEvent.click(container.querySelector('input')!);
|
|
|
|
expect(document.querySelector('.ant-wave')).toBeFalsy();
|
2022-06-27 16:57:34 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not throw when click it', () => {
|
|
|
|
expect(() => {
|
2022-08-21 23:25:00 +08:00
|
|
|
const { container } = render(
|
2022-06-27 16:57:34 +08:00
|
|
|
<Wave>
|
|
|
|
<div />
|
|
|
|
</Wave>,
|
|
|
|
);
|
2022-08-21 23:25:00 +08:00
|
|
|
fireEvent.click(container);
|
2022-06-27 16:57:34 +08:00
|
|
|
}).not.toThrow();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not throw when no children', () => {
|
2022-08-21 23:25:00 +08:00
|
|
|
expect(() => render(<Wave />)).not.toThrow();
|
2022-06-27 16:57:34 +08:00
|
|
|
});
|
2022-06-28 15:20:09 +08:00
|
|
|
|
2022-12-28 23:20:22 +08:00
|
|
|
it('wave color should inferred if border is transparent and background is not', () => {
|
2022-08-23 18:00:20 +08:00
|
|
|
const { container, unmount } = render(
|
2022-06-28 15:20:09 +08:00
|
|
|
<Wave>
|
|
|
|
<button type="button" style={{ borderColor: 'transparent', background: 'red' }}>
|
|
|
|
button
|
|
|
|
</button>
|
|
|
|
</Wave>,
|
|
|
|
);
|
2022-08-23 18:00:20 +08:00
|
|
|
fireEvent.click(container.querySelector('button')!);
|
2022-12-28 23:20:22 +08:00
|
|
|
|
|
|
|
const style = getWaveStyle();
|
|
|
|
expect(style['--wave-color']).toEqual('red');
|
|
|
|
|
2022-08-23 18:00:20 +08:00
|
|
|
unmount();
|
2022-06-28 15:20:09 +08:00
|
|
|
});
|
|
|
|
|
2022-12-28 23:20:22 +08:00
|
|
|
it('wave color should inferred if borderTopColor is transparent and borderColor is not', () => {
|
2022-08-23 18:00:20 +08:00
|
|
|
const { container, unmount } = render(
|
2022-06-28 15:20:09 +08:00
|
|
|
<Wave>
|
|
|
|
<button type="button" style={{ borderColor: 'red', borderTopColor: 'transparent' }}>
|
|
|
|
button
|
|
|
|
</button>
|
|
|
|
</Wave>,
|
|
|
|
);
|
2022-12-28 23:20:22 +08:00
|
|
|
|
2022-08-23 18:00:20 +08:00
|
|
|
fireEvent.click(container.querySelector('button')!);
|
2022-08-23 23:50:33 +08:00
|
|
|
|
2022-12-28 23:20:22 +08:00
|
|
|
const style = getWaveStyle();
|
|
|
|
expect(style['--wave-color']).toEqual('red');
|
|
|
|
|
2022-08-23 18:00:20 +08:00
|
|
|
unmount();
|
2022-06-28 15:20:09 +08:00
|
|
|
});
|
2022-09-22 11:47:05 +08:00
|
|
|
|
2022-09-21 17:43:07 +08:00
|
|
|
it('Wave style should append to validate element', () => {
|
|
|
|
const { container } = render(
|
|
|
|
<Wave>
|
|
|
|
<div className="bamboo" style={{ borderColor: 'red' }} />
|
|
|
|
</Wave>,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Mock shadow container
|
|
|
|
const fakeDoc = document.createElement('div');
|
|
|
|
fakeDoc.append('text');
|
|
|
|
fakeDoc.appendChild(document.createElement('span'));
|
|
|
|
expect(fakeDoc.childNodes).toHaveLength(2);
|
|
|
|
|
2022-12-28 23:20:22 +08:00
|
|
|
const elem = container.querySelector('.bamboo')!;
|
|
|
|
elem.getRootNode = () => fakeDoc;
|
2022-09-21 17:43:07 +08:00
|
|
|
|
2022-12-28 23:20:22 +08:00
|
|
|
// Click should not throw
|
|
|
|
fireEvent.click(elem);
|
2022-09-21 17:43:07 +08:00
|
|
|
|
2022-12-28 23:20:22 +08:00
|
|
|
expect(fakeDoc.querySelector('.ant-wave')).toBeTruthy();
|
2022-09-21 17:43:07 +08:00
|
|
|
});
|
2020-03-19 19:27:02 +08:00
|
|
|
});
|