Skip to content

Commit 47f8d06

Browse files
committed
test: Add tests cases for calendar components
1 parent efa2631 commit 47f8d06

File tree

6 files changed

+273
-1
lines changed

6 files changed

+273
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { mount } from '@vue/test-utils';
3+
import { DpCalendarCell } from '@packages/components/Calendar';
4+
import { addDays } from 'date-fns';
5+
6+
const mountComponent = (day: Date) => {
7+
return mount(DpCalendarCell, {
8+
props: { day },
9+
slots: {
10+
default: `
11+
<template #default="{isToday}">
12+
<span v-if="isToday" data-test-id="today-marker"></span>
13+
</template>
14+
`,
15+
},
16+
});
17+
};
18+
19+
describe('DpCalendarCell', () => {
20+
it('Should render component', () => {
21+
const wrapper = mount(DpCalendarCell, { props: { day: new Date() } });
22+
23+
expect(wrapper.html()).toContain('td');
24+
});
25+
26+
it('Should render component as custom HTML tag', () => {
27+
const wrapper = mount(DpCalendarCell, { props: { as: 'div', day: new Date() } });
28+
29+
expect(wrapper.html()).toContain('div');
30+
});
31+
32+
it('Should properly mark today', () => {
33+
const wrapper = mountComponent(new Date());
34+
35+
const todayMark = wrapper.find(`[data-test-id="today-marker"]`);
36+
expect(todayMark.html()).toBeTruthy();
37+
38+
const wrapper2 = mountComponent(addDays(new Date(), 1));
39+
const todayMark2 = wrapper2.find(`[data-test-id="today-marker"]`);
40+
expect(() => todayMark2.html()).toThrowError();
41+
});
42+
});

src/packages/components/Calendar/CalendarGrid/DpCalendarCellTrigger/DpCalendarCellTrigger.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export type DPCalendarCellTriggerProps = ExtractPropTypes<typeof props>;
99
export const DPCalendarCellTrigger = defineComponent({
1010
props,
1111
setup(props: DPCalendarCellTriggerProps, { attrs, slots }) {
12-
return () => h('button', { type: 'button', ...attrs }, [slots.default?.()]);
12+
return () =>
13+
h(props.as, { ...(props.as === 'button' ? { type: 'button' } : {}), ...attrs }, [slots.default?.()]);
1314
},
1415
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { mount } from '@vue/test-utils';
3+
import { DPCalendarCellTrigger } from '@packages/components/Calendar';
4+
5+
describe('DPCalendarCellTrigger', () => {
6+
it('Should render component', () => {
7+
const wrapper = mount(DPCalendarCellTrigger);
8+
9+
expect(wrapper.html()).toContain('button');
10+
});
11+
12+
it('Should render component as custom HTML tag', () => {
13+
const wrapper = mount(DPCalendarCellTrigger, { props: { as: 'div' } });
14+
15+
expect(wrapper.html()).toContain('<div>');
16+
});
17+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { mount } from '@vue/test-utils';
3+
import { DpCalendarGrid } from '@packages/components/Calendar';
4+
5+
describe('DpCalendarGrid', () => {
6+
it('Should render component', () => {
7+
const wrapper = mount(DpCalendarGrid);
8+
9+
expect(wrapper.html()).toContain('<table>');
10+
});
11+
12+
it('Should render component as custom HTML tag', () => {
13+
const wrapper = mount(DpCalendarGrid, { props: { as: 'div' } });
14+
15+
expect(wrapper.html()).toContain('<div>');
16+
});
17+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { mount } from '@vue/test-utils';
3+
import { DpCalendarGridRow } from '@packages/components/Calendar';
4+
5+
describe('DpCalendarGridRow', () => {
6+
it('Should render component', () => {
7+
const wrapper = mount(DpCalendarGridRow);
8+
9+
expect(wrapper.html()).toContain('<tr>');
10+
});
11+
12+
it('Should render component as custom HTML tag', () => {
13+
const wrapper = mount(DpCalendarGridRow, { props: { as: 'div' } });
14+
15+
expect(wrapper.html()).toContain('<div>');
16+
});
17+
});

0 commit comments

Comments
 (0)