111 lines
2.6 KiB
TypeScript
111 lines
2.6 KiB
TypeScript
import { renderToStaticMarkup } from "react-dom/server"
|
|
import { beforeEach, describe, expect, it, vi } from "vitest"
|
|
|
|
import { es } from "@/i18n/dictionaries/es"
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
getI18n: vi.fn(),
|
|
findByIdWithUser: vi.fn(),
|
|
findById: vi.fn(),
|
|
redirect: vi.fn(),
|
|
personForm: vi.fn(),
|
|
}))
|
|
|
|
vi.mock("@/i18n/server", () => ({
|
|
getI18n: mocks.getI18n,
|
|
}))
|
|
|
|
vi.mock("@/services/person.service", () => ({
|
|
PersonService: {
|
|
findByIdWithUser: mocks.findByIdWithUser,
|
|
findById: mocks.findById,
|
|
},
|
|
}))
|
|
|
|
vi.mock("next/navigation", () => ({
|
|
redirect: mocks.redirect,
|
|
useRouter: () => ({
|
|
push: vi.fn(),
|
|
}),
|
|
}))
|
|
|
|
vi.mock("@/actions/person.actions", () => ({
|
|
createNewPerson: vi.fn(),
|
|
updatePerson: vi.fn(),
|
|
updatePersonUserAction: vi.fn(),
|
|
}))
|
|
|
|
vi.mock("@/app/(dashboard)/people/_components/edit.person.form", () => ({
|
|
default: (props: unknown) => {
|
|
mocks.personForm(props)
|
|
return null
|
|
},
|
|
}))
|
|
|
|
vi.mock("sonner", () => ({
|
|
toast: {
|
|
error: vi.fn(),
|
|
success: vi.fn(),
|
|
},
|
|
}))
|
|
|
|
describe("person pages", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
mocks.getI18n.mockResolvedValue({ dictionary: es, locale: "es" })
|
|
})
|
|
|
|
it("renders the edit person page with Person heading and passes person to unified form", async () => {
|
|
const { default: PersonEditPage } = await import(
|
|
"@/app/(dashboard)/people/[personId]/edit/page"
|
|
)
|
|
|
|
mocks.findByIdWithUser.mockResolvedValue({
|
|
id: "person-1",
|
|
firstName: "Ada",
|
|
lastName: "Lovelace",
|
|
email: "ada@example.test",
|
|
phone: "1234",
|
|
department: "ENGINEERING",
|
|
userId: null,
|
|
isActive: true,
|
|
createdAt: new Date("2024-01-01"),
|
|
updatedAt: new Date("2024-01-01"),
|
|
user: null,
|
|
})
|
|
|
|
const html = renderToStaticMarkup(
|
|
await PersonEditPage({
|
|
params: Promise.resolve({ personId: "person-1" }),
|
|
}),
|
|
)
|
|
|
|
expect(html).toContain("Editar persona")
|
|
expect(mocks.personForm).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
person: expect.objectContaining({
|
|
id: "person-1",
|
|
firstName: "Ada",
|
|
lastName: "Lovelace",
|
|
}),
|
|
}),
|
|
)
|
|
})
|
|
|
|
it("renders a Person not-found message on edit page", async () => {
|
|
const { default: PersonEditPage } = await import(
|
|
"@/app/(dashboard)/people/[personId]/edit/page"
|
|
)
|
|
|
|
mocks.findByIdWithUser.mockResolvedValue(null)
|
|
|
|
const html = renderToStaticMarkup(
|
|
await PersonEditPage({
|
|
params: Promise.resolve({ personId: "missing-person" }),
|
|
}),
|
|
)
|
|
|
|
expect(html).toContain("Persona no encontrada")
|
|
})
|
|
})
|