Files
alrahma_sunday_school_api/apps/api/src/services/invoicePdfService.test.ts
T
2026-06-11 03:22:12 -04:00

58 lines
2.1 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from 'vitest'
const renderToBufferMock = vi.fn().mockResolvedValue(Buffer.from('pdf-bytes'))
vi.mock('@react-pdf/renderer', () => ({
renderToBuffer: (...args: any[]) => renderToBufferMock(...args),
Document: 'Document',
Page: 'Page',
Text: 'Text',
View: 'View',
StyleSheet: { create: (styles: any) => styles },
}))
import { buildInvoiceNumber, generateInvoicePdf } from './invoicePdfService'
describe('invoicePdfService', () => {
afterEach(() => {
vi.clearAllMocks()
})
it('builds stable legacy invoice numbers from the creation year and id suffix', () => {
expect(buildInvoiceNumber('invoice_abcdef', new Date('2026-06-09T12:00:00.000Z'))).toBe('INV-2026-ABCDEF')
expect(buildInvoiceNumber('short', new Date('2027-01-01T00:00:00.000Z'))).toBe('INV-2027-SHORT')
})
it('renders a subscription invoice document into a buffer', async () => {
const result = await generateInvoicePdf({
invoiceNumber: 'INV-2026-000001',
issueDate: '2026-06-09T00:00:00.000Z',
dueDate: null,
company: { name: 'Atlas Cars', email: 'billing@atlas.test', phone: '+212600000000', address: { formatted: 'Casablanca' } },
subscription: { plan: 'PRO', billingPeriod: 'MONTHLY', currency: 'MAD' },
amount: 99000,
currency: 'MAD',
status: 'PAID',
paymentProvider: 'PAYPAL',
transactionId: 'txn_1',
paidAt: '2026-06-09T12:00:00.000Z',
lineItems: [{ description: 'Pro monthly subscription', amount: 99000, currency: 'MAD', quantity: 1, unitAmount: 99000 }],
totals: {
subtotalAmount: 99000,
discountAmount: 0,
creditAmount: 0,
taxAmount: 0,
totalAmount: 99000,
amountPaid: 99000,
amountDue: 0,
},
})
expect(result).toEqual(Buffer.from('pdf-bytes'))
expect(renderToBufferMock).toHaveBeenCalledTimes(1)
expect(renderToBufferMock.mock.calls[0][0]).toEqual(expect.objectContaining({
props: expect.objectContaining({ data: expect.objectContaining({ invoiceNumber: 'INV-2026-000001' }) }),
}))
})
})