2022-08-15 15:57:19 +08:00
|
|
|
import Dayjs from 'dayjs';
|
|
|
|
import 'dayjs/locale/zh-cn';
|
2023-02-22 19:49:06 +08:00
|
|
|
import MockDate from 'mockdate';
|
|
|
|
import { type PickerPanelProps } from 'rc-picker';
|
2022-08-15 15:57:19 +08:00
|
|
|
import dayjsGenerateConfig from 'rc-picker/lib/generate/dayjs';
|
2022-08-08 22:43:57 +08:00
|
|
|
import type { Locale } from 'rc-picker/lib/interface';
|
2023-04-02 13:45:57 +08:00
|
|
|
import { resetWarned } from 'rc-util/lib/warning';
|
2024-01-15 16:28:45 +08:00
|
|
|
import React from 'react';
|
2022-08-08 22:43:57 +08:00
|
|
|
import Calendar from '..';
|
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
|
|
|
import rtlTest from '../../../tests/shared/rtlTest';
|
|
|
|
import { fireEvent, render } from '../../../tests/utils';
|
|
|
|
import Group from '../../radio/group';
|
|
|
|
import Button from '../../radio/radioButton';
|
|
|
|
import Select from '../../select';
|
2022-10-03 20:31:37 +08:00
|
|
|
import Header, { type CalendarHeaderProps } from '../Header';
|
|
|
|
|
2022-10-19 11:50:18 +08:00
|
|
|
const ref: {
|
2024-01-15 16:28:45 +08:00
|
|
|
calendarProps?: PickerPanelProps<unknown>;
|
2022-10-19 11:50:18 +08:00
|
|
|
calendarHeaderProps?: CalendarHeaderProps<unknown>;
|
|
|
|
} = {};
|
2022-10-03 20:31:37 +08:00
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.mock('../Header', () => {
|
|
|
|
const HeaderModule = jest.requireActual('../Header');
|
2022-10-03 20:31:37 +08:00
|
|
|
const HeaderComponent = HeaderModule.default;
|
2023-06-07 21:59:21 +08:00
|
|
|
return (props: CalendarHeaderProps<any>) => {
|
|
|
|
ref.calendarHeaderProps = props;
|
|
|
|
return <HeaderComponent {...props} />;
|
2022-10-03 20:31:37 +08:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.mock('rc-picker', () => {
|
|
|
|
const RcPicker = jest.requireActual('rc-picker');
|
2022-10-03 20:31:37 +08:00
|
|
|
const PickerPanelComponent = RcPicker.PickerPanel;
|
|
|
|
return {
|
|
|
|
...RcPicker,
|
2024-01-15 16:28:45 +08:00
|
|
|
PickerPanel: (props: PickerPanelProps<unknown>) => {
|
2022-10-19 11:50:18 +08:00
|
|
|
ref.calendarProps = props;
|
2022-10-03 20:31:37 +08:00
|
|
|
return <PickerPanelComponent {...props} />;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
2022-08-08 22:43:57 +08:00
|
|
|
|
|
|
|
describe('Calendar', () => {
|
|
|
|
mountTest(Calendar);
|
2023-02-22 19:49:06 +08:00
|
|
|
rtlTest(Calendar, true);
|
2022-08-08 22:43:57 +08:00
|
|
|
|
|
|
|
function openSelect(wrapper: HTMLElement, className: string) {
|
|
|
|
fireEvent.mouseDown(wrapper.querySelector(className)!.querySelector('.ant-select-selector')!);
|
|
|
|
}
|
|
|
|
|
|
|
|
function findSelectItem(wrapper: HTMLElement) {
|
|
|
|
return wrapper.querySelectorAll('.ant-select-item-option')!;
|
|
|
|
}
|
|
|
|
|
|
|
|
function clickSelectItem(wrapper: HTMLElement, index = 0) {
|
|
|
|
fireEvent.click(findSelectItem(wrapper)[index]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/30392
|
|
|
|
it('should be able to set undefined or null', () => {
|
|
|
|
expect(() => {
|
2022-10-03 20:31:37 +08:00
|
|
|
render(<Calendar value={null as any} />);
|
2022-08-08 22:43:57 +08:00
|
|
|
}).not.toThrow();
|
2022-10-03 20:31:37 +08:00
|
|
|
|
2022-08-08 22:43:57 +08:00
|
|
|
expect(() => {
|
2022-10-03 20:31:37 +08:00
|
|
|
render(<Calendar value={undefined} />);
|
2022-08-08 22:43:57 +08:00
|
|
|
}).not.toThrow();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Calendar should be selectable', () => {
|
2022-08-15 15:57:19 +08:00
|
|
|
MockDate.set(Dayjs('2000-01-01').valueOf());
|
2022-08-08 22:43:57 +08:00
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
const onSelect = jest.fn();
|
|
|
|
const onChange = jest.fn();
|
2022-08-08 22:43:57 +08:00
|
|
|
const { container } = render(<Calendar onSelect={onSelect} onChange={onChange} />);
|
|
|
|
|
|
|
|
fireEvent.click(container.querySelector('.ant-picker-cell')!);
|
2023-05-17 21:18:51 +08:00
|
|
|
expect(onSelect).toHaveBeenCalledWith(expect.anything(), { source: 'date' });
|
2022-08-08 22:43:57 +08:00
|
|
|
|
|
|
|
const value = onSelect.mock.calls[0][0];
|
2022-08-15 15:57:19 +08:00
|
|
|
expect(Dayjs.isDayjs(value)).toBe(true);
|
2022-08-08 22:43:57 +08:00
|
|
|
|
|
|
|
expect(onChange).toHaveBeenCalled();
|
|
|
|
|
|
|
|
MockDate.reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('only Valid range should be selectable', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const onSelect = jest.fn();
|
2022-08-15 15:57:19 +08:00
|
|
|
const validRange: [Dayjs.Dayjs, Dayjs.Dayjs] = [Dayjs('2018-02-02'), Dayjs('2018-02-18')];
|
2022-10-03 20:31:37 +08:00
|
|
|
const wrapper = render(
|
2022-08-15 15:57:19 +08:00
|
|
|
<Calendar onSelect={onSelect} validRange={validRange} defaultValue={Dayjs('2018-02-02')} />,
|
2022-08-08 22:43:57 +08:00
|
|
|
);
|
2022-10-03 20:31:37 +08:00
|
|
|
fireEvent.click(wrapper.container.querySelectorAll('[title="2018-02-01"]')[0]);
|
|
|
|
fireEvent.click(wrapper.container.querySelectorAll('[title="2018-02-02"]')[0]);
|
2022-08-08 22:43:57 +08:00
|
|
|
expect(onSelect.mock.calls.length).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('dates other than in valid range should be disabled', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const onSelect = jest.fn();
|
2022-08-15 15:57:19 +08:00
|
|
|
const validRange: [Dayjs.Dayjs, Dayjs.Dayjs] = [Dayjs('2018-02-02'), Dayjs('2018-02-18')];
|
2022-08-08 22:43:57 +08:00
|
|
|
const { container } = render(
|
2022-08-15 15:57:19 +08:00
|
|
|
<Calendar onSelect={onSelect} validRange={validRange} defaultValue={Dayjs('2018-02-02')} />,
|
2022-08-08 22:43:57 +08:00
|
|
|
);
|
|
|
|
fireEvent.click(container.querySelector('[title="2018-02-20"]')!);
|
|
|
|
const elem = container
|
|
|
|
.querySelector('[title="2018-02-20"]')!
|
|
|
|
.className.includes('ant-picker-cell-disabled');
|
2023-02-10 16:15:16 +08:00
|
|
|
expect(elem).toBe(true);
|
2022-08-08 22:43:57 +08:00
|
|
|
expect(onSelect.mock.calls.length).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('months other than in valid range should be disabled', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const onSelect = jest.fn();
|
2022-08-15 15:57:19 +08:00
|
|
|
const validRange: [Dayjs.Dayjs, Dayjs.Dayjs] = [Dayjs('2018-02-02'), Dayjs('2018-05-18')];
|
2022-08-08 22:43:57 +08:00
|
|
|
const { container } = render(
|
|
|
|
<Calendar
|
|
|
|
onSelect={onSelect}
|
|
|
|
validRange={validRange}
|
2022-08-15 15:57:19 +08:00
|
|
|
defaultValue={Dayjs('2018-02-02')}
|
2022-08-08 22:43:57 +08:00
|
|
|
mode="year"
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
expect(
|
|
|
|
container.querySelector('[title="2018-01"]')?.className.includes('ant-picker-cell-disabled'),
|
|
|
|
).toBe(true);
|
|
|
|
expect(
|
|
|
|
container.querySelector('[title="2018-02"]')?.className.includes('ant-picker-cell-disabled'),
|
|
|
|
).toBe(false);
|
|
|
|
expect(
|
|
|
|
container.querySelector('[title="2018-06"]')?.className.includes('ant-picker-cell-disabled'),
|
|
|
|
).toBe(true);
|
|
|
|
fireEvent.click(container.querySelector('[title="2018-01"]')!);
|
|
|
|
fireEvent.click(container.querySelector('[title="2018-03"]')!);
|
|
|
|
expect(onSelect.mock.calls.length).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('months other than in valid range should not be shown in header', () => {
|
2022-08-15 15:57:19 +08:00
|
|
|
const validRange: [Dayjs.Dayjs, Dayjs.Dayjs] = [Dayjs('2017-02-02'), Dayjs('2018-05-18')];
|
2022-08-08 22:43:57 +08:00
|
|
|
const { container } = render(<Calendar validRange={validRange} />);
|
|
|
|
openSelect(container, '.ant-picker-calendar-year-select');
|
|
|
|
clickSelectItem(container);
|
|
|
|
openSelect(container, '.ant-picker-calendar-month-select');
|
|
|
|
// 2 years and 11 months
|
|
|
|
expect(container.querySelectorAll('.ant-select-item-option').length).toBe(13);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('getDateRange should returns a disabledDate function', () => {
|
2022-08-15 15:57:19 +08:00
|
|
|
const validRange: [Dayjs.Dayjs, Dayjs.Dayjs] = [Dayjs('2018-02-02'), Dayjs('2018-05-18')];
|
2022-10-11 20:29:55 +08:00
|
|
|
render(<Calendar validRange={validRange} defaultValue={Dayjs('2018-02-02')} />);
|
2024-01-15 16:28:45 +08:00
|
|
|
expect(ref.calendarProps?.disabledDate?.(Dayjs('2018-06-02'))).toBe(true);
|
|
|
|
expect(ref.calendarProps?.disabledDate?.(Dayjs('2018-04-02'))).toBe(false);
|
2022-08-08 22:43:57 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('validRange should work with disabledDate function', () => {
|
2022-08-15 15:57:19 +08:00
|
|
|
const validRange: [Dayjs.Dayjs, Dayjs.Dayjs] = [Dayjs('2018-02-02'), Dayjs('2018-05-18')];
|
2022-10-03 20:31:37 +08:00
|
|
|
render(
|
2022-11-19 13:47:33 +08:00
|
|
|
<Calendar
|
|
|
|
validRange={validRange}
|
|
|
|
disabledDate={(data) => data.isSame(Dayjs('2018-02-03'))}
|
|
|
|
/>,
|
2022-08-08 22:43:57 +08:00
|
|
|
);
|
|
|
|
|
2024-01-15 16:28:45 +08:00
|
|
|
expect(ref.calendarProps?.disabledDate?.(Dayjs('2018-02-01'))).toBe(true);
|
|
|
|
expect(ref.calendarProps?.disabledDate?.(Dayjs('2018-02-02'))).toBe(false);
|
|
|
|
expect(ref.calendarProps?.disabledDate?.(Dayjs('2018-02-03'))).toBe(true);
|
|
|
|
expect(ref.calendarProps?.disabledDate?.(Dayjs('2018-02-04'))).toBe(false);
|
|
|
|
expect(ref.calendarProps?.disabledDate?.(Dayjs('2018-06-01'))).toBe(true);
|
2022-08-08 22:43:57 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Calendar MonthSelect should display correct label', () => {
|
2022-08-15 15:57:19 +08:00
|
|
|
const validRange: [Dayjs.Dayjs, Dayjs.Dayjs] = [Dayjs('2018-02-02'), Dayjs('2019-06-1')];
|
2022-10-11 20:29:55 +08:00
|
|
|
const wrapper = render(<Calendar validRange={validRange} defaultValue={Dayjs('2019-01-01')} />);
|
2022-10-03 20:31:37 +08:00
|
|
|
expect(wrapper.container.children[0]).toMatchSnapshot();
|
2022-08-08 22:43:57 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Calendar should change mode by prop', () => {
|
|
|
|
const monthMode = 'month';
|
|
|
|
const yearMode = 'year';
|
2022-10-03 20:31:37 +08:00
|
|
|
const wrapper = render(<Calendar />);
|
2022-10-19 11:50:18 +08:00
|
|
|
expect(ref.calendarHeaderProps?.mode).toEqual(monthMode);
|
2022-10-03 20:31:37 +08:00
|
|
|
wrapper.rerender(<Calendar mode={yearMode} />);
|
2022-10-19 11:50:18 +08:00
|
|
|
expect(ref.calendarHeaderProps?.mode).toEqual(yearMode);
|
2022-08-08 22:43:57 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Calendar should switch mode', () => {
|
|
|
|
const monthMode = 'month';
|
|
|
|
const yearMode = 'year';
|
2023-06-07 21:59:21 +08:00
|
|
|
const onPanelChangeStub = jest.fn();
|
2022-10-03 20:31:37 +08:00
|
|
|
const wrapper = render(<Calendar mode={yearMode} onPanelChange={onPanelChangeStub} />);
|
2022-10-19 11:50:18 +08:00
|
|
|
expect(ref.calendarHeaderProps?.mode).toEqual(yearMode);
|
2022-10-03 20:31:37 +08:00
|
|
|
wrapper.rerender(<Calendar mode={monthMode} onPanelChange={onPanelChangeStub} />);
|
2022-10-19 11:50:18 +08:00
|
|
|
expect(ref.calendarHeaderProps?.mode).toEqual(monthMode);
|
2022-08-08 22:43:57 +08:00
|
|
|
expect(onPanelChangeStub).toHaveBeenCalledTimes(0);
|
|
|
|
});
|
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
it('Calendar should support locale', () => {
|
2022-08-15 15:57:19 +08:00
|
|
|
MockDate.set(Dayjs('2018-10-19').valueOf());
|
2022-08-08 22:43:57 +08:00
|
|
|
// eslint-disable-next-line global-require
|
2023-06-07 21:59:21 +08:00
|
|
|
const zhCN = require('../locale/zh_CN').default;
|
2022-10-03 20:31:37 +08:00
|
|
|
const wrapper = render(<Calendar locale={zhCN} />);
|
|
|
|
expect(wrapper.container.children[0]).toMatchSnapshot();
|
2022-08-08 22:43:57 +08:00
|
|
|
MockDate.reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('onPanelChange', () => {
|
|
|
|
it('trigger when click last month of date', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const onPanelChange = jest.fn();
|
2022-08-15 15:57:19 +08:00
|
|
|
const date = Dayjs('1990-09-03');
|
2022-10-03 20:31:37 +08:00
|
|
|
const wrapper = render(<Calendar onPanelChange={onPanelChange} value={date} />);
|
2022-08-08 22:43:57 +08:00
|
|
|
|
2022-10-03 20:31:37 +08:00
|
|
|
fireEvent.click(Array.from(wrapper.container.querySelectorAll('.ant-picker-cell')).at(0)!);
|
2022-08-08 22:43:57 +08:00
|
|
|
|
|
|
|
expect(onPanelChange).toHaveBeenCalled();
|
|
|
|
expect(onPanelChange.mock.calls[0][0].month()).toEqual(date.month() - 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('not trigger when in same month', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const onPanelChange = jest.fn();
|
2022-08-15 15:57:19 +08:00
|
|
|
const date = Dayjs('1990-09-03');
|
2022-10-03 20:31:37 +08:00
|
|
|
const wrapper = render(<Calendar onPanelChange={onPanelChange} value={date} />);
|
2022-08-08 22:43:57 +08:00
|
|
|
|
2022-10-03 20:31:37 +08:00
|
|
|
fireEvent.click(Array.from(wrapper.container.querySelectorAll('.ant-picker-cell')).at(10)!);
|
2022-08-08 22:43:57 +08:00
|
|
|
|
|
|
|
expect(onPanelChange).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('switch should work correctly without prop mode', async () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const onPanelChange = jest.fn();
|
2022-08-15 15:57:19 +08:00
|
|
|
const date = Dayjs(new Date(Date.UTC(2017, 7, 9, 8)));
|
2022-10-03 20:31:37 +08:00
|
|
|
const wrapper = render(<Calendar onPanelChange={onPanelChange} value={date} />);
|
2022-08-08 22:43:57 +08:00
|
|
|
|
2022-10-19 11:50:18 +08:00
|
|
|
expect(ref.calendarHeaderProps?.mode).toBe('month');
|
2022-10-03 20:31:37 +08:00
|
|
|
expect(wrapper.container.querySelectorAll('.ant-picker-date-panel').length).toBe(1);
|
|
|
|
expect(wrapper.container.querySelectorAll('.ant-picker-month-panel').length).toBe(0);
|
|
|
|
fireEvent.click(wrapper.container.querySelector('.ant-radio-button-input[value="year"]')!);
|
|
|
|
expect(wrapper.container.querySelectorAll('.ant-picker-date-panel').length).toBe(0);
|
|
|
|
expect(wrapper.container.querySelectorAll('.ant-picker-month-panel').length).toBe(1);
|
2022-08-08 22:43:57 +08:00
|
|
|
|
|
|
|
expect(onPanelChange).toHaveBeenCalled();
|
|
|
|
expect(onPanelChange.mock.calls[0][1]).toEqual('year');
|
|
|
|
});
|
|
|
|
|
|
|
|
const createWrapper = (
|
2022-08-15 15:57:19 +08:00
|
|
|
start: Dayjs.Dayjs,
|
|
|
|
end: Dayjs.Dayjs,
|
|
|
|
value: Dayjs.Dayjs,
|
|
|
|
onValueChange: (v: Dayjs.Dayjs) => void,
|
2022-08-08 22:43:57 +08:00
|
|
|
) => {
|
|
|
|
const wrapper = render(
|
|
|
|
// @ts-ignore
|
|
|
|
<Header
|
|
|
|
prefixCls="ant-picker-calendar"
|
2022-08-15 15:57:19 +08:00
|
|
|
generateConfig={dayjsGenerateConfig}
|
2022-08-08 22:43:57 +08:00
|
|
|
onChange={onValueChange}
|
|
|
|
value={value}
|
|
|
|
validRange={[start, end]}
|
|
|
|
locale={{ year: '年' } as Locale}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
openSelect(wrapper.container, '.ant-picker-calendar-year-select');
|
|
|
|
clickSelectItem(wrapper.container);
|
|
|
|
};
|
|
|
|
|
|
|
|
it('if value.month > end.month, set value.month to end.month', () => {
|
2022-08-15 15:57:19 +08:00
|
|
|
const value = Dayjs('1990-01-03');
|
|
|
|
const start = Dayjs('2019-04-01');
|
|
|
|
const end = Dayjs('2019-11-01');
|
2023-06-07 21:59:21 +08:00
|
|
|
const onValueChange = jest.fn();
|
2022-08-08 22:43:57 +08:00
|
|
|
createWrapper(start, end, value, onValueChange);
|
2023-05-17 21:18:51 +08:00
|
|
|
expect(onValueChange).toHaveBeenCalledWith(value.year(2019).month(3), 'year');
|
2022-08-08 22:43:57 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('if start.month > value.month, set value.month to start.month', () => {
|
2022-08-15 15:57:19 +08:00
|
|
|
const value = Dayjs('1990-01-03');
|
|
|
|
const start = Dayjs('2019-11-01');
|
|
|
|
const end = Dayjs('2019-03-01');
|
2023-06-07 21:59:21 +08:00
|
|
|
const onValueChange = jest.fn();
|
2022-08-08 22:43:57 +08:00
|
|
|
createWrapper(start, end, value, onValueChange);
|
2023-05-17 21:18:51 +08:00
|
|
|
expect(onValueChange).toHaveBeenCalledWith(value.year(2019).month(10), 'year');
|
2022-08-08 22:43:57 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('if change year and month > end month, set value.month to end.month', () => {
|
2022-08-15 15:57:19 +08:00
|
|
|
const value = Dayjs('2018-11-03');
|
|
|
|
const start = Dayjs('2000-01-01');
|
|
|
|
const end = Dayjs('2019-03-01');
|
2023-06-07 21:59:21 +08:00
|
|
|
const onValueChange = jest.fn();
|
2022-08-08 22:43:57 +08:00
|
|
|
const wrapper = render(
|
|
|
|
<Header
|
|
|
|
prefixCls="ant-picker-calendar"
|
2022-08-15 15:57:19 +08:00
|
|
|
generateConfig={dayjsGenerateConfig}
|
2022-08-08 22:43:57 +08:00
|
|
|
onChange={onValueChange}
|
|
|
|
value={value}
|
|
|
|
validRange={[start, end]}
|
|
|
|
// @ts-ignore
|
|
|
|
locale={{ year: '年' }}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
openSelect(wrapper.container, '.ant-picker-calendar-year-select');
|
|
|
|
fireEvent.click(
|
|
|
|
Array.from(wrapper.container.querySelectorAll('.ant-select-item-option')).at(-1)!,
|
|
|
|
);
|
2023-05-17 21:18:51 +08:00
|
|
|
expect(onValueChange).toHaveBeenCalledWith(value.year(2019).month(2), 'year');
|
2022-08-08 22:43:57 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('onMonthChange should work correctly', () => {
|
2022-08-15 15:57:19 +08:00
|
|
|
const start = Dayjs('2018-11-01');
|
|
|
|
const end = Dayjs('2019-03-01');
|
|
|
|
const value = Dayjs('2018-12-03');
|
2023-06-07 21:59:21 +08:00
|
|
|
const onValueChange = jest.fn();
|
2022-08-08 22:43:57 +08:00
|
|
|
const wrapper = render(
|
|
|
|
<Header
|
|
|
|
prefixCls="ant-picker-calendar"
|
2022-08-15 15:57:19 +08:00
|
|
|
generateConfig={dayjsGenerateConfig}
|
2022-08-08 22:43:57 +08:00
|
|
|
onChange={onValueChange}
|
|
|
|
value={value}
|
|
|
|
validRange={[start, end]}
|
|
|
|
// @ts-ignore
|
|
|
|
locale={{ year: '年', locale: 'zh_CN' }}
|
|
|
|
mode="month"
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
openSelect(wrapper.container, '.ant-picker-calendar-month-select');
|
|
|
|
clickSelectItem(wrapper.container);
|
2023-05-17 21:18:51 +08:00
|
|
|
expect(onValueChange).toHaveBeenCalledWith(value.month(10), 'month');
|
2022-08-08 22:43:57 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('onTypeChange should work correctly', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const onTypeChange = jest.fn();
|
2022-08-15 15:57:19 +08:00
|
|
|
const value = Dayjs('2018-12-03');
|
2022-08-08 22:43:57 +08:00
|
|
|
const wrapper = render(
|
|
|
|
<Header
|
|
|
|
prefixCls="ant-picker-calendar"
|
2022-08-15 15:57:19 +08:00
|
|
|
generateConfig={dayjsGenerateConfig}
|
2022-08-08 22:43:57 +08:00
|
|
|
onModeChange={onTypeChange}
|
|
|
|
locale={{ year: '年', month: '月', locale: 'zh_CN' } as any}
|
|
|
|
value={value}
|
|
|
|
// @ts-ignore
|
|
|
|
type="date"
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
fireEvent.click(
|
|
|
|
Array.from(wrapper.container.querySelectorAll(`.ant-radio-button-input`)).at(1)!,
|
|
|
|
);
|
|
|
|
expect(onTypeChange).toHaveBeenCalledWith('year');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('headerRender should work correctly', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const onMonthChange = jest.fn();
|
|
|
|
const onYearChange = jest.fn();
|
|
|
|
const onTypeChange = jest.fn();
|
2022-08-08 22:43:57 +08:00
|
|
|
|
|
|
|
// Year
|
2023-06-07 21:59:21 +08:00
|
|
|
const headerRender = jest.fn(({ value }) => {
|
2022-08-08 22:43:57 +08:00
|
|
|
const year = value.year();
|
|
|
|
const options = [];
|
|
|
|
for (let i = year - 100; i < year + 100; i += 1) {
|
|
|
|
options.push(
|
|
|
|
<Select.Option className="year-item" key={i} value={i}>
|
|
|
|
{i}
|
|
|
|
</Select.Option>,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Select
|
|
|
|
size="small"
|
|
|
|
dropdownMatchSelectWidth={false}
|
|
|
|
className="my-year-select"
|
|
|
|
onChange={onYearChange}
|
|
|
|
value={String(year)}
|
|
|
|
>
|
|
|
|
{options}
|
|
|
|
</Select>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
const uiWithYear = <Calendar fullscreen={false} headerRender={headerRender} />;
|
|
|
|
const wrapperWithYear = render(uiWithYear);
|
|
|
|
|
|
|
|
openSelect(wrapperWithYear.container, '.ant-select');
|
|
|
|
wrapperWithYear.rerender(uiWithYear);
|
|
|
|
|
|
|
|
fireEvent.click(Array.from(findSelectItem(wrapperWithYear.container)).at(-1)!);
|
|
|
|
|
|
|
|
expect(onYearChange).toHaveBeenCalled();
|
|
|
|
|
|
|
|
// Month
|
2023-06-07 21:59:21 +08:00
|
|
|
const headerRenderWithMonth = jest.fn(({ value }) => {
|
2022-08-08 22:43:57 +08:00
|
|
|
const start = 0;
|
|
|
|
const end = 12;
|
|
|
|
const monthOptions = [];
|
|
|
|
const current = value.clone();
|
|
|
|
const localeData = value.localeData();
|
|
|
|
const months = [];
|
|
|
|
for (let i = 0; i < 12; i += 1) {
|
|
|
|
current.month(i);
|
|
|
|
months.push(localeData.monthsShort(current));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let index = start; index < end; index += 1) {
|
|
|
|
monthOptions.push(
|
2023-07-21 12:33:05 +08:00
|
|
|
<Select.Option className="month-item" key={index} value={index}>
|
2022-08-08 22:43:57 +08:00
|
|
|
{months[index]}
|
|
|
|
</Select.Option>,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const month = value.month();
|
|
|
|
return (
|
|
|
|
<Select
|
|
|
|
size="small"
|
|
|
|
dropdownMatchSelectWidth={false}
|
|
|
|
className="my-month-select"
|
|
|
|
onChange={onMonthChange}
|
|
|
|
value={String(month)}
|
|
|
|
>
|
|
|
|
{monthOptions}
|
|
|
|
</Select>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
const uiWithMonth = <Calendar fullscreen={false} headerRender={headerRenderWithMonth} />;
|
|
|
|
const wrapperWithMonth = render(uiWithMonth);
|
|
|
|
openSelect(wrapperWithMonth.container, '.ant-select');
|
|
|
|
wrapperWithMonth.rerender(uiWithMonth);
|
|
|
|
|
|
|
|
fireEvent.click(Array.from(findSelectItem(wrapperWithMonth.container)).at(-1)!);
|
|
|
|
|
|
|
|
expect(onMonthChange).toHaveBeenCalled();
|
|
|
|
|
|
|
|
// Type
|
2023-06-07 21:59:21 +08:00
|
|
|
const headerRenderWithTypeChange = jest.fn(({ type }) => (
|
2022-08-08 22:43:57 +08:00
|
|
|
<Group size="small" onChange={onTypeChange} value={type}>
|
|
|
|
<Button value="month">Month</Button>
|
|
|
|
<Button value="year">Year</Button>
|
|
|
|
</Group>
|
|
|
|
));
|
|
|
|
|
|
|
|
const wrapperWithTypeChange = render(
|
|
|
|
<Calendar fullscreen={false} headerRender={headerRenderWithTypeChange} />,
|
|
|
|
);
|
|
|
|
|
|
|
|
fireEvent.click(
|
|
|
|
Array.from(wrapperWithTypeChange.container.querySelectorAll('.ant-radio-button-input')).at(
|
|
|
|
-1,
|
|
|
|
)!,
|
|
|
|
);
|
|
|
|
expect(onTypeChange).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('dateFullCellRender', () => {
|
|
|
|
const { container } = render(
|
|
|
|
<Calendar dateFullCellRender={() => <div className="light">Bamboo</div>} />,
|
|
|
|
);
|
|
|
|
expect(container.querySelectorAll('.light')[0].innerHTML).toEqual('Bamboo');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('monthFullCellRender', () => {
|
|
|
|
const { container } = render(
|
|
|
|
<Calendar mode="year" monthFullCellRender={() => <div className="bamboo">Light</div>} />,
|
|
|
|
);
|
|
|
|
expect(container.querySelectorAll('.bamboo')[0].innerHTML).toEqual('Light');
|
|
|
|
});
|
|
|
|
|
2023-04-02 13:45:57 +08:00
|
|
|
it('fullCellRender in date', () => {
|
|
|
|
const { container } = render(
|
|
|
|
<Calendar fullCellRender={() => <div className="light">Bamboo</div>} />,
|
|
|
|
);
|
|
|
|
expect(container.querySelectorAll('.light')[0].innerHTML).toEqual('Bamboo');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fullCellRender in month', () => {
|
|
|
|
const { container } = render(
|
|
|
|
<Calendar mode="year" fullCellRender={() => <div className="bamboo">Light</div>} />,
|
|
|
|
);
|
|
|
|
expect(container.querySelectorAll('.bamboo')[0].innerHTML).toEqual('Light');
|
|
|
|
});
|
|
|
|
|
2022-08-08 22:43:57 +08:00
|
|
|
it('when fullscreen is false, the element returned by dateFullCellRender should be interactive', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const onClick = jest.fn();
|
2022-08-08 22:43:57 +08:00
|
|
|
const { container } = render(
|
|
|
|
<Calendar
|
|
|
|
fullscreen={false}
|
|
|
|
dateFullCellRender={() => (
|
|
|
|
<div className="bamboo" onClick={onClick}>
|
|
|
|
Light
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
fireEvent.click(container.querySelectorAll('.bamboo')[0]);
|
2022-08-30 10:57:13 +08:00
|
|
|
expect(onClick).toHaveBeenCalled();
|
2022-08-08 22:43:57 +08:00
|
|
|
});
|
2023-04-02 13:45:57 +08:00
|
|
|
|
|
|
|
it('deprecated dateCellRender and monthCellRender', () => {
|
|
|
|
resetWarned();
|
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
2023-04-02 13:45:57 +08:00
|
|
|
const { container } = render(
|
|
|
|
<Calendar
|
|
|
|
dateCellRender={() => <div className="bamboo">Light</div>}
|
|
|
|
monthCellRender={() => <div className="bar">Bar</div>}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
expect(errSpy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [antd: Calendar] `monthCellRender` is deprecated. Please use `cellRender` instead.',
|
|
|
|
);
|
|
|
|
expect(errSpy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [antd: Calendar] `dateCellRender` is deprecated. Please use `cellRender` instead.',
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(container.querySelector('.bamboo')).toBeTruthy();
|
|
|
|
|
|
|
|
fireEvent.click(Array.from(container.querySelectorAll(`.ant-radio-button-input`)).at(1)!);
|
|
|
|
expect(container.querySelector('.bar')).toBeTruthy();
|
|
|
|
errSpy.mockRestore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('deprecated dateFullCellRender and monthFullCellRender', () => {
|
|
|
|
resetWarned();
|
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
2023-04-02 13:45:57 +08:00
|
|
|
const { container } = render(
|
|
|
|
<Calendar
|
|
|
|
dateFullCellRender={() => <div className="bamboo">Light</div>}
|
|
|
|
monthFullCellRender={() => <div className="bar">Bar</div>}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
expect(errSpy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [antd: Calendar] `dateFullCellRender` is deprecated. Please use `fullCellRender` instead.',
|
|
|
|
);
|
|
|
|
expect(errSpy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [antd: Calendar] `monthFullCellRender` is deprecated. Please use `fullCellRender` instead.',
|
|
|
|
);
|
|
|
|
expect(container.querySelector('.bamboo')).toBeTruthy();
|
|
|
|
fireEvent.click(Array.from(container.querySelectorAll(`.ant-radio-button-input`)).at(1)!);
|
|
|
|
expect(container.querySelector('.bar')).toBeTruthy();
|
|
|
|
errSpy.mockRestore();
|
|
|
|
});
|
2023-04-12 17:20:08 +08:00
|
|
|
|
|
|
|
it('support Calendar.generateCalendar', () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useFakeTimers().setSystemTime(new Date('2000-01-01'));
|
2023-04-19 14:23:29 +08:00
|
|
|
|
2023-04-12 17:20:08 +08:00
|
|
|
const MyCalendar = Calendar.generateCalendar(dayjsGenerateConfig);
|
|
|
|
const { container } = render(<MyCalendar />);
|
|
|
|
expect(container.firstChild).toMatchSnapshot();
|
2023-04-19 14:23:29 +08:00
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useRealTimers();
|
2023-04-12 17:20:08 +08:00
|
|
|
});
|
2022-08-08 22:43:57 +08:00
|
|
|
});
|