113 lines
3.1 KiB
TypeScript
113 lines
3.1 KiB
TypeScript
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],
|
|
},
|
|
})
|
|
})
|
|
})
|