Skip to content

Commit

Permalink
fix(components/forms): ensure form error always has errorName attri…
Browse files Browse the repository at this point in the history
…bute for test harnesses (#3071)
  • Loading branch information
Blackbaud-CoreyArcher authored Jan 28, 2025
1 parent 252b0b7 commit 46c1e63
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
ChangeDetectionStrategy,
Component,
HostBinding,
Input,
inject,
} from '@angular/core';
Expand Down Expand Up @@ -49,6 +50,10 @@ export class SkyFormErrorComponent {
@Input({ required: true })
public errorText!: string;

@HostBinding('attr.errorName') public get hostErrorName(): string {
return this.errorName;
}

protected readonly formErrors = inject(SKY_FORM_ERRORS_ENABLED, {
optional: true,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ import { SkyFormErrorsHarness } from './form-errors-harness';
[dirty]="true"
[labelText]="errorText"
[errors]="errors"
/>
>
@if (customErrorName) {
<sky-form-error
[errorName]="customErrorName"
errorText="Custom error"
/>
}
</sky-form-errors>
<sky-form-errors
data-sky-id="other-error"
labelText="other error"
Expand All @@ -37,6 +44,7 @@ import { SkyFormErrorsHarness } from './form-errors-harness';
class TestComponent {
public errorText: string | undefined = 'Form';
public errors: ValidationErrors | undefined;
public customErrorName: string | undefined;
}
//#endregion Test component

Expand Down Expand Up @@ -112,4 +120,19 @@ describe('Form errors harness', () => {
{ errorName: 'maxlength' },
]);
});

it('should return custom errors', async () => {
const { formErrorsHarness, fixture } = await setupTest();

fixture.componentInstance.customErrorName = 'custom';
fixture.detectChanges();

await expectAsync(formErrorsHarness.hasError('custom')).toBeResolvedTo(
true,
);

await expectAsync(formErrorsHarness.getFormErrors()).toBeResolvedTo([
{ errorName: 'custom' },
]);
});
});

0 comments on commit 46c1e63

Please sign in to comment.