Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions core/src/components/datetime/test/data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getToday,
getCombinedDateColumnData,
getTimeColumnsData,
getMonthColumnData,
} from '../utils/data';

// The minutes are the same across all hour cycles, so we don't check those
Expand Down Expand Up @@ -516,3 +517,33 @@ describe('getCombinedDateColumnData', () => {
]);
});
});

describe('getMonthColumnData()', () => {
const monthValues = [1, 3, 5, 7, 9, 11];
const getValues = (refParts: DatetimeParts, minParts?: DatetimeParts, maxParts?: DatetimeParts) =>
getMonthColumnData('en-US', refParts, minParts, maxParts, monthValues).map((option) => option.value);

it('should apply the min month only in the min year when monthValues is set', () => {
const minParts = { month: 6, day: 1, year: 2020 };

expect(getValues({ month: 5, day: 1, year: 2020 }, minParts)).toEqual([7, 9, 11]);
expect(getValues({ month: 3, day: 1, year: 2024 }, minParts)).toEqual([1, 3, 5, 7, 9, 11]);
});

it('should apply the max month only in the max year when monthValues is set', () => {
const maxParts = { month: 4, day: 30, year: 2025 };

expect(getValues({ month: 3, day: 1, year: 2025 }, undefined, maxParts)).toEqual([1, 3]);
expect(getValues({ month: 9, day: 1, year: 2024 }, undefined, maxParts)).toEqual([1, 3, 5, 7, 9, 11]);
});

it('should match the months shown when monthValues is not set', () => {
const minParts = { month: 6, day: 1, year: 2020 };
const maxParts = { month: 4, day: 30, year: 2025 };
const refParts = { month: 3, day: 1, year: 2024 };

const allMonths = getMonthColumnData('en-US', refParts, minParts, maxParts).map((option) => option.value);
expect(allMonths).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
expect(getValues(refParts, minParts, maxParts)).toEqual([1, 3, 5, 7, 9, 11]);
});
});
9 changes: 7 additions & 2 deletions core/src/components/datetime/utils/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,10 +359,15 @@ export const getMonthColumnData = (

if (monthValues !== undefined) {
let processedMonths = monthValues;
if (maxParts?.month !== undefined) {
/**
* The max and min months only apply to the
* max and min years. For example, with a max of
* 2025-03, every month of 2024 is still allowed.
*/
if (maxParts?.month !== undefined && maxParts.year === year) {
processedMonths = processedMonths.filter((month) => month <= maxParts.month!);
}
if (minParts?.month !== undefined) {
if (minParts?.month !== undefined && minParts.year === year) {
processedMonths = processedMonths.filter((month) => month >= minParts.month!);
}

Expand Down
Loading