feat: unify Person and User creation form with conditional password
This commit is contained in:
@@ -1,17 +1,12 @@
|
||||
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"
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
getI18n: vi.fn(),
|
||||
findById: vi.fn(),
|
||||
createNewPerson: vi.fn(),
|
||||
updatePerson: vi.fn(),
|
||||
push: vi.fn(),
|
||||
toastError: vi.fn(),
|
||||
toastSuccess: vi.fn(),
|
||||
redirect: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock("@/i18n/server", () => ({
|
||||
@@ -24,52 +19,39 @@ vi.mock("@/services/person.service", () => ({
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock("@/actions/person.actions", () => ({
|
||||
createNewPerson: mocks.createNewPerson,
|
||||
updatePerson: mocks.updatePerson,
|
||||
vi.mock("next/navigation", () => ({
|
||||
redirect: mocks.redirect,
|
||||
useRouter: () => ({
|
||||
push: vi.fn(),
|
||||
}),
|
||||
}))
|
||||
|
||||
vi.mock("next/navigation", () => ({
|
||||
useRouter: () => ({
|
||||
push: mocks.push,
|
||||
}),
|
||||
vi.mock("@/actions/person.actions", () => ({
|
||||
createNewPerson: vi.fn(),
|
||||
updatePerson: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock("sonner", () => ({
|
||||
toast: {
|
||||
error: mocks.toastError,
|
||||
success: mocks.toastSuccess,
|
||||
error: vi.fn(),
|
||||
success: vi.fn(),
|
||||
},
|
||||
}))
|
||||
|
||||
describe("person form pages", () => {
|
||||
describe("person pages", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
mocks.getI18n.mockResolvedValue({ dictionary: es, locale: "es" })
|
||||
})
|
||||
|
||||
it("renders the new person page with Person form copy and no username field", async () => {
|
||||
it("redirects /people/new to /admin/users/new", async () => {
|
||||
const { default: NewPersonPage } = await import(
|
||||
"@/app/(dashboard)/people/new/page"
|
||||
)
|
||||
|
||||
const html = renderToStaticMarkup(await NewPersonPage())
|
||||
await NewPersonPage()
|
||||
|
||||
// Person form, not Recipient
|
||||
expect(html).toContain("Agregar persona")
|
||||
// No username label or placeholder
|
||||
expect(html).not.toContain("Usuario")
|
||||
expect(html).not.toContain('placeholder="Usuario"')
|
||||
// Has expected person form fields
|
||||
expect(html).toContain("Nombre")
|
||||
expect(html).toContain("Apellido")
|
||||
expect(html).toContain("Selecciona un departamento")
|
||||
expect(html).toContain('option value="ENGINEERING"')
|
||||
expect(html).toContain(">Ingeniería</option>")
|
||||
// Has department options from PERSON_DEPARTMENTS
|
||||
expect(html).toContain("Correo electrónico")
|
||||
expect(html).toContain("Teléfono")
|
||||
expect(html).toContain("Crear persona")
|
||||
expect(mocks.redirect).toHaveBeenCalledWith("/admin/users/new")
|
||||
})
|
||||
|
||||
it("renders the edit person page with Person heading and no username", async () => {
|
||||
@@ -112,16 +94,4 @@ describe("person form pages", () => {
|
||||
|
||||
expect(html).toContain("Persona no encontrada")
|
||||
})
|
||||
|
||||
it("wires English Person form submit copy through the new page", async () => {
|
||||
const { default: NewPersonPage } = await import(
|
||||
"@/app/(dashboard)/people/new/page"
|
||||
)
|
||||
|
||||
mocks.getI18n.mockResolvedValueOnce({ dictionary: en, locale: "en" })
|
||||
|
||||
const html = renderToStaticMarkup(await NewPersonPage())
|
||||
|
||||
expect(html).toContain("Create Person")
|
||||
})
|
||||
})
|
||||
|
||||
@@ -3,7 +3,6 @@ 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"
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
getI18n: vi.fn(),
|
||||
@@ -33,23 +32,6 @@ describe("person form schema wiring", () => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it("passes server-resolved Person schema copy into the new person form boundary", async () => {
|
||||
mocks.getI18n.mockResolvedValue({ dictionary: es, locale: "es" })
|
||||
|
||||
const { default: NewPersonPage } = await import(
|
||||
"@/app/(dashboard)/people/new/page"
|
||||
)
|
||||
|
||||
renderToStaticMarkup(await NewPersonPage())
|
||||
|
||||
expect(mocks.personForm).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
mode: "create",
|
||||
schemaCopy: es.inventory.people.schema,
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it("passes server-resolved Person schema copy into the edit person form boundary", async () => {
|
||||
mocks.getI18n.mockResolvedValue({ dictionary: en, locale: "en" })
|
||||
mocks.findById.mockResolvedValue({
|
||||
|
||||
Reference in New Issue
Block a user