feat(i18n): localize item validation messages
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import { describe, expect, it } from "vitest"
|
||||
|
||||
import { localizeItemFieldErrors } from "@/actions/item.messages"
|
||||
|
||||
const actionCopy = {
|
||||
createSuccess: "Artículo creado correctamente",
|
||||
createFailure: "Error al crear el artículo",
|
||||
updateSuccess: "Artículo actualizado correctamente",
|
||||
updateFailure: "Error al actualizar el artículo",
|
||||
deleteSuccess: "Artículo eliminado correctamente",
|
||||
deleteFailure: "Error al eliminar el artículo",
|
||||
duplicateName: "El artículo ya existe",
|
||||
notFound: "Artículo no encontrado",
|
||||
hasAssets: "No se puede eliminar un artículo con activos",
|
||||
hasStock: "No se puede eliminar un artículo con stock",
|
||||
invalidStock: "Stock inválido",
|
||||
negativeStock: "El stock no puede ser negativo",
|
||||
}
|
||||
|
||||
describe("item action message localization", () => {
|
||||
it("localizes known item field errors", () => {
|
||||
expect(
|
||||
localizeItemFieldErrors(
|
||||
{
|
||||
name: ["An item with this name already exists"],
|
||||
id: [
|
||||
"Item not found",
|
||||
"Item has assets, you cannot delete it",
|
||||
"Item has stock, you cannot delete it",
|
||||
],
|
||||
stock: ["Stock cannot be negative", "Invalid stock"],
|
||||
},
|
||||
actionCopy,
|
||||
),
|
||||
).toEqual({
|
||||
name: [actionCopy.duplicateName],
|
||||
id: [actionCopy.notFound, actionCopy.hasAssets, actionCopy.hasStock],
|
||||
stock: [actionCopy.negativeStock, actionCopy.invalidStock],
|
||||
})
|
||||
})
|
||||
|
||||
it("keeps unknown messages unchanged", () => {
|
||||
expect(
|
||||
localizeItemFieldErrors({ name: ["Unexpected item issue"] }, actionCopy),
|
||||
).toEqual({ name: ["Unexpected item issue"] })
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,86 @@
|
||||
import { describe, expect, it } from "vitest"
|
||||
|
||||
import {
|
||||
buildCreateItemSchema,
|
||||
buildGetItemByIdSchema,
|
||||
buildUpdateItemSchema,
|
||||
} from "@/schemas/item.schema"
|
||||
|
||||
const schemaCopy = {
|
||||
nameRequired: "El nombre es obligatorio",
|
||||
categoryRequired: "La categoría es obligatoria",
|
||||
stockRequired: "El stock es obligatorio",
|
||||
itemRequired: "El artículo es obligatorio",
|
||||
}
|
||||
|
||||
describe("item schema localization", () => {
|
||||
it("uses localized create validation messages", () => {
|
||||
const result = buildCreateItemSchema(schemaCopy).safeParse({
|
||||
name: "",
|
||||
categoryId: "",
|
||||
stock: -1,
|
||||
})
|
||||
|
||||
expect(result.success).toBe(false)
|
||||
if (!result.success) {
|
||||
const errors = result.error.flatten().fieldErrors
|
||||
|
||||
expect(errors.name).toContain(schemaCopy.nameRequired)
|
||||
expect(errors.categoryId).toContain(schemaCopy.categoryRequired)
|
||||
expect(errors.stock).toContain(schemaCopy.stockRequired)
|
||||
}
|
||||
})
|
||||
|
||||
it("uses localized update identifier validation messages", () => {
|
||||
const result = buildUpdateItemSchema(schemaCopy).safeParse({
|
||||
id: "",
|
||||
name: "Laptop",
|
||||
categoryId: "category-1",
|
||||
stock: 1,
|
||||
})
|
||||
|
||||
expect(result.success).toBe(false)
|
||||
if (!result.success) {
|
||||
expect(result.error.flatten().fieldErrors.id).toContain(
|
||||
schemaCopy.itemRequired,
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
it("uses localized get-by-id validation messages", () => {
|
||||
const result = buildGetItemByIdSchema(schemaCopy).safeParse({ id: "" })
|
||||
|
||||
expect(result.success).toBe(false)
|
||||
if (!result.success) {
|
||||
expect(result.error.flatten().fieldErrors.id).toContain(
|
||||
schemaCopy.itemRequired,
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
it("preserves stock coercion and integer validation semantics", () => {
|
||||
const validResult = buildCreateItemSchema(schemaCopy).safeParse({
|
||||
name: "Laptop",
|
||||
categoryId: "category-1",
|
||||
stock: "2",
|
||||
})
|
||||
|
||||
expect(validResult.success).toBe(true)
|
||||
if (validResult.success) {
|
||||
expect(validResult.data.stock).toBe(2)
|
||||
}
|
||||
|
||||
const invalidResult = buildCreateItemSchema(schemaCopy).safeParse({
|
||||
name: "Laptop",
|
||||
categoryId: "category-1",
|
||||
stock: 1.5,
|
||||
})
|
||||
|
||||
expect(invalidResult.success).toBe(false)
|
||||
if (!invalidResult.success) {
|
||||
expect(invalidResult.error.flatten().fieldErrors.stock).toContain(
|
||||
schemaCopy.stockRequired,
|
||||
)
|
||||
}
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user