Skip to content

Commit 644ee12

Browse files
committed
fix(package): calculate the password strength on init if the password value is provided #149
1 parent e84ec60 commit 644ee12

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

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

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

78+
it('should calculate the strength of the password on int if the password is provided', () => {
79+
const calculatePasswordStrengthSpy = jest.spyOn(component, 'calculatePasswordStrength');
80+
component.password = '#A2lsam,#af21af1!';
81+
component.ngOnInit();
82+
fixture.detectChanges();
83+
expect(calculatePasswordStrengthSpy).toHaveBeenCalled();
84+
});
85+
86+
it('should calculate the strength of the password when password is directly provided', () => {
87+
const calculatePasswordStrengthSpy = jest.spyOn(component, 'calculatePasswordStrength');
88+
component.password = 'testPass3';
89+
component.externalError = false;
90+
component.ngOnChanges({
91+
password: new SimpleChange( component.password, component.password, false),
92+
});
93+
fixture.detectChanges();
94+
expect(calculatePasswordStrengthSpy).toHaveBeenCalled();
95+
});
96+
7897
it('should have min input', () => {
7998
const calculatePasswordStrengthSpy = jest.spyOn(component, 'calculatePasswordStrength');
8099
component.ngOnInit();

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,20 @@ export class MatPasswordStrengthComponent implements OnInit, OnChanges {
5656

5757
ngOnInit(): void {
5858
this.setRulesAndValidators();
59+
if (this.password) {
60+
this.calculatePasswordStrength();
61+
}
5962
}
6063

6164
ngOnChanges(changes: SimpleChanges): void {
62-
65+
console.log('changes: ', changes);
6366
if ((changes.externalError && changes.externalError.firstChange) || changes.password.isFirstChange()) {
6467
return;
6568
} else if (changes.externalError && changes.externalError.currentValue) {
6669
this._color = Colors.warn;
6770
return;
71+
} else if (changes.password.previousValue === changes.password.currentValue && !changes.password.firstChange) {
72+
this.calculatePasswordStrength();
6873
} else {
6974
this.password && this.password.length > 0 ?
7075
this.calculatePasswordStrength() : this.reset();

0 commit comments

Comments
 (0)