Skip to content

Commit

Permalink
backward-compatible test-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-kot committed Jan 14, 2025
1 parent b053bea commit e1dc31c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,15 @@ exports[`test-utils selectors 1`] = `
],
"date-range-picker": [
"awsui_apply-button_mgja0",
"awsui_calendar-date_1afkv",
"awsui_calendar-header_mgja0",
"awsui_calendar-next-month-btn_mgja0",
"awsui_calendar-prev-month-btn_mgja0",
"awsui_calendar-week_1afkv",
"awsui_cancel-button_mgja0",
"awsui_clear-button_mgja0",
"awsui_custom-range-duration-input_16zmw",
"awsui_custom-range-unit-select_16zmw",
"awsui_day_1mfbn",
"awsui_disabled-reason-tooltip_1mfbn",
"awsui_dropdown_mgja0",
"awsui_end-date-input_mgja0",
Expand All @@ -235,7 +236,6 @@ exports[`test-utils selectors 1`] = `
"awsui_start-date_1mfbn",
"awsui_start-time-input_mgja0",
"awsui_validation-error_mgja0",
"awsui_week_1mfbn",
],
"drawer": [
"awsui_drawer_1sxt8",
Expand Down
27 changes: 23 additions & 4 deletions src/date-range-picker/calendar/grids/monthly-grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { getCalendarMonthWithSixRows } from '../../../internal/utils/date-time/c
import { DateRangePickerProps, DayIndex } from '../../interfaces';
import { GridCell } from './grid-cell';

import testStyles from '../../test-classes/styles.css.js';
import styles from './styles.css.js';

/**
Expand Down Expand Up @@ -101,8 +102,20 @@ export function MonthlyGrid({
// eslint-disable-next-line react-hooks/exhaustive-deps
[baseDateTime, startOfWeek, dateType]
);
// Week indices are only added for weeks with at least some days belonging to the base date month.
const weeksWithIndices = weeks.reduce(
(acc, week, index) => {
const isWeekBelongsToMonth = week.some(date => isSameMonth(date, baseDate));
if (isWeekBelongsToMonth) {
acc.push({ week, testIndex: (acc[index - 1]?.testIndex ?? 0) + 1 });
} else {
acc.push({ week });
}
return acc;
},
[] as Array<{ week: Date[]; testIndex?: number }>
);
const weekdays = weeks[0].map(date => date.getDay());

return (
<table role="grid" aria-labelledby={ariaLabelledby} className={clsx(styles.grid, className)}>
<thead>
Expand All @@ -116,9 +129,14 @@ export function MonthlyGrid({
</tr>
</thead>
<tbody onKeyDown={onGridKeyDownHandler}>
{weeks.map((week, weekIndex) => {
{weeksWithIndices.map(({ week, testIndex }, weekIndex) => {
const isWeekBelongsToMonth = week.some(date => isSameMonth(date, baseDate));
return (
<tr key={weekIndex} className={styles.week}>
<tr
key={weekIndex}
className={clsx(styles.week, isWeekBelongsToMonth && testStyles['calendar-week'])}
data-awsui-weekindex={testIndex}
>
{week.map((date, dateIndex) => {
const isStartDate = !!selectedStartDate && isSameDay(date, selectedStartDate);
const isEndDate = !!selectedEndDate && isSameDay(date, selectedEndDate);
Expand Down Expand Up @@ -146,6 +164,7 @@ export function MonthlyGrid({

const baseClasses = {
[styles.day]: true,
[testStyles['calendar-date']]: isDateBelongsToMonth,
[styles['grid-cell']]: true,
[styles['in-first-row']]: weekIndex === 0,
[styles['in-first-column']]: dateIndex === 0,
Expand Down Expand Up @@ -196,7 +215,7 @@ export function MonthlyGrid({
ref={isFocused ? focusedDateRef : undefined}
key={`${weekIndex}:${dateIndex}`}
className={clsx(baseClasses, {
[styles['in-current-month']]: isSameMonth(date, baseDate),
[styles['in-current-month']]: true,
[styles.enabled]: isEnabled,
[styles.selected]: isSelected && isDateBelongsToMonth,
[styles['start-date']]: isStartDate,
Expand Down
2 changes: 1 addition & 1 deletion src/date-range-picker/calendar/grids/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
}

.in-first-column {
border-inline-start: 1px solid transparent;
border-inline-start: 1px solid calendar.$grid-border;

&.in-current-month {
border-inline-start: calendar.$grid-border;
Expand Down
9 changes: 9 additions & 0 deletions src/date-range-picker/test-classes/styles.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

.calendar-week,
.calendar-date {
/* used in test-utils */
}
3 changes: 2 additions & 1 deletion src/test-utils/dom/date-range-picker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import SelectWrapper from '../select';
import gridStyles from '../../../date-range-picker/calendar/grids/styles.selectors.js';
import relativeRangeStyles from '../../../date-range-picker/relative-range/styles.selectors.js';
import styles from '../../../date-range-picker/styles.selectors.js';
import testStyles from '../../../date-range-picker/test-classes/styles.selectors.js';

export class CalendarDateWrapper extends ComponentWrapper {
findDisabledReason(): ElementWrapper | null {
Expand Down Expand Up @@ -122,7 +123,7 @@ export class DrpDropdownWrapper extends ComponentWrapper {
): CalendarDateWrapper {
const gridClassName = grid === 'right' ? styles['second-grid'] : styles['first-grid'];
return this.findComponent(
`.${gridClassName} .${gridStyles.week}:nth-child(${row}) .${gridStyles.day}:nth-child(${column})`,
`.${gridClassName} .${testStyles['calendar-week']}[data-awsui-weekindex="${row}"] .${testStyles['calendar-date']}:nth-child(${column})`,
CalendarDateWrapper
)!;
}
Expand Down

0 comments on commit e1dc31c

Please sign in to comment.