Skip to content

Commit 0952602

Browse files
test: added new test cases for password utils
1 parent 530d43a commit 0952602

File tree

1 file changed

+94
-11
lines changed

1 file changed

+94
-11
lines changed

src/components/PasswordInput/__test__/PasswordInput.spec.tsx

+94-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import React from "react";
22
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";
411
import { PasswordInput } from "..";
512

613
describe("PasswordInput component", () => {
@@ -12,26 +19,26 @@ describe("PasswordInput component", () => {
1219
label="Password"
1320
isFullWidth={true} />
1421
);
15-
const input = screen.getByLabelText('Password');
22+
const input = screen.getByLabelText("Password");
1623
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();
1926
});
2027

21-
it('renders with placeholder text', () => {
28+
it("renders with placeholder text", () => {
2229
const { getByPlaceholderText } = render(
2330
<PasswordInput label="Enter your password" />
2431
);
25-
expect(getByPlaceholderText('Enter your password')).toBeInTheDocument();
32+
expect(getByPlaceholderText("Enter your password")).toBeInTheDocument();
2633
});
2734

28-
it('displays custom error message when provided', async () => {
35+
it("displays custom error message when provided", async () => {
2936
const { getByLabelText, getByText } = render(
3037
<PasswordInput label="Password" value="weak" message="Custom error message" />
3138
);
32-
const input = getByLabelText('Password');
39+
const input = getByLabelText("Password");
3340
await userEvent.click(input);
34-
expect(getByText('Custom error message')).toBeInTheDocument();
41+
expect(getByText("Custom error message")).toBeInTheDocument();
3542
})
3643

3744
it("handles onChange event", async () => {
@@ -90,11 +97,87 @@ describe("PasswordInput component", () => {
9097
expect(container.querySelector(".deriv-password__meter")).not.toBeInTheDocument();
9198
});
9299

93-
it('initializes isTouched state correctly', async() => {
100+
it("initializes isTouched state correctly", async() => {
94101
render(<PasswordInput label="Password" />);
95102
const input = screen.getByLabelText("Password");
96103
await userEvent.click(input);
97-
expect(input).toHaveValue('');
104+
expect(input).toHaveValue("");
98105
});
99106

100107
});
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

Comments
 (0)