Skip to content

Commit

Permalink
Added Unit Test for SigninWithRds component
Browse files Browse the repository at this point in the history
  • Loading branch information
sunilk429 committed Oct 9, 2024
1 parent d511888 commit b613056
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/__tests__/unit/components/SignInWithRds.test.tsx
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()
})
})

0 comments on commit b613056

Please sign in to comment.