Skip to content

Commit

Permalink
core(): fixed unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Arielpetit committed Jan 16, 2025
1 parent bc94bc7 commit 355d502
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
3 changes: 1 addition & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import OtpPage from "./pages/OtpPage";
import DashboardPage from "./pages/DashboardPage";
import Header from "./components/Header";
import "./App.css";
import 'react-toastify/dist/ReactToastify.css';

import "react-toastify/dist/ReactToastify.css";

const App: React.FC = () => {
return (
Expand Down
9 changes: 6 additions & 3 deletions src/__tests__/Register.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ import Register from "../pages/RegisterPage";
import "@testing-library/jest-dom";
import { MemoryRouter } from "react-router-dom";
import { describe, it, beforeEach, vi, expect } from "vitest";
import { toast } from "react-toastify";

describe("Register component", () => {
beforeEach(() => {
vi.clearAllMocks(); // Clear mocks before each test
window.alert = vi.fn();
vi.spyOn(window, "alert").mockImplementation(() => {});
// Mock the toast.success and toast.error methods to return a mock ID (string or number)
vi.spyOn(toast, "success").mockImplementation(() => "mock-toast-id");
vi.spyOn(toast, "error").mockImplementation(() => "mock-toast-id");
});

const renderWithRouter = (component: React.ReactNode) => {
Expand All @@ -28,7 +31,7 @@ describe("Register component", () => {
fireEvent.click(sendOTPButton);

// Wait for the alert to be shown
await waitFor(() => expect(window.alert).toHaveBeenCalledTimes(1));
await waitFor(() => expect(toast.error).toHaveBeenCalledTimes(1));
});

it("displays error message on invalid phone number", async () => {
Expand All @@ -42,7 +45,7 @@ describe("Register component", () => {
fireEvent.click(sendOTPButton);

await waitFor(() =>
expect(window.alert).toHaveBeenCalledWith(
expect(toast.error).toHaveBeenCalledWith(
"Please enter a valid phone number.",
),
);
Expand Down
3 changes: 1 addition & 2 deletions src/pages/OtpPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { useNavigate, useLocation } from "react-router-dom";
import { RequestToValidateOTP } from "../services/keyManagement/requestService.ts";
import { toast, ToastContainer } from "react-toastify";


const Otp = () => {
const navigate = useNavigate();
const location = useLocation();
Expand All @@ -26,7 +25,7 @@ const Otp = () => {

if (response.split(" ")[0] === "Registration") {
toast.success("Registration successful");
// Simulate an async action (e.g., sending OTP)
// Simulate an async action (e.g., sending OTP)
await new Promise((resolve) => setTimeout(resolve, 2000));
navigate("/dashboard");
} else {
Expand Down
10 changes: 5 additions & 5 deletions src/services/keyManagement/apiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export const sendOTP = async (
try {
// Send the post request to the backend
const response = await axios.post(
// `${envVariables.VITE_BACKEND_URL}/api/otp/send`,
'http://localhost:8080/api/otp/send',
`${envVariables.VITE_BACKEND_URL}/api/otp/send`,
// 'http://localhost:8080/api/otp/send',
requestBody,
{ headers },
);
Expand Down Expand Up @@ -56,8 +56,8 @@ export const validateOTP = async (
try {
// Send the post request to the backend
const response = await axios.post(
// `${envVariables.VITE_BACKEND_URL}/api/otp/validate`,
"http://localhost:8080/api/otp/validate",
`${envVariables.VITE_BACKEND_URL}/api/otp/validate`,
// "http://localhost:8080/api/otp/validate",
requestBody,
{ headers },
);
Expand All @@ -70,4 +70,4 @@ export const validateOTP = async (
throw new Error("Incorrect OTP");
}
};
export const getAccountId = () => accountId;
export const getAccountId = () => accountId;

0 comments on commit 355d502

Please sign in to comment.