Files
stock-manager/tests/unit/actions/recipient.actions.test.ts
T

104 lines
2.9 KiB
TypeScript

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")
})
})