feat: unify Person and User creation form with conditional password
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
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(() => ({
|
||||
createPersonUser: vi.fn(),
|
||||
getI18n: vi.fn(),
|
||||
push: vi.fn(),
|
||||
toastError: vi.fn(),
|
||||
toastSuccess: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock("@/i18n/server", () => ({
|
||||
getI18n: mocks.getI18n,
|
||||
}))
|
||||
|
||||
vi.mock("@/actions/person.actions", () => ({
|
||||
createPersonUserAction: mocks.createPersonUser,
|
||||
}))
|
||||
|
||||
vi.mock("@/services/person.service", () => ({
|
||||
PersonService: {
|
||||
findById: vi.fn(),
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock("next/navigation", () => ({
|
||||
useRouter: () => ({
|
||||
push: mocks.push,
|
||||
}),
|
||||
}))
|
||||
|
||||
vi.mock("sonner", () => ({
|
||||
toast: {
|
||||
error: mocks.toastError,
|
||||
success: mocks.toastSuccess,
|
||||
},
|
||||
}))
|
||||
|
||||
describe("unified creation form page", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
mocks.getI18n.mockResolvedValue({ dictionary: es, locale: "es" })
|
||||
})
|
||||
|
||||
it("renders unified form with Person fields, email, password, role, and NO_USER option in Spanish", async () => {
|
||||
const { default: NewUserPage } = await import(
|
||||
"@/app/(dashboard)/admin/users/new/page"
|
||||
)
|
||||
|
||||
const html = renderToStaticMarkup(await NewUserPage())
|
||||
|
||||
// Person fields
|
||||
expect(html).toContain("Nombre")
|
||||
expect(html).toContain("Apellido")
|
||||
expect(html).toContain("Departamento")
|
||||
expect(html).toContain("Teléfono")
|
||||
|
||||
// User fields
|
||||
expect(html).toContain("Correo electrónico")
|
||||
expect(html).toContain("Contraseña")
|
||||
expect(html).toContain("Rol")
|
||||
|
||||
// NO_USER role option
|
||||
expect(html).toContain("Sin cuenta de usuario")
|
||||
expect(html).toContain('value="NO_USER"')
|
||||
|
||||
// Other role options still present
|
||||
expect(html).toContain('value="ADMIN"')
|
||||
expect(html).toContain('value="MANAGER"')
|
||||
expect(html).toContain('value="STAFF"')
|
||||
expect(html).toContain('value="VIEWER"')
|
||||
})
|
||||
|
||||
it("renders unified form with Person fields and NO_USER option in English", async () => {
|
||||
mocks.getI18n.mockResolvedValue({ dictionary: en, locale: "en" })
|
||||
|
||||
const { default: NewUserPage } = await import(
|
||||
"@/app/(dashboard)/admin/users/new/page"
|
||||
)
|
||||
|
||||
const html = renderToStaticMarkup(await NewUserPage())
|
||||
|
||||
// Person fields
|
||||
expect(html).toContain("First Name")
|
||||
expect(html).toContain("Last Name")
|
||||
expect(html).toContain("Department")
|
||||
expect(html).toContain("Phone")
|
||||
|
||||
// User fields
|
||||
expect(html).toContain("Password")
|
||||
expect(html).toContain("Email")
|
||||
|
||||
// NO_USER role option
|
||||
expect(html).toContain("No user account")
|
||||
expect(html).toContain('value="NO_USER"')
|
||||
})
|
||||
|
||||
it("renders Person field placeholders from the unified form dictionary", async () => {
|
||||
const { default: NewUserPage } = await import(
|
||||
"@/app/(dashboard)/admin/users/new/page"
|
||||
)
|
||||
|
||||
const html = renderToStaticMarkup(await NewUserPage())
|
||||
|
||||
// Person field placeholders
|
||||
expect(html).toContain('placeholder="Nombre"') // firstNamePlaceholder (es)
|
||||
expect(html).toContain('placeholder="Apellido"') // lastNamePlaceholder (es)
|
||||
expect(html).toContain("Selecciona un departamento") // departmentPlaceholder
|
||||
expect(html).toContain('placeholder="Teléfono"') // phonePlaceholder (es)
|
||||
})
|
||||
|
||||
it("renders department select with all PERSON_DEPARTMENTS values", async () => {
|
||||
const { default: NewUserPage } = await import(
|
||||
"@/app/(dashboard)/admin/users/new/page"
|
||||
)
|
||||
|
||||
const html = renderToStaticMarkup(await NewUserPage())
|
||||
|
||||
// Department values must use canonical enum values
|
||||
expect(html).toContain('value="ADMINISTRATION"')
|
||||
})
|
||||
})
|
||||
@@ -5,9 +5,7 @@ import { en } from "@/i18n/dictionaries/en"
|
||||
import { es } from "@/i18n/dictionaries/es"
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
createUserAction: vi.fn(),
|
||||
updateUserAction: vi.fn(),
|
||||
resetUserPasswordAction: vi.fn(),
|
||||
createPersonUser: vi.fn(),
|
||||
getUserProfileById: vi.fn(),
|
||||
getI18n: vi.fn(),
|
||||
push: vi.fn(),
|
||||
@@ -19,10 +17,19 @@ vi.mock("@/i18n/server", () => ({
|
||||
getI18n: mocks.getI18n,
|
||||
}))
|
||||
|
||||
vi.mock("@/actions/person.actions", () => ({
|
||||
createPersonUserAction: mocks.createPersonUser,
|
||||
}))
|
||||
|
||||
vi.mock("@/actions/user.actions", () => ({
|
||||
createUserAction: mocks.createUserAction,
|
||||
updateUserAction: mocks.updateUserAction,
|
||||
resetUserPasswordAction: mocks.resetUserPasswordAction,
|
||||
updateUserAction: vi.fn(),
|
||||
resetUserPasswordAction: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock("@/services/person.service", () => ({
|
||||
PersonService: {
|
||||
findById: vi.fn(),
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock("@/services/user.service", () => ({
|
||||
@@ -51,18 +58,7 @@ describe("new user form localization", () => {
|
||||
mocks.getI18n.mockResolvedValue({ dictionary: es, locale: "es" })
|
||||
})
|
||||
|
||||
it("passes server-resolved schema and role copy into the new user form boundary", async () => {
|
||||
const { default: NewUserPage } = await import(
|
||||
"@/app/(dashboard)/admin/users/new/page"
|
||||
)
|
||||
|
||||
renderToStaticMarkup(await NewUserPage())
|
||||
|
||||
// The page must pass formCopy, schemaCopy, and roleLabels to the form
|
||||
// We verify this by checking the form renders localized content
|
||||
})
|
||||
|
||||
it("renders new user page with localized title and form labels in Spanish", async () => {
|
||||
it("renders new user page with localized title and unified form labels in Spanish", async () => {
|
||||
const { default: NewUserPage } = await import(
|
||||
"@/app/(dashboard)/admin/users/new/page"
|
||||
)
|
||||
@@ -72,27 +68,29 @@ describe("new user form localization", () => {
|
||||
// Title
|
||||
expect(html).toContain("Nuevo usuario")
|
||||
|
||||
// Form labels from dictionary
|
||||
// Person field labels
|
||||
expect(html).toContain("Nombre")
|
||||
expect(html).toContain("Apellido")
|
||||
expect(html).toContain("Departamento")
|
||||
expect(html).toContain("Teléfono")
|
||||
|
||||
// User field labels
|
||||
expect(html).toContain("Correo electrónico")
|
||||
expect(html).toContain("Contraseña")
|
||||
expect(html).toContain("Rol")
|
||||
|
||||
// Placeholders from dictionary
|
||||
expect(html).toContain("Nombre completo")
|
||||
expect(html).toContain("Mínimo 8 caracteres")
|
||||
|
||||
// Role labels (display) with canonical values
|
||||
expect(html).toContain("Administrador")
|
||||
expect(html).toContain("Gerente")
|
||||
expect(html).toContain("Personal")
|
||||
expect(html).toContain("Visor")
|
||||
expect(html).toContain("Sin cuenta de usuario")
|
||||
|
||||
// Submit button text
|
||||
expect(html).toContain("Crear usuario")
|
||||
})
|
||||
|
||||
it("renders new user page with English form labels in English locale", async () => {
|
||||
it("renders new user page with English unified form labels in English locale", async () => {
|
||||
mocks.getI18n.mockResolvedValue({ dictionary: en, locale: "en" })
|
||||
|
||||
const { default: NewUserPage } = await import(
|
||||
@@ -102,17 +100,27 @@ describe("new user form localization", () => {
|
||||
const html = renderToStaticMarkup(await NewUserPage())
|
||||
|
||||
expect(html).toContain("New User")
|
||||
expect(html).toContain("Full name")
|
||||
|
||||
// Person fields
|
||||
expect(html).toContain("First Name")
|
||||
expect(html).toContain("Last Name")
|
||||
expect(html).toContain("Department")
|
||||
expect(html).toContain("Phone")
|
||||
|
||||
// User fields
|
||||
expect(html).toContain("Email")
|
||||
expect(html).toContain("Password")
|
||||
expect(html).toContain("Minimum 8 characters")
|
||||
expect(html).toContain("Role")
|
||||
|
||||
expect(html).toContain("Create User")
|
||||
expect(html).toContain("Admin")
|
||||
expect(html).toContain("Manager")
|
||||
expect(html).toContain("Staff")
|
||||
expect(html).toContain("Viewer")
|
||||
expect(html).toContain("No user account")
|
||||
})
|
||||
|
||||
it("keeps canonical role values in option value attributes, not localized labels", async () => {
|
||||
it("keeps canonical role values in option value attributes including NO_USER, not localized labels", async () => {
|
||||
const { default: NewUserPage } = await import(
|
||||
"@/app/(dashboard)/admin/users/new/page"
|
||||
)
|
||||
@@ -124,6 +132,7 @@ describe("new user form localization", () => {
|
||||
expect(html).toContain('value="MANAGER"')
|
||||
expect(html).toContain('value="STAFF"')
|
||||
expect(html).toContain('value="VIEWER"')
|
||||
expect(html).toContain('value="NO_USER"')
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { describe, expect, it } from "vitest"
|
||||
|
||||
import { formatUserRole } from "@/app/(dashboard)/admin/users/_components/user.copy"
|
||||
import {
|
||||
formatPersonDepartment,
|
||||
formatUserRole,
|
||||
} from "@/app/(dashboard)/admin/users/_components/user.copy"
|
||||
|
||||
describe("user copy helpers", () => {
|
||||
const roleCopy = {
|
||||
@@ -34,3 +37,47 @@ describe("user copy helpers", () => {
|
||||
).toBe("Rol desconocido")
|
||||
})
|
||||
})
|
||||
|
||||
describe("formatPersonDepartment helper", () => {
|
||||
const departmentCopy = {
|
||||
IT: "IT",
|
||||
ENGINEERING: "Ingeniería",
|
||||
LOGISTICS: "Logística",
|
||||
TRAFFIC: "Tráfico",
|
||||
DRIVER: "Chofer",
|
||||
ADMINISTRATION: "Administración",
|
||||
SALES: "Ventas",
|
||||
OTHER: "Otro",
|
||||
}
|
||||
|
||||
const fallbackCopy = {
|
||||
unknownDepartment: "Departamento desconocido",
|
||||
}
|
||||
|
||||
it("formats known department values with localized labels", () => {
|
||||
expect(
|
||||
formatPersonDepartment("ENGINEERING", departmentCopy, fallbackCopy),
|
||||
).toBe("Ingeniería")
|
||||
expect(
|
||||
formatPersonDepartment("ADMINISTRATION", departmentCopy, fallbackCopy),
|
||||
).toBe("Administración")
|
||||
})
|
||||
|
||||
it("falls back for unknown department values", () => {
|
||||
expect(
|
||||
formatPersonDepartment("UNKNOWN_DEPT", departmentCopy, fallbackCopy),
|
||||
).toBe("Departamento desconocido")
|
||||
})
|
||||
|
||||
it("falls back for null department values", () => {
|
||||
expect(formatPersonDepartment(null, departmentCopy, fallbackCopy)).toBe(
|
||||
"Departamento desconocido",
|
||||
)
|
||||
})
|
||||
|
||||
it("falls back for undefined department values", () => {
|
||||
expect(
|
||||
formatPersonDepartment(undefined, departmentCopy, fallbackCopy),
|
||||
).toBe("Departamento desconocido")
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user