db953b1e28
- Implement tests for client validation functions including tax ID, VAT ID, IBAN, BIC, website, and company form validation. - Create tests for revenue and expense categories ensuring all expected categories and labels are present. - Add tests for invoice number generation with various scenarios including prefix handling and sequence padding. - Introduce tests for default categories and their integration, ensuring no overlaps and consistent naming conventions. - Implement Zod schema validation tests for currency, tax rates, IBAN, tax ID, VAT ID, invoices, companies, and customers. - Add utility tests for tax calculations, including item amounts and invoice totals, ensuring correct handling of tax rates and formatting. - Set up Vitest configuration and global test setup for consistent testing environment.
64 lines
1.7 KiB
TypeScript
64 lines
1.7 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import prisma from "@/lib/prisma.server";
|
|
import { generateInvoiceNumber } from "@/lib/invoice-number.server";
|
|
|
|
// Mock the Prisma client
|
|
vi.mock("@/lib/prisma.server", () => ({
|
|
default: {
|
|
company: {
|
|
update: vi.fn(),
|
|
},
|
|
},
|
|
}));
|
|
|
|
describe("invoice-number.server.ts", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it("should generate invoice number with year and sequence", async () => {
|
|
const mockCompany = {
|
|
invoicePrefix: "RE",
|
|
invoiceSequence: 5, // Already incremented by Prisma
|
|
};
|
|
|
|
(prisma.company.update as any).mockResolvedValue(mockCompany);
|
|
|
|
const result = await generateInvoiceNumber("company-123");
|
|
|
|
// invoiceSequence is already 5 (after increment), so we expect 005
|
|
expect(result).toBe("RE-2026-005");
|
|
expect(prisma.company.update).toHaveBeenCalledWith({
|
|
where: { id: "company-123" },
|
|
data: { invoiceSequence: { increment: 1 } },
|
|
select: { invoicePrefix: true, invoiceSequence: true },
|
|
});
|
|
});
|
|
|
|
it("should pad sequence with zeros", async () => {
|
|
const mockCompany = {
|
|
invoicePrefix: "RG",
|
|
invoiceSequence: 2, // Already incremented by Prisma
|
|
};
|
|
|
|
(prisma.company.update as any).mockResolvedValue(mockCompany);
|
|
|
|
const result = await generateInvoiceNumber("company-456");
|
|
|
|
expect(result).toBe("RG-2026-002");
|
|
});
|
|
|
|
it("should handle custom prefix", async () => {
|
|
const mockCompany = {
|
|
invoicePrefix: "INV",
|
|
invoiceSequence: 10, // Already incremented by Prisma
|
|
};
|
|
|
|
(prisma.company.update as any).mockResolvedValue(mockCompany);
|
|
|
|
const result = await generateInvoiceNumber("company-789");
|
|
|
|
expect(result).toBe("INV-2026-010");
|
|
});
|
|
});
|