-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Unit Test for SigninWithRds component
- Loading branch information
Showing
1 changed file
with
65 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { cleanup, fireEvent, render, screen } from "@testing-library/react" | ||
import { AppRouterInstance } from "next/dist/shared/lib/app-router-context.shared-runtime" | ||
import { useRouter } from "next/navigation" | ||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest" | ||
|
||
import { SignInWithRds } from "@/components/signin-with-rds" | ||
import { TGlobalStoreUser, useGlobalStore } from "@/store/global-store" | ||
|
||
// Mock the next/navigation module | ||
vi.mock("next/navigation", () => ({ | ||
useRouter: vi.fn(), | ||
})) | ||
|
||
// Mock the global store | ||
vi.mock("@/store/global-store", () => ({ | ||
useGlobalStore: vi.fn(), | ||
})) | ||
|
||
describe("SignInWithRds", () => { | ||
const mockPush = vi.fn() | ||
|
||
beforeEach(() => { | ||
vi.mocked(useRouter).mockReturnValue({ | ||
push: mockPush, | ||
} as unknown as AppRouterInstance) | ||
}) | ||
|
||
afterEach(() => { | ||
cleanup() | ||
vi.clearAllMocks() | ||
}) | ||
|
||
it("renders sign-in button for unauthenticated user", () => { | ||
vi.mocked(useGlobalStore).mockReturnValue({ | ||
user: null, | ||
} as unknown as TGlobalStoreUser) | ||
|
||
render(<SignInWithRds />) | ||
|
||
expect(screen.getByText("SignIn within rds")).toBeDefined() | ||
}) | ||
|
||
it("handles sign-in button click", () => { | ||
vi.mocked(useGlobalStore).mockReturnValue({ | ||
user: null, | ||
} as unknown as TGlobalStoreUser) | ||
|
||
render(<SignInWithRds />) | ||
|
||
fireEvent.click(screen.getByText("SignIn within rds")) | ||
|
||
expect(mockPush).toHaveBeenCalledWith(expect.stringContaining("/auth/github/login?redirectURL=")) | ||
}) | ||
|
||
it("renders user info for authenticated user", () => { | ||
const mockUser = { name: "John Doe", profilePicture: "/path/to/image.jpg" } | ||
vi.mocked(useGlobalStore).mockReturnValue({ | ||
user: mockUser, | ||
} as unknown as TGlobalStoreUser) | ||
|
||
render(<SignInWithRds />) | ||
expect(screen.getByText("John Doe")).toBeDefined() | ||
expect(screen.getByRole("img")).toBeDefined() | ||
}) | ||
}) |