-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from atlp-rwanda/ft-user-logout-#187984515
User: Auth logout #187984515
- Loading branch information
Showing
22 changed files
with
1,002 additions
and
247 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import "@testing-library/jest-dom"; | ||
import { render, screen } from "@testing-library/react"; | ||
import { MemoryRouter, Routes, Route } from "react-router-dom"; | ||
|
||
import HomeButton from "../components/dashboard/HomeButton"; | ||
|
||
describe("HomeButton", () => { | ||
it("should render the Home button", () => { | ||
render( | ||
<MemoryRouter initialEntries={["/test"]}> | ||
<Routes> | ||
<Route path="/test" element={<HomeButton />} /> | ||
</Routes> | ||
</MemoryRouter>, | ||
); | ||
|
||
const homeButton = screen.getByText("Home"); | ||
expect(homeButton).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import "@testing-library/jest-dom"; | ||
import React from "react"; | ||
import { render, screen, fireEvent } from "@testing-library/react"; | ||
|
||
import { | ||
LogoutProvider, | ||
useLogout, | ||
} from "../components/dashboard/admin/LogoutContext"; | ||
|
||
// Mock LogoutModal component | ||
jest.mock("../components/dashboard/admin/LogoutModal", () => ({ | ||
__esModule: true, | ||
default: ({ isOpen, onClose, onConfirm }: any) => | ||
(isOpen ? ( | ||
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black bg-opacity-50"> | ||
<div className="p-6 bg-white rounded shadow-lg"> | ||
<h2 className="mb-4 text-xl">Confirm Logout</h2> | ||
<p className="mb-4">Are you sure you want to logout?</p> | ||
<div className="flex justify-end gap-4"> | ||
<button | ||
className="px-4 py-2 text-white bg-gray-500 rounded" | ||
onClick={onClose} | ||
> | ||
Cancel | ||
</button> | ||
<button | ||
className="px-4 py-2 text-white bg-red-500 rounded" | ||
onClick={onConfirm} | ||
> | ||
Logout | ||
</button> | ||
</div> | ||
</div> | ||
</div> | ||
) : null), | ||
})); | ||
|
||
const TestComponent: React.FC = () => { | ||
const { openLogoutModal } = useLogout(); | ||
return <button onClick={openLogoutModal}>Open Logout Modal</button>; | ||
}; | ||
|
||
describe("LogoutProvider Component", () => { | ||
it("should render LogoutProvider and trigger logout modal", () => { | ||
render( | ||
<LogoutProvider> | ||
<TestComponent /> | ||
</LogoutProvider>, | ||
); | ||
|
||
const openModalButton = screen.getByText("Open Logout Modal"); | ||
fireEvent.click(openModalButton); | ||
|
||
// Verify that the modal is rendered | ||
expect(screen.getByText("Confirm Logout")).toBeInTheDocument(); | ||
expect( | ||
screen.getByText("Are you sure you want to logout?"), | ||
).toBeInTheDocument(); | ||
expect(screen.getByText("Cancel")).toBeInTheDocument(); | ||
expect(screen.getByText("Logout")).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import "@testing-library/jest-dom"; | ||
import React from "react"; | ||
import { render, screen, fireEvent } from "@testing-library/react"; | ||
import { BrowserRouter as Router } from "react-router-dom"; | ||
|
||
import { useLogout } from "../components/dashboard/admin/LogoutContext"; // Ensure the correct path | ||
import ProfileDropdown from "../components/common/ProfileDropdown"; | ||
|
||
// Mock the useLogout hook | ||
jest.mock("../components/dashboard/admin/LogoutContext", () => ({ | ||
useLogout: jest.fn(), | ||
})); | ||
|
||
describe("ProfileDropdown", () => { | ||
const openLogoutModalMock = jest.fn(); | ||
|
||
beforeEach(() => { | ||
(useLogout as jest.Mock).mockReturnValue({ | ||
openLogoutModal: openLogoutModalMock, | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should render profile and orders links for a user with roleId 1", () => { | ||
const userInfo = { roleId: 1 }; | ||
|
||
render( | ||
<Router> | ||
<ProfileDropdown userInfo={userInfo} /> | ||
</Router>, | ||
); | ||
|
||
expect(screen.getByText("Profile")).toBeInTheDocument(); | ||
expect(screen.getByText("My Orders")).toBeInTheDocument(); | ||
expect(screen.getByText("Logout")).toBeInTheDocument(); | ||
}); | ||
|
||
it("should render the correct dashboard link for roleId 2", () => { | ||
const userInfo = { roleId: 2 }; | ||
|
||
render( | ||
<Router> | ||
<ProfileDropdown userInfo={userInfo} /> | ||
</Router>, | ||
); | ||
|
||
expect(screen.getByText("My Dashboard")).toBeInTheDocument(); | ||
expect(screen.getByRole("link", { name: /My Dashboard/i })).toHaveAttribute( | ||
"href", | ||
"/dashboard", | ||
); | ||
}); | ||
|
||
it("should render the correct dashboard link for roleId 3", () => { | ||
const userInfo = { roleId: 3 }; | ||
|
||
render( | ||
<Router> | ||
<ProfileDropdown userInfo={userInfo} /> | ||
</Router>, | ||
); | ||
|
||
expect(screen.getByText("My Dashboard")).toBeInTheDocument(); | ||
expect(screen.getByRole("link", { name: /My Dashboard/i })).toHaveAttribute( | ||
"href", | ||
"/admin/dashboard", | ||
); | ||
}); | ||
|
||
it("should call openLogoutModal when logout button is clicked", () => { | ||
const userInfo = { roleId: 1 }; | ||
|
||
render( | ||
<Router> | ||
<ProfileDropdown userInfo={userInfo} /> | ||
</Router>, | ||
); | ||
|
||
const logoutButton = screen.getByText("Logout"); | ||
fireEvent.click(logoutButton); | ||
|
||
expect(openLogoutModalMock).toHaveBeenCalled(); | ||
}); | ||
|
||
it("should render login link if userInfo is not provided", () => { | ||
render( | ||
<Router> | ||
<ProfileDropdown userInfo={null} /> | ||
</Router>, | ||
); | ||
|
||
expect(screen.getByText("Login")).toBeInTheDocument(); | ||
}); | ||
}); |
Oops, something went wrong.