2022-06-22 14:57:09 +08:00
|
|
|
import React from 'react';
|
2024-04-08 14:04:08 +08:00
|
|
|
|
2019-06-25 12:08:32 +08:00
|
|
|
import Switch from '..';
|
2017-11-19 01:41:40 +08:00
|
|
|
import focusTest from '../../../tests/shared/focusTest';
|
2019-08-26 22:53:20 +08:00
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
2020-01-02 19:10:16 +08:00
|
|
|
import rtlTest from '../../../tests/shared/rtlTest';
|
2023-01-03 16:08:20 +08:00
|
|
|
import { act, fireEvent, render } from '../../../tests/utils';
|
2017-11-19 01:41:40 +08:00
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.mock('rc-util/lib/Dom/isVisible', () => {
|
2022-12-28 23:20:22 +08:00
|
|
|
const mockFn = () => true;
|
2023-06-07 21:59:21 +08:00
|
|
|
return mockFn;
|
2022-12-28 23:20:22 +08:00
|
|
|
});
|
|
|
|
|
2017-11-19 01:41:40 +08:00
|
|
|
describe('Switch', () => {
|
2020-04-26 22:33:36 +08:00
|
|
|
focusTest(Switch, { refFocus: true });
|
2019-08-26 22:53:20 +08:00
|
|
|
mountTest(Switch);
|
2020-01-02 19:10:16 +08:00
|
|
|
rtlTest(Switch);
|
2018-11-12 12:38:02 +08:00
|
|
|
|
2022-12-28 23:20:22 +08:00
|
|
|
it('should has click wave effect', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useFakeTimers();
|
2022-07-01 11:55:03 +08:00
|
|
|
const { container } = render(<Switch />);
|
|
|
|
fireEvent.click(container.querySelector('.ant-switch')!);
|
2023-01-03 16:08:20 +08:00
|
|
|
act(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.advanceTimersByTime(100);
|
2023-01-03 16:08:20 +08:00
|
|
|
});
|
2023-08-04 11:22:33 +08:00
|
|
|
|
|
|
|
// Second time for raf to render wave effect
|
|
|
|
act(() => {
|
|
|
|
jest.advanceTimersByTime(100);
|
|
|
|
});
|
2022-12-28 23:20:22 +08:00
|
|
|
expect(document.querySelector('.ant-wave')).toBeTruthy();
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.clearAllTimers();
|
|
|
|
jest.useRealTimers();
|
2018-11-12 12:38:02 +08:00
|
|
|
});
|
2019-08-27 18:29:20 +08:00
|
|
|
|
2023-11-09 13:42:25 +08:00
|
|
|
it('should be controlled by value', () => {
|
|
|
|
const mockChangeHandler = jest.fn();
|
2019-08-27 18:29:20 +08:00
|
|
|
|
2023-11-09 13:42:25 +08:00
|
|
|
const { getByRole } = render(<Switch value onChange={mockChangeHandler} />);
|
|
|
|
|
|
|
|
const switchNode = getByRole('switch');
|
|
|
|
expect(switchNode).toBeTruthy();
|
|
|
|
expect(getByRole('switch')).toBeChecked();
|
|
|
|
|
|
|
|
fireEvent.click(switchNode);
|
|
|
|
|
|
|
|
expect(mockChangeHandler).toHaveBeenCalledWith(false, expect.anything());
|
|
|
|
// controlled component, so still true after click
|
|
|
|
expect(getByRole('switch')).toBeChecked();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be uncontrolled by defaultValue', () => {
|
|
|
|
const mockChangeHandler = jest.fn();
|
|
|
|
|
|
|
|
const { getByRole } = render(<Switch defaultValue onChange={mockChangeHandler} />);
|
|
|
|
|
|
|
|
const switchNode = getByRole('switch');
|
|
|
|
expect(switchNode).toBeTruthy();
|
|
|
|
expect(getByRole('switch')).toBeChecked();
|
|
|
|
|
|
|
|
fireEvent.click(switchNode);
|
|
|
|
|
|
|
|
expect(mockChangeHandler).toHaveBeenCalledWith(false, expect.anything());
|
|
|
|
// uncontrolled component, so false after click
|
|
|
|
expect(getByRole('switch')).not.toBeChecked();
|
2019-08-27 18:29:20 +08:00
|
|
|
});
|
2023-11-10 18:33:01 +08:00
|
|
|
|
|
|
|
it('have static property for type detecting', () => {
|
|
|
|
expect(Switch.__ANT_SWITCH).toBeTruthy();
|
|
|
|
});
|
2017-11-19 01:41:40 +08:00
|
|
|
});
|