feat(i18n): localize recipient validation messages
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest"
|
||||
|
||||
import { es } from "@/i18n/dictionaries/es"
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
revalidatePath: vi.fn(),
|
||||
getI18n: vi.fn(),
|
||||
createRecipientUseCase: vi.fn(),
|
||||
updateRecipientUseCase: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock("next/cache", () => ({
|
||||
revalidatePath: mocks.revalidatePath,
|
||||
}))
|
||||
|
||||
vi.mock("@/i18n/server", () => ({
|
||||
getI18n: mocks.getI18n,
|
||||
}))
|
||||
|
||||
vi.mock("@/use-cases/recipient.use-cases", () => ({
|
||||
createRecipientUseCase: mocks.createRecipientUseCase,
|
||||
updateRecipientUseCase: mocks.updateRecipientUseCase,
|
||||
}))
|
||||
|
||||
import {
|
||||
createNewRecipient,
|
||||
updateRecipient,
|
||||
} from "@/actions/recipient.actions"
|
||||
|
||||
describe("recipient actions localization", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
mocks.getI18n.mockResolvedValue({ dictionary: es, locale: "es" })
|
||||
})
|
||||
|
||||
it("returns localized schema validation errors for invalid create input", async () => {
|
||||
const result = await createNewRecipient({
|
||||
username: "",
|
||||
firstName: "",
|
||||
lastName: "",
|
||||
department: "",
|
||||
email: "not-an-email",
|
||||
} as unknown as Parameters<typeof createNewRecipient>[0])
|
||||
|
||||
expect(mocks.getI18n).toHaveBeenCalledOnce()
|
||||
expect(mocks.createRecipientUseCase).not.toHaveBeenCalled()
|
||||
expect(result).toEqual({
|
||||
success: false,
|
||||
errors: {
|
||||
username: [es.inventory.recipients.schema.usernameRequired],
|
||||
firstName: [es.inventory.recipients.schema.firstNameRequired],
|
||||
lastName: [es.inventory.recipients.schema.lastNameRequired],
|
||||
department: [es.inventory.recipients.schema.departmentRequired],
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
it("localizes mapped duplicate field errors for create failures", async () => {
|
||||
mocks.createRecipientUseCase.mockResolvedValue({
|
||||
success: false,
|
||||
errors: {
|
||||
username: ["Username already exists"],
|
||||
email: ["Email already exists"],
|
||||
},
|
||||
})
|
||||
|
||||
const result = await createNewRecipient({
|
||||
username: "ada",
|
||||
firstName: "Ada",
|
||||
lastName: "Lovelace",
|
||||
department: "ENGINEERING",
|
||||
email: "ada@example.test",
|
||||
})
|
||||
|
||||
expect(result).toEqual({
|
||||
success: false,
|
||||
errors: {
|
||||
username: [es.inventory.recipients.actions.duplicateUsername],
|
||||
email: [es.inventory.recipients.actions.duplicateEmail],
|
||||
},
|
||||
message: es.inventory.recipients.actions.createFailure,
|
||||
})
|
||||
})
|
||||
|
||||
it("returns a localized update success message and revalidates recipients", async () => {
|
||||
mocks.updateRecipientUseCase.mockResolvedValue({ success: true })
|
||||
|
||||
const result = await updateRecipient({
|
||||
id: "recipient-1",
|
||||
username: "ada",
|
||||
firstName: "Ada",
|
||||
lastName: "Lovelace",
|
||||
department: "ENGINEERING",
|
||||
email: "ada@example.test",
|
||||
})
|
||||
|
||||
expect(result).toEqual({
|
||||
success: true,
|
||||
message: es.inventory.recipients.actions.updateSuccess,
|
||||
})
|
||||
expect(mocks.revalidatePath).toHaveBeenCalledWith("/recipients")
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,38 @@
|
||||
import { describe, expect, it } from "vitest"
|
||||
|
||||
import { localizeRecipientFieldErrors } from "@/actions/recipient.messages"
|
||||
|
||||
const actionCopy = {
|
||||
createSuccess: "Destinatario creado correctamente",
|
||||
createFailure: "Error al crear el destinatario",
|
||||
updateSuccess: "Destinatario actualizado correctamente",
|
||||
updateFailure: "Error al actualizar el destinatario",
|
||||
duplicateUsername: "El nombre de usuario ya existe",
|
||||
duplicateEmail: "El correo electrónico ya existe",
|
||||
}
|
||||
|
||||
describe("recipient action message localization", () => {
|
||||
it("localizes known recipient field errors", () => {
|
||||
expect(
|
||||
localizeRecipientFieldErrors(
|
||||
{
|
||||
username: ["Username already exists"],
|
||||
email: ["Email already exists"],
|
||||
},
|
||||
actionCopy,
|
||||
),
|
||||
).toEqual({
|
||||
username: [actionCopy.duplicateUsername],
|
||||
email: [actionCopy.duplicateEmail],
|
||||
})
|
||||
})
|
||||
|
||||
it("keeps unknown messages unchanged", () => {
|
||||
expect(
|
||||
localizeRecipientFieldErrors(
|
||||
{ username: ["Unexpected recipient issue"] },
|
||||
actionCopy,
|
||||
),
|
||||
).toEqual({ username: ["Unexpected recipient issue"] })
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,82 @@
|
||||
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"
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
getI18n: vi.fn(),
|
||||
findById: vi.fn(),
|
||||
recipientForm: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock("@/i18n/server", () => ({
|
||||
getI18n: mocks.getI18n,
|
||||
}))
|
||||
|
||||
vi.mock("@/services/recipient.service", () => ({
|
||||
RecipientService: {
|
||||
findById: mocks.findById,
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock("@/app/(dashboard)/recipients/_components/recipient.form", () => ({
|
||||
default: (props: unknown) => {
|
||||
mocks.recipientForm(props)
|
||||
return createElement("div", null, "Recipient form")
|
||||
},
|
||||
}))
|
||||
|
||||
describe("recipient form schema wiring", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it("passes server-resolved schema copy into the new recipient form boundary", async () => {
|
||||
mocks.getI18n.mockResolvedValue({ dictionary: es, locale: "es" })
|
||||
|
||||
const { default: NewRecipientPage } = await import(
|
||||
"@/app/(dashboard)/recipients/new/page"
|
||||
)
|
||||
|
||||
renderToStaticMarkup(await NewRecipientPage())
|
||||
|
||||
expect(mocks.recipientForm).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
mode: "create",
|
||||
schemaCopy: es.inventory.recipients.schema,
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it("passes server-resolved schema copy into the edit recipient form boundary", async () => {
|
||||
mocks.getI18n.mockResolvedValue({ dictionary: en, locale: "en" })
|
||||
mocks.findById.mockResolvedValue({
|
||||
id: "recipient-1",
|
||||
username: "ada",
|
||||
firstName: "Ada",
|
||||
lastName: "Lovelace",
|
||||
department: "ENGINEERING",
|
||||
email: "ada@example.test",
|
||||
phone: "1234",
|
||||
})
|
||||
|
||||
const { default: RecipientEditPage } = await import(
|
||||
"@/app/(dashboard)/recipients/[recipientId]/edit/page"
|
||||
)
|
||||
|
||||
renderToStaticMarkup(
|
||||
await RecipientEditPage({
|
||||
params: Promise.resolve({ recipientId: "recipient-1" }),
|
||||
}),
|
||||
)
|
||||
|
||||
expect(mocks.recipientForm).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
mode: "edit",
|
||||
schemaCopy: en.inventory.recipients.schema,
|
||||
}),
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -623,6 +623,22 @@ describe("i18n dictionaries", () => {
|
||||
SALES: "Sales",
|
||||
OTHER: "Other",
|
||||
},
|
||||
actions: {
|
||||
createSuccess: "Recipient created successfully",
|
||||
createFailure: "Failed to create recipient",
|
||||
updateSuccess: "Recipient updated successfully",
|
||||
updateFailure: "Failed to update recipient",
|
||||
duplicateUsername: "Username already exists",
|
||||
duplicateEmail: "Email already exists",
|
||||
},
|
||||
schema: {
|
||||
usernameRequired: "Username is required",
|
||||
firstNameRequired: "First name is required",
|
||||
lastNameRequired: "Last name is required",
|
||||
departmentRequired: "Department is required",
|
||||
emailInvalid: "Email format is invalid",
|
||||
idRequired: "ID is required",
|
||||
},
|
||||
})
|
||||
|
||||
expect(getDictionary("es").inventory.recipients).toEqual({
|
||||
@@ -688,6 +704,22 @@ describe("i18n dictionaries", () => {
|
||||
SALES: "Ventas",
|
||||
OTHER: "Otro",
|
||||
},
|
||||
actions: {
|
||||
createSuccess: "Destinatario creado correctamente",
|
||||
createFailure: "Error al crear el destinatario",
|
||||
updateSuccess: "Destinatario actualizado correctamente",
|
||||
updateFailure: "Error al actualizar el destinatario",
|
||||
duplicateUsername: "El nombre de usuario ya existe",
|
||||
duplicateEmail: "El correo electrónico ya existe",
|
||||
},
|
||||
schema: {
|
||||
usernameRequired: "El usuario es obligatorio",
|
||||
firstNameRequired: "El nombre es obligatorio",
|
||||
lastNameRequired: "El apellido es obligatorio",
|
||||
departmentRequired: "El departamento es obligatorio",
|
||||
emailInvalid: "El correo electrónico no es válido",
|
||||
idRequired: "El ID es obligatorio",
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
import { describe, expect, it } from "vitest"
|
||||
|
||||
import {
|
||||
buildCreateRecipientSchema,
|
||||
buildUpdateRecipientSchema,
|
||||
} from "@/schemas/recipient.schema"
|
||||
|
||||
const schemaCopy = {
|
||||
usernameRequired: "El usuario es obligatorio",
|
||||
firstNameRequired: "El nombre es obligatorio",
|
||||
lastNameRequired: "El apellido es obligatorio",
|
||||
departmentRequired: "El departamento es obligatorio",
|
||||
emailInvalid: "El correo electrónico no es válido",
|
||||
idRequired: "El ID es obligatorio",
|
||||
}
|
||||
|
||||
describe("recipient schema localization", () => {
|
||||
it("uses localized required-field validation messages for create", () => {
|
||||
const result = buildCreateRecipientSchema(schemaCopy).safeParse({
|
||||
username: "",
|
||||
firstName: "",
|
||||
lastName: "",
|
||||
department: "",
|
||||
})
|
||||
|
||||
expect(result.success).toBe(false)
|
||||
if (!result.success) {
|
||||
const errors = result.error.flatten().fieldErrors
|
||||
|
||||
expect(errors.username).toContain(schemaCopy.usernameRequired)
|
||||
expect(errors.firstName).toContain(schemaCopy.firstNameRequired)
|
||||
expect(errors.lastName).toContain(schemaCopy.lastNameRequired)
|
||||
expect(errors.department).toContain(schemaCopy.departmentRequired)
|
||||
}
|
||||
})
|
||||
|
||||
it("uses a localized invalid email message for create", () => {
|
||||
const result = buildCreateRecipientSchema(schemaCopy).safeParse({
|
||||
username: "ada",
|
||||
firstName: "Ada",
|
||||
lastName: "Lovelace",
|
||||
department: "ENGINEERING",
|
||||
email: "not-an-email",
|
||||
})
|
||||
|
||||
expect(result.success).toBe(false)
|
||||
if (!result.success) {
|
||||
expect(result.error.flatten().fieldErrors.email).toContain(
|
||||
schemaCopy.emailInvalid,
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
it("uses localized update identifier validation messages", () => {
|
||||
const result = buildUpdateRecipientSchema(schemaCopy).safeParse({
|
||||
id: "",
|
||||
username: "ada",
|
||||
firstName: "Ada",
|
||||
lastName: "Lovelace",
|
||||
department: "ENGINEERING",
|
||||
email: "ada@example.test",
|
||||
})
|
||||
|
||||
expect(result.success).toBe(false)
|
||||
if (!result.success) {
|
||||
expect(result.error.flatten().fieldErrors.id).toContain(
|
||||
schemaCopy.idRequired,
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
it("preserves canonical department values and optional empty email semantics", () => {
|
||||
const result = buildCreateRecipientSchema(schemaCopy).safeParse({
|
||||
username: "ada",
|
||||
firstName: "Ada",
|
||||
lastName: "Lovelace",
|
||||
department: "ENGINEERING",
|
||||
email: "",
|
||||
})
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
if (result.success) {
|
||||
expect(result.data.department).toBe("ENGINEERING")
|
||||
expect(result.data.email).toBe("")
|
||||
}
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user