-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! feat(components): gs-date-range-selector: add attributes `init…
…ialDateFrom`, `initialDateTo` #245
- Loading branch information
1 parent
12b8302
commit 5528222
Showing
7 changed files
with
269 additions
and
158 deletions.
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
components/src/preact/dateRangeSelector/computeInitialValues.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { computeInitialValues } from './computeInitialValues'; | ||
import { PRESET_VALUE_CUSTOM, PRESET_VALUE_LAST_3_MONTHS, PRESET_VALUE_LAST_6_MONTHS } from './selectableOptions'; | ||
|
||
const today = new Date(); | ||
const earliestDate = '1900-01-01'; | ||
|
||
describe('computeInitialValues', () => { | ||
it('should compute for initial value if initial "from" and "to" are unset', () => { | ||
const result = computeInitialValues(PRESET_VALUE_LAST_3_MONTHS, undefined, undefined, earliestDate, []); | ||
|
||
const expectedFrom = new Date(); | ||
expectedFrom.setMonth(today.getMonth() - 3); | ||
|
||
expect(result.initialSelectedDateRange).toEqual(PRESET_VALUE_LAST_3_MONTHS); | ||
expectDateMatches(result.initialSelectedDateFrom, expectedFrom); | ||
expectDateMatches(result.initialSelectedDateTo, today); | ||
}); | ||
|
||
it('should fall back to default when initial value is unknown', () => { | ||
const result = computeInitialValues('not a known value', undefined, undefined, earliestDate, []); | ||
|
||
const expectedFrom = new Date(); | ||
expectedFrom.setMonth(today.getMonth() - 6); | ||
|
||
expect(result.initialSelectedDateRange).toEqual(PRESET_VALUE_LAST_6_MONTHS); | ||
expectDateMatches(result.initialSelectedDateFrom, expectedFrom); | ||
expectDateMatches(result.initialSelectedDateTo, today); | ||
}); | ||
|
||
it('should overwrite initial value if initial "from" is set', () => { | ||
const initialDateFrom = '2020-01-01'; | ||
const result = computeInitialValues(PRESET_VALUE_LAST_3_MONTHS, initialDateFrom, undefined, earliestDate, []); | ||
|
||
expect(result.initialSelectedDateRange).toEqual(PRESET_VALUE_CUSTOM); | ||
expectDateMatches(result.initialSelectedDateFrom, new Date(initialDateFrom)); | ||
expectDateMatches(result.initialSelectedDateTo, today); | ||
}); | ||
|
||
it('should overwrite initial value if initial "to" is set', () => { | ||
const initialDateTo = '2020-01-01'; | ||
const result = computeInitialValues(PRESET_VALUE_LAST_3_MONTHS, undefined, initialDateTo, earliestDate, []); | ||
|
||
expect(result.initialSelectedDateRange).toEqual(PRESET_VALUE_CUSTOM); | ||
expectDateMatches(result.initialSelectedDateFrom, new Date(earliestDate)); | ||
expectDateMatches(result.initialSelectedDateTo, new Date(initialDateTo)); | ||
}); | ||
|
||
it('should overwrite initial value if initial "to" and "from" are set', () => { | ||
const initialDateFrom = '2020-01-01'; | ||
const initialDateTo = '2022-01-01'; | ||
const result = computeInitialValues( | ||
PRESET_VALUE_LAST_3_MONTHS, | ||
initialDateFrom, | ||
initialDateTo, | ||
earliestDate, | ||
[], | ||
); | ||
|
||
expect(result.initialSelectedDateRange).toEqual(PRESET_VALUE_CUSTOM); | ||
expectDateMatches(result.initialSelectedDateFrom, new Date(initialDateFrom)); | ||
expectDateMatches(result.initialSelectedDateTo, new Date(initialDateTo)); | ||
}); | ||
|
||
it('should set initial "to" to "from" if "from" is after "to"', () => { | ||
const initialDateFrom = '2020-01-01'; | ||
const initialDateTo = '1900-01-01'; | ||
const result = computeInitialValues( | ||
PRESET_VALUE_LAST_3_MONTHS, | ||
initialDateFrom, | ||
initialDateTo, | ||
earliestDate, | ||
[], | ||
); | ||
|
||
expect(result.initialSelectedDateRange).toEqual(PRESET_VALUE_CUSTOM); | ||
expectDateMatches(result.initialSelectedDateFrom, new Date(initialDateFrom)); | ||
expectDateMatches(result.initialSelectedDateTo, new Date(initialDateFrom)); | ||
}); | ||
|
||
it('should throw if initial "from" is not a valid date', () => { | ||
expect(() => | ||
computeInitialValues(PRESET_VALUE_LAST_3_MONTHS, 'not a date', undefined, earliestDate, []), | ||
).toThrowError('Invalid initialDateFrom'); | ||
}); | ||
|
||
it('should throw if initial "to" is not a valid date', () => { | ||
expect(() => | ||
computeInitialValues(PRESET_VALUE_LAST_3_MONTHS, undefined, 'not a date', earliestDate, []), | ||
).toThrowError('Invalid initialDateTo'); | ||
}); | ||
|
||
function expectDateMatches(actual: Date, expected: Date) { | ||
expect(actual.getFullYear()).toEqual(expected.getFullYear()); | ||
expect(actual.getMonth()).toEqual(expected.getMonth()); | ||
expect(actual.getDate()).toEqual(expected.getDate()); | ||
} | ||
}); |
73 changes: 73 additions & 0 deletions
73
components/src/preact/dateRangeSelector/computeInitialValues.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { | ||
type CustomSelectOption, | ||
getDatesForSelectorValue, | ||
getSelectableOptions, | ||
PRESET_VALUE_CUSTOM, | ||
PRESET_VALUE_LAST_6_MONTHS, | ||
type PresetOptionValues, | ||
} from './selectableOptions'; | ||
import { UserFacingError } from '../components/error-display'; | ||
|
||
export function computeInitialValues<CustomLabel extends string>( | ||
initialValue: PresetOptionValues | CustomLabel | undefined, | ||
initialDateFrom: string | undefined, | ||
initialDateTo: string | undefined, | ||
earliestDate: string, | ||
customSelectOptions: CustomSelectOption<CustomLabel>[], | ||
): { | ||
initialSelectedDateRange: CustomLabel | PresetOptionValues; | ||
initialSelectedDateFrom: Date; | ||
initialSelectedDateTo: Date; | ||
} { | ||
if (isUndefinedOrEmpty(initialDateFrom) && isUndefinedOrEmpty(initialDateTo)) { | ||
const selectableOptions = getSelectableOptions(customSelectOptions); | ||
const initialSelectedDateRange = | ||
initialValue !== undefined && selectableOptions.some((option) => option.value === initialValue) | ||
? initialValue | ||
: PRESET_VALUE_LAST_6_MONTHS; | ||
|
||
const { dateFrom, dateTo } = getDatesForSelectorValue( | ||
initialSelectedDateRange, | ||
customSelectOptions, | ||
earliestDate, | ||
); | ||
|
||
return { | ||
initialSelectedDateRange, | ||
initialSelectedDateFrom: dateFrom, | ||
initialSelectedDateTo: dateTo, | ||
}; | ||
} | ||
|
||
const initialSelectedDateFrom = isUndefinedOrEmpty(initialDateFrom) | ||
? new Date(earliestDate) | ||
: new Date(initialDateFrom); | ||
let initialSelectedDateTo = isUndefinedOrEmpty(initialDateTo) ? new Date() : new Date(initialDateTo); | ||
|
||
if (isNaN(initialSelectedDateFrom.getTime())) { | ||
throw new UserFacingError( | ||
'Invalid initialDateFrom', | ||
`Invalid initialDateFrom "${initialDateFrom}", It must be of the format YYYY-MM-DD`, | ||
); | ||
} | ||
if (isNaN(initialSelectedDateTo.getTime())) { | ||
throw new UserFacingError( | ||
'Invalid initialDateTo', | ||
`Invalid initialDateTo "${initialDateTo}", It must be of the format YYYY-MM-DD`, | ||
); | ||
} | ||
|
||
if (initialSelectedDateFrom > initialSelectedDateTo) { | ||
initialSelectedDateTo = initialSelectedDateFrom; | ||
} | ||
|
||
return { | ||
initialSelectedDateRange: PRESET_VALUE_CUSTOM, | ||
initialSelectedDateFrom, | ||
initialSelectedDateTo, | ||
}; | ||
} | ||
|
||
function isUndefinedOrEmpty(value: string | undefined): value is undefined | '' { | ||
return value === undefined || value === ''; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.