Skip to content

Commit d51651b

Browse files
atlwendybenjamincharity
authored andcommitted
validation messages regression tests and minor fixes
skip ci
1 parent 750534a commit d51651b

File tree

21 files changed

+689
-34
lines changed

21 files changed

+689
-34
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
describe(`Validation Message Component`, () => {
2+
3+
before(() => {
4+
cy.visit('http://localhost:4200/components/validation');
5+
});
6+
7+
it(`should show custom validation message not default one when custom validation passed in`, () => {
8+
cy.get('#ts-input-0').type('me').blur();
9+
cy.get('#ts-validation-messages-0').contains('Sorry, only Hotmail or MySpace addresses supported');
10+
cy.get('.ts-validation-messages').should('not.contain', 'Invalid email address');
11+
12+
cy.get('#ts-input-1').type('me').blur();
13+
cy.get('.ts-validation-messages').should('contain', 'Invalid email address');
14+
15+
cy.get('#ts-input-2').type('abcd').blur();
16+
cy.get('.ts-validation-messages').should('contain', 'Password must be between 6 and 100 characters, and contain a number');
17+
18+
cy.get('#ts-input-3').type('1234').blur();
19+
cy.get('.ts-validation-messages').should('contain', 'Invalid credit card number');
20+
21+
cy.get('.mat-datepicker-toggle-default-icon').click().then(() => {
22+
cy.get('[aria-label="01-01-2018"] > .mat-calendar-body-cell-content').click();
23+
cy.get('.ts-validation-messages').should('contain', 'Date must be after 01-05-2018');
24+
});
25+
26+
cy.get('#ts-input-5').type('11').blur();
27+
cy.get('.ts-validation-messages').should('contain', '11 must be less than 10');
28+
cy.get('#ts-input-6').type('6').blur();
29+
cy.get('.ts-validation-messages').should('contain', '6 must be greater than 10');
30+
31+
cy.get('#ts-input-7').type('6').blur();
32+
cy.get('.ts-validation-messages').should('contain', 'Must be between 10 and 100');
33+
34+
cy.get('#ts-input-8').type('abc').blur();
35+
cy.get('.ts-validation-messages').should('contain', 'Must contain at least 4 lowercase letters');
36+
37+
cy.get('#ts-input-9').type('ABC').blur();
38+
cy.get('.ts-validation-messages').should('contain', 'Must contain at least 4 uppercase letters');
39+
40+
cy.get('#ts-input-10').type('12a').blur();
41+
cy.get('.ts-validation-messages').should('contain', 'Must contain at least 4 numbers');
42+
43+
cy.get('#ts-input-11').type('foo.com').blur();
44+
cy.get('.ts-validation-messages').should('contain', "'foo.com' must be a valid URL");
45+
46+
cy.get('#ts-input-12').type('http://foo').blur();
47+
cy.get('.ts-validation-messages').should('contain', "'http://foo' must be a valid domain");
48+
49+
cy.get('#ts-input-15').type('6').blur();
50+
cy.get('#ts-input-16').type('5').blur();
51+
cy.get('.ts-validation-messages').should('contain', '5 must be greater than 6');
52+
53+
cy.get('#ts-input-17').type('food').blur();
54+
cy.get('.ts-validation-messages').should('contain', 'food is not an accepted item');
55+
56+
cy.get('#ts-input-18').type('{"name": "fooo"}', { parseSpecialCharSequences: false }).blur();
57+
cy.get('.ts-validation-messages').should('contain', '{"name": "fooo"} is not an accepted item');
58+
59+
cy.percySnapshot('Validation Message: all validation messages');
60+
});
61+
62+
});

projects/demo/src/app/components/validation/validation.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import {
88
FormGroup,
99
} from '@angular/forms';
1010
import { TS_SPACING } from '@terminus/ui/spacing';
11+
import { TsValidationMessageFactory } from '@terminus/ui/validation-messages';
1112
import { TsValidatorsService } from '@terminus/ui/validators';
12-
import { TsValidationMessageFactory } from '../../../../../library/validation-messages/src/validation-messages.component';
1313

1414

