Skip to content

Commit 81b06e7

Browse files
Add ValidationMessageComponent tests
1 parent 614d4d9 commit 81b06e7

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import { TestBed, ComponentFixture } from '@angular/core/testing';
2+
import { ValidationMessageComponent } from './validation-message.component';
3+
import { ValidationError } from '../validation-error';
4+
import { Component, ViewChild } from '@angular/core';
5+
6+
describe('ValidationMessageComponent', () => {
7+
function setupCanHandleTests(): { control: any, component: ValidationMessageComponent, error: ValidationError } {
8+
const control: any = {
9+
errors: {
10+
required: true
11+
}
12+
};
13+
14+
return {
15+
control: control,
16+
component: new ValidationMessageComponent(undefined),
17+
error: ValidationError.fromFirstError(control)
18+
};
19+
}
20+
21+
it(`canHandle returns true when the error key and component key are equal (without for)`, () => {
22+
const { control, component, error } = setupCanHandleTests();
23+
component.key = 'required';
24+
25+
const result = component.canHandle(error);
26+
27+
expect(result).toBe(true);
28+
});
29+
30+
it(`canHandle returns true when the error key and component key are equal (with for)`, () => {
31+
const { control, component, error } = setupCanHandleTests();
32+
component.for = control;
33+
component.key = 'required';
34+
35+
const result = component.canHandle(error);
36+
37+
expect(result).toBe(true);
38+
});
39+
40+
it(`canHandle returns false when the component 'for' doesn't equal the error's control`, () => {
41+
const { control, component, error } = setupCanHandleTests();
42+
component.for = <any>{};
43+
component.key = 'required';
44+
45+
const result = component.canHandle(error);
46+
47+
expect(result).toBe(false);
48+
});
49+
50+
it(`canHandle returns false when the error key doesn't equal the component key`, () => {
51+
const { control, component, error } = setupCanHandleTests();
52+
component.key = 'minlength';
53+
54+
const result = component.canHandle(error);
55+
56+
expect(result).toBe(false);
57+
});
58+
59+
function setupErrorMessageTests(): {
60+
fixture: ComponentFixture<TestHostComponent>,
61+
validationMessageComponent: ValidationMessageComponent
62+
} {
63+
TestBed.configureTestingModule({
64+
declarations: [ValidationMessageComponent, TestHostComponent]
65+
});
66+
67+
const fixture: ComponentFixture<TestHostComponent> = TestBed.createComponent(TestHostComponent);
68+
return {
69+
fixture: fixture,
70+
validationMessageComponent: fixture.componentInstance.validationMessageComponent
71+
};
72+
}
73+
74+
it(`show displays the error message`, () => {
75+
const { fixture, validationMessageComponent } = setupErrorMessageTests();
76+
const error = ValidationError.fromFirstError(TestHostComponent.getFormControl());
77+
78+
validationMessageComponent.show(error);
79+
80+
expect(fixture.nativeElement.querySelector('.message')).toBeFalsy();
81+
fixture.detectChanges();
82+
expect(fixture.nativeElement.querySelector('.message')).not.toBeFalsy();
83+
expect(fixture.nativeElement.querySelector('.message').textContent)
84+
.toBe(`The message is shown. requiredLength: ${error.errorObject.requiredLength}`);
85+
});
86+
87+
it(`reset hides the error message`, () => {
88+
const { fixture, validationMessageComponent } = setupErrorMessageTests();
89+
const error = ValidationError.fromFirstError(TestHostComponent.getFormControl());
90+
91+
validationMessageComponent.show(error);
92+
validationMessageComponent.reset();
93+
fixture.detectChanges();
94+
expect(fixture.nativeElement.querySelector('.message')).toBeFalsy();
95+
});
96+
97+
it(`show sets the context to the error object`, () => {
98+
const { fixture, validationMessageComponent } = setupErrorMessageTests();
99+
const error = ValidationError.fromFirstError(TestHostComponent.getFormControl());
100+
101+
validationMessageComponent.show(error);
102+
expect(validationMessageComponent.context).toEqual(error.errorObject);
103+
});
104+
});
105+
106+
@Component({
107+
template: `
108+
<arv-validation-message #minlengthValidation key="minlength">
109+
<p class="message">The message is shown. requiredLength: {{minlengthValidation.context?.requiredLength}}</p>
110+
</arv-validation-message>`
111+
})
112+
class TestHostComponent {
113+
@ViewChild(ValidationMessageComponent) validationMessageComponent: ValidationMessageComponent;
114+
115+
static getFormControl(): any {
116+
return {
117+
errors: {
118+
minlength: { requiredLength: 10, actualLength: 5 }
119+
}
120+
};
121+
}
122+
}

0 commit comments

Comments
 (0)