Files
stock-manager/tests/unit/app/people/edit-person-form-wiring.test.ts
T

171 lines
4.4 KiB
TypeScript

import { createElement } from "react"
import { renderToStaticMarkup } from "react-dom/server"
import { beforeEach, describe, expect, it, vi } from "vitest"
import { en } from "@/i18n/dictionaries/en"
import { es } from "@/i18n/dictionaries/es"
import type { PersonWithUser } from "@/services/person.service"
const mocks = vi.hoisted(() => ({
getI18n: vi.fn(),
findByIdWithUser: vi.fn(),
findById: vi.fn(),
personForm: vi.fn(),
push: vi.fn(),
toastError: vi.fn(),
toastSuccess: vi.fn(),
}))
vi.mock("@/i18n/server", () => ({
getI18n: mocks.getI18n,
}))
vi.mock("@/services/person.service", () => ({
PersonService: {
findByIdWithUser: mocks.findByIdWithUser,
findById: mocks.findById,
},
}))
vi.mock("@/app/(dashboard)/people/_components/edit.person.form", () => ({
default: (props: unknown) => {
mocks.personForm(props)
return createElement("div", null, "Edit person form")
},
}))
vi.mock("next/navigation", () => ({
useRouter: () => ({ push: mocks.push }),
redirect: vi.fn(),
}))
vi.mock("@/actions/person.actions", () => ({
updatePersonUserAction: vi.fn(),
}))
vi.mock("sonner", () => ({
toast: {
error: mocks.toastError,
success: mocks.toastSuccess,
},
}))
const basePerson: PersonWithUser = {
id: "person-1",
firstName: "Ada",
lastName: "Lovelace",
department: "ENGINEERING",
email: "ada@example.test",
phone: "1234",
userId: null,
isActive: true,
createdAt: new Date("2024-01-01"),
updatedAt: new Date("2024-01-01"),
user: null,
}
const personWithUser: PersonWithUser = {
...basePerson,
id: "person-2",
userId: "user-1",
user: {
id: "user-1",
name: "Ada Lovelace",
email: "ada@example.test",
role: "ADMIN",
isActive: true,
createdAt: new Date("2024-01-01"),
updatedAt: new Date("2024-01-01"),
password: "hashed",
},
}
describe("edit person page wiring", () => {
beforeEach(() => {
vi.clearAllMocks()
mocks.getI18n.mockResolvedValue({ dictionary: en, locale: "en" })
})
it("loads the person without user, passes PersonWithoutUser to the edit form", async () => {
mocks.findByIdWithUser.mockResolvedValue({ ...basePerson, user: null })
const { default: PersonEditPage } = await import(
"@/app/(dashboard)/people/[personId]/edit/page"
)
renderToStaticMarkup(
await PersonEditPage({
params: Promise.resolve({ personId: "person-1" }),
}),
)
expect(mocks.findByIdWithUser).toHaveBeenCalledWith("person-1")
expect(mocks.personForm).toHaveBeenCalledWith(
expect.objectContaining({
person: expect.objectContaining({
id: "person-1",
user: null,
}),
formCopy: en.admin.users.form,
schemaCopy: {
...en.admin.users.schema,
...en.inventory.people.schema,
},
roleLabels: en.admin.users.roles,
}),
)
})
it("passes Spanish copy in es locale and passes the linked User to the form", async () => {
mocks.getI18n.mockResolvedValue({ dictionary: es, locale: "es" })
mocks.findByIdWithUser.mockResolvedValue(personWithUser)
const { default: PersonEditPage } = await import(
"@/app/(dashboard)/people/[personId]/edit/page"
)
renderToStaticMarkup(
await PersonEditPage({
params: Promise.resolve({ personId: "person-2" }),
}),
)
expect(mocks.personForm).toHaveBeenCalledWith(
expect.objectContaining({
person: expect.objectContaining({
id: "person-2",
user: expect.objectContaining({
id: "user-1",
role: "ADMIN",
isActive: true,
}),
}),
formCopy: es.admin.users.form,
roleLabels: es.admin.users.roles,
departmentCopy: es.inventory.people.departments,
fallbackCopy: expect.objectContaining({
unknownDepartment: es.inventory.people.fallback.unknownDepartment,
}),
}),
)
})
it("renders 'Person not found' in Spanish when person does not exist", async () => {
mocks.getI18n.mockResolvedValue({ dictionary: es, locale: "es" })
mocks.findByIdWithUser.mockResolvedValue(null)
const { default: PersonEditPage } = await import(
"@/app/(dashboard)/people/[personId]/edit/page"
)
const html = renderToStaticMarkup(
await PersonEditPage({
params: Promise.resolve({ personId: "missing" }),
}),
)
expect(html).toContain("Persona no encontrada")
expect(mocks.personForm).not.toHaveBeenCalled()
})
})