|
| 1 | +import Moment from 'moment' |
| 2 | +import { mount } from '@vue/test-utils' |
| 3 | +import Vue from 'vue' |
| 4 | +import Calendar from '..' |
| 5 | + |
| 6 | +function $$ (className) { |
| 7 | + return document.body.querySelectorAll(className) |
| 8 | +} |
| 9 | +describe('Calendar', () => { |
| 10 | + it('Calendar should be selectable', () => { |
| 11 | + const onSelect = jest.fn() |
| 12 | + const wrapper = mount( |
| 13 | + { |
| 14 | + render () { |
| 15 | + return <Calendar onSelect={onSelect} /> |
| 16 | + }, |
| 17 | + }, |
| 18 | + { sync: false } |
| 19 | + ) |
| 20 | + wrapper.findAll('.ant-fullcalendar-cell').at(0).trigger('click') |
| 21 | + expect(onSelect).toBeCalledWith(expect.anything()) |
| 22 | + const value = onSelect.mock.calls[0][0] |
| 23 | + expect(Moment.isMoment(value)).toBe(true) |
| 24 | + }) |
| 25 | + |
| 26 | + it('only Valid range should be selectable', () => { |
| 27 | + const onSelect = jest.fn() |
| 28 | + const validRange = [Moment('2018-02-02'), Moment('2018-02-18')] |
| 29 | + const wrapper = mount( |
| 30 | + { |
| 31 | + render () { |
| 32 | + return <Calendar onSelect={onSelect} validRange={validRange} defaultValue={Moment('2018-02-02')} /> |
| 33 | + }, |
| 34 | + }, |
| 35 | + { sync: false } |
| 36 | + ) |
| 37 | + wrapper.findAll('[title="February 1, 2018"]').at(0).trigger('click') |
| 38 | + wrapper.findAll('[title="February 2, 2018"]').at(0).trigger('click') |
| 39 | + expect(onSelect.mock.calls.length).toBe(1) |
| 40 | + }) |
| 41 | + |
| 42 | + it('dates other than in valid range should be disabled', () => { |
| 43 | + const onSelect = jest.fn() |
| 44 | + const validRange = [Moment('2018-02-02'), Moment('2018-02-18')] |
| 45 | + const wrapper = mount( |
| 46 | + { |
| 47 | + render () { |
| 48 | + return <Calendar onSelect={onSelect} validRange={validRange} defaultValue={Moment('2018-02-02')} /> |
| 49 | + }, |
| 50 | + } |
| 51 | + ) |
| 52 | + wrapper.findAll('[title="February 20, 2018"]').at(0).trigger('click') |
| 53 | + expect(wrapper.find('[title="February 20, 2018"]').classes()).toContain('ant-fullcalendar-disabled-cell') |
| 54 | + expect(onSelect.mock.calls.length).toBe(0) |
| 55 | + }) |
| 56 | + |
| 57 | + it('months other than in valid range should be disabled', () => { |
| 58 | + const onSelect = jest.fn() |
| 59 | + const validRange = [Moment('2018-02-02'), Moment('2018-05-18')] |
| 60 | + const wrapper = mount( |
| 61 | + { |
| 62 | + render () { |
| 63 | + return <Calendar onSelect={onSelect} validRange={validRange} defaultValue={Moment('2018-02-02')} mode='year' /> |
| 64 | + }, |
| 65 | + }, |
| 66 | + { sync: false } |
| 67 | + ) |
| 68 | + expect(wrapper.findAll('[title="Jan"]').at(0).classes()).toContain('ant-fullcalendar-month-panel-cell-disabled') |
| 69 | + expect(wrapper.findAll('[title="Feb"]').at(0).classes()).not.toContain('ant-fullcalendar-month-panel-cell-disabled') |
| 70 | + expect(wrapper.findAll('[title="Jun"]').at(0).classes()).toContain('ant-fullcalendar-month-panel-cell-disabled') |
| 71 | + wrapper.findAll('[title="Jan"]').at(0).trigger('click') |
| 72 | + wrapper.findAll('[title="Mar"]').at(0).trigger('click') |
| 73 | + expect(onSelect.mock.calls.length).toBe(1) |
| 74 | + }) |
| 75 | + |
| 76 | + it('months other than in valid range should not be shown in header', (done) => { |
| 77 | + document.body.innerHTML = '' |
| 78 | + const validRange = [Moment('2017-02-02'), Moment('2018-05-18')] |
| 79 | + const wrapper = mount( |
| 80 | + { |
| 81 | + render () { |
| 82 | + return <Calendar validRange={validRange} /> |
| 83 | + }, |
| 84 | + }, |
| 85 | + { sync: false } |
| 86 | + ) |
| 87 | + wrapper.find('.ant-fullcalendar-year-select').trigger('click') |
| 88 | + Vue.nextTick(() => { |
| 89 | + $$('.ant-select-dropdown-menu-item')[0].click() |
| 90 | + wrapper.find('.ant-fullcalendar-month-select').trigger('click') |
| 91 | + Vue.nextTick(() => { |
| 92 | + expect($$('.ant-select-dropdown-menu-item').length).toBe(13) |
| 93 | + done() |
| 94 | + }) |
| 95 | + }) |
| 96 | + }) |
| 97 | + |
| 98 | + it('getDateRange should returns a disabledDate function', () => { |
| 99 | + const validRange = [Moment('2018-02-02'), Moment('2018-05-18')] |
| 100 | + const wrapper = mount( |
| 101 | + Calendar, { |
| 102 | + propsData: { |
| 103 | + validRange, |
| 104 | + defaultValue: Moment('2018-02-02'), |
| 105 | + }, |
| 106 | + } |
| 107 | + ) |
| 108 | + const instance = wrapper.vm |
| 109 | + const disabledDate = instance.getDateRange(validRange) |
| 110 | + expect(disabledDate(Moment('2018-06-02'))).toBe(true) |
| 111 | + expect(disabledDate(Moment('2018-04-02'))).toBe(false) |
| 112 | + }) |
| 113 | + |
| 114 | + it('Calendar should change mode by prop', (done) => { |
| 115 | + const monthMode = 'month' |
| 116 | + const yearMode = 'year' |
| 117 | + const wrapper = mount(Calendar, { sync: false }) |
| 118 | + expect(wrapper.vm.sMode).toEqual(monthMode) |
| 119 | + wrapper.setProps({ mode: 'year' }) |
| 120 | + Vue.nextTick(() => { |
| 121 | + expect(wrapper.vm.sMode).toEqual(yearMode) |
| 122 | + done() |
| 123 | + }) |
| 124 | + }) |
| 125 | + |
| 126 | + it('Calendar should switch mode', () => { |
| 127 | + const monthMode = 'month' |
| 128 | + const yearMode = 'year' |
| 129 | + const onPanelChangeStub = jest.fn() |
| 130 | + const wrapper = mount( |
| 131 | + Calendar, { |
| 132 | + propsData: { |
| 133 | + mode: yearMode, |
| 134 | + }, |
| 135 | + listeners: { |
| 136 | + panelChange: onPanelChangeStub, |
| 137 | + }, |
| 138 | + sync: false, |
| 139 | + } |
| 140 | + ) |
| 141 | + expect(wrapper.vm.sMode).toEqual(yearMode) |
| 142 | + wrapper.vm.setType('date') |
| 143 | + expect(wrapper.vm.sMode).toEqual(monthMode) |
| 144 | + expect(onPanelChangeStub).toHaveBeenCalledTimes(1) |
| 145 | + }) |
| 146 | +}) |
0 commit comments