|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import VueDatePicker from '@/components/VueDatePicker.vue'; |
| 3 | +import { mount } from '@vue/test-utils'; |
| 4 | + |
| 5 | +const mountDatepicker = (props: any = { modelValue: '' }) => |
| 6 | + mount(VueDatePicker, { props }); |
| 7 | + |
| 8 | +describe('VueCalendar', () => { |
| 9 | + it('Should display correct date items for a single date', () => { |
| 10 | + const wrapper = mountDatepicker({ |
| 11 | + modelValue: '2028-02-15', |
| 12 | + }); |
| 13 | + |
| 14 | + const headerDate = wrapper.find('.sib-calendar__header__date'); |
| 15 | + |
| 16 | + expect(headerDate.html()).toContain('February'); |
| 17 | + expect(headerDate.html()).toContain('2028'); |
| 18 | + }); |
| 19 | + |
| 20 | + it('Should display correct date items for a date range', () => { |
| 21 | + const wrapper = mountDatepicker({ |
| 22 | + modelValue: ['2028-02-15', '2030-03-20'], |
| 23 | + range: true, |
| 24 | + }); |
| 25 | + |
| 26 | + const headerDate = wrapper.find('.sib-calendar__header__date'); |
| 27 | + |
| 28 | + expect(headerDate.html()).toContain('March'); |
| 29 | + expect(headerDate.html()).toContain('2030'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('Should display correct weekday items when startWeekOnMonday is set', () => { |
| 33 | + const wrapper = mountDatepicker({ |
| 34 | + modelValue: '2028-02-15', |
| 35 | + startWeekOnMonday: true, |
| 36 | + }); |
| 37 | + |
| 38 | + const tableHeadItem = wrapper.findAll('.sib-calendar__table__head__item'); |
| 39 | + |
| 40 | + expect(tableHeadItem[0].text()).toEqual('Mo'); |
| 41 | + expect(tableHeadItem[6].text()).toEqual('Su'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('Should display correct calendar table', () => { |
| 45 | + const wrapper = mountDatepicker({ |
| 46 | + modelValue: '2028-02', |
| 47 | + monthPicker: true, |
| 48 | + }); |
| 49 | + |
| 50 | + const table = wrapper.find('[aria-labelledby="Months calendar"]'); |
| 51 | + |
| 52 | + expect((table.element as HTMLTableElement).style.display).toBe(''); |
| 53 | + }); |
| 54 | + |
| 55 | + it('Should display correct calendar days', () => { |
| 56 | + const wrapper = mountDatepicker({ |
| 57 | + modelValue: '2028-02-15', |
| 58 | + }); |
| 59 | + |
| 60 | + const firstDay = wrapper.find('[data-test-id="2028-01-30"]'); |
| 61 | + const lastDay = wrapper.find('[data-test-id="2028-03-04"]'); |
| 62 | + |
| 63 | + expect(firstDay.exists()).toBe(true); |
| 64 | + expect(lastDay.exists()).toBe(true); |
| 65 | + }); |
| 66 | + |
| 67 | + it('Should display correct calendar months', () => { |
| 68 | + const wrapper = mountDatepicker({ |
| 69 | + modelValue: '2028-02-15', |
| 70 | + monthPicker: true, |
| 71 | + }); |
| 72 | + |
| 73 | + const months = wrapper.findAll('.sib-calendar__table__body__item--months'); |
| 74 | + |
| 75 | + expect(months[0].html()).toContain('Jan'); |
| 76 | + expect(months[11].html()).toContain('Dec'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('Should display correct calendar years', () => { |
| 80 | + const wrapper = mountDatepicker({ |
| 81 | + modelValue: '2028-02-15', |
| 82 | + yearPicker: true, |
| 83 | + }); |
| 84 | + |
| 85 | + const months = wrapper.findAll('.sib-calendar__table__body__item--years'); |
| 86 | + |
| 87 | + expect(months[0].html()).toContain('1928'); |
| 88 | + expect(months[200].html()).toContain('2128'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('Should select correct calendar day for a single date', () => { |
| 92 | + const wrapper = mountDatepicker({ |
| 93 | + modelValue: '2028-02-15', |
| 94 | + }); |
| 95 | + |
| 96 | + const selectedDay = wrapper.find( |
| 97 | + '.sib-calendar__table__body__item--selected', |
| 98 | + ); |
| 99 | + |
| 100 | + expect(selectedDay.html()).toContain('data-test-id="2028-02-15"'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('Should select correct calendar days for a date range', () => { |
| 104 | + const wrapper = mountDatepicker({ |
| 105 | + modelValue: ['2028-02-15', '2028-02-20'], |
| 106 | + range: true, |
| 107 | + }); |
| 108 | + |
| 109 | + const selectedDays = wrapper.findAll( |
| 110 | + '.sib-calendar__table__body__item--selected', |
| 111 | + ); |
| 112 | + |
| 113 | + expect(selectedDays[0].html()).toContain('data-test-id="2028-02-15"'); |
| 114 | + expect(selectedDays[1].html()).toContain('data-test-id="2028-02-20"'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('Should select correct calendar months for a date range', () => { |
| 118 | + const wrapper = mountDatepicker({ |
| 119 | + modelValue: ['2028-02', '2028-06'], |
| 120 | + range: true, |
| 121 | + monthPicker: true, |
| 122 | + }); |
| 123 | + |
| 124 | + const selectedMonths = wrapper.findAll( |
| 125 | + '.sib-calendar__table__body__item--selected', |
| 126 | + ); |
| 127 | + |
| 128 | + expect(selectedMonths[0].html()).toContain('data-test-id="2028-02"'); |
| 129 | + expect(selectedMonths[1].html()).toContain('data-test-id="2028-06"'); |
| 130 | + }); |
| 131 | + |
| 132 | + it('Should select correct calendar years for a date range', () => { |
| 133 | + const wrapper = mountDatepicker({ |
| 134 | + modelValue: ['2028', '2032'], |
| 135 | + range: true, |
| 136 | + yearPicker: true, |
| 137 | + }); |
| 138 | + |
| 139 | + const selectedYears = wrapper.findAll( |
| 140 | + '.sib-calendar__table__body__item--selected', |
| 141 | + ); |
| 142 | + |
| 143 | + expect(selectedYears[0].html()).toContain('data-test-id="2028"'); |
| 144 | + expect(selectedYears[1].html()).toContain('data-test-id="2032"'); |
| 145 | + }); |
| 146 | + |
| 147 | + it('Should switch to correct month once header button is clicked', async () => { |
| 148 | + const wrapper = mountDatepicker({ |
| 149 | + modelValue: '2028-02-15', |
| 150 | + }); |
| 151 | + |
| 152 | + const datepicker = wrapper.find('.sib-datepicker'); |
| 153 | + await datepicker.trigger('click'); |
| 154 | + |
| 155 | + const buttons = wrapper.findAll('.sib-calendar__header__button'); |
| 156 | + await buttons[1].trigger('click'); |
| 157 | + await buttons[1].trigger('click'); |
| 158 | + await buttons[0].trigger('click'); |
| 159 | + |
| 160 | + const headerDate = wrapper.find('.sib-calendar__header__date'); |
| 161 | + const firstDayOfMonth = wrapper.find('[data-test-id="2028-03-01"]'); |
| 162 | + |
| 163 | + expect(headerDate.html()).toContain('March'); |
| 164 | + expect(firstDayOfMonth.exists()).toBe(true); |
| 165 | + }); |
| 166 | + |
| 167 | + it('Should switch to correct calendar date once selected through date items', async () => { |
| 168 | + Element.prototype.scrollIntoView = () => {}; |
| 169 | + const wrapper = mountDatepicker({ |
| 170 | + modelValue: '2028-02-15', |
| 171 | + }); |
| 172 | + |
| 173 | + const datepicker = wrapper.find('.sib-datepicker'); |
| 174 | + await datepicker.trigger('click'); |
| 175 | + |
| 176 | + const dateItems = wrapper.findAll('.sib-calendar__header__date__item'); |
| 177 | + |
| 178 | + await dateItems[1].trigger('click'); |
| 179 | + const selectYear = wrapper.find('[data-test-id="2032"]'); |
| 180 | + await selectYear.trigger('click'); |
| 181 | + |
| 182 | + await dateItems[0].trigger('click'); |
| 183 | + const selectMonth = wrapper.find('[data-test-id="2032-06"]'); |
| 184 | + await selectMonth.trigger('click'); |
| 185 | + |
| 186 | + const headerDate = wrapper.find('.sib-calendar__header__date'); |
| 187 | + const firstDayOfMonth = wrapper.find('[data-test-id="2032-06-01"]'); |
| 188 | + |
| 189 | + expect(headerDate.html()).toContain('June'); |
| 190 | + expect(headerDate.html()).toContain('2032'); |
| 191 | + expect(firstDayOfMonth.exists()).toBe(true); |
| 192 | + }); |
| 193 | + |
| 194 | + it('Should restrict correct header button when min is set', () => { |
| 195 | + const wrapper = mountDatepicker({ |
| 196 | + modelValue: '2028-02-15', |
| 197 | + min: '2028-02-01', |
| 198 | + }); |
| 199 | + |
| 200 | + const buttons = wrapper.findAll('.sib-calendar__header__button'); |
| 201 | + |
| 202 | + expect(buttons[0].classes('sib-calendar__header__button--restricted')).toBe( |
| 203 | + true, |
| 204 | + ); |
| 205 | + }); |
| 206 | + |
| 207 | + it('Should restrict correct header button when max is set', () => { |
| 208 | + const wrapper = mountDatepicker({ |
| 209 | + modelValue: '2028-02-15', |
| 210 | + max: '2028-02-29', |
| 211 | + }); |
| 212 | + |
| 213 | + const buttons = wrapper.findAll('.sib-calendar__header__button'); |
| 214 | + |
| 215 | + expect(buttons[1].classes('sib-calendar__header__button--restricted')).toBe( |
| 216 | + true, |
| 217 | + ); |
| 218 | + }); |
| 219 | + |
| 220 | + it('Should restrict correct days when min is set', () => { |
| 221 | + const wrapper = mountDatepicker({ |
| 222 | + modelValue: '2028-02-15', |
| 223 | + min: '2028-02-05', |
| 224 | + }); |
| 225 | + |
| 226 | + const days = wrapper.findAll('.sib-calendar__table__body__item--days'); |
| 227 | + |
| 228 | + expect(days[5].classes('sib-calendar__table__body__item--restricted')).toBe( |
| 229 | + true, |
| 230 | + ); |
| 231 | + expect(days[6].classes('sib-calendar__table__body__item--restricted')).toBe( |
| 232 | + false, |
| 233 | + ); |
| 234 | + }); |
| 235 | + |
| 236 | + it('Should restrict correct days when max is set', () => { |
| 237 | + const wrapper = mountDatepicker({ |
| 238 | + modelValue: '2028-02-15', |
| 239 | + max: '2028-02-25', |
| 240 | + }); |
| 241 | + |
| 242 | + const days = wrapper.findAll('.sib-calendar__table__body__item--days'); |
| 243 | + |
| 244 | + expect( |
| 245 | + days[26].classes('sib-calendar__table__body__item--restricted'), |
| 246 | + ).toBe(false); |
| 247 | + expect( |
| 248 | + days[27].classes('sib-calendar__table__body__item--restricted'), |
| 249 | + ).toBe(true); |
| 250 | + }); |
| 251 | + |
| 252 | + it('Should restrict correct months when min is set', () => { |
| 253 | + const wrapper = mountDatepicker({ |
| 254 | + modelValue: '2028-02-15', |
| 255 | + monthPicker: true, |
| 256 | + min: '2028-02-01', |
| 257 | + }); |
| 258 | + |
| 259 | + const months = wrapper.findAll('.sib-calendar__table__body__item--months'); |
| 260 | + |
| 261 | + expect( |
| 262 | + months[0].classes('sib-calendar__table__body__item--restricted'), |
| 263 | + ).toBe(true); |
| 264 | + expect( |
| 265 | + months[1].classes('sib-calendar__table__body__item--restricted'), |
| 266 | + ).toBe(false); |
| 267 | + }); |
| 268 | + |
| 269 | + it('Should restrict correct months when max is set', () => { |
| 270 | + const wrapper = mountDatepicker({ |
| 271 | + modelValue: '2028-02-15', |
| 272 | + monthPicker: true, |
| 273 | + max: '2028-02-29', |
| 274 | + }); |
| 275 | + |
| 276 | + const months = wrapper.findAll('.sib-calendar__table__body__item--months'); |
| 277 | + |
| 278 | + expect( |
| 279 | + months[1].classes('sib-calendar__table__body__item--restricted'), |
| 280 | + ).toBe(false); |
| 281 | + expect( |
| 282 | + months[2].classes('sib-calendar__table__body__item--restricted'), |
| 283 | + ).toBe(true); |
| 284 | + }); |
| 285 | + |
| 286 | + it('Should restrict correct years when min is set', () => { |
| 287 | + const wrapper = mountDatepicker({ |
| 288 | + modelValue: '2028-02-15', |
| 289 | + yearPicker: true, |
| 290 | + min: '2028-01-01', |
| 291 | + }); |
| 292 | + |
| 293 | + const years = wrapper.findAll('.sib-calendar__table__body__item--years'); |
| 294 | + |
| 295 | + expect( |
| 296 | + years[99].classes('sib-calendar__table__body__item--restricted'), |
| 297 | + ).toBe(true); |
| 298 | + expect( |
| 299 | + years[100].classes('sib-calendar__table__body__item--restricted'), |
| 300 | + ).toBe(false); |
| 301 | + }); |
| 302 | + |
| 303 | + it('Should restrict correct years when max is set', () => { |
| 304 | + const wrapper = mountDatepicker({ |
| 305 | + modelValue: '2028-02-15', |
| 306 | + yearPicker: true, |
| 307 | + max: '2028-12-31', |
| 308 | + }); |
| 309 | + |
| 310 | + const years = wrapper.findAll('.sib-calendar__table__body__item--years'); |
| 311 | + |
| 312 | + expect( |
| 313 | + years[100].classes('sib-calendar__table__body__item--restricted'), |
| 314 | + ).toBe(false); |
| 315 | + expect( |
| 316 | + years[101].classes('sib-calendar__table__body__item--restricted'), |
| 317 | + ).toBe(true); |
| 318 | + }); |
| 319 | +}); |
0 commit comments