|
| 1 | +/** |
| 2 | + * ! Autogenerated code ! |
| 3 | + * Do not make changes to this file. |
| 4 | + * Changes may be overwritten as part of auto-generation. |
| 5 | + */ |
| 6 | + |
| 7 | +import { PaymentMethodsResource, type ListCustomerPaymentMethodQueryParameters } from '../../resources'; |
| 8 | +import { getPaddleTestClient } from '../helpers/test-client'; |
| 9 | +import { |
| 10 | + PaymentMethodMockResponse, |
| 11 | + PaymentMethodMock, |
| 12 | + ListPaymentMethodMockResponse, |
| 13 | +} from '../mocks/resources/payment-methods.mock'; |
| 14 | + |
| 15 | +describe('PaymentMethodsResource', () => { |
| 16 | + test('should return a list of payment methods', async () => { |
| 17 | + const customerId = PaymentMethodMock.customer_id; |
| 18 | + |
| 19 | + const paddleInstance = getPaddleTestClient(); |
| 20 | + paddleInstance.get = jest.fn().mockResolvedValue(ListPaymentMethodMockResponse); |
| 21 | + |
| 22 | + const paymentMethodsResource = new PaymentMethodsResource(paddleInstance); |
| 23 | + const paymentMethodCollection = paymentMethodsResource.list(customerId); |
| 24 | + |
| 25 | + let paymentMethods = await paymentMethodCollection.next(); |
| 26 | + expect(paddleInstance.get).toBeCalledWith(`/customers/${customerId}/payment-methods?`); |
| 27 | + expect(paymentMethods.length).toBe(1); |
| 28 | + |
| 29 | + paymentMethods = await paymentMethodCollection.next(); |
| 30 | + expect(paddleInstance.get).toBeCalledWith(`/customers/${customerId}/payment-methods?after=1`); |
| 31 | + expect(paymentMethods.length).toBe(1); |
| 32 | + }); |
| 33 | + |
| 34 | + test('should accept query params and return a list of payment methods', async () => { |
| 35 | + const customerId = PaymentMethodMock.customer_id; |
| 36 | + |
| 37 | + const paddleInstance = getPaddleTestClient(); |
| 38 | + paddleInstance.get = jest.fn().mockResolvedValue(ListPaymentMethodMockResponse); |
| 39 | + const paymentMethodsResource = new PaymentMethodsResource(paddleInstance); |
| 40 | + const queryParams: ListCustomerPaymentMethodQueryParameters = { |
| 41 | + after: '2', |
| 42 | + addressId: ['adr_123'], |
| 43 | + }; |
| 44 | + |
| 45 | + const paymentMethodCollection = paymentMethodsResource.list(customerId, queryParams); |
| 46 | + let paymentMethods = await paymentMethodCollection.next(); |
| 47 | + |
| 48 | + expect(paddleInstance.get).toBeCalledWith(`/customers/${customerId}/payment-methods?after=2&address_id=adr_123`); |
| 49 | + expect(paymentMethods.length).toBe(1); |
| 50 | + }); |
| 51 | + |
| 52 | + test('should return a single payment method', async () => { |
| 53 | + const paymentMethodId = PaymentMethodMock.id; |
| 54 | + const customerId = PaymentMethodMock.customer_id; |
| 55 | + |
| 56 | + const paddleInstance = getPaddleTestClient(); |
| 57 | + paddleInstance.get = jest.fn().mockResolvedValue(PaymentMethodMockResponse); |
| 58 | + |
| 59 | + const paymentMethodsResource = new PaymentMethodsResource(paddleInstance); |
| 60 | + const paymentMethod = await paymentMethodsResource.get(customerId, paymentMethodId); |
| 61 | + |
| 62 | + expect(paddleInstance.get).toBeCalledWith(`/customers/${customerId}/payment-methods/${paymentMethodId}`); |
| 63 | + expect(paymentMethod).toBeDefined(); |
| 64 | + expect(paymentMethod.id).toBe(paymentMethodId); |
| 65 | + }); |
| 66 | + |
| 67 | + test('should delete an existing payment method', async () => { |
| 68 | + const paymentMethodId = PaymentMethodMock.id; |
| 69 | + const customerId = PaymentMethodMock.customer_id; |
| 70 | + |
| 71 | + const paddleInstance = getPaddleTestClient(); |
| 72 | + paddleInstance.delete = jest.fn().mockResolvedValue(PaymentMethodMockResponse); |
| 73 | + |
| 74 | + const paymentMethodsResource = new PaymentMethodsResource(paddleInstance); |
| 75 | + const updatedPaymentMethod = await paymentMethodsResource.delete(customerId, paymentMethodId); |
| 76 | + |
| 77 | + expect(paddleInstance.delete).toBeCalledWith(`/customers/${customerId}/payment-methods/${paymentMethodId}`); |
| 78 | + expect(updatedPaymentMethod).toBeUndefined(); |
| 79 | + }); |
| 80 | +}); |
0 commit comments