Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Fix validation to fire for user input #34

Merged
merged 3 commits into from
Apr 25, 2019
Merged
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"dependencies": {},
"devDependencies": {
"@blackbaud/skyux": "2.46.1",
"@blackbaud/skyux": "2.48.1",
"@blackbaud/skyux-builder": "1.33.1",
"@skyux-sdk/builder-plugin-skyux": "1.0.0"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,52 @@ describe('Date range picker', function () {
});
}));

it('should error when end date comes before start date', fakeAsync(function () {
detectChanges();

const control = component.dateRange;
const calculatorIdControl = component.dateRangePicker.formGroup.get('calculatorId');

control.setValue({
calculatorId: SkyDateRangeCalculatorId.SpecificRange
});

detectChanges();

expect(control.errors).toBeFalsy();
expect(calculatorIdControl.errors).toBeFalsy();

const datepickerInputs = fixture.nativeElement.querySelectorAll('.sky-datepicker input');

datepickerInputs.item(0).value = '1/2/2000';
datepickerInputs.item(1).value = '1/1/2000';

SkyAppTestUtility.fireDomEvent(datepickerInputs.item(0), 'change');
SkyAppTestUtility.fireDomEvent(datepickerInputs.item(1), 'change');

detectChanges();

const expectedError = {
skyDateRange: {
calculatorId: SkyDateRangeCalculatorId.SpecificRange,
errors: {
endDateBeforeStartDate: true
}
}
};

expect(control.errors).toEqual(expectedError);
expect(calculatorIdControl.errors).toEqual(expectedError);

datepickerInputs.item(1).value = '1/3/2000';
SkyAppTestUtility.fireDomEvent(datepickerInputs.item(1), 'change');

detectChanges();

expect(control.errors).toBeFalsy();
expect(calculatorIdControl.errors).toBeFalsy();
}));

it('should be accessible', async(function () {
fixture.detectChanges();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export class SkyDateRangePickerComponent
this.updateCalculators().then(() => {
this.isReady = true;

this.resetFormState();
this.resetFormGroupValue();
this.showRelevantFormFields();

// Fill in any unprovided values after the calculators have been initialized.
Expand Down Expand Up @@ -275,7 +275,7 @@ export class SkyDateRangePickerComponent
if (!found) {
const newValue = this.defaultCalculator.getValue();
this.setValue(newValue);
this.resetFormState(newValue);
this.resetFormGroupValue(newValue);
this.showRelevantFormFields();
}
});
Expand Down Expand Up @@ -307,7 +307,7 @@ export class SkyDateRangePickerComponent
this.onChange(this.defaultValue);
}

this.resetFormState();
this.resetFormGroupValue();
this.showRelevantFormFields();
}
}
Expand Down Expand Up @@ -339,6 +339,9 @@ export class SkyDateRangePickerComponent
}

if (!errors) {
// Clear any errors on the calculator select.
// tslint:disable-next-line:no-null-keyword
idControl.setErrors(null);
return;
}

Expand Down Expand Up @@ -431,19 +434,7 @@ export class SkyDateRangePickerComponent
this.changeDetector.markForCheck();
}

private resetFormState(value?: SkyDateRangeCalculation): void {

// Clear any errors first.
/* tslint:disable:no-null-keyword */
this.control.setErrors(null, {
emitEvent: false
});

this.formGroup.setErrors(null, {
emitEvent: false
});
/* tslint:enable */

private resetFormGroupValue(value?: SkyDateRangeCalculation): void {
// Do not emit a value change event on the underlying form group
// because we're already watching for changes that are triggered by the end user.
// For example, if we change the value of the form group internally, we don't want the event
Expand Down Expand Up @@ -484,7 +475,7 @@ export class SkyDateRangePickerComponent
const newValue = calculator.getValue();

this.setValue(newValue);
this.resetFormState(newValue);
this.resetFormGroupValue(newValue);
this.showRelevantFormFields();
});

Expand All @@ -494,7 +485,6 @@ export class SkyDateRangePickerComponent
.takeUntil(this.ngUnsubscribe)
.subscribe((startDate) => {
this.patchValue({ startDate });
this.resetFormState();
});

// Watch for end date value changes.
Expand All @@ -503,7 +493,6 @@ export class SkyDateRangePickerComponent
.takeUntil(this.ngUnsubscribe)
.subscribe((endDate) => {
this.patchValue({ endDate });
this.resetFormState();
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const enum SkyDateRangeCalculatorId {
export enum SkyDateRangeCalculatorId {
AnyTime,
Before,
After,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const enum SkyDateRangeCalculatorType {
export enum SkyDateRangeCalculatorType {
Copy link
Member Author

@Blackbaud-SteveBrush Blackbaud-SteveBrush Apr 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding const was causing this enum to return an empty value. I'm guessing it has something to do with our version of TypeScript. At any rate, removing the const fixes the problem.

After,
Before,
Range,
Expand Down