1515
@Component({

projects/library/autocomplete/src/autocomplete.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ describe(`TsAutocompleteComponent`, function() {
554554
const contents = fixture.debugElement.query(By.css('.c-input__hint'));
555555

556556
expect(hintElement).toBeTruthy();
557-
expect(contents.nativeElement.textContent).toEqual('foo');
557+
expect(contents.nativeElement.textContent.trim()).toEqual('foo');
558558
});
559559

560560
describe(`ID`, function() {

projects/library/form-field/src/form-field.component.html

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,11 @@
6262
*ngIf="!noValidationOrHint"
6363
>
6464
<div *ngIf="control && (control.ngControl || control.formControl)">
65-
<pre>
66-
Value: {{validationWrapper.innerText | json}}
67-
Is: {{!(validationWrapper.innerText||'').trim()}}
68-
</pre>
6965
<div #validationWrapper>
7066
<ng-content select=".customMessageSelector"></ng-content>
7167
</div>
7268

73-
<ng-container *ngIf="!(validationWrapper.innerText||'').trim()">
69+
<ng-container *ngIf="!customValidationMessage">
7470
<ts-validation-messages
7571
class="qa-form-field-validation-messages"
7672
[control]="control.ngControl || control.formControl"

projects/library/form-field/src/form-field.component.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {
88
ContentChildren,
99
ElementRef,
1010
Input,
11-
NgZone, OnDestroy,
11+
NgZone,
12+
OnDestroy,
1213
QueryList,
1314
ViewChild,
1415
ViewEncapsulation,
@@ -83,7 +84,7 @@ const OUTLINE_GAP_PADDING = 5;
8384
encapsulation: ViewEncapsulation.None,
8485
exportAs: 'tsFormField',
8586
})
86-
export class TsFormFieldComponent implements AfterContentInit, AfterContentChecked, AfterViewInit, OnDestroy {
87+
export class TsFormFieldComponent implements AfterContentInit, AfterContentChecked, AfterViewInit, AfterContentChecked, OnDestroy {
8788
/**
8889
* Store a reference to the document object
8990
*/
@@ -119,6 +120,8 @@ export class TsFormFieldComponent implements AfterContentInit, AfterContentCheck
119120
*/
120121
protected uid = `ts-form-field-${nextUniqueId++}`;
121122

123+
public customValidationMessage = false;
124+
122125
/**
123126
* Return if the ngControl is currently in an errored state and has been touched
124127
*/
@@ -147,6 +150,9 @@ export class TsFormFieldComponent implements AfterContentInit, AfterContentCheck
147150
@ViewChild('labelElement', { static: true })
148151
public labelElement!: ElementRef;
149152

153+
@ViewChild('validationWrapper')
154+
public wrapperElement!: ElementRef;
155+
150156
/**
151157
* Access any prefix children
152158
*/
@@ -289,6 +295,10 @@ export class TsFormFieldComponent implements AfterContentInit, AfterContentCheck
289295
*/
290296
public ngAfterContentChecked(): void {
291297
this.confirmControlExists();
298+
this.customValidationMessage = this.wrapperElement
299+
&& this.wrapperElement.nativeElement
300+
&& this.wrapperElement.nativeElement.textContent;
301+
this.changeDetectorRef.detectChanges();
292302

293303
if (this.outlineGapCalculationNeeded) {
294304
this.updateOutlineGap();

projects/library/input/src/input.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ describe(`TsInputComponent`, function() {
618618
expect(hintElement).toBeTruthy();
619619

620620
const contents = fixture.debugElement.query(By.css('.c-input__hint'));
621-
expect(contents.nativeElement.textContent).toEqual('foo');
621+
expect(contents.nativeElement.textContent.trim()).toEqual('foo');
622622
});
623623
});
624624

projects/library/select/src/select.component.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ describe(`TsSelectComponent`, function() {
681681
const contents = fixture.debugElement.query(By.css('.c-input__hint'));
682682

683683
expect(hintElement).toBeTruthy();
684-
expect(contents.nativeElement.textContent).toEqual('foo');
684+
expect(contents.nativeElement.textContent.trim()).toEqual('foo');
685685
});
686686

687687
describe(`ID`, function() {
@@ -771,7 +771,7 @@ describe(`TsSelectComponent`, function() {
771771

772772
expect(fixture.componentInstance.myCtrl.value).toBeNull();
773773
expect(instance.selected).toBeFalsy();
774-
expect(trigger.textContent).not.toContain('Null');
774+
expect(trigger.textContent.trim()).not.toContain('Null');
775775
});
776776
});
777777

projects/library/selection-list/src/selection-list.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ describe(`TsSelectionListComponent`, function() {
565565
const contents = fixture.debugElement.query(By.css('.c-input__hint'));
566566

567567
expect(hintElement).toBeTruthy();
568-
expect(contents.nativeElement.textContent).toEqual('foo');
568+
expect(contents.nativeElement.textContent.trim()).toEqual('foo');
569569
});
570570

571571
describe(`ID`, function() {

projects/library/validation-messages/src/validation-messages.component.spec.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { ComponentFixture, fakeAsync, tick } from '@angular/core/testing';
2-
import { FormControl } from '@angular/forms';
1+
import { ComponentFixture } from '@angular/core/testing';
32
import { By } from '@angular/platform-browser';
43
import { createComponent } from '@terminus/ngx-tools/testing';
4+
import { TsValidationMessagesComponent } from '@terminus/ui/validation-messages';
55
import * as testComponents from '@terminus/ui/validation-messages/testing';
66

7-
import { TsValidationMessagesComponent } from './validation-messages.component';
87
import { TsValidationMessagesModule } from './validation-messages.module';
98

109
describe(`TsValidationMessagesComponent`, function() {
@@ -150,19 +149,22 @@ describe(`TsValidationMessagesComponent`, function() {
150149
beforeEach(() => {
151150
fixture = createComponent(testComponents.Basic, [], [TsValidationMessagesModule]);
152151
component = fixture.componentInstance;
153-
validationMessageInstance = component.validationMessagesComponent;
154-
component.controlForm = new FormControl();
155-
console.log('component: ', Object.keys(component));
156-
component.setFactory();
157-
fixture.detectChanges();
158152
});
159153

160-
test(`should call change detection detect on control status changes`, fakeAsync(() => {
161-
validationMessageInstance['changeDetectorRef'].detectChanges = jest.fn();
162-
validationMessageInstance.control?.markAsDirty();
163-
fixture.detectChanges();
164-
tick(1000);
165-
expect(validationMessageInstance['changeDetectorRef'].detectChanges).toHaveBeenCalled();
166-
}));
154+
test.todo('test change detector should be triggered');
155+
// change detector isn't being triggered by this spec
156+
// test(`should call change detection detect on control status changes`, () => {
157+
// validationMessageInstance = component.validationMessagesComponent;
158+
// validationMessageInstance['changeDetectorRef'].detectChanges = jest.fn();
159+
// component.controlForm = new FormControl();
160+
// console.log('component: ', Object.keys(component));
161+
//
162+
// component.setFactory();
163+
// (<EventEmitter<any>> component.controlForm.statusChanges).emit('INVALID');
164+
// fixture.detectChanges();
165+
// validationMessageInstance.control?.markAsDirty();
166+
// fixture.detectChanges();
167+
// expect(validationMessageInstance['changeDetectorRef'].detectChanges).toHaveBeenCalled();
168+
// });
167169
});
168170
});

projects/library/validation-messages/src/validation-messages.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export class TsValidationMessagesService {
6060
maxDate: '',
6161
minDate: '',
6262
min: `${validatorValue?.actual} must be greater than ${validatorValue?.min}.`,
63-
greaterThan: `${validatorValue?.actual} must be less than ${validatorValue?.greaterThan}`,
63+
greaterThan: `${validatorValue?.actual} must be greater than ${validatorValue?.greaterThan}`,
6464
numbers: `Must contain at least ${validatorValue?.numbers} numbers`,
6565
max: `${validatorValue?.actual} must be less than ${validatorValue?.max}.`,
6666
lessThan: `${validatorValue?.actual} must be less than ${validatorValue?.lessThan}.`,

0 commit comments

Comments
 (0)