|
| 1 | +import "@testing-library/jest-dom"; |
| 2 | +import React from "react"; |
| 3 | +import { |
| 4 | + render, screen, fireEvent, waitFor, |
| 5 | +} from "@testing-library/react"; |
| 6 | + |
| 7 | +import { |
| 8 | + LogoutProvider, |
| 9 | + useLogout, |
| 10 | +} from "../components/dashboard/admin/LogoutContext"; |
| 11 | +import api from "../redux/api/api"; |
| 12 | + |
| 13 | +// Mock the API and localStorage |
| 14 | +jest.mock("../redux/api/api"); |
| 15 | +const mockPost = api.post as jest.MockedFunction<typeof api.post>; |
| 16 | + |
| 17 | +// Mock LogoutModal component |
| 18 | +jest.mock("../components/dashboard/admin/LogoutModal", () => ({ |
| 19 | + __esModule: true, |
| 20 | + default: ({ isOpen, onClose, onConfirm }: any) => |
| 21 | + (isOpen ? ( |
| 22 | + <div className="fixed inset-0 z-50 flex items-center justify-center bg-black bg-opacity-50"> |
| 23 | + <div className="p-6 bg-white rounded shadow-lg"> |
| 24 | + <h2 className="mb-4 text-xl">Confirm Logout</h2> |
| 25 | + <p className="mb-4">Are you sure you want to logout?</p> |
| 26 | + <div className="flex justify-end gap-4"> |
| 27 | + <button |
| 28 | + className="px-4 py-2 text-white bg-gray-500 rounded" |
| 29 | + onClick={onClose} |
| 30 | + > |
| 31 | + Cancel |
| 32 | + </button> |
| 33 | + <button |
| 34 | + className="px-4 py-2 text-white bg-red-500 rounded" |
| 35 | + onClick={onConfirm} |
| 36 | + > |
| 37 | + Logout |
| 38 | + </button> |
| 39 | + </div> |
| 40 | + </div> |
| 41 | + </div> |
| 42 | + ) : null), |
| 43 | +})); |
| 44 | + |
| 45 | +const TestComponent: React.FC = () => { |
| 46 | + const { openLogoutModal } = useLogout(); |
| 47 | + return <button onClick={openLogoutModal}>Open Logout Modal</button>; |
| 48 | +}; |
| 49 | + |
| 50 | +describe("LogoutProvider Component", () => { |
| 51 | + beforeAll(() => { |
| 52 | + jest.spyOn(console, "error").mockImplementation(() => {}); |
| 53 | + }); |
| 54 | + |
| 55 | + afterAll(() => { |
| 56 | + (console.error as jest.Mock).mockRestore(); |
| 57 | + }); |
| 58 | + |
| 59 | + it("should render LogoutProvider and trigger logout modal", () => { |
| 60 | + render( |
| 61 | + <LogoutProvider> |
| 62 | + <TestComponent /> |
| 63 | + </LogoutProvider>, |
| 64 | + ); |
| 65 | + |
| 66 | + const openModalButton = screen.getByText("Open Logout Modal"); |
| 67 | + fireEvent.click(openModalButton); |
| 68 | + |
| 69 | + // Verify that the modal is rendered |
| 70 | + expect(screen.getByText("Confirm Logout")).toBeInTheDocument(); |
| 71 | + expect( |
| 72 | + screen.getByText("Are you sure you want to logout?"), |
| 73 | + ).toBeInTheDocument(); |
| 74 | + expect(screen.getByText("Cancel")).toBeInTheDocument(); |
| 75 | + expect(screen.getByText("Logout")).toBeInTheDocument(); |
| 76 | + }); |
| 77 | + |
| 78 | + it("should call confirmLogout and perform logout", async () => { |
| 79 | + localStorage.setItem("accessToken", "mockToken"); |
| 80 | + mockPost.mockResolvedValue({}); |
| 81 | + |
| 82 | + render( |
| 83 | + <LogoutProvider> |
| 84 | + <TestComponent /> |
| 85 | + </LogoutProvider>, |
| 86 | + ); |
| 87 | + |
| 88 | + fireEvent.click(screen.getByText("Open Logout Modal")); |
| 89 | + fireEvent.click(screen.getByText("Logout")); |
| 90 | + |
| 91 | + await waitFor(() => { |
| 92 | + expect(mockPost).toHaveBeenCalledWith( |
| 93 | + "/users/logout", |
| 94 | + {}, |
| 95 | + { |
| 96 | + headers: { |
| 97 | + Authorization: `Bearer mockToken`, |
| 98 | + Accept: "*/*", |
| 99 | + }, |
| 100 | + }, |
| 101 | + ); |
| 102 | + expect(localStorage.getItem("accessToken")).toBeNull(); |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + it("should handle logout failure", async () => { |
| 107 | + localStorage.setItem("accessToken", "mockToken"); |
| 108 | + mockPost.mockRejectedValue(new Error("Network error")); |
| 109 | + |
| 110 | + render( |
| 111 | + <LogoutProvider> |
| 112 | + <TestComponent /> |
| 113 | + </LogoutProvider>, |
| 114 | + ); |
| 115 | + |
| 116 | + fireEvent.click(screen.getByText("Open Logout Modal")); |
| 117 | + fireEvent.click(screen.getByText("Logout")); |
| 118 | + |
| 119 | + await waitFor(() => { |
| 120 | + expect(console.error).toHaveBeenCalledWith( |
| 121 | + "Failed to logout:", |
| 122 | + expect.any(Error), |
| 123 | + ); |
| 124 | + }); |
| 125 | + }); |
| 126 | +}); |
0 commit comments