Skip to content

Commit 32b2d63

Browse files
committed
refactor!: Add dp- prefix to calendar cell ids, match the data-test-id attributes to the same value (fixes #1051)
1 parent f809f79 commit 32b2d63

File tree

5 files changed

+20
-21
lines changed

5 files changed

+20
-21
lines changed

src/VueDatePicker/components/DatePicker/DpCalendar.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
:aria-disabled="dayVal.classData.dp__cell_disabled || undefined"
4545
:aria-label="defaultedAriaLabels?.day?.(dayVal)"
4646
:tabindex="!dayVal.current && hideOffsetDates ? undefined : 0"
47-
:data-test-id="dayVal.value"
47+
:data-test-id="getCellId(dayVal.value)"
4848
@click.prevent="onDateSelect($event, dayVal)"
4949
@touchend="onDateSelect($event, dayVal, false)"
5050
@keydown="checkKeyDown($event, () => $emit('select-date', dayVal))"

src/VueDatePicker/utils/date-utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ export const checkHighlightYear = (defaultedHighlight: Highlight | HighlightFn,
443443
};
444444

445445
export const getCellId = (date: Date) => {
446-
return format(date, 'yyyy-MM-dd');
446+
return `dp-${format(date, 'yyyy-MM-dd')}`;
447447
};
448448

449449
export const getBeforeAndAfterInRange = (range: number, date: Date) => {

tests/unit/behaviour.spec.ts

+11-13
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { resetDateTime } from '@/utils/date-utils';
2424
import {
2525
clickCalendarDate,
2626
clickSelectBtn,
27+
getCalendarCell,
2728
getCellClasses,
2829
getMonthName,
2930
hoverCalendarDate,
@@ -94,11 +95,11 @@ describe('It should validate various picker scenarios', () => {
9495
it('Should not switch calendars in 1 month range with multi-calendars enabled (#472)', async () => {
9596
const start = set(new Date(), { month: 5 });
9697
const dp = await openMenu({ multiCalendars: true, range: true, startDate: start });
97-
const firstDate = resetDateTime(start);
98-
const secondDate = resetDateTime(set(firstDate, { month: getMonth(addMonths(firstDate, 1)), date: 15 }));
98+
// const firstDate = resetDateTime(start);
99+
const end = set(start, { month: getMonth(addMonths(start, 1)), date: 15 });
99100

100-
const firstDateEl = dp.find(`[data-test-id="${firstDate}"]`);
101-
const secondDateEl = dp.find(`[data-test-id="${secondDate}"]`);
101+
const firstDateEl = getCalendarCell(dp, start);
102+
const secondDateEl = getCalendarCell(dp, end);
102103

103104
await firstDateEl.trigger('click');
104105
await secondDateEl.trigger('click');
@@ -246,23 +247,20 @@ describe('It should validate various picker scenarios', () => {
246247
});
247248

248249
it('Should not break flow on changing months and years when calendar is first step (#553)', async () => {
249-
const start = startOfYear(new Date());
250+
const start = addDays(startOfYear(new Date()), 1);
250251
const flow = [FlowStep.calendar, FlowStep.time];
251252
const dp = await openMenu({ flow, startDate: start });
252-
const today = resetDateTime(start);
253-
const nextMonth = addMonths(today, 1);
253+
const nextMonth = addMonths(start, 1);
254254

255-
await clickCalendarDate(dp, today);
255+
await clickCalendarDate(dp, start);
256256

257257
expect(dp.html()).toContain('dp__overlay');
258258

259259
await reOpenMenu(dp);
260260

261261
await dp.find(`[data-test-id="month-toggle-overlay-0"]`).trigger('click');
262262
await dp.find(`[data-test-id="${getMonthName(nextMonth)}"]`).trigger('click');
263-
264-
const cell = dp.find(`[data-test-id="${nextMonth}"]`);
265-
263+
const cell = getCalendarCell(dp, nextMonth);
266264
expect(cell.html()).toBeTruthy();
267265
dp.unmount();
268266
});
@@ -297,7 +295,7 @@ describe('It should validate various picker scenarios', () => {
297295

298296
const selectRange = async () => {
299297
await clickCalendarDate(dp, today);
300-
await dp.find(`[data-test-id="${secondDate}"]`).trigger('click');
298+
await getCalendarCell(dp, secondDate).trigger('click');
301299
};
302300

303301
await selectRange();
@@ -401,7 +399,7 @@ describe('It should validate various picker scenarios', () => {
401399

402400
const dp = await openMenu({ highlight });
403401

404-
const calendarCell = dp.find(`[data-test-id="${resetDateTime(start)}"]`).find('.dp__cell_inner');
402+
const calendarCell = getCalendarCell(dp, start).find('.dp__cell_inner');
405403

406404
expect(calendarCell.classes()).toContain('dp__cell_highlight');
407405

tests/unit/components/Calendar.spec.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import DatepickerMenu from '@/components/DatepickerMenu.vue';
66
import DatePicker from '@/components/DatePicker/DatePicker.vue';
77

88
import { resetDateTime } from '@/utils/date-utils';
9+
import { getCalendarCell } from '../../utils.ts';
910
import type { ComponentPublicInstance } from 'vue';
1011

1112
const mountCalendar = async () => {
@@ -46,7 +47,7 @@ describe('Calendar component', () => {
4647
await menu.vm.$nextTick();
4748
const calendar = menu.findComponent(DpCalendar);
4849

49-
await calendar.find(`[data-test-id="${resetDateTime(new Date())}"]`).trigger('mouseenter');
50+
await getCalendarCell(calendar, new Date()).trigger('mouseenter');
5051
await calendar.vm.$nextTick();
5152

5253
expect(calendar.html()).toContain('dp__marker_tooltip');
@@ -55,7 +56,7 @@ describe('Calendar component', () => {
5556
it('Should emit hover date on mouse over', async () => {
5657
const { calendar, date } = await mountCalendar();
5758

58-
await calendar.find(`[data-test-id="${resetDateTime(date)}"]`).trigger('mouseenter');
59+
await getCalendarCell(calendar, date).trigger('mouseenter');
5960
await calendar.vm.$nextTick();
6061

6162
expect(calendar.emitted()).toHaveProperty('set-hover-date');
@@ -65,7 +66,7 @@ describe('Calendar component', () => {
6566
it('Should emit date when calendar day is clicked', async () => {
6667
const { calendar, date } = await mountCalendar();
6768

68-
await calendar.find(`[data-test-id="${resetDateTime(date)}"]`).trigger('click');
69+
await getCalendarCell(calendar, date).trigger('click');
6970
await calendar.vm.$nextTick();
7071

7172
expect(calendar.emitted()).toHaveProperty('select-date');

tests/utils.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { flushPromises, mount, VueWrapper } from '@vue/test-utils';
2+
import { format } from 'date-fns';
23

34
import VueDatePicker from '@/VueDatePicker.vue';
45

56
import type { AllPropsType } from '@/props';
6-
import { resetDateTime } from '@/utils/date-utils';
77

88
export const openMenu = async (
99
props: Partial<AllPropsType>,
@@ -45,9 +45,9 @@ export const clickSelectBtn = async (dp: VueWrapper<any>) => {
4545
};
4646

4747
export const padZero = (val: number) => (val < 10 ? `0${val}` : val);
48-
48+
// `dp-${format(date, 'yyyy-MM-dd')}`
4949
export const getCalendarCell = (dp: VueWrapper<any>, date: Date) => {
50-
return dp.find(`[data-test-id="${resetDateTime(date)}"]`);
50+
return dp.find(`[data-test-id="dp-${format(date, 'yyyy-MM-dd')}"]`);
5151
};
5252

5353
export const getCellClasses = (dp: VueWrapper<any>, date: Date) => {

0 commit comments

Comments
 (0)