ant-design/components/date-picker/__tests__/RangePicker.test.tsx

118 lines
3.5 KiB
TypeScript
Raw Normal View History

2022-08-23 18:00:20 +08:00
import dayjs from 'dayjs';
import customParseFormat from 'dayjs/plugin/customParseFormat';
import React from 'react';
import DatePicker from '..';
import focusTest from '../../../tests/shared/focusTest';
import { render, resetMockDate, setMockDate } from '../../../tests/utils';
import enUS from '../locale/en_US';
import { closePicker, openPicker, selectCell } from './utils';
2022-08-23 18:00:20 +08:00
dayjs.extend(customParseFormat);
const { RangePicker } = DatePicker;
describe('RangePicker', () => {
focusTest(RangePicker, { refFocus: true });
beforeEach(() => {
setMockDate();
});
afterEach(() => {
resetMockDate();
});
// issue: https://github.com/ant-design/ant-design/issues/5872
it('should not throw error when value is reset to `[]`', () => {
2022-08-23 18:00:20 +08:00
const birthday = dayjs('2000-01-01', 'YYYY-MM-DD');
const wrapper1 = render(<RangePicker value={[birthday, birthday]} open />);
const wrapper2 = render(<RangePicker value={[] as unknown as null} open />);
expect(() => {
openPicker(wrapper1);
selectCell(wrapper1, 3);
closePicker(wrapper1);
openPicker(wrapper1, 1);
selectCell(wrapper1, 5, 1);
closePicker(wrapper1, 1);
openPicker(wrapper2);
selectCell(wrapper2, 3);
closePicker(wrapper2);
openPicker(wrapper2, 1);
selectCell(wrapper2, 5, 1);
closePicker(wrapper2, 1);
}).not.toThrow();
});
it('customize separator', () => {
const { container } = render(<RangePicker separator="test" />);
expect(container.firstChild).toMatchSnapshot();
});
// https://github.com/ant-design/ant-design/issues/13302
describe('in "month" mode, when the left and right panels select the same month', () => {
it('the cell status is correct', () => {
2022-08-23 18:00:20 +08:00
let rangePickerValue: dayjs.Dayjs[] = [] as any;
class Test extends React.Component {
state = { value: null };
render() {
return (
<RangePicker
value={this.state.value}
mode={['month', 'month']}
onPanelChange={value => {
this.setState({ value });
2022-08-23 18:00:20 +08:00
rangePickerValue = value as any;
}}
/>
);
}
}
const wrapper = render(<Test />);
openPicker(wrapper);
selectCell(wrapper, 'Feb');
openPicker(wrapper, 1);
selectCell(wrapper, 'Feb');
closePicker(wrapper, 1);
2022-08-23 18:00:20 +08:00
const [start, end] = rangePickerValue;
expect(start.isSame(end, 'date')).toBeTruthy();
});
});
describe('ranges', () => {
it('RangePicker support presetted ranges with Tags', () => {
const { container } = render(
<RangePicker
open
ranges={{
2022-08-23 18:00:20 +08:00
Today: [dayjs(), dayjs()],
'This Month': [dayjs().startOf('month'), dayjs().endOf('month')],
}}
/>,
);
expect(Array.from(container.children)).toMatchSnapshot();
});
});
it('placeholder', () => {
const { container } = render(<RangePicker placeholder={undefined} />);
const inputLists = container.querySelectorAll('input');
expect(inputLists[0]?.placeholder).toEqual('Start date');
expect(inputLists[inputLists.length - 1].placeholder).toEqual('End date');
});
it('RangePicker picker quarter placeholder', () => {
const { container } = render(<RangePicker picker="quarter" locale={enUS} />);
expect(container.querySelectorAll('input')[0]?.placeholder).toEqual('Start quarter');
expect(container.querySelectorAll('input')[1]?.placeholder).toEqual('End quarter');
});
});