All files / src/components/utils/validation relativeRangeDatePicker.ts

68.42% Statements 13/19
76.92% Branches 20/26
33.33% Functions 1/3
68.42% Lines 13/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 683x                                   20x 6x     14x 20x   20x                   20x         14x                   20x 4x 4x     14x         14x              
import {dateTimeParse} from '@gravity-ui/date-utils';
import type {DateTime} from '@gravity-ui/date-utils';
 
import type {RelativeRangeDatePickerValue} from '../../RelativeRangeDatePicker';
import type {ExtractFunctionType} from '../../types';
 
import {getValidationResult} from './datePicker';
import {i18n} from './i18n';
 
export function getRangeValidationResult(
    value: RelativeRangeDatePickerValue | null,
    allowNullableValues: boolean | undefined,
    minValue: DateTime | undefined,
    maxValue: DateTime | undefined,
    isDateUnavailable: ((v: DateTime, endpoint: 'start' | 'end') => boolean) | undefined,
    timeZone: string,
    t: ExtractFunctionType<typeof i18n> = i18n,
) {
    if (!value) {
        return {isInvalid: false, errors: []};
    }
 
    const startDate = value.start ? dateTimeParse(value.start.value, {timeZone}) : null;
    const endDate = value.end ? dateTimeParse(value.end.value, {timeZone, roundUp: true}) : null;
 
    const startValidationResult = getValidationResult(
        startDate,
        minValue,
        maxValue,
        isDateUnavailable ? (date) => isDateUnavailable(date, 'start') : undefined,
        timeZone,
        t('"From"'),
        t,
    );
 
    Iif (!startDate && !allowNullableValues) {
        startValidationResult.isInvalid = true;
        startValidationResult.errors.push(t('"From" is required.'));
    }
 
    const endValidationResult = getValidationResult(
        endDate,
        minValue,
        maxValue,
        isDateUnavailable ? (date) => isDateUnavailable(date, 'end') : undefined,
        timeZone,
        t('"To"'),
        t,
    );
 
    if (!endDate && !allowNullableValues) {
        endValidationResult.isInvalid = true;
        endValidationResult.errors.push(t('"To" is required.'));
    }
 
    Iif (startDate && endDate && endDate.isBefore(startDate)) {
        startValidationResult.isInvalid = true;
        startValidationResult.errors.push(t(`"From" can't be after "To".`));
    }
 
    return {
        isInvalid: startValidationResult.isInvalid || endValidationResult.isInvalid,
        startValidationResult,
        endValidationResult,
        errors: startValidationResult.errors.concat(endValidationResult.errors),
    };
}