2017-08-24 12:56:25 +08:00
|
|
|
import React from 'react';
|
2024-04-08 14:04:08 +08:00
|
|
|
|
2017-08-24 12:56:25 +08:00
|
|
|
import Checkbox from '..';
|
2024-04-08 14:04:08 +08:00
|
|
|
import { resetWarned } from '../../_util/warning';
|
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';
|
2024-12-03 19:07:28 +08:00
|
|
|
import { act, fireEvent, render } from '../../../tests/utils';
|
2017-08-24 12:56:25 +08:00
|
|
|
|
|
|
|
describe('Checkbox', () => {
|
2020-11-20 08:58:14 +08:00
|
|
|
focusTest(Checkbox, { refFocus: true });
|
2019-08-26 22:53:20 +08:00
|
|
|
mountTest(Checkbox);
|
2020-01-02 19:10:16 +08:00
|
|
|
rtlTest(Checkbox);
|
2017-11-19 01:41:40 +08:00
|
|
|
|
2024-12-03 19:07:28 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
2017-08-24 12:56:25 +08:00
|
|
|
it('responses hover events', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const onMouseEnter = jest.fn();
|
|
|
|
const onMouseLeave = jest.fn();
|
2017-08-24 12:56:25 +08:00
|
|
|
|
2022-05-05 10:25:22 +08:00
|
|
|
const { container } = render(
|
|
|
|
<Checkbox onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave} />,
|
|
|
|
);
|
2017-08-24 12:56:25 +08:00
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
fireEvent.mouseEnter(container.querySelector('label')!);
|
2017-08-24 12:56:25 +08:00
|
|
|
expect(onMouseEnter).toHaveBeenCalled();
|
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
fireEvent.mouseLeave(container.querySelector('label')!);
|
2017-08-24 12:56:25 +08:00
|
|
|
expect(onMouseLeave).toHaveBeenCalled();
|
|
|
|
});
|
2019-08-27 18:29:20 +08:00
|
|
|
|
|
|
|
it('warning if set `value`', () => {
|
|
|
|
resetWarned();
|
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
2022-05-05 10:25:22 +08:00
|
|
|
render(<Checkbox value />);
|
2019-08-27 18:29:20 +08:00
|
|
|
expect(errorSpy).toHaveBeenCalledWith(
|
2020-04-14 16:48:23 +08:00
|
|
|
'Warning: [antd: Checkbox] `value` is not a valid prop, do you mean `checked`?',
|
2019-08-27 18:29:20 +08:00
|
|
|
);
|
|
|
|
errorSpy.mockRestore();
|
|
|
|
});
|
2024-09-14 14:39:53 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/50768
|
|
|
|
it('onFocus / onBlur', () => {
|
|
|
|
const onBlur = jest.fn();
|
|
|
|
const onFocus = jest.fn();
|
|
|
|
|
|
|
|
const { container } = render(<Checkbox onBlur={onBlur} onFocus={onFocus} />);
|
|
|
|
const inputEl = container.querySelector('input')!;
|
|
|
|
|
|
|
|
fireEvent.focus(inputEl);
|
|
|
|
fireEvent.blur(inputEl);
|
|
|
|
|
|
|
|
expect(onFocus).toHaveBeenCalledTimes(1);
|
|
|
|
expect(onBlur).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
2024-10-23 16:28:03 +08:00
|
|
|
|
|
|
|
it('should reflect indeterminate state correctly', () => {
|
|
|
|
const { rerender, container } = render(<Checkbox indeterminate />);
|
|
|
|
|
|
|
|
const checkboxInput = container.querySelector('input')!;
|
|
|
|
expect(checkboxInput.indeterminate).toBe(true);
|
|
|
|
|
|
|
|
rerender(<Checkbox indeterminate={false} />);
|
|
|
|
|
|
|
|
expect(checkboxInput.indeterminate).toBe(false);
|
|
|
|
});
|
2024-12-03 19:07:28 +08:00
|
|
|
|
|
|
|
it('event bubble should not trigger twice', () => {
|
|
|
|
const onClick = jest.fn();
|
|
|
|
const onRootClick = jest.fn();
|
|
|
|
|
|
|
|
const { container } = render(
|
|
|
|
<div onClick={onRootClick}>
|
|
|
|
<Checkbox onClick={onClick} />
|
|
|
|
</div>,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Click on label
|
|
|
|
fireEvent.click(container.querySelector('label')!);
|
|
|
|
expect(onClick).toHaveBeenCalledTimes(1);
|
|
|
|
expect(onRootClick).toHaveBeenCalledTimes(1);
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Click on input
|
|
|
|
fireEvent.click(container.querySelector('input')!);
|
|
|
|
expect(onClick).toHaveBeenCalledTimes(2);
|
|
|
|
expect(onRootClick).toHaveBeenCalledTimes(2);
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Click on input again
|
|
|
|
fireEvent.click(container.querySelector('input')!);
|
|
|
|
expect(onClick).toHaveBeenCalledTimes(3);
|
|
|
|
expect(onRootClick).toHaveBeenCalledTimes(3);
|
|
|
|
});
|
2017-08-24 12:56:25 +08:00
|
|
|
});
|