@@ -47,6 +47,21 @@ describe('PasswordStrengthComponent', () => {
47
47
} ) ;
48
48
fixture . detectChanges ( ) ;
49
49
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);
50
65
} ) ;
51
66
52
67
it ( 'should calculate the strength of the password' , ( ) => {
@@ -95,18 +110,55 @@ describe('PasswordStrengthComponent', () => {
95
110
96
111
it ( 'should strength = 60 and color = accent when the password fulfills 3 criteria ' ,
97
112
( ) => {
98
- const charsList = [ 'a' , 'A' , '1 ' , '!' , '1234567 ' ] ;
113
+ const charsList = [ 'a' , 'A' , '9 ' , '!' , '123456 ' ] ;
99
114
const combinations = generator . loadCombinationList ( charsList , 3 , 3 , true ) ;
100
115
console . log ( 'combinations = ' , combinations ) ;
101
116
102
117
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
+ }
110
162
} ) ;
111
163
} ) ;
112
164
} ) ;
0 commit comments