Skip to content

Commit 9ca125c

Browse files
committed
test(ngx-material-password-strength): added first tests
1 parent 465c244 commit 9ca125c

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

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

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
22

3-
import {PasswordStrengthComponent} from './password-strength.component';
3+
import {Colors, PasswordStrengthComponent} from './password-strength.component';
44
import {MatProgressBarModule} from '@angular/material';
5+
import {SimpleChange} from '@angular/core';
56

67
describe('PasswordStrengthComponent', () => {
78
let component: PasswordStrengthComponent;
@@ -24,4 +25,60 @@ describe('PasswordStrengthComponent', () => {
2425
it('should create', () => {
2526
expect(component).toBeTruthy();
2627
});
28+
29+
it('should have a password input', () => {
30+
expect(component.password).toBeUndefined();
31+
});
32+
33+
it('should have a primary color and strength equal to 0 when no password is provided', () => {
34+
expect(component.color).toBe(Colors.primary);
35+
expect(component.strength).toBe(0);
36+
});
37+
38+
it('should not calculate the strength of the password', () => {
39+
const calculatePasswordStrengthSpy = jest.spyOn(component, 'calculatePasswordStrength');
40+
component.password = 'testPass';
41+
component.externalError = true;
42+
component.ngOnChanges({
43+
password: new SimpleChange(null, component.password, true),
44+
externalError: new SimpleChange(null, component.externalError, true)
45+
});
46+
fixture.detectChanges();
47+
expect(calculatePasswordStrengthSpy).not.toHaveBeenCalled();
48+
});
49+
50+
it('should calculate the strength of the password', () => {
51+
const calculatePasswordStrengthSpy = jest.spyOn(component, 'calculatePasswordStrength');
52+
component.password = 'testPass2';
53+
component.externalError = true;
54+
component.ngOnChanges({
55+
password: new SimpleChange('testPass', component.password, false),
56+
});
57+
fixture.detectChanges();
58+
expect(calculatePasswordStrengthSpy).toHaveBeenCalled();
59+
});
60+
61+
it('should strength = 20 and color = warn when the password only contain one char ', () => {
62+
const testChars = ['A', '1', 'a', '.'];
63+
testChars.forEach(char => {
64+
component.password = char;
65+
console.log('char = ', char);
66+
component.calculatePasswordStrength();
67+
expect(component.strength).toBe(20);
68+
expect(component.color).toBe(Colors.warn);
69+
});
70+
});
71+
72+
it('should strength = 40 and color = warn when the password fulfills 2 criteria ', () => {
73+
const combinations = ['Aa', 'aA', '1a', 'A!'];
74+
// const combinations = generator(chars, 2, 3);
75+
console.log('combinations = ', combinations);
76+
combinations.forEach(combination => {
77+
component.password = combination;
78+
console.log('combination = ', combination);
79+
component.calculatePasswordStrength();
80+
expect(component.strength).toBe(40);
81+
expect(component.color).toBe(Colors.accent);
82+
});
83+
});
2784
});

0 commit comments

Comments
 (0)