From 06af39724d460974c59cbc440efe5362b45b3e18 Mon Sep 17 00:00:00 2001 From: Xavier Abad <77491413+xabg2@users.noreply.github.com> Date: Fri, 31 Jan 2025 11:16:27 +0100 Subject: [PATCH] tests: getInvoices --- test/drive/payments/index.test.ts | 57 ++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/test/drive/payments/index.test.ts b/test/drive/payments/index.test.ts index c265aab..a75609c 100644 --- a/test/drive/payments/index.test.ts +++ b/test/drive/payments/index.test.ts @@ -2,7 +2,7 @@ import sinon from 'sinon'; import { ApiSecurity, AppDetails } from '../../../src/shared'; import { headersWithToken } from '../../../src/shared/headers'; import { Payments } from '../../../src/drive'; -import { CreatePaymentSessionPayload, StripeSessionMode } from '../../../src/drive/payments/types'; +import { CreatePaymentSessionPayload, StripeSessionMode, UserType } from '../../../src/drive/payments/types'; import { HttpClient } from '../../../src/shared/http/client'; const httpClient = HttpClient.create(''); @@ -35,6 +35,61 @@ describe('payments service', () => { }); }); + describe('getInvoices', () => { + afterEach(() => { + sinon.restore(); + }); + + it('should call with right params & return data', async () => { + const callStub = sinon.stub(httpClient, 'get').resolves([ + { id: 'invoice_123', amount: 1000 }, + { id: 'invoice_456', amount: 2000 }, + ]); + + const { client, headers } = clientAndHeadersWithToken(); + + const params = { + subscriptionId: 'sub_789', + startingAfter: 'invoice_123', + userType: UserType.Individual, + limit: 10, + }; + + const invoices = await client.getInvoices(params); + + const expectedQuery = new URLSearchParams({ + subscription: 'sub_789', + starting_after: 'invoice_123', + userType: UserType.Individual, + limit: '10', + }).toString(); + + expect(callStub.firstCall.args).toEqual([`/invoices?${expectedQuery}`, headers]); + + expect(invoices).toEqual([ + { id: 'invoice_123', amount: 1000 }, + { id: 'invoice_456', amount: 2000 }, + ]); + }); + + it('should handle missing optional parameters correctly', async () => { + const callStub = sinon.stub(httpClient, 'get').resolves([]); + + const { client, headers } = clientAndHeadersWithToken(); + + const params = {}; + + const expectedQuery = new URLSearchParams({ + userType: UserType.Individual, + }).toString(); + + const invoices = await client.getInvoices(params as any); + + expect(callStub.firstCall.args).toEqual([`/invoices?${expectedQuery}`, headers]); + expect(invoices).toEqual([]); + }); + }); + describe('check if product is available', () => { it('should call with right params & return data', async () => { const callStub = sinon.stub(httpClient, 'get').resolves({