1- /*
2- Password Validation
1+ const { passwordValidator } = require ( "./password-validator" ) ;
32
4- Write a program that should check if a password is valid
5- and returns a boolean
3+ test ( "should return false when password has fewer than 5 characters" , ( ) => {
4+ expect ( passwordValidator ( "Ab1!" ) ) . toEqual ( false ) ;
5+ } ) ;
66
7- To be valid, a password must:
8- - Have at least 5 characters.
9- - Have at least one English uppercase letter (A-Z)
10- - Have at least one English lowercase letter (a-z)
11- - Have at least one number (0-9)
12- - Have at least one of the following non-alphanumeric symbols: ("!", "#", "$", "%", ".", "*", "&")
13- - Must not be any previous password in the passwords array.
7+ test ( "should return false when password has no uppercase letter" , ( ) => {
8+ expect ( passwordValidator ( "hello1!" ) ) . toEqual ( false ) ;
9+ } ) ;
1410
15- You must breakdown this problem in order to solve it. Find one test case first and get that working
16- */
17- const isValidPassword = require ( "./password-validator" ) ;
18- test ( "password has at least 5 characters" , ( ) => {
19- // Arrange
20- const password = "12345" ;
21- // Act
22- const result = isValidPassword ( password ) ;
23- // Assert
24- expect ( result ) . toEqual ( true ) ;
25- }
26- ) ;
11+ test ( "should return false when password has no lowercase letter" , ( ) => {
12+ expect ( passwordValidator ( "HELLO1!" ) ) . toEqual ( false ) ;
13+ } ) ;
14+
15+ test ( "should return false when password has no number" , ( ) => {
16+ expect ( passwordValidator ( "Hello!" ) ) . toEqual ( false ) ;
17+ } ) ;
18+
19+ test ( "should return false when password has no special character" , ( ) => {
20+ expect ( passwordValidator ( "Hello1" ) ) . toEqual ( false ) ;
21+ } ) ;
22+
23+ test ( "should return false when password is a previous password" , ( ) => {
24+ expect ( passwordValidator ( "Pass1!" ) ) . toEqual ( false ) ;
25+ } ) ;
26+
27+ test ( "should return true for a valid password meeting all criteria" , ( ) => {
28+ expect ( passwordValidator ( "Valid1!" ) ) . toEqual ( true ) ;
29+ expect ( passwordValidator ( "MyPass1$" ) ) . toEqual ( true ) ;
30+ } ) ;
0 commit comments