2017-03-03 15:26:23 +08:00
|
|
|
import React from 'react';
|
|
|
|
import Moment from 'moment';
|
|
|
|
import { mount } from 'enzyme';
|
2018-10-21 12:03:32 +08:00
|
|
|
import MockDate from 'mockdate';
|
2017-03-03 15:26:23 +08:00
|
|
|
import Calendar from '..';
|
2019-03-08 21:46:44 +08:00
|
|
|
import Header from '../Header';
|
2017-03-03 15:26:23 +08:00
|
|
|
|
|
|
|
describe('Calendar', () => {
|
|
|
|
it('Calendar should be selectable', () => {
|
|
|
|
const onSelect = jest.fn();
|
2018-12-07 16:17:45 +08:00
|
|
|
const wrapper = mount(<Calendar onSelect={onSelect} />);
|
|
|
|
wrapper
|
|
|
|
.find('.ant-fullcalendar-cell')
|
|
|
|
.at(0)
|
|
|
|
.simulate('click');
|
2017-03-03 15:26:23 +08:00
|
|
|
expect(onSelect).toBeCalledWith(expect.anything());
|
|
|
|
const value = onSelect.mock.calls[0][0];
|
|
|
|
expect(Moment.isMoment(value)).toBe(true);
|
|
|
|
});
|
2018-02-04 16:50:50 +08:00
|
|
|
|
2018-02-03 18:54:04 +08:00
|
|
|
it('only Valid range should be selectable', () => {
|
|
|
|
const onSelect = jest.fn();
|
|
|
|
const validRange = [Moment('2018-02-02'), Moment('2018-02-18')];
|
|
|
|
const wrapper = mount(
|
2018-12-07 16:17:45 +08:00
|
|
|
<Calendar onSelect={onSelect} validRange={validRange} defaultValue={Moment('2018-02-02')} />,
|
2018-02-03 18:54:04 +08:00
|
|
|
);
|
2018-12-07 16:17:45 +08:00
|
|
|
wrapper
|
|
|
|
.find('[title="February 1, 2018"]')
|
|
|
|
.at(0)
|
|
|
|
.simulate('click');
|
|
|
|
wrapper
|
|
|
|
.find('[title="February 2, 2018"]')
|
|
|
|
.at(0)
|
|
|
|
.simulate('click');
|
2018-02-03 18:54:04 +08:00
|
|
|
expect(onSelect.mock.calls.length).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('dates other than in valid range should be disabled', () => {
|
|
|
|
const onSelect = jest.fn();
|
|
|
|
const validRange = [Moment('2018-02-02'), Moment('2018-02-18')];
|
|
|
|
const wrapper = mount(
|
2018-12-07 16:17:45 +08:00
|
|
|
<Calendar onSelect={onSelect} validRange={validRange} defaultValue={Moment('2018-02-02')} />,
|
2018-02-03 18:54:04 +08:00
|
|
|
);
|
2018-12-07 16:17:45 +08:00
|
|
|
wrapper
|
|
|
|
.find('[title="February 20, 2018"]')
|
|
|
|
.at(0)
|
|
|
|
.simulate('click');
|
|
|
|
const elem = wrapper
|
|
|
|
.find('[title="February 20, 2018"]')
|
|
|
|
.hasClass('ant-fullcalendar-disabled-cell');
|
2018-02-03 18:54:04 +08:00
|
|
|
expect(elem).toEqual(true);
|
|
|
|
expect(onSelect.mock.calls.length).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('months other than in valid range should be disabled', () => {
|
|
|
|
const onSelect = jest.fn();
|
|
|
|
const validRange = [Moment('2018-02-02'), Moment('2018-05-18')];
|
|
|
|
const wrapper = mount(
|
2018-12-07 16:17:45 +08:00
|
|
|
<Calendar
|
|
|
|
onSelect={onSelect}
|
|
|
|
validRange={validRange}
|
|
|
|
defaultValue={Moment('2018-02-02')}
|
|
|
|
mode="year"
|
|
|
|
/>,
|
2018-02-03 18:54:04 +08:00
|
|
|
);
|
2018-12-07 16:17:45 +08:00
|
|
|
expect(
|
|
|
|
wrapper
|
|
|
|
.find('[title="Jan"]')
|
|
|
|
.at(0)
|
|
|
|
.hasClass('ant-fullcalendar-month-panel-cell-disabled'),
|
|
|
|
).toBe(true);
|
|
|
|
expect(
|
|
|
|
wrapper
|
|
|
|
.find('[title="Feb"]')
|
|
|
|
.at(0)
|
|
|
|
.hasClass('ant-fullcalendar-month-panel-cell-disabled'),
|
|
|
|
).toBe(false);
|
|
|
|
expect(
|
|
|
|
wrapper
|
|
|
|
.find('[title="Jun"]')
|
|
|
|
.at(0)
|
|
|
|
.hasClass('ant-fullcalendar-month-panel-cell-disabled'),
|
|
|
|
).toBe(true);
|
|
|
|
wrapper
|
|
|
|
.find('[title="Jan"]')
|
|
|
|
.at(0)
|
|
|
|
.simulate('click');
|
|
|
|
wrapper
|
|
|
|
.find('[title="Mar"]')
|
|
|
|
.at(0)
|
|
|
|
.simulate('click');
|
2018-02-03 18:54:04 +08:00
|
|
|
expect(onSelect.mock.calls.length).toBe(1);
|
|
|
|
});
|
|
|
|
|
2018-03-09 17:41:44 +08:00
|
|
|
it('months other than in valid range should not be shown in header', () => {
|
|
|
|
const validRange = [Moment('2017-02-02'), Moment('2018-05-18')];
|
2018-12-07 16:17:45 +08:00
|
|
|
const wrapper = mount(<Calendar validRange={validRange} />);
|
|
|
|
wrapper
|
|
|
|
.find('.ant-fullcalendar-year-select')
|
|
|
|
.hostNodes()
|
|
|
|
.simulate('click');
|
|
|
|
wrapper
|
|
|
|
.find('.ant-select-dropdown-menu-item')
|
|
|
|
.first()
|
|
|
|
.simulate('click');
|
|
|
|
wrapper
|
|
|
|
.find('.ant-fullcalendar-month-select')
|
|
|
|
.hostNodes()
|
|
|
|
.simulate('click');
|
2018-03-09 17:41:44 +08:00
|
|
|
// 2 years and 11 months
|
|
|
|
expect(wrapper.find('.ant-select-dropdown-menu-item').length).toBe(13);
|
|
|
|
});
|
|
|
|
|
2018-02-03 18:54:04 +08:00
|
|
|
it('getDateRange should returns a disabledDate function', () => {
|
|
|
|
const validRange = [Moment('2018-02-02'), Moment('2018-05-18')];
|
2018-12-07 16:17:45 +08:00
|
|
|
const wrapper = mount(<Calendar validRange={validRange} defaultValue={Moment('2018-02-02')} />);
|
2018-02-03 18:54:04 +08:00
|
|
|
const instance = wrapper.instance();
|
|
|
|
const disabledDate = instance.getDateRange(validRange);
|
|
|
|
expect(disabledDate(Moment('2018-06-02'))).toBe(true);
|
|
|
|
expect(disabledDate(Moment('2018-04-02'))).toBe(false);
|
|
|
|
});
|
|
|
|
|
2018-02-04 16:50:50 +08:00
|
|
|
it('Calendar should change mode by prop', () => {
|
|
|
|
const monthMode = 'month';
|
|
|
|
const yearMode = 'year';
|
2018-12-07 16:17:45 +08:00
|
|
|
const wrapper = mount(<Calendar />);
|
2018-02-04 16:50:50 +08:00
|
|
|
expect(wrapper.state().mode).toEqual(monthMode);
|
|
|
|
wrapper.setProps({ mode: 'year' });
|
|
|
|
expect(wrapper.state().mode).toEqual(yearMode);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Calendar should switch mode', () => {
|
|
|
|
const monthMode = 'month';
|
|
|
|
const yearMode = 'year';
|
|
|
|
const onPanelChangeStub = jest.fn();
|
2018-12-07 16:17:45 +08:00
|
|
|
const wrapper = mount(<Calendar mode={yearMode} onPanelChange={onPanelChangeStub} />);
|
2018-02-04 16:50:50 +08:00
|
|
|
expect(wrapper.state().mode).toEqual(yearMode);
|
2019-03-08 15:34:29 +08:00
|
|
|
wrapper.setProps({ mode: monthMode });
|
2018-02-04 16:50:50 +08:00
|
|
|
expect(wrapper.state().mode).toEqual(monthMode);
|
2019-03-08 15:34:29 +08:00
|
|
|
expect(onPanelChangeStub).toHaveBeenCalledTimes(0);
|
2018-02-04 16:50:50 +08:00
|
|
|
});
|
2018-10-19 23:54:10 +08:00
|
|
|
|
|
|
|
it('Calendar should support locale', () => {
|
2018-10-21 12:03:32 +08:00
|
|
|
MockDate.set(Moment('2018-10-19'));
|
2018-10-19 23:54:10 +08:00
|
|
|
// eslint-disable-next-line
|
|
|
|
const zhCN = require('../locale/zh_CN').default;
|
2018-12-07 16:17:45 +08:00
|
|
|
const wrapper = mount(<Calendar locale={zhCN} />);
|
2018-10-19 23:54:10 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
2018-10-21 12:03:32 +08:00
|
|
|
MockDate.reset();
|
2018-10-19 23:54:10 +08:00
|
|
|
});
|
2019-02-26 19:56:55 +08:00
|
|
|
|
|
|
|
it('should trigger onPanelChange when click last month of date', () => {
|
|
|
|
const onPanelChange = jest.fn();
|
|
|
|
const date = new Moment('1990-09-03');
|
|
|
|
const wrapper = mount(<Calendar onPanelChange={onPanelChange} value={date} />);
|
|
|
|
|
|
|
|
wrapper
|
|
|
|
.find('.ant-fullcalendar-cell')
|
|
|
|
.at(0)
|
|
|
|
.simulate('click');
|
|
|
|
|
|
|
|
expect(onPanelChange).toBeCalled();
|
|
|
|
expect(onPanelChange.mock.calls[0][0].month()).toEqual(date.month() - 1);
|
|
|
|
});
|
2019-03-12 14:38:53 +08:00
|
|
|
|
2019-03-12 16:31:29 +08:00
|
|
|
it('switch should work correctly without prop mode', async () => {
|
2019-03-12 14:38:53 +08:00
|
|
|
const onPanelChange = jest.fn();
|
2019-03-12 15:46:12 +08:00
|
|
|
const date = new Moment(new Date(Date.UTC(2017, 7, 9, 8)));
|
2019-03-12 14:59:41 +08:00
|
|
|
const wrapper = mount(<Calendar onPanelChange={onPanelChange} value={date} />);
|
2019-03-12 14:38:53 +08:00
|
|
|
expect(wrapper.state().mode).toBe('month');
|
2019-03-12 17:01:11 +08:00
|
|
|
expect(wrapper.find('.ant-fullcalendar-table').length).toBe(1);
|
|
|
|
expect(wrapper.find('.ant-fullcalendar-month-panel-table').length).toBe(0);
|
2019-03-12 15:06:41 +08:00
|
|
|
wrapper.find('.ant-radio-button-input[value="year"]').simulate('change');
|
2019-03-12 17:01:11 +08:00
|
|
|
expect(wrapper.find('.ant-fullcalendar-table').length).toBe(0);
|
|
|
|
expect(wrapper.find('.ant-fullcalendar-month-panel-table').length).toBe(1);
|
2019-03-12 14:38:53 +08:00
|
|
|
expect(onPanelChange).toBeCalled();
|
|
|
|
expect(onPanelChange.mock.calls[0][1]).toEqual('year');
|
|
|
|
});
|
2019-03-09 19:01:08 +08:00
|
|
|
|
|
|
|
const createWrapper = (start, end, value, onValueChange) => {
|
2019-03-08 21:46:44 +08:00
|
|
|
const wrapper = mount(
|
|
|
|
<Header
|
|
|
|
onValueChange={onValueChange}
|
|
|
|
value={value}
|
|
|
|
validRange={[start, end]}
|
|
|
|
locale={{ year: '年' }}
|
|
|
|
/>,
|
2019-03-09 18:26:30 +08:00
|
|
|
);
|
|
|
|
wrapper
|
|
|
|
.find('.ant-fullcalendar-year-select')
|
|
|
|
.hostNodes()
|
|
|
|
.simulate('click');
|
|
|
|
wrapper
|
|
|
|
.find('.ant-select-dropdown-menu-item')
|
|
|
|
.at(0)
|
|
|
|
.simulate('click');
|
2019-03-09 19:01:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
it('if value.month > end.month, value.month -> end.month', () => {
|
|
|
|
const value = new Moment('1990-01-03');
|
|
|
|
const start = new Moment('2019-04-01');
|
|
|
|
const end = new Moment('2019-11-01');
|
|
|
|
const onValueChange = jest.fn();
|
|
|
|
createWrapper(start, end, value, onValueChange);
|
|
|
|
expect(onValueChange).toHaveBeenCalledWith(value.year('2019').month('3'));
|
2019-03-08 21:46:44 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('if start.month > value.month, value.month -> start.month ', () => {
|
|
|
|
const value = new Moment('1990-01-03');
|
|
|
|
const start = new Moment('2019-11-01');
|
|
|
|
const end = new Moment('2019-03-01');
|
2019-03-09 18:26:30 +08:00
|
|
|
const onValueChange = jest.fn();
|
2019-03-09 19:01:08 +08:00
|
|
|
createWrapper(start, end, value, onValueChange);
|
2019-03-09 18:26:30 +08:00
|
|
|
expect(onValueChange).toHaveBeenCalledWith(value.year('2019').month('10'));
|
2019-03-08 21:46:44 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('onMonthChange should work correctly', () => {
|
2019-03-09 18:26:30 +08:00
|
|
|
const start = new Moment('2018-11-01');
|
|
|
|
const end = new Moment('2019-03-01');
|
|
|
|
const value = new Moment('2018-12-03');
|
|
|
|
const onValueChange = jest.fn();
|
2019-03-08 21:46:44 +08:00
|
|
|
const wrapper = mount(
|
2019-03-09 18:26:30 +08:00
|
|
|
<Header
|
|
|
|
onValueChange={onValueChange}
|
|
|
|
value={value}
|
|
|
|
validRange={[start, end]}
|
|
|
|
locale={{ year: '年' }}
|
|
|
|
type="date"
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
wrapper
|
|
|
|
.find('.ant-fullcalendar-month-select')
|
|
|
|
.hostNodes()
|
|
|
|
.simulate('click');
|
|
|
|
wrapper
|
|
|
|
.find('.ant-select-dropdown-menu-item')
|
|
|
|
.at(0)
|
|
|
|
.simulate('click');
|
|
|
|
expect(onValueChange).toHaveBeenCalledWith(value.month(10));
|
2019-03-08 21:46:44 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('onTypeChange should work correctly', () => {
|
2019-03-09 18:26:30 +08:00
|
|
|
const onTypeChange = jest.fn();
|
|
|
|
const value = new Moment('2018-12-03');
|
2019-03-08 21:46:44 +08:00
|
|
|
const wrapper = mount(
|
2019-03-09 18:26:30 +08:00
|
|
|
<Header
|
|
|
|
onTypeChange={onTypeChange}
|
|
|
|
locale={{ year: '年', month: '月' }}
|
|
|
|
value={value}
|
|
|
|
type="date"
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
wrapper
|
|
|
|
.find('input')
|
|
|
|
.at(1)
|
|
|
|
.simulate('change');
|
|
|
|
expect(onTypeChange).toBeCalledWith('month');
|
2019-03-08 21:46:44 +08:00
|
|
|
});
|
2017-03-03 15:26:23 +08:00
|
|
|
});
|