refactor: complete i18n rename recipients to people, finalize tests
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest"
|
||||
|
||||
import { en } from "@/i18n/dictionaries/en"
|
||||
import { es } from "@/i18n/dictionaries/es"
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
revalidatePath: vi.fn(),
|
||||
getI18n: vi.fn(),
|
||||
createPersonUseCase: vi.fn(),
|
||||
updatePersonUseCase: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock("next/cache", () => ({
|
||||
revalidatePath: mocks.revalidatePath,
|
||||
}))
|
||||
|
||||
vi.mock("@/i18n/server", () => ({
|
||||
getI18n: mocks.getI18n,
|
||||
}))
|
||||
|
||||
vi.mock("@/use-cases/person.use-cases", () => ({
|
||||
createPersonUseCase: mocks.createPersonUseCase,
|
||||
updatePersonUseCase: mocks.updatePersonUseCase,
|
||||
}))
|
||||
|
||||
import { createNewPerson, updatePerson } from "@/actions/person.actions"
|
||||
|
||||
describe("person actions localization", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
mocks.getI18n.mockResolvedValue({ dictionary: es, locale: "es" })
|
||||
})
|
||||
|
||||
it("returns localized schema validation errors for invalid create input (no username)", async () => {
|
||||
const result = await createNewPerson({
|
||||
firstName: "",
|
||||
lastName: "",
|
||||
department: "",
|
||||
email: "not-an-email",
|
||||
} as unknown as Parameters<typeof createNewPerson>[0])
|
||||
|
||||
expect(mocks.getI18n).toHaveBeenCalledOnce()
|
||||
expect(mocks.createPersonUseCase).not.toHaveBeenCalled()
|
||||
expect(result).toEqual({
|
||||
success: false,
|
||||
errors: {
|
||||
firstName: [es.inventory.people.schema.firstNameRequired],
|
||||
lastName: [es.inventory.people.schema.lastNameRequired],
|
||||
department: [es.inventory.people.schema.departmentRequired],
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
it("localizes mapped duplicate field errors for create failures (no duplicateUsername)", async () => {
|
||||
mocks.createPersonUseCase.mockResolvedValue({
|
||||
success: false,
|
||||
errors: {
|
||||
email: ["Email already exists"],
|
||||
},
|
||||
})
|
||||
|
||||
const result = await createNewPerson({
|
||||
firstName: "Ada",
|
||||
lastName: "Lovelace",
|
||||
department: "ENGINEERING",
|
||||
email: "ada@example.test",
|
||||
})
|
||||
|
||||
expect(result).toEqual({
|
||||
success: false,
|
||||
errors: {
|
||||
email: [es.inventory.people.actions.duplicateEmail],
|
||||
},
|
||||
message: es.inventory.people.actions.createFailure,
|
||||
})
|
||||
})
|
||||
|
||||
it("returns a localized update success message and revalidates /people", async () => {
|
||||
mocks.getI18n.mockResolvedValue({ dictionary: en, locale: "en" })
|
||||
mocks.updatePersonUseCase.mockResolvedValue({ success: true })
|
||||
|
||||
const result = await updatePerson({
|
||||
id: "person-1",
|
||||
firstName: "Ada",
|
||||
lastName: "Lovelace",
|
||||
department: "ENGINEERING",
|
||||
email: "ada@example.test",
|
||||
})
|
||||
|
||||
expect(result).toEqual({
|
||||
success: true,
|
||||
message: en.inventory.people.actions.updateSuccess,
|
||||
})
|
||||
expect(mocks.revalidatePath).toHaveBeenCalledWith("/people")
|
||||
})
|
||||
|
||||
it("returns localized validation error for invalid userId UUID on create", async () => {
|
||||
const result = await createNewPerson({
|
||||
firstName: "Ada",
|
||||
lastName: "Lovelace",
|
||||
department: "ENGINEERING",
|
||||
userId: "not-a-uuid",
|
||||
} as unknown as Parameters<typeof createNewPerson>[0])
|
||||
|
||||
expect(result).toEqual({
|
||||
success: false,
|
||||
errors: {
|
||||
userId: [es.inventory.people.schema.userIdInvalid],
|
||||
},
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,43 @@
|
||||
import { describe, expect, it } from "vitest"
|
||||
|
||||
import { localizePersonFieldErrors } from "@/actions/person.messages"
|
||||
|
||||
const actionCopy = {
|
||||
createSuccess: "Persona creada correctamente",
|
||||
createFailure: "Error al crear la persona",
|
||||
updateSuccess: "Persona actualizada correctamente",
|
||||
updateFailure: "Error al actualizar la persona",
|
||||
duplicateEmail: "El correo electrónico ya existe",
|
||||
}
|
||||
|
||||
describe("person action message localization", () => {
|
||||
it("localizes known person field errors (email only, no username)", () => {
|
||||
expect(
|
||||
localizePersonFieldErrors(
|
||||
{
|
||||
email: ["Email already exists"],
|
||||
},
|
||||
actionCopy,
|
||||
),
|
||||
).toEqual({
|
||||
email: [actionCopy.duplicateEmail],
|
||||
})
|
||||
})
|
||||
|
||||
it("keeps unknown messages unchanged", () => {
|
||||
expect(
|
||||
localizePersonFieldErrors(
|
||||
{ firstName: ["Unexpected person issue"] },
|
||||
actionCopy,
|
||||
),
|
||||
).toEqual({ firstName: ["Unexpected person issue"] })
|
||||
})
|
||||
|
||||
it("returns undefined when errors are undefined", () => {
|
||||
expect(localizePersonFieldErrors(undefined, actionCopy)).toBeUndefined()
|
||||
})
|
||||
|
||||
it("returns undefined when errors are empty", () => {
|
||||
expect(localizePersonFieldErrors({}, actionCopy)).toEqual({})
|
||||
})
|
||||
})
|
||||
@@ -1,103 +0,0 @@
|
||||
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")
|
||||
})
|
||||
})
|
||||
@@ -1,38 +0,0 @@
|
||||
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"] })
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user