2017-01-19 13:55:04 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { mount, render } from 'enzyme';
|
|
|
|
import moment from 'moment';
|
2018-06-22 21:05:13 +08:00
|
|
|
import DatePicker from '..';
|
2018-01-21 01:03:51 +08:00
|
|
|
import { setMockDate, resetMockDate } from '../../../tests/utils';
|
2018-07-25 16:31:18 +08:00
|
|
|
import { selectDate, openPanel } from './utils';
|
2017-11-30 09:54:31 +08:00
|
|
|
import focusTest from '../../../tests/shared/focusTest';
|
2017-01-19 13:55:04 +08:00
|
|
|
|
2018-01-13 19:11:53 +08:00
|
|
|
const { RangePicker } = DatePicker;
|
|
|
|
|
2017-01-19 13:55:04 +08:00
|
|
|
describe('RangePicker', () => {
|
2017-11-30 09:54:31 +08:00
|
|
|
focusTest(RangePicker);
|
|
|
|
|
2018-02-01 16:05:57 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
setMockDate();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
resetMockDate();
|
|
|
|
});
|
|
|
|
|
2017-01-19 13:55:04 +08:00
|
|
|
it('show month panel according to value', () => {
|
2017-04-01 14:43:36 +08:00
|
|
|
const birthday = moment('2000-01-01', 'YYYY-MM-DD').locale('zh-cn');
|
2017-01-19 13:55:04 +08:00
|
|
|
const wrapper = mount(
|
2018-12-07 20:02:01 +08:00
|
|
|
<RangePicker getCalendarContainer={trigger => trigger} format="YYYY/MM/DD" showTime open />,
|
2017-01-19 13:55:04 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
wrapper.setProps({ value: [birthday, birthday] });
|
2018-12-07 20:02:01 +08:00
|
|
|
expect(
|
|
|
|
render(
|
|
|
|
wrapper
|
|
|
|
.find('Trigger')
|
|
|
|
.instance()
|
|
|
|
.getComponent(),
|
|
|
|
),
|
|
|
|
).toMatchSnapshot();
|
2017-01-19 13:55:04 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('switch to corresponding month panel when click presetted ranges', () => {
|
2017-04-01 14:43:36 +08:00
|
|
|
const birthday = moment('2000-01-01', 'YYYY-MM-DD').locale('zh-cn');
|
2017-01-19 13:55:04 +08:00
|
|
|
const wrapper = mount(
|
|
|
|
<RangePicker
|
|
|
|
ranges={{
|
|
|
|
'My Birthday': [birthday, birthday],
|
|
|
|
}}
|
|
|
|
getCalendarContainer={trigger => trigger}
|
|
|
|
format="YYYY/MM/DD"
|
2017-05-15 12:03:02 +08:00
|
|
|
showTime
|
|
|
|
open
|
2018-12-07 20:02:01 +08:00
|
|
|
/>,
|
2017-01-19 13:55:04 +08:00
|
|
|
);
|
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
const rangeCalendarWrapper = mount(
|
|
|
|
wrapper
|
|
|
|
.find('Trigger')
|
|
|
|
.instance()
|
|
|
|
.getComponent(),
|
|
|
|
);
|
|
|
|
rangeCalendarWrapper.find('.ant-calendar-range-quick-selector Tag').simulate('click');
|
|
|
|
expect(
|
|
|
|
render(
|
|
|
|
wrapper
|
|
|
|
.find('Trigger')
|
|
|
|
.instance()
|
|
|
|
.getComponent(),
|
|
|
|
),
|
|
|
|
).toMatchSnapshot();
|
2017-01-19 13:55:04 +08:00
|
|
|
});
|
2017-04-26 14:44:07 +08:00
|
|
|
|
2017-05-22 09:42:04 +08:00
|
|
|
it('highlight range when hover presetted range', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<RangePicker
|
|
|
|
ranges={{
|
2017-05-31 10:59:27 +08:00
|
|
|
'This Month': [moment().startOf('month'), moment().endOf('month')],
|
2017-05-22 09:42:04 +08:00
|
|
|
}}
|
|
|
|
getCalendarContainer={trigger => trigger}
|
|
|
|
format="YYYY/MM/DD"
|
|
|
|
open
|
2018-12-07 20:02:01 +08:00
|
|
|
/>,
|
2017-05-22 09:42:04 +08:00
|
|
|
);
|
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
let rangeCalendarWrapper = mount(
|
|
|
|
wrapper
|
|
|
|
.find('Trigger')
|
|
|
|
.instance()
|
|
|
|
.getComponent(),
|
|
|
|
);
|
|
|
|
rangeCalendarWrapper.find('.ant-calendar-range-quick-selector Tag').simulate('mouseEnter');
|
|
|
|
rangeCalendarWrapper = mount(
|
|
|
|
wrapper
|
|
|
|
.find('Trigger')
|
|
|
|
.instance()
|
|
|
|
.getComponent(),
|
|
|
|
);
|
2017-05-22 09:42:04 +08:00
|
|
|
expect(rangeCalendarWrapper.find('.ant-calendar-selected-day').length).toBe(2);
|
|
|
|
});
|
|
|
|
|
2017-12-01 10:50:25 +08:00
|
|
|
it('should trigger onCalendarChange when change value', () => {
|
|
|
|
const onCalendarChangeFn = jest.fn();
|
|
|
|
const wrapper = mount(
|
|
|
|
<RangePicker
|
|
|
|
getCalendarContainer={trigger => trigger}
|
|
|
|
onCalendarChange={onCalendarChangeFn}
|
|
|
|
open
|
2018-12-07 20:02:01 +08:00
|
|
|
/>,
|
|
|
|
);
|
|
|
|
const rangeCalendarWrapper = mount(
|
|
|
|
wrapper
|
|
|
|
.find('Trigger')
|
|
|
|
.instance()
|
|
|
|
.getComponent(),
|
2017-12-01 10:50:25 +08:00
|
|
|
);
|
2018-12-07 20:02:01 +08:00
|
|
|
rangeCalendarWrapper
|
|
|
|
.find('.ant-calendar-cell')
|
|
|
|
.at(15)
|
|
|
|
.simulate('click');
|
2017-12-01 10:50:25 +08:00
|
|
|
expect(onCalendarChangeFn).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2017-04-26 14:44:07 +08:00
|
|
|
// issue: https://github.com/ant-design/ant-design/issues/5872
|
|
|
|
it('should not throw error when value is reset to `[]`', () => {
|
|
|
|
const birthday = moment('2000-01-01', 'YYYY-MM-DD');
|
|
|
|
const wrapper = mount(
|
2018-12-07 20:02:01 +08:00
|
|
|
<RangePicker getCalendarContainer={trigger => trigger} value={[birthday, birthday]} open />,
|
2017-04-26 14:44:07 +08:00
|
|
|
);
|
|
|
|
wrapper.setProps({ value: [] });
|
2018-12-07 20:02:01 +08:00
|
|
|
const rangeCalendarWrapper = mount(
|
|
|
|
wrapper
|
|
|
|
.find('Trigger')
|
|
|
|
.instance()
|
|
|
|
.getComponent(),
|
|
|
|
);
|
|
|
|
expect(() =>
|
|
|
|
rangeCalendarWrapper
|
|
|
|
.find('.ant-calendar-cell')
|
|
|
|
.at(15)
|
|
|
|
.simulate('click')
|
|
|
|
.simulate('click'),
|
|
|
|
).not.toThrow();
|
2017-04-26 14:44:07 +08:00
|
|
|
});
|
2017-08-04 16:48:52 +08:00
|
|
|
|
|
|
|
// issue: https://github.com/ant-design/ant-design/issues/7077
|
|
|
|
it('should not throw error when select after clear', () => {
|
2018-12-07 20:02:01 +08:00
|
|
|
const wrapper = mount(<RangePicker getCalendarContainer={trigger => trigger} open />);
|
|
|
|
let rangeCalendarWrapper = mount(
|
|
|
|
wrapper
|
|
|
|
.find('Trigger')
|
|
|
|
.instance()
|
|
|
|
.getComponent(),
|
2017-08-04 16:48:52 +08:00
|
|
|
);
|
2018-12-07 20:02:01 +08:00
|
|
|
rangeCalendarWrapper
|
|
|
|
.find('.ant-calendar-cell')
|
|
|
|
.at(15)
|
|
|
|
.simulate('click')
|
|
|
|
.simulate('click');
|
2017-09-20 16:26:18 +08:00
|
|
|
wrapper.update();
|
2018-12-07 20:02:01 +08:00
|
|
|
wrapper
|
|
|
|
.find('.ant-calendar-picker-clear')
|
|
|
|
.hostNodes()
|
|
|
|
.simulate('click');
|
2017-08-04 16:48:52 +08:00
|
|
|
wrapper.find('.ant-calendar-picker-input').simulate('click');
|
2018-12-07 20:02:01 +08:00
|
|
|
rangeCalendarWrapper = mount(
|
|
|
|
wrapper
|
|
|
|
.find('Trigger')
|
|
|
|
.instance()
|
|
|
|
.getComponent(),
|
|
|
|
);
|
|
|
|
expect(() =>
|
|
|
|
rangeCalendarWrapper
|
|
|
|
.find('.ant-calendar-cell')
|
|
|
|
.at(15)
|
|
|
|
.simulate('click')
|
|
|
|
.simulate('click'),
|
|
|
|
).not.toThrow();
|
2017-08-04 16:48:52 +08:00
|
|
|
});
|
2018-01-21 01:03:51 +08:00
|
|
|
|
|
|
|
it('clear hover value after panel close', () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
const wrapper = mount(
|
|
|
|
<div>
|
|
|
|
<RangePicker value={[moment(), moment().add(2, 'day')]} />
|
2018-12-07 20:02:01 +08:00
|
|
|
</div>,
|
2018-01-21 01:03:51 +08:00
|
|
|
);
|
|
|
|
wrapper.find('.ant-calendar-picker-input').simulate('click');
|
2018-12-07 20:02:01 +08:00
|
|
|
wrapper
|
|
|
|
.find('.ant-calendar-cell')
|
|
|
|
.at(25)
|
|
|
|
.simulate('click');
|
|
|
|
wrapper
|
|
|
|
.find('.ant-calendar-cell')
|
|
|
|
.at(27)
|
|
|
|
.simulate('mouseEnter');
|
2018-01-21 01:03:51 +08:00
|
|
|
document.dispatchEvent(new MouseEvent('mousedown'));
|
|
|
|
jest.runAllTimers();
|
|
|
|
wrapper.find('.ant-calendar-picker-input').simulate('click');
|
|
|
|
expect(
|
2018-12-07 20:02:01 +08:00
|
|
|
wrapper
|
|
|
|
.find('.ant-calendar-cell')
|
|
|
|
.at(23)
|
|
|
|
.hasClass('ant-calendar-in-range-cell'),
|
2018-01-21 01:03:51 +08:00
|
|
|
).toBe(true);
|
2018-02-01 16:05:57 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('preset range', () => {
|
|
|
|
it('static range', () => {
|
|
|
|
const range = [moment().subtract(2, 'd'), moment()];
|
|
|
|
const format = 'YYYY-MM-DD HH:mm:ss';
|
2018-12-07 20:02:01 +08:00
|
|
|
const wrapper = mount(<RangePicker ranges={{ 'recent two days': range }} format={format} />);
|
2018-02-01 16:05:57 +08:00
|
|
|
wrapper.find('.ant-calendar-picker-input').simulate('click');
|
2018-07-11 20:09:58 +08:00
|
|
|
wrapper.find('.ant-calendar-range-quick-selector Tag').simulate('click');
|
2018-02-01 16:05:57 +08:00
|
|
|
expect(
|
2018-12-07 20:02:01 +08:00
|
|
|
wrapper
|
|
|
|
.find('.ant-calendar-range-picker-input')
|
|
|
|
.first()
|
|
|
|
.getDOMNode().value,
|
2018-02-01 16:05:57 +08:00
|
|
|
).toBe(range[0].format(format));
|
|
|
|
expect(
|
2018-12-07 20:02:01 +08:00
|
|
|
wrapper
|
|
|
|
.find('.ant-calendar-range-picker-input')
|
|
|
|
.last()
|
|
|
|
.getDOMNode().value,
|
2018-02-01 16:05:57 +08:00
|
|
|
).toBe(range[1].format(format));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('function range', () => {
|
|
|
|
const range = [moment().subtract(2, 'd'), moment()];
|
|
|
|
const format = 'YYYY-MM-DD HH:mm:ss';
|
|
|
|
const wrapper = mount(
|
2018-12-07 20:02:01 +08:00
|
|
|
<RangePicker ranges={{ 'recent two days': () => range }} format={format} />,
|
2018-02-01 16:05:57 +08:00
|
|
|
);
|
|
|
|
wrapper.find('.ant-calendar-picker-input').simulate('click');
|
2018-07-11 20:09:58 +08:00
|
|
|
wrapper.find('.ant-calendar-range-quick-selector Tag').simulate('click');
|
2018-02-01 16:05:57 +08:00
|
|
|
expect(
|
2018-12-07 20:02:01 +08:00
|
|
|
wrapper
|
|
|
|
.find('.ant-calendar-range-picker-input')
|
|
|
|
.first()
|
|
|
|
.getDOMNode().value,
|
2018-02-01 16:05:57 +08:00
|
|
|
).toBe(range[0].format(format));
|
|
|
|
expect(
|
2018-12-07 20:02:01 +08:00
|
|
|
wrapper
|
|
|
|
.find('.ant-calendar-range-picker-input')
|
|
|
|
.last()
|
|
|
|
.getDOMNode().value,
|
2018-02-01 16:05:57 +08:00
|
|
|
).toBe(range[1].format(format));
|
|
|
|
});
|
2018-01-21 01:03:51 +08:00
|
|
|
});
|
2018-01-31 12:01:27 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/6999
|
|
|
|
it('input date manually', () => {
|
|
|
|
const wrapper = mount(<RangePicker open />);
|
|
|
|
const dateString = '2008-12-31';
|
|
|
|
const input = wrapper.find('.ant-calendar-input').first();
|
|
|
|
input.simulate('change', { target: { value: dateString } });
|
|
|
|
expect(input.getDOMNode().value).toBe(dateString);
|
|
|
|
});
|
2018-02-04 16:41:02 +08:00
|
|
|
|
|
|
|
it('triggers onOk when click on preset range', () => {
|
|
|
|
const handleOk = jest.fn();
|
|
|
|
const range = [moment().subtract(2, 'd'), moment()];
|
2018-12-07 20:02:01 +08:00
|
|
|
const wrapper = mount(<RangePicker ranges={{ 'recent two days': range }} onOk={handleOk} />);
|
2018-02-04 16:41:02 +08:00
|
|
|
wrapper.find('.ant-calendar-picker-input').simulate('click');
|
2018-07-11 20:09:58 +08:00
|
|
|
wrapper.find('.ant-calendar-range-quick-selector Tag').simulate('click');
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(handleOk).toHaveBeenCalledWith(range);
|
2018-02-04 16:41:02 +08:00
|
|
|
});
|
2018-02-07 14:22:35 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/9267
|
|
|
|
it('invali end date not throw error', () => {
|
|
|
|
const wrapper = mount(<RangePicker />);
|
|
|
|
wrapper.find('.ant-calendar-picker-input').simulate('click');
|
|
|
|
selectDate(wrapper, moment('2017-09-18'), 0);
|
|
|
|
selectDate(wrapper, moment('2017-10-18'), 1);
|
|
|
|
wrapper.find('.ant-calendar-picker-input').simulate('click');
|
2018-12-07 20:02:01 +08:00
|
|
|
expect(() =>
|
|
|
|
wrapper
|
|
|
|
.find('.ant-calendar-input')
|
|
|
|
.at(1)
|
|
|
|
.simulate('change', { target: { value: '2016-01-01' } }),
|
|
|
|
).not.toThrow();
|
2018-02-07 14:22:35 +08:00
|
|
|
});
|
2018-07-25 16:31:18 +08:00
|
|
|
|
|
|
|
it('changes year/month when under control', () => {
|
|
|
|
const wrapper = mount(<RangePicker value={[moment('2018-07-01'), moment('2018-07-02')]} />);
|
|
|
|
openPanel(wrapper);
|
2018-12-07 20:02:01 +08:00
|
|
|
expect(
|
|
|
|
wrapper
|
|
|
|
.find('.ant-calendar-my-select')
|
|
|
|
.first()
|
|
|
|
.text(),
|
|
|
|
).toBe('Jul2018');
|
|
|
|
wrapper
|
|
|
|
.find('.ant-calendar-prev-year-btn')
|
|
|
|
.first()
|
|
|
|
.simulate('click');
|
|
|
|
wrapper
|
|
|
|
.find('.ant-calendar-prev-month-btn')
|
|
|
|
.first()
|
|
|
|
.simulate('click');
|
|
|
|
expect(
|
|
|
|
wrapper
|
|
|
|
.find('.ant-calendar-my-select')
|
|
|
|
.first()
|
|
|
|
.text(),
|
|
|
|
).toBe('Jun2017');
|
2018-07-25 16:31:18 +08:00
|
|
|
});
|
2018-12-07 12:15:36 +08:00
|
|
|
|
2018-09-10 00:26:26 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/11631
|
|
|
|
it('triggers onOpenChange when click on preset range', () => {
|
|
|
|
const handleOpenChange = jest.fn();
|
|
|
|
const range = [moment().subtract(2, 'd'), moment()];
|
2018-12-07 20:02:01 +08:00
|
|
|
const wrapper = mount(
|
|
|
|
<RangePicker onOpenChange={handleOpenChange} ranges={{ 'recent two days': range }} />,
|
|
|
|
);
|
2018-09-10 00:26:26 +08:00
|
|
|
wrapper.find('.ant-calendar-picker-input').simulate('click');
|
|
|
|
wrapper.find('.ant-calendar-range-quick-selector Tag').simulate('click');
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(handleOpenChange).toHaveBeenCalledWith(false);
|
2018-09-10 00:26:26 +08:00
|
|
|
});
|
2019-02-28 16:11:10 +08:00
|
|
|
|
|
|
|
it('customize separator', () => {
|
|
|
|
const wrapper = mount(<RangePicker separator="test" />);
|
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
|
|
});
|
2019-04-01 14:49:40 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/13302
|
|
|
|
describe('in "month" mode, when the left and right panels select the same month', () => {
|
|
|
|
it('left panel and right panel could be the same month', () => {
|
|
|
|
const wrapper = mount(<RangePicker mode={['month', 'month']} />);
|
|
|
|
wrapper.setProps({ value: [moment(), moment()] });
|
|
|
|
expect(
|
|
|
|
wrapper
|
|
|
|
.find('.ant-calendar-range-picker-input')
|
|
|
|
.first()
|
|
|
|
.getDOMNode().value,
|
|
|
|
).toBe(
|
|
|
|
wrapper
|
|
|
|
.find('.ant-calendar-range-picker-input')
|
|
|
|
.last()
|
|
|
|
.getDOMNode().value,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('the cell status is correct', () => {
|
|
|
|
const wrapper = mount(<RangePicker mode={['month', 'month']} />);
|
|
|
|
wrapper.find('.ant-calendar-picker-input').simulate('click');
|
|
|
|
wrapper
|
|
|
|
.find('.ant-calendar-range-left .ant-calendar-month-panel-cell')
|
|
|
|
.at(3)
|
|
|
|
.simulate('click');
|
|
|
|
wrapper
|
|
|
|
.find('.ant-calendar-range-right .ant-calendar-month-panel-cell')
|
|
|
|
.at(3)
|
|
|
|
.simulate('click');
|
|
|
|
expect(
|
|
|
|
wrapper
|
|
|
|
.find('.ant-calendar-range-left .ant-calendar-month-panel-cell')
|
|
|
|
.at(3)
|
|
|
|
.hasClass('ant-calendar-month-panel-selected-cell'),
|
|
|
|
).toBe(true);
|
|
|
|
expect(
|
|
|
|
wrapper
|
|
|
|
.find('.ant-calendar-range-left .ant-calendar-month-panel-cell')
|
|
|
|
.at(3)
|
|
|
|
.hasClass('ant-calendar-month-panel-selected-cell'),
|
|
|
|
).toBe(true);
|
|
|
|
expect(
|
|
|
|
wrapper
|
|
|
|
.find('.ant-calendar-range-left .ant-calendar-month-panel-cell')
|
|
|
|
.at(2)
|
|
|
|
.hasClass('ant-calendar-month-panel-cell-disabled'),
|
|
|
|
).toBe(false);
|
|
|
|
expect(
|
|
|
|
wrapper
|
|
|
|
.find('.ant-calendar-range-left .ant-calendar-month-panel-cell')
|
|
|
|
.at(4)
|
|
|
|
.hasClass('ant-calendar-month-panel-cell-disabled'),
|
|
|
|
).toBe(true);
|
|
|
|
expect(
|
|
|
|
wrapper
|
|
|
|
.find('.ant-calendar-range-right .ant-calendar-month-panel-cell')
|
|
|
|
.at(2)
|
|
|
|
.hasClass('ant-calendar-month-panel-cell-disabled'),
|
|
|
|
).toBe(true);
|
|
|
|
expect(
|
|
|
|
wrapper
|
|
|
|
.find('.ant-calendar-range-right .ant-calendar-month-panel-cell')
|
|
|
|
.at(4)
|
|
|
|
.hasClass('ant-calendar-month-panel-cell-disabled'),
|
|
|
|
).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
2019-07-22 17:07:13 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/17135
|
|
|
|
it('the end time should be less than the start time', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<RangePicker defaultValue={[moment(), moment()]} />,
|
|
|
|
);
|
|
|
|
wrapper.find('.ant-calendar-picker-input').simulate('click');
|
|
|
|
const firstInput = wrapper.find('.ant-calendar-input').first();
|
|
|
|
const secondInput = wrapper.find('.ant-calendar-input').last();
|
|
|
|
firstInput.simulate('change', { target: { value: moment().add(1, 'day').format('YYYY-MM-DD')}});
|
|
|
|
expect(firstInput.getDOMNode().value).toBe(moment().add(1, 'day').format('YYYY-MM-DD'));
|
|
|
|
expect(secondInput.getDOMNode().value).toBe('');
|
|
|
|
});
|
2017-01-19 13:55:04 +08:00
|
|
|
});
|