1
1
import { async , ComponentFixture , TestBed } from '@angular/core/testing' ;
2
2
3
- import { PasswordStrengthComponent } from './password-strength.component' ;
3
+ import { Colors , PasswordStrengthComponent } from './password-strength.component' ;
4
4
import { MatProgressBarModule } from '@angular/material' ;
5
+ import { SimpleChange } from '@angular/core' ;
5
6
6
7
describe ( 'PasswordStrengthComponent' , ( ) => {
7
8
let component : PasswordStrengthComponent ;
@@ -24,4 +25,60 @@ describe('PasswordStrengthComponent', () => {
24
25
it ( 'should create' , ( ) => {
25
26
expect ( component ) . toBeTruthy ( ) ;
26
27
} ) ;
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
+ } ) ;
27
84
} ) ;
0 commit comments