Skip to content

Commit 0c88d7d

Browse files
committed
test(package): added a unit test for the min input property of the component
1 parent 1bc6f02 commit 0c88d7d

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

src/module/component/mat-password-strength-info/mat-password-strength-info.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
</div>
5959

6060
<div class="info-row" @items *ngIf="passwordComponent.enableLengthRule">
61-
<div *ngIf="passwordComponent.containAtLeastEightChars; then done else error">
61+
<div *ngIf="passwordComponent.containAtLeastMinChars; then done else error">
6262
</div>
6363
<ng-template #done>
6464
<mat-icon @positiveState color="primary">done</mat-icon>

src/module/component/mat-password-strength/mat-password-strength.component.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,29 @@ describe('PasswordStrengthComponent', () => {
7575
expect(calculatePasswordStrengthSpy).toHaveBeenCalled();
7676
});
7777

78+
it('should have min input', () => {
79+
const calculatePasswordStrengthSpy = jest.spyOn(component, 'calculatePasswordStrength');
80+
component.ngOnInit();
81+
// default values
82+
expect(component.min).toEqual(8);
83+
expect(component.containAtLeastMinChars).toBeUndefined();
84+
component.min = 5;
85+
component.password = '1234';
86+
component.ngOnChanges({
87+
min: new SimpleChange(component.min, 5, false),
88+
password: new SimpleChange(undefined, component.password, false),
89+
});
90+
fixture.detectChanges();
91+
expect(calculatePasswordStrengthSpy).toHaveBeenCalled();
92+
expect(component.containAtLeastMinChars).toBeFalsy();
93+
component.password = '12345';
94+
component.ngOnChanges({
95+
password: new SimpleChange('1234', component.password, false),
96+
});
97+
fixture.detectChanges();
98+
expect(component.containAtLeastMinChars).toBeTruthy();
99+
});
100+
78101
it('should strength = 20 and color = warn when the password only contain one char ', () => {
79102
const testChars = ['A', '1', 'a', '.'];
80103
testChars.forEach(char => {

src/module/component/mat-password-strength/mat-password-strength.component.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class MatPasswordStrengthComponent implements OnInit, OnChanges {
4242

4343
criteriaMap = new Map<Criteria, RegExp>();
4444

45-
containAtLeastEightChars: boolean;
45+
containAtLeastMinChars: boolean;
4646
containAtLeastOneLowerCaseLetter: boolean;
4747
containAtLeastOneUpperCaseLetter: boolean;
4848
containAtLeastOneDigit: boolean;
@@ -86,9 +86,9 @@ export class MatPasswordStrengthComponent implements OnInit, OnChanges {
8686
}
8787
}
8888

89-
private _containAtLeastEightChars(): boolean {
90-
this.containAtLeastEightChars = this.password.length >= this.min;
91-
return this.containAtLeastEightChars;
89+
private _containAtLeastMinChars(): boolean {
90+
this.containAtLeastMinChars = this.password.length >= this.min;
91+
return this.containAtLeastMinChars;
9292
}
9393

9494
private _containAtLeastOneLowerCaseLetter(): boolean {
@@ -148,15 +148,15 @@ export class MatPasswordStrengthComponent implements OnInit, OnChanges {
148148

149149
}
150150

151-
calculatePasswordStrength() {
151+
calculatePasswordStrength(): void {
152152
const requirements: Array<boolean> = [];
153153
const unit = 100 / this.criteriaMap.size;
154154

155155
// console.log('this.criteriaMap.size = ', this.criteriaMap.size);
156156
// console.log('unit = ', unit);
157157

158158
requirements.push(
159-
this.enableLengthRule ? this._containAtLeastEightChars() : false,
159+
this.enableLengthRule ? this._containAtLeastMinChars() : false,
160160
this.enableLowerCaseLetterRule ? this._containAtLeastOneLowerCaseLetter() : false,
161161
this.enableUpperCaseLetterRule ? this._containAtLeastOneUpperCaseLetter() : false,
162162
this.enableDigitRule ? this._containAtLeastOneDigit() : false,
@@ -169,7 +169,7 @@ export class MatPasswordStrengthComponent implements OnInit, OnChanges {
169169

170170
reset() {
171171
this._strength = 0;
172-
this.containAtLeastEightChars =
172+
this.containAtLeastMinChars =
173173
this.containAtLeastOneLowerCaseLetter =
174174
this.containAtLeastOneUpperCaseLetter =
175175
this.containAtLeastOneDigit =

0 commit comments

Comments
 (0)