You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

551 lines
20 KiB

import Dayjs from 'dayjs';
import 'dayjs/locale/zh-cn';
import MockDate from 'mockdate';
import { type PickerPanelProps } from 'rc-picker';
import dayjsGenerateConfig from 'rc-picker/lib/generate/dayjs';
import type { Locale } from 'rc-picker/lib/interface';
import { resetWarned } from 'rc-util/lib/warning';
import React from 'react';
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';
import Header, { type CalendarHeaderProps } from '../Header';
const ref: {
calendarProps?: PickerPanelProps<unknown>;
calendarHeaderProps?: CalendarHeaderProps<unknown>;
} = {};
jest.mock('../Header', () => {
const HeaderModule = jest.requireActual('../Header');
const HeaderComponent = HeaderModule.default;
return (props: CalendarHeaderProps<any>) => {
ref.calendarHeaderProps = props;
return <HeaderComponent {...props} />;
};
});
jest.mock('rc-picker', () => {
const RcPicker = jest.requireActual('rc-picker');
const PickerPanelComponent = RcPicker.PickerPanel;
return {
...RcPicker,
PickerPanel: (props: PickerPanelProps<unknown>) => {
ref.calendarProps = props;
return <PickerPanelComponent {...props} />;
},
};
});
describe('Calendar', () => {
mountTest(Calendar);
rtlTest(Calendar, true);
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(() => {
render(<Calendar value={null as any} />);
}).not.toThrow();
expect(() => {
render(<Calendar value={undefined} />);
}).not.toThrow();
});
it('Calendar should be selectable', () => {
MockDate.set(Dayjs('2000-01-01').valueOf());
const onSelect = jest.fn();
const onChange = jest.fn();
const { container } = render(<Calendar onSelect={onSelect} onChange={onChange} />);
fireEvent.click(container.querySelector('.ant-picker-cell')!);
expect(onSelect).toHaveBeenCalledWith(expect.anything(), { source: 'date' });
const value = onSelect.mock.calls[0][0];
expect(Dayjs.isDayjs(value)).toBe(true);
expect(onChange).toHaveBeenCalled();
MockDate.reset();
});
it('only Valid range should be selectable', () => {
const onSelect = jest.fn();
const validRange: [Dayjs.Dayjs, Dayjs.Dayjs] = [Dayjs('2018-02-02'), Dayjs('2018-02-18')];
const wrapper = render(
<Calendar onSelect={onSelect} validRange={validRange} defaultValue={Dayjs('2018-02-02')} />,
);
fireEvent.click(wrapper.container.querySelectorAll('[title="2018-02-01"]')[0]);
fireEvent.click(wrapper.container.querySelectorAll('[title="2018-02-02"]')[0]);
expect(onSelect.mock.calls.length).toBe(1);
});
it('dates other than in valid range should be disabled', () => {
const onSelect = jest.fn();
const validRange: [Dayjs.Dayjs, Dayjs.Dayjs] = [Dayjs('2018-02-02'), Dayjs('2018-02-18')];
const { container } = render(
<Calendar onSelect={onSelect} validRange={validRange} defaultValue={Dayjs('2018-02-02')} />,
);
fireEvent.click(container.querySelector('[title="2018-02-20"]')!);
const elem = container
.querySelector('[title="2018-02-20"]')!
.className.includes('ant-picker-cell-disabled');
expect(elem).toBe(true);
expect(onSelect.mock.calls.length).toBe(0);
});
it('months other than in valid range should be disabled', () => {
const onSelect = jest.fn();
const validRange: [Dayjs.Dayjs, Dayjs.Dayjs] = [Dayjs('2018-02-02'), Dayjs('2018-05-18')];
const { container } = render(
<Calendar
onSelect={onSelect}
validRange={validRange}
defaultValue={Dayjs('2018-02-02')}
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', () => {
const validRange: [Dayjs.Dayjs, Dayjs.Dayjs] = [Dayjs('2017-02-02'), Dayjs('2018-05-18')];
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', () => {
const validRange: [Dayjs.Dayjs, Dayjs.Dayjs] = [Dayjs('2018-02-02'), Dayjs('2018-05-18')];
render(<Calendar validRange={validRange} defaultValue={Dayjs('2018-02-02')} />);
expect(ref.calendarProps?.disabledDate?.(Dayjs('2018-06-02'))).toBe(true);
expect(ref.calendarProps?.disabledDate?.(Dayjs('2018-04-02'))).toBe(false);
});
it('validRange should work with disabledDate function', () => {
const validRange: [Dayjs.Dayjs, Dayjs.Dayjs] = [Dayjs('2018-02-02'), Dayjs('2018-05-18')];
render(
<Calendar
validRange={validRange}
disabledDate={(data) => data.isSame(Dayjs('2018-02-03'))}
/>,
);
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);
});
it('Calendar MonthSelect should display correct label', () => {
const validRange: [Dayjs.Dayjs, Dayjs.Dayjs] = [Dayjs('2018-02-02'), Dayjs('2019-06-1')];
const wrapper = render(<Calendar validRange={validRange} defaultValue={Dayjs('2019-01-01')} />);
expect(wrapper.container.children[0]).toMatchSnapshot();
});
it('Calendar should change mode by prop', () => {
const monthMode = 'month';
const yearMode = 'year';
const wrapper = render(<Calendar />);
expect(ref.calendarHeaderProps?.mode).toEqual(monthMode);
wrapper.rerender(<Calendar mode={yearMode} />);
expect(ref.calendarHeaderProps?.mode).toEqual(yearMode);
});
it('Calendar should switch mode', () => {
const monthMode = 'month';
const yearMode = 'year';
const onPanelChangeStub = jest.fn();
const wrapper = render(<Calendar mode={yearMode} onPanelChange={onPanelChangeStub} />);
expect(ref.calendarHeaderProps?.mode).toEqual(yearMode);
wrapper.rerender(<Calendar mode={monthMode} onPanelChange={onPanelChangeStub} />);
expect(ref.calendarHeaderProps?.mode).toEqual(monthMode);
expect(onPanelChangeStub).toHaveBeenCalledTimes(0);
});
it('Calendar should support locale', () => {
MockDate.set(Dayjs('2018-10-19').valueOf());
// eslint-disable-next-line global-require
const zhCN = require('../locale/zh_CN').default;
const wrapper = render(<Calendar locale={zhCN} />);
expect(wrapper.container.children[0]).toMatchSnapshot();
MockDate.reset();
});
describe('onPanelChange', () => {
it('trigger when click last month of date', () => {
const onPanelChange = jest.fn();
const date = Dayjs('1990-09-03');
const wrapper = render(<Calendar onPanelChange={onPanelChange} value={date} />);
fireEvent.click(Array.from(wrapper.container.querySelectorAll('.ant-picker-cell')).at(0)!);
expect(onPanelChange).toHaveBeenCalled();
expect(onPanelChange.mock.calls[0][0].month()).toEqual(date.month() - 1);
});
it('not trigger when in same month', () => {
const onPanelChange = jest.fn();
const date = Dayjs('1990-09-03');
const wrapper = render(<Calendar onPanelChange={onPanelChange} value={date} />);
fireEvent.click(Array.from(wrapper.container.querySelectorAll('.ant-picker-cell')).at(10)!);
expect(onPanelChange).not.toHaveBeenCalled();
});
});
it('switch should work correctly without prop mode', async () => {
const onPanelChange = jest.fn();
const date = Dayjs(new Date(Date.UTC(2017, 7, 9, 8)));
const wrapper = render(<Calendar onPanelChange={onPanelChange} value={date} />);
expect(ref.calendarHeaderProps?.mode).toBe('month');
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);
expect(onPanelChange).toHaveBeenCalled();
expect(onPanelChange.mock.calls[0][1]).toEqual('year');
});
const createWrapper = (
start: Dayjs.Dayjs,
end: Dayjs.Dayjs,
value: Dayjs.Dayjs,
onValueChange: (v: Dayjs.Dayjs) => void,
) => {
const wrapper = render(
// @ts-ignore
<Header
prefixCls="ant-picker-calendar"
generateConfig={dayjsGenerateConfig}
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', () => {
const value = Dayjs('1990-01-03');
const start = Dayjs('2019-04-01');
const end = Dayjs('2019-11-01');
const onValueChange = jest.fn();
createWrapper(start, end, value, onValueChange);
expect(onValueChange).toHaveBeenCalledWith(value.year(2019).month(3), 'year');
});
it('if start.month > value.month, set value.month to start.month', () => {
const value = Dayjs('1990-01-03');
const start = Dayjs('2019-11-01');
const end = Dayjs('2019-03-01');
const onValueChange = jest.fn();
createWrapper(start, end, value, onValueChange);
expect(onValueChange).toHaveBeenCalledWith(value.year(2019).month(10), 'year');
});
it('if change year and month > end month, set value.month to end.month', () => {
const value =