Files
NextEdu/tests/integration/dashboard-routing.test.ts
SpecialX 99f116cb64
Some checks failed
CI / build-deploy (push) Has been cancelled
=test_update_homework_tests_and_work_log
2026-03-19 13:16:49 +08:00

68 lines
2.3 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest"
const mocks = vi.hoisted(() => ({
authMock: vi.fn(),
getUserProfileMock: vi.fn(),
redirectMock: vi.fn((target: string) => {
throw new Error(`REDIRECT:${target}`)
}),
}))
vi.mock("@/auth", () => ({
auth: mocks.authMock,
}))
vi.mock("@/modules/users/data-access", () => ({
getUserProfile: mocks.getUserProfileMock,
}))
vi.mock("next/navigation", () => ({
redirect: mocks.redirectMock,
}))
import DashboardPage from "@/app/(dashboard)/dashboard/page"
describe("dashboard route dispatcher", () => {
beforeEach(() => {
vi.resetAllMocks()
mocks.redirectMock.mockImplementation((target: string) => {
throw new Error(`REDIRECT:${target}`)
})
})
it("redirects to login when session is missing", async () => {
mocks.authMock.mockResolvedValue(null)
await expect(DashboardPage()).rejects.toThrow("REDIRECT:/login")
})
it("redirects to login when user profile is missing", async () => {
mocks.authMock.mockResolvedValue({ user: { id: "u_1" } })
mocks.getUserProfileMock.mockResolvedValue(null)
await expect(DashboardPage()).rejects.toThrow("REDIRECT:/login")
})
it("redirects admin to admin dashboard", async () => {
mocks.authMock.mockResolvedValue({ user: { id: "u_admin" } })
mocks.getUserProfileMock.mockResolvedValue({ role: "admin" })
await expect(DashboardPage()).rejects.toThrow("REDIRECT:/admin/dashboard")
})
it("redirects student to student dashboard", async () => {
mocks.authMock.mockResolvedValue({ user: { id: "u_student" } })
mocks.getUserProfileMock.mockResolvedValue({ role: "student" })
await expect(DashboardPage()).rejects.toThrow("REDIRECT:/student/dashboard")
})
it("redirects parent to parent dashboard", async () => {
mocks.authMock.mockResolvedValue({ user: { id: "u_parent" } })
mocks.getUserProfileMock.mockResolvedValue({ role: "parent" })
await expect(DashboardPage()).rejects.toThrow("REDIRECT:/parent/dashboard")
})
it("falls back to student dashboard when role is unknown", async () => {
mocks.authMock.mockResolvedValue({ user: { id: "u_teacher" } })
mocks.getUserProfileMock.mockResolvedValue({ role: "" })
await expect(DashboardPage()).rejects.toThrow("REDIRECT:/student/dashboard")
})
})