@@ -4,119 +4,113 @@ import { ValidationError } from '../validation-error';
44import { Component , ViewChild } from '@angular/core' ;
55
66describe ( '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- }
7+ describe ( 'canHandle' , ( ) => {
8+ let control : any ;
9+ let component : ValidationMessageComponent ;
10+ let error : ValidationError ;
11+
12+ beforeEach ( ( ) => {
13+ control = {
14+ errors : {
15+ required : true
16+ }
17+ } ;
18+ component = new ValidationMessageComponent ( undefined ) ;
19+ error = ValidationError . fromFirstError ( control ) ;
20+ } ) ;
2021
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' ;
22+ it ( `returns true when the error key and component key are equal (without for)` , ( ) => {
23+ component . key = 'required' ;
2424
25- const result = component . canHandle ( error ) ;
25+ const result = component . canHandle ( error ) ;
2626
27- expect ( result ) . toEqual ( true ) ;
28- } ) ;
27+ expect ( result ) . toEqual ( true ) ;
28+ } ) ;
2929
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' ;
30+ it ( `returns true when the error key and component key are equal (with for)` , ( ) => {
31+ component . for = control ;
32+ component . key = 'required' ;
3433
35- const result = component . canHandle ( error ) ;
34+ const result = component . canHandle ( error ) ;
3635
37- expect ( result ) . toEqual ( true ) ;
38- } ) ;
36+ expect ( result ) . toEqual ( true ) ;
37+ } ) ;
3938
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' ;
39+ it ( `returns false when the component 'for' doesn't equal the error's control` , ( ) => {
40+ component . for = < any > { } ;
41+ component . key = 'required' ;
4442
45- const result = component . canHandle ( error ) ;
43+ const result = component . canHandle ( error ) ;
4644
47- expect ( result ) . toEqual ( false ) ;
48- } ) ;
45+ expect ( result ) . toEqual ( false ) ;
46+ } ) ;
4947
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' ;
48+ it ( `returns false when the error key doesn't equal the component key` , ( ) => {
49+ component . key = 'minlength' ;
5350
54- const result = component . canHandle ( error ) ;
51+ const result = component . canHandle ( error ) ;
5552
56- expect ( result ) . toEqual ( false ) ;
53+ expect ( result ) . toEqual ( false ) ;
54+ } ) ;
5755 } ) ;
5856
59- function setupErrorMessageTests ( ) : {
60- fixture : ComponentFixture < TestHostComponent > ,
61- validationMessageComponent : ValidationMessageComponent
62- } {
63- TestBed . configureTestingModule ( {
64- declarations : [ ValidationMessageComponent , TestHostComponent ]
57+ describe ( 'error messages' , ( ) => {
58+ let fixture : ComponentFixture < TestHostComponent > ;
59+ let validationMessageComponent : ValidationMessageComponent ;
60+ beforeEach ( ( ) => {
61+ TestBed . configureTestingModule ( {
62+ declarations : [ ValidationMessageComponent , TestHostComponent ]
63+ } ) ;
64+
65+ fixture = TestBed . createComponent ( TestHostComponent ) ;
66+ validationMessageComponent = fixture . componentInstance . validationMessageComponent ;
6567 } ) ;
6668
67- const fixture : ComponentFixture < TestHostComponent > = TestBed . createComponent ( TestHostComponent ) ;
68- return {
69- fixture : fixture ,
70- validationMessageComponent : fixture . componentInstance . validationMessageComponent
71- } ;
72- }
69+ it ( `are displayed by the show function` , ( ) => {
70+ const error = ValidationError . fromFirstError ( TestHostComponent . getFormControl ( ) ) ;
7371
74- it ( `show displays the error message` , ( ) => {
75- const { fixture, validationMessageComponent } = setupErrorMessageTests ( ) ;
76- const error = ValidationError . fromFirstError ( TestHostComponent . getFormControl ( ) ) ;
72+ validationMessageComponent . show ( error ) ;
7773
78- validationMessageComponent . show ( error ) ;
74+ expect ( fixture . nativeElement . querySelector ( '.message' ) ) . toBeFalsy ( ) ;
75+ fixture . detectChanges ( ) ;
76+ expect ( fixture . nativeElement . querySelector ( '.message' ) ) . not . toBeFalsy ( ) ;
77+ expect ( fixture . nativeElement . querySelector ( '.message' ) . textContent )
78+ . toEqual ( `The message is shown. requiredLength: ${ error . errorObject . requiredLength } ` ) ;
79+ } ) ;
7980
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- . toEqual ( `The message is shown. requiredLength: ${ error . errorObject . requiredLength } ` ) ;
85- } ) ;
81+ it ( `are hidden by the reset function` , ( ) => {
82+ const error = ValidationError . fromFirstError ( TestHostComponent . getFormControl ( ) ) ;
8683
87- it ( `reset hides the error message` , ( ) => {
88- const { fixture, validationMessageComponent } = setupErrorMessageTests ( ) ;
89- const error = ValidationError . fromFirstError ( TestHostComponent . getFormControl ( ) ) ;
84+ validationMessageComponent . show ( error ) ;
85+ fixture . detectChanges ( ) ;
86+ validationMessageComponent . reset ( ) ;
87+ fixture . detectChanges ( ) ;
88+ expect ( fixture . nativeElement . querySelector ( '.message' ) ) . toBeFalsy ( ) ;
89+ } ) ;
9090
91- validationMessageComponent . show ( error ) ;
92- validationMessageComponent . reset ( ) ;
93- fixture . detectChanges ( ) ;
94- expect ( fixture . nativeElement . querySelector ( '.message' ) ) . toBeFalsy ( ) ;
95- } ) ;
91+ it ( `and their context is set by the show function` , ( ) => {
92+ const error = ValidationError . fromFirstError ( TestHostComponent . getFormControl ( ) ) ;
9693
97- it ( `show sets the context to the error object` , ( ) => {
98- const { fixture , validationMessageComponent } = setupErrorMessageTests ( ) ;
99- const error = ValidationError . fromFirstError ( TestHostComponent . getFormControl ( ) ) ;
94+ validationMessageComponent . show ( error ) ;
95+ expect ( validationMessageComponent . context ) . toEqual ( error . errorObject ) ;
96+ } ) ;
10097
101- validationMessageComponent . show ( error ) ;
102- expect ( validationMessageComponent . context ) . toEqual ( error . errorObject ) ;
98+ @Component ( {
99+ template : `
100+ <arv-validation-message #minlengthValidation key="minlength">
101+ <p class="message">The message is shown. requiredLength: {{minlengthValidation.context?.requiredLength}}</p>
102+ </arv-validation-message>`
103+ } )
104+ class TestHostComponent {
105+ @ViewChild ( ValidationMessageComponent ) validationMessageComponent : ValidationMessageComponent ;
106+
107+ static getFormControl ( ) : any {
108+ return {
109+ errors : {
110+ minlength : { requiredLength : 10 , actualLength : 5 }
111+ }
112+ } ;
113+ }
114+ }
103115 } ) ;
104116} ) ;
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