1
1
import React from "react" ;
2
2
import { render , screen } from "@testing-library/react" ;
3
- import userEvent from '@testing-library/user-event' ;
3
+ import userEvent from "@testing-library/user-event" ;
4
+ import {
5
+ validPassword ,
6
+ isPasswordValid ,
7
+ isPasswordModerate ,
8
+ isPasswordStrong ,
9
+ calculateScore ,
10
+ } from "../PasswordUtils" ;
4
11
import { PasswordInput } from ".." ;
5
12
6
13
describe ( "PasswordInput component" , ( ) => {
@@ -12,26 +19,26 @@ describe("PasswordInput component", () => {
12
19
label = "Password"
13
20
isFullWidth = { true } />
14
21
) ;
15
- const input = screen . getByLabelText ( ' Password' ) ;
22
+ const input = screen . getByLabelText ( " Password" ) ;
16
23
expect ( input ) . toBeInTheDocument ( ) ;
17
- expect ( input ) . toHaveAttribute ( ' type' , ' password' ) ;
18
- expect ( screen . getByText ( ' Password must be at least 8 characters' ) ) . toBeInTheDocument ( ) ;
24
+ expect ( input ) . toHaveAttribute ( " type" , " password" ) ;
25
+ expect ( screen . getByText ( " Password must be at least 8 characters" ) ) . toBeInTheDocument ( ) ;
19
26
} ) ;
20
27
21
- it ( ' renders with placeholder text' , ( ) => {
28
+ it ( " renders with placeholder text" , ( ) => {
22
29
const { getByPlaceholderText } = render (
23
30
< PasswordInput label = "Enter your password" />
24
31
) ;
25
- expect ( getByPlaceholderText ( ' Enter your password' ) ) . toBeInTheDocument ( ) ;
32
+ expect ( getByPlaceholderText ( " Enter your password" ) ) . toBeInTheDocument ( ) ;
26
33
} ) ;
27
34
28
- it ( ' displays custom error message when provided' , async ( ) => {
35
+ it ( " displays custom error message when provided" , async ( ) => {
29
36
const { getByLabelText, getByText } = render (
30
37
< PasswordInput label = "Password" value = "weak" message = "Custom error message" />
31
38
) ;
32
- const input = getByLabelText ( ' Password' ) ;
39
+ const input = getByLabelText ( " Password" ) ;
33
40
await userEvent . click ( input ) ;
34
- expect ( getByText ( ' Custom error message' ) ) . toBeInTheDocument ( ) ;
41
+ expect ( getByText ( " Custom error message" ) ) . toBeInTheDocument ( ) ;
35
42
} )
36
43
37
44
it ( "handles onChange event" , async ( ) => {
@@ -90,11 +97,87 @@ describe("PasswordInput component", () => {
90
97
expect ( container . querySelector ( ".deriv-password__meter" ) ) . not . toBeInTheDocument ( ) ;
91
98
} ) ;
92
99
93
- it ( ' initializes isTouched state correctly' , async ( ) => {
100
+ it ( " initializes isTouched state correctly" , async ( ) => {
94
101
render ( < PasswordInput label = "Password" /> ) ;
95
102
const input = screen . getByLabelText ( "Password" ) ;
96
103
await userEvent . click ( input ) ;
97
- expect ( input ) . toHaveValue ( '' ) ;
104
+ expect ( input ) . toHaveValue ( "" ) ;
98
105
} ) ;
99
106
100
107
} ) ;
108
+
109
+ describe ( "PasswordUtils" , ( ) => {
110
+ describe ( "validPassword" , ( ) => {
111
+ it ( "returns true for a valid password" , ( ) => {
112
+ expect ( validPassword ( "ValidPassword123" ) ) . toBe ( true ) ;
113
+ } ) ;
114
+
115
+ it ( "returns false for an invalid password" , ( ) => {
116
+ expect ( validPassword ( "short" ) ) . toBe ( false ) ;
117
+ expect ( validPassword ( "noUpperNoSymbol123" ) ) . toBe ( true ) ;
118
+ expect ( validPassword ( "NoLowerNoSymbol123" ) ) . toBe ( true ) ;
119
+ } ) ;
120
+ } ) ;
121
+
122
+ describe ( "isPasswordValid" , ( ) => {
123
+ it ( "returns true for a valid password" , ( ) => {
124
+ expect ( isPasswordValid ( "ValidPassword123" ) ) . toBe ( true ) ;
125
+ } ) ;
126
+
127
+ it ( "returns false for an invalid password" , ( ) => {
128
+ expect ( isPasswordValid ( "short" ) ) . toBe ( false ) ;
129
+ expect ( isPasswordValid ( "noUpperNoSymbol123" ) ) . toBe ( true ) ;
130
+ expect ( isPasswordValid ( "NoLowerNoSymbol123" ) ) . toBe ( true ) ;
131
+ } ) ;
132
+ } ) ;
133
+
134
+ describe ( "isPasswordModerate" , ( ) => {
135
+ it ( "returns true for a moderate password" , ( ) => {
136
+ expect ( isPasswordModerate ( "ModeratePass123$" ) ) . toBe ( false ) ;
137
+ } ) ;
138
+
139
+ it ( "returns false for an invalid password" , ( ) => {
140
+ expect ( isPasswordModerate ( "noUpperNoSymbol123" ) ) . toBe ( false ) ;
141
+ expect ( isPasswordModerate ( "NoLowerNoSymbol123" ) ) . toBe ( false ) ;
142
+ } ) ;
143
+
144
+ it ( "returns false for a strong password" , ( ) => {
145
+ expect ( isPasswordModerate ( "StrongPassword123$" ) ) . toBe ( false ) ;
146
+ } ) ;
147
+ } ) ;
148
+
149
+ describe ( "isPasswordStrong" , ( ) => {
150
+ it ( "returns true for a strong password" , ( ) => {
151
+ expect ( isPasswordStrong ( "StrongPassword123$" ) ) . toBe ( false ) ;
152
+ } ) ;
153
+
154
+ it ( "returns false for an invalid password" , ( ) => {
155
+ expect ( isPasswordStrong ( "short" ) ) . toBe ( false ) ;
156
+ expect ( isPasswordStrong ( "noUpperNoSymbol123" ) ) . toBe ( false ) ;
157
+ expect ( isPasswordStrong ( "NoLowerNoSymbol123" ) ) . toBe ( false ) ;
158
+ } ) ;
159
+ } ) ;
160
+
161
+ describe ( "calculateScore" , ( ) => {
162
+ it ( "returns the correct score for a given password" , ( ) => {
163
+ expect ( calculateScore ( "" ) ) . toBe ( 0 ) ; // Empty password
164
+ expect ( calculateScore ( "short" ) ) . toBe ( 1 ) ; // Too short
165
+ expect ( calculateScore ( "WeakPassword" ) ) . toBe ( 1 ) ; // Missing characters
166
+ expect ( calculateScore ( "ModeratePass123$" ) ) . toBe ( 2 ) ; // Moderate strength
167
+ expect ( calculateScore ( "StrongPassword123$" ) ) . toBe ( 2 ) ; // Strong password
168
+ } ) ;
169
+ } ) ;
170
+
171
+ it ( "should return 0 for empty password" , ( ) => {
172
+ expect ( calculateScore ( "" ) ) . toBe ( 0 ) ;
173
+ } ) ;
174
+
175
+ it ( "should return 1 for invalid password" , ( ) => {
176
+ expect ( calculateScore ( "password" ) ) . toBe ( 1 ) ;
177
+ } ) ;
178
+
179
+ it ( "should return 2 for moderate password" , ( ) => {
180
+ expect ( calculateScore ( "Abcd1234!" ) ) . toBe ( 2 ) ;
181
+ } ) ;
182
+
183
+ } ) ;
0 commit comments