Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
anbraten committed Nov 6, 2023
1 parent 5c678d6 commit 0225b1e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
22 changes: 19 additions & 3 deletions packages/server/src/loop.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,17 @@ describe('Loop', () => {

// then
expect(persistAndFlush).toBeCalledTimes(2);
const [[updatedInvoice]] = persistAndFlush.mock.calls[1] as [[Invoice]];
const [[updatedInvoice, payment]] = persistAndFlush.mock.calls[1] as [[Invoice, Payment]];
expect(updatedInvoice).toBeDefined();
expect(updatedInvoice.items.getItems().find((i) => i.description === 'Credit')).toBeDefined();
expect(updatedInvoice.amount).toStrictEqual(Invoice.roundPrice(invoiceAmount - balance));
expect(updatedInvoice.totalAmount).toStrictEqual(
Invoice.roundPrice((invoiceAmount - balance) * (1 + invoice.vatRate / 100)),
);

expect(payment).toBeDefined();
expect(payment.amount).toStrictEqual(updatedInvoice.totalAmount);
expect(payment.status).toStrictEqual('processing');
});

it('should apply customer balance when charging and keep remaining balance', async () => {
Expand Down Expand Up @@ -242,20 +246,32 @@ describe('Loop', () => {
customer.balance = balance;
const invoiceAmount = invoice.amount;

const now = dayjs('2021-01-01').toDate();
vi.setSystemTime(now);

// when
await chargeCustomerInvoice(invoice);

// then
expect(persistAndFlush).toBeCalledTimes(2);
expect(persistAndFlush).toBeCalledTimes(3);

const [[updatedCustomer]] = persistAndFlush.mock.calls[0] as [[Customer]];
expect(updatedCustomer).toBeDefined();
expect(updatedCustomer.balance).toStrictEqual(balance - invoiceAmount);

const [[updatedInvoice]] = persistAndFlush.mock.calls[1] as [[Invoice]];
const [[updatedSubscription]] = persistAndFlush.mock.calls[1] as [[Subscription]];
expect(updatedSubscription).toBeDefined();
expect(updatedSubscription.lastPayment).toStrictEqual(now);

const [[updatedInvoice, payment]] = persistAndFlush.mock.calls[2] as [[Invoice, Payment]];
expect(updatedInvoice).toBeDefined();
expect(updatedInvoice.items.getItems().find((i) => i.description === 'Credit')).toBeDefined();
expect(updatedInvoice.amount).toStrictEqual(0);
expect(updatedInvoice.totalAmount).toStrictEqual(0);
expect(updatedInvoice.status).toStrictEqual('paid');

expect(payment).toBeDefined();
expect(payment.amount).toStrictEqual(updatedInvoice.totalAmount);
expect(payment.status).toStrictEqual('paid');
});
});
2 changes: 1 addition & 1 deletion packages/server/src/loop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export async function chargeCustomerInvoice(invoice: Invoice): Promise<void> {
);
customer.balance = Invoice.roundPrice(customer.balance - creditAmount);
await database.em.persistAndFlush([customer]);
amount = Invoice.roundPrice(invoice.totalAmount);
amount = Invoice.roundPrice(invoice.totalAmount); // update amount from updated invoice
log.debug({ customerId: customer._id, creditAmount }, 'Credit applied');
}

Expand Down

0 comments on commit 0225b1e

Please sign in to comment.