Skip to content

Commit e648d86

Browse files
committed
test(ngx-material-password-strength): added tests specs for the main component
1 parent b0bc979 commit e648d86

File tree

1 file changed

+60
-8
lines changed

1 file changed

+60
-8
lines changed

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

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ describe('PasswordStrengthComponent', () => {
4747
});
4848
fixture.detectChanges();
4949
expect(calculatePasswordStrengthSpy).not.toHaveBeenCalled();
50+
expect(component.color).toBe(Colors.primary);
51+
});
52+
53+
it('should not calculate the strength of the password when externalError is provided', () => {
54+
const calculatePasswordStrengthSpy = jest.spyOn(component, 'calculatePasswordStrength');
55+
component.password = 'testPass123!';
56+
component.externalError = true;
57+
component.ngOnChanges({
58+
password: new SimpleChange('testPass', component.password, false),
59+
externalError: new SimpleChange(false, component.externalError, false)
60+
});
61+
fixture.detectChanges();
62+
expect(calculatePasswordStrengthSpy).not.toHaveBeenCalled();
63+
// todo: 19.4.18 - to check
64+
// expect(component.color).toBe(Colors.warn);
5065
});
5166

5267
it('should calculate the strength of the password', () => {
@@ -95,18 +110,55 @@ describe('PasswordStrengthComponent', () => {
95110

96111
it('should strength = 60 and color = accent when the password fulfills 3 criteria ',
97112
() => {
98-
const charsList = ['a', 'A', '1', '!', '1234567'];
113+
const charsList = ['a', 'A', '9', '!', '123456'];
99114
const combinations = generator.loadCombinationList(charsList, 3, 3, true);
100115
console.log('combinations = ', combinations);
101116

102117
combinations.forEach(combination => {
103-
console.log('combination = ', combination);
104-
const repeats = /(.)\1/;
105-
console.log('repeats = ', repeats.test(combination));
106-
component.password = combination;
107-
component.calculatePasswordStrength();
108-
// expect(component.strength).toBe(60);
109-
// expect(component.color).toBe(Colors.accent);
118+
const isCharDuplicate = new RegExp(/^.*(.).*\1.*$/);
119+
console.log('repeats = ', isCharDuplicate.test(combination), ' for --> ', combination);
120+
if (!isCharDuplicate.test(combination)) {
121+
component.password = combination;
122+
component.calculatePasswordStrength();
123+
expect(component.strength).toBeGreaterThanOrEqual(60);
124+
expect(component.color).toBe(Colors.accent);
125+
}
126+
});
127+
});
128+
129+
it('should strength at least 80 and color = accent or primary when the password fulfills 4 criteria ',
130+
() => {
131+
const charsList = ['a', 'A', '9', '!', 'bcdef'];
132+
const combinations = generator.loadCombinationList(charsList, 4, 4, true);
133+
console.log('combinations = ', combinations);
134+
135+
combinations.forEach(combination => {
136+
const isCharDuplicate = new RegExp(/^.*(.).*\1.*$/);
137+
console.log('repeats = ', isCharDuplicate.test(combination), ' for --> ', combination);
138+
if (!isCharDuplicate.test(combination)) {
139+
component.password = combination;
140+
component.calculatePasswordStrength();
141+
expect(component.strength).toBeGreaterThanOrEqual(80);
142+
component.strength > 80 ? expect(component.color).toBe(Colors.primary) : expect(component.color).toBe(Colors.accent);
143+
}
144+
});
145+
});
146+
147+
it('should strength equal 100 and color = primary when the password fulfills all 5 criteria ',
148+
() => {
149+
const charsList = ['a', 'A', '9', '!', 'bcdef'];
150+
const combinations = generator.loadCombinationList(charsList, 5, 5, true);
151+
console.log('combinations = ', combinations);
152+
153+
combinations.forEach(combination => {
154+
const isCharDuplicate = new RegExp(/^.*(.).*\1.*$/);
155+
console.log('repeats = ', isCharDuplicate.test(combination), ' for --> ', combination);
156+
if (!isCharDuplicate.test(combination)) {
157+
component.password = combination;
158+
component.calculatePasswordStrength();
159+
expect(component.strength).toBe(100);
160+
expect(component.color).toBe(Colors.primary);
161+
}
110162
});
111163
});
112164
});

0 commit comments

Comments
 (0)