refactor: consolidate admin/users management under /people

This commit is contained in:
2026-06-17 09:32:26 +02:00
parent 4f370eee70
commit d6b42d78e7
31 changed files with 1928 additions and 855 deletions
@@ -1,227 +0,0 @@
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(),
getUserProfileById: 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("@/actions/user.actions", () => ({
updateUserAction: vi.fn(),
resetUserPasswordAction: vi.fn(),
}))
vi.mock("@/services/person.service", () => ({
PersonService: {
findById: vi.fn(),
},
}))
vi.mock("@/services/user.service", () => ({
getUserProfileById: mocks.getUserProfileById,
}))
vi.mock("next/navigation", () => ({
useRouter: () => ({
push: mocks.push,
}),
notFound: () => {
throw new Error("NOT_FOUND")
},
}))
vi.mock("sonner", () => ({
toast: {
error: mocks.toastError,
success: mocks.toastSuccess,
},
}))
describe("new user form localization", () => {
beforeEach(() => {
vi.clearAllMocks()
mocks.getI18n.mockResolvedValue({ dictionary: es, locale: "es" })
})
it("renders new user page with localized title and unified form labels in Spanish", async () => {
const { default: NewUserPage } = await import(
"@/app/(dashboard)/people/new/page"
)
const html = renderToStaticMarkup(await NewUserPage())
// Title
expect(html).toContain("Nueva persona")
// 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")
// 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 unified form labels in English locale", async () => {
mocks.getI18n.mockResolvedValue({ dictionary: en, locale: "en" })
const { default: NewUserPage } = await import(
"@/app/(dashboard)/people/new/page"
)
const html = renderToStaticMarkup(await NewUserPage())
expect(html).toContain("New Person")
// 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("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 including NO_USER, not localized labels", async () => {
const { default: NewUserPage } = await import(
"@/app/(dashboard)/people/new/page"
)
const html = renderToStaticMarkup(await NewUserPage())
// Canonical values must be in value attributes
expect(html).toContain('value="ADMIN"')
expect(html).toContain('value="MANAGER"')
expect(html).toContain('value="STAFF"')
expect(html).toContain('value="VIEWER"')
expect(html).toContain('value="NO_USER"')
})
})
describe("edit user form localization", () => {
beforeEach(() => {
vi.clearAllMocks()
mocks.getI18n.mockResolvedValue({ dictionary: es, locale: "es" })
mocks.getUserProfileById.mockResolvedValue({
id: "user-1",
name: "Ada Lovelace",
email: "ada@example.test",
role: "ADMIN",
isActive: true,
})
})
it("renders edit user page with localized title, form labels, and reset-password section in Spanish", async () => {
const { default: EditUserPage } = await import(
"@/app/(dashboard)/admin/users/[userId]/edit/page"
)
const html = renderToStaticMarkup(
await EditUserPage({
params: Promise.resolve({ userId: "user-1" }),
}),
)
// Title
expect(html).toContain("Editar usuario")
// Form labels
expect(html).toContain("Nombre")
expect(html).toContain("Nueva contraseña")
// Role labels with canonical values
expect(html).toContain("Administrador")
expect(html).toContain('value="ADMIN"')
// Active user checkbox label
expect(html).toContain("Usuario activo")
// Submit button
expect(html).toContain("Actualizar usuario")
// Reset password section
expect(html).toContain("Restablecer contraseña")
expect(html).toContain("Nueva contraseña")
expect(html).toContain("Mínimo 8 caracteres")
})
it("renders edit user page with English labels in English locale", async () => {
mocks.getI18n.mockResolvedValue({ dictionary: en, locale: "en" })
const { default: EditUserPage } = await import(
"@/app/(dashboard)/admin/users/[userId]/edit/page"
)
const html = renderToStaticMarkup(
await EditUserPage({
params: Promise.resolve({ userId: "user-1" }),
}),
)
expect(html).toContain("Edit User")
expect(html).toContain("Active user")
expect(html).toContain("Update User")
expect(html).toContain("Reset password")
expect(html).toContain("New password")
expect(html).toContain("Reset Password")
})
it("renders edit user form with role option values as canonical enums regardless of locale", async () => {
const { default: EditUserPage } = await import(
"@/app/(dashboard)/admin/users/[userId]/edit/page"
)
const html = renderToStaticMarkup(
await EditUserPage({
params: Promise.resolve({ userId: "user-1" }),
}),
)
// Canonical role values
expect(html).toContain('value="ADMIN"')
expect(html).toContain('value="MANAGER"')
expect(html).toContain('value="STAFF"')
expect(html).toContain('value="VIEWER"')
// Spanish labels for roles
expect(html).toContain("Administrador")
})
})
-183
View File
@@ -1,183 +0,0 @@
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 { getUsers as _getUsers } from "@/services/user.service"
type UserData = Awaited<ReturnType<typeof _getUsers>>["data"][number]
const mocks = vi.hoisted(() => ({
getUsers: vi.fn(),
getI18n: vi.fn(),
}))
vi.mock("@/i18n/server", () => ({
getI18n: mocks.getI18n,
}))
vi.mock("@/services/user.service", () => ({
getUsers: mocks.getUsers,
}))
vi.mock("@/components/common/pageheader", () => ({
default: ({ title, addLabel }: { title?: string; addLabel?: string }) =>
createElement(
"header",
null,
[title, addLabel].filter(Boolean).join(" | "),
),
}))
vi.mock("@/components/common/pagination", () => ({
default: ({ totalPages }: { totalPages: number }) =>
createElement("nav", { "aria-label": "Pagination" }, totalPages),
}))
describe("user pages localization", () => {
beforeEach(() => {
vi.clearAllMocks()
mocks.getI18n.mockResolvedValue({ dictionary: es, locale: "es" })
})
it("renders the user list in Spanish with localized headers, status/role labels, and unchanged user data", async () => {
const { default: UsersPage } = await import(
"@/app/(dashboard)/admin/users/page"
)
mocks.getUsers.mockResolvedValue({
data: [
{
id: "user-1",
name: "Ada Lovelace",
email: "ada@example.test",
role: "ADMIN",
isActive: true,
},
{
id: "user-2",
name: "Grace Hopper",
email: "grace@example.test",
role: "STAFF",
isActive: false,
},
],
totalPages: 1,
})
const html = renderToStaticMarkup(
await UsersPage({ searchParams: Promise.resolve({}) }),
)
// Title and add label
expect(html).toContain("Usuarios")
expect(html).toContain("Agregar usuario")
// Table headers from dictionary
expect(html).toContain("Nombre")
expect(html).toContain("Correo electrónico")
expect(html).toContain("Rol")
expect(html).toContain("Estado")
expect(html).toContain("Acciones")
// Status labels from dictionary (display-only, not canonical)
expect(html).toContain("Activo")
expect(html).toContain("Inactivo")
// Role labels from dictionary (display-only, not canonical)
expect(html).toContain("Administrador")
expect(html).toContain("Personal")
// User data is never translated
expect(html).toContain("Ada Lovelace")
expect(html).toContain("ada@example.test")
expect(html).toContain("Grace Hopper")
expect(html).toContain("grace@example.test")
// Canonical role values must NOT appear as display text
expect(html).not.toContain(">ADMIN<")
expect(html).not.toContain(">STAFF<")
})
it("renders the localized user empty state when no users exist", async () => {
const { default: UsersPage } = await import(
"@/app/(dashboard)/admin/users/page"
)
mocks.getUsers.mockResolvedValue({
data: [],
totalPages: 0,
})
const html = renderToStaticMarkup(
await UsersPage({ searchParams: Promise.resolve({}) }),
)
expect(html).toContain("No se encontraron usuarios.")
})
it("renders the user list in English with English dictionary labels", async () => {
const { default: UsersPage } = await import(
"@/app/(dashboard)/admin/users/page"
)
mocks.getI18n.mockResolvedValue({ dictionary: en, locale: "en" })
mocks.getUsers.mockResolvedValue({
data: [
{
id: "user-1",
name: "Ada Lovelace",
email: "ada@example.test",
role: "MANAGER",
isActive: true,
},
],
totalPages: 1,
})
const html = renderToStaticMarkup(
await UsersPage({ searchParams: Promise.resolve({}) }),
)
// English dictionary labels
expect(html).toContain("Users")
expect(html).toContain("Add User")
expect(html).toContain("Name")
expect(html).toContain("Email")
expect(html).toContain("Role")
expect(html).toContain("Status")
expect(html).toContain("Actions")
expect(html).toContain("Active")
expect(html).toContain("Manager")
// Canonical enum value must NOT appear as display text
expect(html).not.toContain(">MANAGER<")
})
it("renders unknown role via fallback when role is not in dictionary", async () => {
const { default: UsersPage } = await import(
"@/app/(dashboard)/admin/users/page"
)
mocks.getUsers.mockResolvedValue({
data: [
{
id: "user-1",
name: "Test User",
email: "test@example.test",
role: "UNKNOWN_ROLE",
isActive: true,
} as unknown as UserData,
],
totalPages: 1,
})
const html = renderToStaticMarkup(
await UsersPage({ searchParams: Promise.resolve({}) }),
)
// Unknown role should use fallback
expect(html).toContain("Rol desconocido")
})
})
+1
View File
@@ -52,6 +52,7 @@ describe("formatPersonDepartment helper", () => {
const fallbackCopy = {
unknownDepartment: "Departamento desconocido",
unknownStatus: "Estado desconocido",
}
it("formats known department values with localized labels", () => {