ant-design-vue/components/calendar/__tests__/index.test.js

230 lines
6.1 KiB
JavaScript
Raw Normal View History

2019-01-12 03:33:27 +00:00
import Moment from 'moment';
import { mount } from '@vue/test-utils';
import { asyncExpect } from '@/tests/utils';
import MockDate from 'mockdate';
import Calendar from '..';
2018-05-21 14:59:04 +00:00
2019-01-12 03:33:27 +00:00
function $$(className) {
return document.body.querySelectorAll(className);
2018-05-21 14:59:04 +00:00
}
describe('Calendar', () => {
2018-12-08 04:05:42 +00:00
it('Calendar should be selectable', async () => {
2019-01-12 03:33:27 +00:00
const onSelect = jest.fn();
2018-05-21 14:59:04 +00:00
const wrapper = mount(
{
2019-01-12 03:33:27 +00:00
render() {
return <Calendar onSelect={onSelect} />;
2018-05-21 14:59:04 +00:00
},
},
2019-01-12 03:33:27 +00:00
{ sync: false },
);
2018-12-08 04:05:42 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
wrapper
.findAll('.ant-fullcalendar-cell')
.at(0)
.trigger('click');
});
2018-12-08 04:05:42 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(onSelect).toBeCalledWith(expect.anything());
const value = onSelect.mock.calls[0][0];
expect(Moment.isMoment(value)).toBe(true);
});
});
2018-05-21 14:59:04 +00:00
2018-12-08 04:05:42 +00:00
it('only Valid range should be selectable', async () => {
2019-01-12 03:33:27 +00:00
const onSelect = jest.fn();
const validRange = [Moment('2018-02-02'), Moment('2018-02-18')];
2018-05-21 14:59:04 +00:00
const wrapper = mount(
{
2019-01-12 03:33:27 +00:00
render() {
return (
<Calendar
onSelect={onSelect}
validRange={validRange}
defaultValue={Moment('2018-02-02')}
/>
);
2018-05-21 14:59:04 +00:00
},
},
2019-01-12 03:33:27 +00:00
{ sync: false },
);
2018-12-08 04:05:42 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
wrapper
.findAll('[title="February 1, 2018"]')
.at(0)
.trigger('click');
wrapper
.findAll('[title="February 2, 2018"]')
.at(0)
.trigger('click');
expect(onSelect.mock.calls.length).toBe(1);
});
});
2018-05-21 14:59:04 +00:00
2018-12-08 04:05:42 +00:00
it('dates other than in valid range should be disabled', async () => {
2019-01-12 03:33:27 +00:00
const onSelect = jest.fn();
const validRange = [Moment('2018-02-02'), Moment('2018-02-18')];
2018-05-21 14:59:04 +00:00
const wrapper = mount(
{
2019-01-12 03:33:27 +00:00
render() {
return (
<Calendar
onSelect={onSelect}
validRange={validRange}
defaultValue={Moment('2018-02-02')}
/>
);
2018-05-21 14:59:04 +00:00
},
2018-05-30 06:12:43 +00:00
},
2019-01-12 03:33:27 +00:00
{ sync: false },
);
2018-12-08 04:05:42 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
wrapper
.findAll('[title="February 20, 2018"]')
.at(0)
.trigger('click');
expect(wrapper.find('[title="February 20, 2018"]').classes()).toContain(
'ant-fullcalendar-disabled-cell',
);
expect(onSelect.mock.calls.length).toBe(0);
});
});
2018-05-21 14:59:04 +00:00
2018-12-08 04:05:42 +00:00
it('months other than in valid range should be disabled', async () => {
2019-01-12 03:33:27 +00:00
const onSelect = jest.fn();
const validRange = [Moment('2018-02-02'), Moment('2018-05-18')];
2018-05-21 14:59:04 +00:00
const wrapper = mount(
{
2019-01-12 03:33:27 +00:00
render() {
return (
<Calendar
onSelect={onSelect}
validRange={validRange}
defaultValue={Moment('2018-02-02')}
mode="year"
/>
);
2018-05-21 14:59:04 +00:00
},
},
2019-01-12 03:33:27 +00:00
{ sync: false },
);
2018-12-08 04:05:42 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(
wrapper
.findAll('[title="Jan"]')
.at(0)
.classes(),
).toContain('ant-fullcalendar-month-panel-cell-disabled');
expect(
wrapper
.findAll('[title="Feb"]')
.at(0)
.classes(),
).not.toContain('ant-fullcalendar-month-panel-cell-disabled');
expect(
wrapper
.findAll('[title="Jun"]')
.at(0)
.classes(),
).toContain('ant-fullcalendar-month-panel-cell-disabled');
wrapper
.findAll('[title="Jan"]')
.at(0)
.trigger('click');
wrapper
.findAll('[title="Mar"]')
.at(0)
.trigger('click');
expect(onSelect.mock.calls.length).toBe(1);
});
});
2018-05-21 14:59:04 +00:00
it('months other than in valid range should not be shown in header', async () => {
2019-01-12 03:33:27 +00:00
document.body.innerHTML = '';
const validRange = [Moment('2017-02-02'), Moment('2018-05-18')];
2018-05-21 14:59:04 +00:00
const wrapper = mount(
{
2019-01-12 03:33:27 +00:00
render() {
return <Calendar validRange={validRange} />;
2018-05-21 14:59:04 +00:00
},
},
2019-01-12 03:33:27 +00:00
{ sync: false, attachToDocument: true },
);
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
wrapper.find('.ant-fullcalendar-year-select').trigger('click');
});
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
$$('.ant-select-dropdown-menu-item')[0].click();
}, 0);
});
2018-05-21 14:59:04 +00:00
2018-12-08 04:05:42 +00:00
it('getDateRange should returns a disabledDate function', async () => {
2019-01-12 03:33:27 +00:00
const validRange = [Moment('2018-02-02'), Moment('2018-05-18')];
const wrapper = mount(Calendar, {
propsData: {
validRange,
defaultValue: Moment('2018-02-02'),
},
sync: false,
});
2018-12-08 04:05:42 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
const instance = wrapper.vm;
const disabledDate = instance.getDateRange(validRange);
expect(disabledDate(Moment('2018-06-02'))).toBe(true);
expect(disabledDate(Moment('2018-04-02'))).toBe(false);
});
});
2018-05-21 14:59:04 +00:00
2018-12-08 04:05:42 +00:00
it('Calendar should change mode by prop', async () => {
2019-01-12 03:33:27 +00:00
const monthMode = 'month';
const yearMode = 'year';
const wrapper = mount(Calendar, { sync: false });
2018-12-08 04:05:42 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(wrapper.vm.sMode).toEqual(monthMode);
wrapper.setProps({ mode: 'year' });
});
2018-12-08 04:05:42 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(wrapper.vm.sMode).toEqual(yearMode);
});
});
2018-05-21 14:59:04 +00:00
2018-12-08 04:05:42 +00:00
it('Calendar should switch mode', async () => {
2019-01-12 03:33:27 +00:00
const monthMode = 'month';
const yearMode = 'year';
const onPanelChangeStub = jest.fn();
const wrapper = mount(Calendar, {
propsData: {
mode: yearMode,
},
listeners: {
panelChange: onPanelChangeStub,
},
sync: false,
});
2018-12-08 04:05:42 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(wrapper.vm.sMode).toEqual(yearMode);
wrapper.vm.setType('date');
});
2018-12-08 04:05:42 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(wrapper.vm.sMode).toEqual(monthMode);
expect(onPanelChangeStub).toHaveBeenCalledTimes(1);
});
});
2018-12-08 04:05:42 +00:00
it('Calendar should support locale', async () => {
2019-01-12 03:33:27 +00:00
MockDate.set(Moment('2018-10-19'));
// eslint-disable-next-line
const zhCN = require('../locale/zh_CN').default;
2019-01-12 03:33:27 +00:00
const wrapper = mount(Calendar, {
propsData: {
locale: zhCN,
},
sync: false,
});
2018-12-08 04:05:42 +00:00
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(wrapper.html()).toMatchSnapshot();
MockDate.reset();
});
});
});