feat(i18n): localize recipient validation messages
This commit is contained in:
@@ -2,19 +2,26 @@
|
|||||||
|
|
||||||
import { revalidatePath } from "next/cache"
|
import { revalidatePath } from "next/cache"
|
||||||
|
|
||||||
|
import { getI18n } from "@/i18n/server"
|
||||||
import {
|
import {
|
||||||
|
buildCreateRecipientSchema,
|
||||||
|
buildUpdateRecipientSchema,
|
||||||
type CreateRecipientFormType,
|
type CreateRecipientFormType,
|
||||||
createRecipientSchema,
|
|
||||||
type UpdateRecipientFormType,
|
type UpdateRecipientFormType,
|
||||||
updateRecipientSchema,
|
|
||||||
} from "@/schemas/recipient.schema"
|
} from "@/schemas/recipient.schema"
|
||||||
import {
|
import {
|
||||||
createRecipientUseCase,
|
createRecipientUseCase,
|
||||||
updateRecipientUseCase,
|
updateRecipientUseCase,
|
||||||
} from "@/use-cases/recipient.use-cases"
|
} from "@/use-cases/recipient.use-cases"
|
||||||
|
|
||||||
|
import { localizeRecipientFieldErrors } from "./recipient.messages"
|
||||||
|
|
||||||
export async function createNewRecipient(formData: CreateRecipientFormType) {
|
export async function createNewRecipient(formData: CreateRecipientFormType) {
|
||||||
const validatedFields = createRecipientSchema.safeParse(formData)
|
const { dictionary } = await getI18n()
|
||||||
|
const copy = dictionary.inventory.recipients
|
||||||
|
const validatedFields = buildCreateRecipientSchema(copy.schema).safeParse(
|
||||||
|
formData,
|
||||||
|
)
|
||||||
|
|
||||||
if (!validatedFields.success) {
|
if (!validatedFields.success) {
|
||||||
return {
|
return {
|
||||||
@@ -27,25 +34,34 @@ export async function createNewRecipient(formData: CreateRecipientFormType) {
|
|||||||
const result = await createRecipientUseCase(validatedFields.data)
|
const result = await createRecipientUseCase(validatedFields.data)
|
||||||
|
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
return result
|
return {
|
||||||
|
...result,
|
||||||
|
errors: localizeRecipientFieldErrors(result.errors, copy.actions),
|
||||||
|
message: copy.actions.createFailure,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
revalidatePath("/recipients")
|
revalidatePath("/recipients")
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
message: "Recipient created successfully",
|
message: copy.actions.createSuccess,
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Database error:", error)
|
console.error("Database error:", error)
|
||||||
return {
|
return {
|
||||||
message: "Failed to create recipient",
|
success: false,
|
||||||
|
message: copy.actions.createFailure,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function updateRecipient(formData: UpdateRecipientFormType) {
|
export async function updateRecipient(formData: UpdateRecipientFormType) {
|
||||||
const validatedFields = updateRecipientSchema.safeParse(formData)
|
const { dictionary } = await getI18n()
|
||||||
|
const copy = dictionary.inventory.recipients
|
||||||
|
const validatedFields = buildUpdateRecipientSchema(copy.schema).safeParse(
|
||||||
|
formData,
|
||||||
|
)
|
||||||
|
|
||||||
if (!validatedFields.success) {
|
if (!validatedFields.success) {
|
||||||
return {
|
return {
|
||||||
@@ -58,19 +74,24 @@ export async function updateRecipient(formData: UpdateRecipientFormType) {
|
|||||||
const result = await updateRecipientUseCase(validatedFields.data)
|
const result = await updateRecipientUseCase(validatedFields.data)
|
||||||
|
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
return result
|
return {
|
||||||
|
...result,
|
||||||
|
errors: localizeRecipientFieldErrors(result.errors, copy.actions),
|
||||||
|
message: copy.actions.updateFailure,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
revalidatePath("/recipients")
|
revalidatePath("/recipients")
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
message: "Recipient updated successfully",
|
message: copy.actions.updateSuccess,
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Database error:", error)
|
console.error("Database error:", error)
|
||||||
return {
|
return {
|
||||||
message: "Failed to update recipient",
|
success: false,
|
||||||
|
message: copy.actions.updateFailure,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
import type { Dictionary } from "@/i18n/dictionaries"
|
||||||
|
|
||||||
|
type RecipientActionCopy = Dictionary["inventory"]["recipients"]["actions"]
|
||||||
|
|
||||||
|
type FieldErrors = Record<string, string[]>
|
||||||
|
|
||||||
|
const recipientErrorMessageKeys = {
|
||||||
|
"Username already exists": "duplicateUsername",
|
||||||
|
"Email already exists": "duplicateEmail",
|
||||||
|
} as const satisfies Record<string, keyof RecipientActionCopy>
|
||||||
|
|
||||||
|
function isRecipientErrorMessage(
|
||||||
|
message: string,
|
||||||
|
): message is keyof typeof recipientErrorMessageKeys {
|
||||||
|
return message in recipientErrorMessageKeys
|
||||||
|
}
|
||||||
|
|
||||||
|
function localizeRecipientMessage(
|
||||||
|
message: string,
|
||||||
|
copy: RecipientActionCopy,
|
||||||
|
): string {
|
||||||
|
if (!isRecipientErrorMessage(message)) return message
|
||||||
|
|
||||||
|
return copy[recipientErrorMessageKeys[message]]
|
||||||
|
}
|
||||||
|
|
||||||
|
export function localizeRecipientFieldErrors(
|
||||||
|
errors: FieldErrors | undefined,
|
||||||
|
copy: RecipientActionCopy,
|
||||||
|
): FieldErrors | undefined {
|
||||||
|
if (!errors) return undefined
|
||||||
|
|
||||||
|
return Object.fromEntries(
|
||||||
|
Object.entries(errors).map(([field, messages]) => [
|
||||||
|
field,
|
||||||
|
messages.map((message) => localizeRecipientMessage(message, copy)),
|
||||||
|
]),
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -26,6 +26,7 @@ export default async function RecipientEditPage({
|
|||||||
initialData={recipient}
|
initialData={recipient}
|
||||||
mode="edit"
|
mode="edit"
|
||||||
formCopy={copy.form}
|
formCopy={copy.form}
|
||||||
|
schemaCopy={copy.schema}
|
||||||
departmentCopy={copy.departments}
|
departmentCopy={copy.departments}
|
||||||
fallbackCopy={copy.fallback}
|
fallbackCopy={copy.fallback}
|
||||||
submitButtonCopy={dictionary.common.submitButton}
|
submitButtonCopy={dictionary.common.submitButton}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
import { zodResolver } from "@hookform/resolvers/zod"
|
import { zodResolver } from "@hookform/resolvers/zod"
|
||||||
import { useRouter } from "next/navigation"
|
import { useRouter } from "next/navigation"
|
||||||
|
import { useMemo } from "react"
|
||||||
import { useForm } from "react-hook-form"
|
import { useForm } from "react-hook-form"
|
||||||
import { toast } from "sonner"
|
import { toast } from "sonner"
|
||||||
import {
|
import {
|
||||||
@@ -14,8 +15,10 @@ import {
|
|||||||
} from "@/components/forms/submitButton"
|
} from "@/components/forms/submitButton"
|
||||||
import { RECIPIENT_DEPARTMENTS } from "@/lib/constants"
|
import { RECIPIENT_DEPARTMENTS } from "@/lib/constants"
|
||||||
import {
|
import {
|
||||||
|
buildCreateRecipientSchema,
|
||||||
|
buildUpdateRecipientSchema,
|
||||||
type CreateRecipientFormType,
|
type CreateRecipientFormType,
|
||||||
recipientSchema,
|
type RecipientSchemaCopy,
|
||||||
type UpdateRecipientFormType,
|
type UpdateRecipientFormType,
|
||||||
} from "@/schemas/recipient.schema"
|
} from "@/schemas/recipient.schema"
|
||||||
import type { Recipient } from "@/types"
|
import type { Recipient } from "@/types"
|
||||||
@@ -31,6 +34,7 @@ interface RecipientFormProps {
|
|||||||
initialData?: Recipient
|
initialData?: Recipient
|
||||||
mode?: "create" | "edit"
|
mode?: "create" | "edit"
|
||||||
formCopy: RecipientFormCopy
|
formCopy: RecipientFormCopy
|
||||||
|
schemaCopy: RecipientSchemaCopy
|
||||||
departmentCopy: RecipientDepartmentCopy
|
departmentCopy: RecipientDepartmentCopy
|
||||||
fallbackCopy: RecipientFallbackCopy
|
fallbackCopy: RecipientFallbackCopy
|
||||||
submitButtonCopy: SubmitButtonCopy
|
submitButtonCopy: SubmitButtonCopy
|
||||||
@@ -40,11 +44,19 @@ export default function RecipientForm({
|
|||||||
initialData,
|
initialData,
|
||||||
mode = "create",
|
mode = "create",
|
||||||
formCopy,
|
formCopy,
|
||||||
|
schemaCopy,
|
||||||
departmentCopy,
|
departmentCopy,
|
||||||
fallbackCopy,
|
fallbackCopy,
|
||||||
submitButtonCopy,
|
submitButtonCopy,
|
||||||
}: RecipientFormProps) {
|
}: RecipientFormProps) {
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
const schema = useMemo(
|
||||||
|
() =>
|
||||||
|
mode === "create"
|
||||||
|
? buildCreateRecipientSchema(schemaCopy)
|
||||||
|
: buildUpdateRecipientSchema(schemaCopy),
|
||||||
|
[mode, schemaCopy],
|
||||||
|
)
|
||||||
|
|
||||||
const {
|
const {
|
||||||
register,
|
register,
|
||||||
@@ -52,7 +64,7 @@ export default function RecipientForm({
|
|||||||
setError,
|
setError,
|
||||||
formState: { errors, isSubmitting, isSubmitSuccessful },
|
formState: { errors, isSubmitting, isSubmitSuccessful },
|
||||||
} = useForm<CreateRecipientFormType>({
|
} = useForm<CreateRecipientFormType>({
|
||||||
resolver: zodResolver(recipientSchema),
|
resolver: zodResolver(schema),
|
||||||
defaultValues: {
|
defaultValues: {
|
||||||
id: initialData?.id || "",
|
id: initialData?.id || "",
|
||||||
username: initialData?.username || "",
|
username: initialData?.username || "",
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ export default async function NewRecipientPage() {
|
|||||||
<RecipientForm
|
<RecipientForm
|
||||||
mode="create"
|
mode="create"
|
||||||
formCopy={copy.form}
|
formCopy={copy.form}
|
||||||
|
schemaCopy={copy.schema}
|
||||||
departmentCopy={copy.departments}
|
departmentCopy={copy.departments}
|
||||||
fallbackCopy={copy.fallback}
|
fallbackCopy={copy.fallback}
|
||||||
submitButtonCopy={dictionary.common.submitButton}
|
submitButtonCopy={dictionary.common.submitButton}
|
||||||
|
|||||||
@@ -318,6 +318,22 @@ export const en = {
|
|||||||
SALES: "Sales",
|
SALES: "Sales",
|
||||||
OTHER: "Other",
|
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",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
movements: {
|
movements: {
|
||||||
list: {
|
list: {
|
||||||
|
|||||||
@@ -322,6 +322,22 @@ export const es = {
|
|||||||
SALES: "Ventas",
|
SALES: "Ventas",
|
||||||
OTHER: "Otro",
|
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",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
movements: {
|
movements: {
|
||||||
list: {
|
list: {
|
||||||
|
|||||||
@@ -1,21 +1,20 @@
|
|||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
|
|
||||||
export const recipientSchema = z.object({
|
import type { Dictionary } from "@/i18n/dictionaries"
|
||||||
id: z.string().optional(),
|
|
||||||
username: z
|
export type RecipientSchemaCopy =
|
||||||
.string()
|
Dictionary["inventory"]["recipients"]["schema"]
|
||||||
.min(1, {
|
|
||||||
error: "Username is required",
|
const defaultRecipientSchemaCopy: RecipientSchemaCopy = {
|
||||||
})
|
usernameRequired: "Username is required",
|
||||||
.nonempty("Username is required"),
|
firstNameRequired: "First name is required",
|
||||||
firstName: z.string().min(1, {
|
lastNameRequired: "Last name is required",
|
||||||
error: "First name is required",
|
departmentRequired: "Department is required",
|
||||||
}),
|
emailInvalid: "Email format is invalid",
|
||||||
lastName: z.string().min(1, {
|
idRequired: "ID is required",
|
||||||
error: "Last name is required",
|
}
|
||||||
}),
|
|
||||||
department: z.enum(
|
const recipientDepartments = [
|
||||||
[
|
|
||||||
"IT",
|
"IT",
|
||||||
"ENGINEERING",
|
"ENGINEERING",
|
||||||
"TRAFFIC",
|
"TRAFFIC",
|
||||||
@@ -24,31 +23,58 @@ export const recipientSchema = z.object({
|
|||||||
"ADMINISTRATION",
|
"ADMINISTRATION",
|
||||||
"SALES",
|
"SALES",
|
||||||
"OTHER",
|
"OTHER",
|
||||||
],
|
] as const
|
||||||
{
|
|
||||||
error: "Department is required",
|
function buildRecipientBaseSchema(copy: RecipientSchemaCopy) {
|
||||||
},
|
return z.object({
|
||||||
),
|
id: z.string().optional(),
|
||||||
|
username: z.string().min(1, {
|
||||||
|
error: copy.usernameRequired,
|
||||||
|
}),
|
||||||
|
firstName: z.string().min(1, {
|
||||||
|
error: copy.firstNameRequired,
|
||||||
|
}),
|
||||||
|
lastName: z.string().min(1, {
|
||||||
|
error: copy.lastNameRequired,
|
||||||
|
}),
|
||||||
|
department: z.enum(recipientDepartments, {
|
||||||
|
error: copy.departmentRequired,
|
||||||
|
}),
|
||||||
email: z.string().optional().nullable(),
|
email: z.string().optional().nullable(),
|
||||||
phone: z.string().optional().nullable(),
|
phone: z.string().optional().nullable(),
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
export const createRecipientSchema = recipientSchema.superRefine(
|
export const recipientSchema = buildRecipientBaseSchema(
|
||||||
(data, ctx) => {
|
defaultRecipientSchemaCopy,
|
||||||
|
)
|
||||||
|
|
||||||
|
export function buildCreateRecipientSchema(copy: RecipientSchemaCopy) {
|
||||||
|
return buildRecipientBaseSchema(copy).superRefine((data, ctx) => {
|
||||||
if (data.email && !z.string().email().safeParse(data.email).success) {
|
if (data.email && !z.string().email().safeParse(data.email).success) {
|
||||||
ctx.addIssue({
|
ctx.addIssue({
|
||||||
code: "custom",
|
code: "custom",
|
||||||
message: "Email format is invalid",
|
message: copy.emailInvalid,
|
||||||
path: ["email"],
|
path: ["email"],
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
},
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export const createRecipientSchema = buildCreateRecipientSchema(
|
||||||
|
defaultRecipientSchemaCopy,
|
||||||
)
|
)
|
||||||
|
|
||||||
export type CreateRecipientFormType = z.infer<typeof createRecipientSchema>
|
export type CreateRecipientFormType = z.infer<typeof createRecipientSchema>
|
||||||
|
|
||||||
export const updateRecipientSchema = recipientSchema.extend({
|
export function buildUpdateRecipientSchema(copy: RecipientSchemaCopy) {
|
||||||
id: z.string().nonempty("ID is required"),
|
return buildRecipientBaseSchema(copy).extend({
|
||||||
|
id: z.string().nonempty(copy.idRequired),
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export const updateRecipientSchema = buildUpdateRecipientSchema(
|
||||||
|
defaultRecipientSchemaCopy,
|
||||||
|
)
|
||||||
|
|
||||||
export type UpdateRecipientFormType = z.infer<typeof updateRecipientSchema>
|
export type UpdateRecipientFormType = z.infer<typeof updateRecipientSchema>
|
||||||
|
|||||||
@@ -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",
|
SALES: "Sales",
|
||||||
OTHER: "Other",
|
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({
|
expect(getDictionary("es").inventory.recipients).toEqual({
|
||||||
@@ -688,6 +704,22 @@ describe("i18n dictionaries", () => {
|
|||||||
SALES: "Ventas",
|
SALES: "Ventas",
|
||||||
OTHER: "Otro",
|
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