From db718d6fc36728c6e8d19cc5ce92db40b44c5ca0 Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Tue, 19 Nov 2024 12:48:10 +0000 Subject: [PATCH] test(user): write tests for getInvoices --- packages/backend/src/models/user.test.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/packages/backend/src/models/user.test.js b/packages/backend/src/models/user.test.js index e02b5b67..90786158 100644 --- a/packages/backend/src/models/user.test.js +++ b/packages/backend/src/models/user.test.js @@ -28,6 +28,7 @@ import { createStep } from '../../test/factories/step.js'; import { createExecution } from '../../test/factories/execution.js'; import { createSubscription } from '../../test/factories/subscription.js'; import { createUsageData } from '../../test/factories/usage-data.js'; +import Billing from '../helpers/billing/index.ee.js'; describe('User model', () => { it('tableName should return correct name', () => { @@ -1105,4 +1106,26 @@ describe('User model', () => { expect(() => user.getPlanAndUsage()).rejects.toThrow('NotFoundError'); }); }); + + describe('getInvoices', () => { + it('should return invoices for the current subscription', async () => { + const user = await createUser(); + const subscription = await createSubscription({ userId: user.id }); + + const getInvoicesSpy = vi + .spyOn(Billing.paddleClient, 'getInvoices') + .mockResolvedValue('dummy-invoices'); + + expect(await user.getInvoices()).toBe('dummy-invoices'); + expect(getInvoicesSpy).toHaveBeenCalledWith( + Number(subscription.paddleSubscriptionId) + ); + }); + + it('should return empty array without any subscriptions', async () => { + const user = await createUser(); + + expect(await user.getInvoices()).toStrictEqual([]); + }); + }); });