|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { mount } from '@vue/test-utils'; |
| 3 | +import { DpCalendarGridBody } from '@packages/components/Calendar'; |
| 4 | +import { DpCalendarKey } from '@packages/components/Calendar/DpCalendar'; |
| 5 | +import { |
| 6 | + endOfMonth, |
| 7 | + endOfWeek, |
| 8 | + getDate, |
| 9 | + getMonth, |
| 10 | + getWeeksInMonth, |
| 11 | + getYear, |
| 12 | + set, |
| 13 | + startOfMonth, |
| 14 | + startOfWeek, |
| 15 | +} from 'date-fns'; |
| 16 | +import type { DpCalendarGridBodyProps } from '@packages/components/Calendar/CalendarGrid/DpCalendarGridBody'; |
| 17 | + |
| 18 | +const props = { |
| 19 | + month: { value: getMonth(new Date()) }, |
| 20 | + year: { value: getYear(new Date()) }, |
| 21 | + weekStart: { value: 1 }, |
| 22 | +}; |
| 23 | + |
| 24 | +const getMonthYear = (date: Date) => { |
| 25 | + return { |
| 26 | + month: { value: getMonth(date) }, |
| 27 | + year: { value: getYear(date) }, |
| 28 | + }; |
| 29 | +}; |
| 30 | + |
| 31 | +const getContext = (overrideProps?: Partial<typeof props>) => { |
| 32 | + return { |
| 33 | + provide: { |
| 34 | + [DpCalendarKey]: { |
| 35 | + ...props, |
| 36 | + ...(overrideProps ?? {}), |
| 37 | + }, |
| 38 | + }, |
| 39 | + }; |
| 40 | +}; |
| 41 | + |
| 42 | +const mountComponent = (overrideProps?: Partial<typeof props>, componentProps?: Partial<DpCalendarGridBodyProps>) => { |
| 43 | + return mount(DpCalendarGridBody, { |
| 44 | + global: getContext(overrideProps), |
| 45 | + props: { as: 'div', ...(componentProps ?? {}) }, |
| 46 | + slots: { |
| 47 | + default: ` |
| 48 | + <template #default="{week}"> |
| 49 | + <div v-for="(week, i) in weeks" :key="i" :data-test-id="i + '-week'" data-week="true"> |
| 50 | + <div v-for="(day, j) in week.days" :key="i" :data-test-id="j + '-day' + '-week-' + i"> |
| 51 | + <span :data-current="day.current"></span> |
| 52 | + {{day.label}} |
| 53 | + </div> |
| 54 | + </div> |
| 55 | + </template>`, |
| 56 | + }, |
| 57 | + }); |
| 58 | +}; |
| 59 | + |
| 60 | +describe('DpCalendarGridBody', () => { |
| 61 | + it('Should render component', () => { |
| 62 | + const wrapper = mount(DpCalendarGridBody, { global: getContext() }); |
| 63 | + |
| 64 | + expect(wrapper.html()).toContain('<tbody>'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('Should render component as custom HTML tag', () => { |
| 68 | + const wrapper = mount(DpCalendarGridBody, { global: getContext(), props: { as: 'div' } }); |
| 69 | + |
| 70 | + expect(wrapper.html()).toContain('<div>'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('Should fail to mount without calendar context', () => { |
| 74 | + expect(() => mount(DpCalendarGridBody)).toThrowError(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('Should provide weeks', () => { |
| 78 | + const wrapper = mountComponent(); |
| 79 | + const weeksInMonth = getWeeksInMonth(new Date(), { weekStartsOn: 1 }); |
| 80 | + const weeks = wrapper.findAll('[data-week="true"]'); |
| 81 | + expect(weeks).toHaveLength(weeksInMonth); |
| 82 | + }); |
| 83 | + |
| 84 | + it('Should start a month with a valid date', () => { |
| 85 | + const wrapper = mountComponent(); |
| 86 | + const first = startOfWeek(startOfMonth(new Date()), { weekStartsOn: 1 }); |
| 87 | + |
| 88 | + const firstEl = wrapper.find(`[data-test-id="${0 + '-day' + '-week-' + 0}"]`); |
| 89 | + |
| 90 | + expect(firstEl.text()).toEqual(`${getDate(first)}`); |
| 91 | + }); |
| 92 | + |
| 93 | + it('Should end a month with a valid date', () => { |
| 94 | + const today = new Date(); |
| 95 | + const wrapper = mountComponent(); |
| 96 | + const last = endOfWeek(endOfMonth(today), { weekStartsOn: 1 }); |
| 97 | + const weeksInMonth = getWeeksInMonth(today, { weekStartsOn: 1 }); |
| 98 | + |
| 99 | + const lastEl = wrapper.find(`[data-test-id="${6 + '-day' + '-week-' + (weeksInMonth - 1)}"]`); |
| 100 | + |
| 101 | + expect(lastEl.text()).toEqual(`${getDate(last)}`); |
| 102 | + }); |
| 103 | + |
| 104 | + it('Should only display weeks that are withing the given month', () => {}); |
| 105 | + |
| 106 | + it('Should display six weeks by appending a week', () => { |
| 107 | + const globalProps = getMonthYear(set(new Date(), { month: 1, year: 2025 })); |
| 108 | + const wrapper = mountComponent(globalProps, { sixWeeks: true }); |
| 109 | + |
| 110 | + const weeks = wrapper.findAll('[data-week="true"]'); |
| 111 | + |
| 112 | + const lastWeek = wrapper.find('[data-test-id="5-week"]'); |
| 113 | + const offsetDays = lastWeek.findAll('[data-current="false"]'); |
| 114 | + |
| 115 | + expect(weeks).toHaveLength(6); |
| 116 | + expect(offsetDays).toHaveLength(7); |
| 117 | + }); |
| 118 | + |
| 119 | + it('Should display six weeks by prepending a week', () => { |
| 120 | + const globalProps = getMonthYear(set(new Date(), { month: 1, year: 2025 })); |
| 121 | + const wrapper = mountComponent(globalProps, { sixWeeks: 'prepend' }); |
| 122 | + |
| 123 | + const weeks = wrapper.findAll('[data-week="true"]'); |
| 124 | + |
| 125 | + const firstWeek = wrapper.find('[data-test-id="0-week"]'); |
| 126 | + const offsetDays = firstWeek.findAll('[data-current="false"]'); |
| 127 | + |
| 128 | + expect(weeks).toHaveLength(6); |
| 129 | + expect(offsetDays).toHaveLength(7); |
| 130 | + }); |
| 131 | + |
| 132 | + it('Should display six weeks by adding a leading week if month starts without offset days', () => { |
| 133 | + let globalProps = getMonthYear(set(new Date(), { month: 8, year: 2025 })); |
| 134 | + let wrapper = mountComponent(globalProps, { sixWeeks: 'center' }); |
| 135 | + |
| 136 | + const weeks = wrapper.findAll('[data-week="true"]'); |
| 137 | + |
| 138 | + const firstWeek = wrapper.find('[data-test-id="0-week"]'); |
| 139 | + const offsetDays = firstWeek.findAll('[data-current="false"]'); |
| 140 | + |
| 141 | + expect(weeks).toHaveLength(6); |
| 142 | + expect(offsetDays).toHaveLength(7); |
| 143 | + |
| 144 | + globalProps = getMonthYear(set(new Date(), { month: 3, year: 2025 })); |
| 145 | + wrapper = mountComponent(globalProps, { sixWeeks: 'center' }); |
| 146 | + |
| 147 | + const lastWeek = wrapper.find('[data-test-id="5-week"]'); |
| 148 | + const lastOffsetDays = lastWeek.findAll('[data-current="false"]'); |
| 149 | + |
| 150 | + expect(weeks).toHaveLength(6); |
| 151 | + expect(lastOffsetDays).toHaveLength(7); |
| 152 | + }); |
| 153 | + |
| 154 | + it('Should display six weeks by appending or prepending based on which has the smaller offset in the partial week', () => { |
| 155 | + let globalProps = getMonthYear(set(new Date(), { month: 1, year: 2025 })); |
| 156 | + let wrapper = mountComponent(globalProps, { sixWeeks: 'fair' }); |
| 157 | + |
| 158 | + const weeks = wrapper.findAll('[data-week="true"]'); |
| 159 | + |
| 160 | + const lastWeek = wrapper.find('[data-test-id="5-week"]'); |
| 161 | + const offsetDaysLast = lastWeek.findAll('[data-current="false"]'); |
| 162 | + |
| 163 | + expect(weeks).toHaveLength(6); |
| 164 | + expect(offsetDaysLast).toHaveLength(7); |
| 165 | + |
| 166 | + wrapper.unmount(); |
| 167 | + |
| 168 | + globalProps = getMonthYear(set(new Date(), { month: 3, year: 2025 })); |
| 169 | + wrapper = mountComponent(globalProps, { sixWeeks: 'fair' }); |
| 170 | + |
| 171 | + const firstWeek = wrapper.find('[data-test-id="0-week"]'); |
| 172 | + |
| 173 | + const offsetDaysFirst = firstWeek.findAll('[data-current="false"]'); |
| 174 | + |
| 175 | + expect(weeks).toHaveLength(6); |
| 176 | + expect(offsetDaysFirst).toHaveLength(7); |
| 177 | + }); |
| 178 | +}); |
0 commit comments