111 lines
2.8 KiB
TypeScript
111 lines
2.8 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest"
|
|
|
|
const { mockDb } = vi.hoisted(() => ({
|
|
mockDb: {
|
|
$transaction: vi.fn(),
|
|
customer: {
|
|
create: vi.fn(),
|
|
findFirst: vi.fn(),
|
|
findMany: vi.fn(),
|
|
update: vi.fn(),
|
|
},
|
|
commission: {
|
|
create: vi.fn(),
|
|
findMany: vi.fn(),
|
|
update: vi.fn(),
|
|
},
|
|
},
|
|
}))
|
|
|
|
vi.mock("./client", () => ({
|
|
db: mockDb,
|
|
}))
|
|
|
|
import {
|
|
createCommission,
|
|
createCustomer,
|
|
createPublicCommissionRequest,
|
|
updateCommissionStatus,
|
|
} from "./commissions"
|
|
|
|
describe("commissions service", () => {
|
|
beforeEach(() => {
|
|
for (const value of Object.values(mockDb)) {
|
|
if (typeof value === "function") {
|
|
value.mockReset()
|
|
continue
|
|
}
|
|
|
|
for (const fn of Object.values(value)) {
|
|
if (typeof fn === "function") {
|
|
fn.mockReset()
|
|
}
|
|
}
|
|
}
|
|
|
|
mockDb.$transaction.mockImplementation(async (callback) => callback(mockDb))
|
|
})
|
|
|
|
it("creates customer and commission payloads", async () => {
|
|
mockDb.customer.create.mockResolvedValue({ id: "customer-1" })
|
|
mockDb.commission.create.mockResolvedValue({ id: "commission-1" })
|
|
|
|
await createCustomer({
|
|
name: "Ada Lovelace",
|
|
email: "ada@example.com",
|
|
isRecurring: true,
|
|
})
|
|
|
|
await createCommission({
|
|
title: "Portrait Request",
|
|
status: "new",
|
|
customerId: "550e8400-e29b-41d4-a716-446655440000",
|
|
})
|
|
|
|
expect(mockDb.customer.create).toHaveBeenCalledTimes(1)
|
|
expect(mockDb.commission.create).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it("creates a public commission request with customer upsert behavior", async () => {
|
|
mockDb.customer.findFirst.mockResolvedValue({
|
|
id: "customer-existing",
|
|
phone: null,
|
|
instagram: null,
|
|
})
|
|
mockDb.customer.update.mockResolvedValue({
|
|
id: "customer-existing",
|
|
})
|
|
mockDb.commission.create.mockResolvedValue({
|
|
id: "commission-2",
|
|
})
|
|
|
|
await createPublicCommissionRequest({
|
|
customerName: "Grace Hopper",
|
|
customerEmail: "GRACE@EXAMPLE.COM",
|
|
customerPhone: "12345",
|
|
title: "Landscape commission",
|
|
description: "Oil painting request",
|
|
budgetMin: 500,
|
|
budgetMax: 900,
|
|
})
|
|
|
|
expect(mockDb.customer.findFirst).toHaveBeenCalledWith({
|
|
where: { email: "grace@example.com" },
|
|
orderBy: { updatedAt: "desc" },
|
|
})
|
|
expect(mockDb.customer.update).toHaveBeenCalledTimes(1)
|
|
expect(mockDb.commission.create).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it("updates commission status", async () => {
|
|
mockDb.commission.update.mockResolvedValue({ id: "commission-1", status: "done" })
|
|
|
|
await updateCommissionStatus({
|
|
id: "550e8400-e29b-41d4-a716-446655440001",
|
|
status: "done",
|
|
})
|
|
|
|
expect(mockDb.commission.update).toHaveBeenCalledTimes(1)
|
|
})
|
|
})
|