Skip to content

Commit

Permalink
fix total count undefined value
Browse files Browse the repository at this point in the history
  • Loading branch information
Alberto Blacutt authored and Alberto Blacutt committed Feb 16, 2024
1 parent 33fdaef commit 83260f7
Show file tree
Hide file tree
Showing 18 changed files with 312 additions and 287 deletions.
4 changes: 2 additions & 2 deletions e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"license": "MIT",
"sideEffects": false,
"scripts": {
"test": "jest --silent",
"test": "jest",
"coverage": "jest --coverage",
"lint": "eslint --ignore-path .eslintignore --ext spec.js,spec.ts .",
"format": "prettier --write \"src/e2e/**/*.{js,ts}\""
Expand Down Expand Up @@ -48,6 +48,6 @@
"contributors": [],
"dependencies": {
"advanced-billing-sdk": "../",
"dotenv": "^16.4.4"
"uid": "^2.0.2"
}
}
174 changes: 112 additions & 62 deletions e2e/src/billingPortalController.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,30 @@ import {
BillingPortalController,
CustomersController,
Customer,
CustomerResponse
CustomerResponse,
} from 'advanced-billing-sdk';

describe('Billing Portal Controller', () => {
let validCustomer: Customer;
let enabledCustomerIdPForBillingPortal: number;
const perPage = 10000;

const validClient = createClient();
const validCustomerBody = {
customer: {
firstName: "Martha",
lastName: "Washington",
email: "[email protected]",
ccEmails: "[email protected]",
}
firstName: 'Martha',
lastName: 'Washington',
email: '[email protected]',
ccEmails: '[email protected]',
},
};
const validSecondCustomerBody = {
customer: {
firstName: "George",
lastName: "Washington",
email: "[email protected]",
ccEmails: "[email protected]",
}
firstName: 'George',
lastName: 'Washington',
email: '[email protected]',
ccEmails: '[email protected]',
},
};
const invalidClient = createClient({
timeout: 0,
Expand All @@ -41,47 +42,70 @@ describe('Billing Portal Controller', () => {

const customersController = new CustomersController(validClient);
const billingPortalController = new BillingPortalController(validClient);
const invalidBillingPortalController = new BillingPortalController(invalidClient);
const invalidBillingPortalController = new BillingPortalController(
invalidClient
);

beforeAll(async () => {
validCustomer = (await customersController.createCustomer(validCustomerBody)).result.customer;
const { result: { customer } } = await billingPortalController.enableBillingPortalForCustomer(validCustomer.id || 0);
validCustomer = (
await customersController.createCustomer(validCustomerBody)
).result.customer;
const {
result: { customer },
} = await billingPortalController.enableBillingPortalForCustomer(
validCustomer.id || 0
);
enabledCustomerIdPForBillingPortal = customer.id || 0;
});

describe('Enable Billing Portal for Customer', () => {
test('should get a billing portal enabled for the customer', async () => {
const { result: { customer }} = await customersController.createCustomer(validSecondCustomerBody);
const customerIDs = (await customersController.listCustomers({})).result
.map((value: CustomerResponse) => value.customer.id);
const {
result: { customer },
} = await customersController.createCustomer(validSecondCustomerBody);
const customerIDs = (
await customersController.listCustomers({ perPage })
).result.map((value: CustomerResponse) => value.customer.id);
const existentCustomerID = customer.id || 0;
expect(existentCustomerID).not.toBe(0);
expect(customerIDs.includes(existentCustomerID)).toBeTruthy();
const { statusCode } = await billingPortalController.enableBillingPortalForCustomer(existentCustomerID);
const { statusCode } =
await billingPortalController.enableBillingPortalForCustomer(
existentCustomerID
);
expect(statusCode).toBe(200);
});

test('should thrown an error when enabled billing portal with invalid customer ID', async () => {
const promise = billingPortalController.enableBillingPortalForCustomer(invalidCustomerId);
const promise =
billingPortalController.enableBillingPortalForCustomer(
invalidCustomerId
);
expect(promise).rejects.toThrow();
await promise.catch((error) => {
expect(error.statusCode).toBe(404);
});
});

test('should thrown an error when enabled billing portal with unauthorized client', async () => {
const promise = invalidBillingPortalController.enableBillingPortalForCustomer(Number(validCustomer.id));
const promise =
invalidBillingPortalController.enableBillingPortalForCustomer(
Number(validCustomer.id)
);
expect(promise).rejects.toThrow();
await promise.catch((error) => {
expect(error.statusCode).toBe(401);
});
});

test('should thrown an error when enabled billing portal with non-existent customer ID', async () => {
const customerIDs = (await customersController.listCustomers({})).result
.map((value: CustomerResponse) => value.customer.id);
const customerIDs = (
await customersController.listCustomers({})
).result.map((value: CustomerResponse) => value.customer.id);
expect(customerIDs.includes(nonExistentCustomerId)).toBeFalsy();
const promise = billingPortalController.enableBillingPortalForCustomer(nonExistentCustomerId);
const promise = billingPortalController.enableBillingPortalForCustomer(
nonExistentCustomerId
);
expect(promise).rejects.toThrow();
await promise.catch((error) => {
expect(error.statusCode).toBe(404);
Expand All @@ -91,39 +115,47 @@ describe('Billing Portal Controller', () => {

describe('Read Billing Portal Management Link', () => {
test('should read a valid management link retrieval', async () => {
const customerIDs = (await customersController.listCustomers({})).result
.map((value: CustomerResponse) => value.customer.id);
const customerIDs = (
await customersController.listCustomers({ perPage })
).result.map((value: CustomerResponse) => value.customer.id);
expect(enabledCustomerIdPForBillingPortal).not.toBe(0);
expect(customerIDs.includes(enabledCustomerIdPForBillingPortal)).toBeTruthy();
const { statusCode } = await billingPortalController.readBillingPortalLink(enabledCustomerIdPForBillingPortal);
expect(
customerIDs.includes(enabledCustomerIdPForBillingPortal)
).toBeTruthy();
const { statusCode } =
await billingPortalController.readBillingPortalLink(
enabledCustomerIdPForBillingPortal
);
expect(statusCode).toBe(200);
});

test('should thrown an error when read billing portal with invalid customer ID', async () => {
const promise = billingPortalController.readBillingPortalLink(invalidCustomerId);
const promise =
billingPortalController.readBillingPortalLink(invalidCustomerId);
expect(promise).rejects.toThrow();
await promise.catch((error) => {
expect(error.statusCode).toBe(404);
});
});

test('should thrown an error when read billing portal with unauthorized client', async () => {
const customerIDs = (await customersController.listCustomers({})).result
.map((value: CustomerResponse) => value.customer.id);
expect(enabledCustomerIdPForBillingPortal).not.toBe(0);
expect(customerIDs.includes(enabledCustomerIdPForBillingPortal)).toBeTruthy();
const promise = invalidBillingPortalController.readBillingPortalLink(enabledCustomerIdPForBillingPortal);
const promise = invalidBillingPortalController.readBillingPortalLink(
enabledCustomerIdPForBillingPortal
);
expect(promise).rejects.toThrow();
await promise.catch((error) => {
expect(error.statusCode).toBe(401);
});
});

test('should thrown an error when read billing portal with non-existent customer ID', async () => {
const customerIDs = (await customersController.listCustomers({})).result
.map((value: CustomerResponse) => value.customer.id);
const customerIDs = (
await customersController.listCustomers({})
).result.map((value: CustomerResponse) => value.customer.id);
expect(customerIDs.includes(nonExistentCustomerId)).toBeFalsy();
const promise = billingPortalController.readBillingPortalLink(nonExistentCustomerId);
const promise = billingPortalController.readBillingPortalLink(
nonExistentCustomerId
);
expect(promise).rejects.toThrow();
await promise.catch((error) => {
expect(error.statusCode).toBe(404);
Expand All @@ -133,39 +165,50 @@ describe('Billing Portal Controller', () => {

describe('Resend Billing Portal Invitation', () => {
test('should resending a billing portal invitation', async () => {
const customerIDs = (await customersController.listCustomers({})).result
.map((value: CustomerResponse) => value.customer.id);
const customerIDs = (
await customersController.listCustomers({ perPage })
).result.map((value: CustomerResponse) => value.customer.id);
expect(enabledCustomerIdPForBillingPortal).not.toBe(0);
expect(customerIDs.includes(enabledCustomerIdPForBillingPortal)).toBeTruthy();
const { statusCode } = await billingPortalController.resendBillingPortalInvitation(enabledCustomerIdPForBillingPortal);
expect(
customerIDs.includes(enabledCustomerIdPForBillingPortal)
).toBeTruthy();
const { statusCode } =
await billingPortalController.resendBillingPortalInvitation(
enabledCustomerIdPForBillingPortal
);
expect(statusCode).toBe(200);
});

test('should thrown an error when resend billing portal invitation with invalid customer ID', async () => {
const promise = billingPortalController.resendBillingPortalInvitation(invalidCustomerId);
const promise =
billingPortalController.resendBillingPortalInvitation(
invalidCustomerId
);
expect(promise).rejects.toThrow();
await promise.catch((error) => {
expect(error.statusCode).toBe(404);
});
});

test('should thrown an error when resend billing portal invitation with unauthorized client', async () => {
const customerIDs = (await customersController.listCustomers({})).result
.map((value: CustomerResponse) => value.customer.id);
expect(enabledCustomerIdPForBillingPortal).not.toBe(0);
expect(customerIDs.includes(enabledCustomerIdPForBillingPortal)).toBeTruthy();
const promise = invalidBillingPortalController.resendBillingPortalInvitation(enabledCustomerIdPForBillingPortal);
const promise =
invalidBillingPortalController.resendBillingPortalInvitation(
enabledCustomerIdPForBillingPortal
);
expect(promise).rejects.toThrow();
await promise.catch((error) => {
expect(error.statusCode).toBe(401);
});
});

test('should thrown an error when resend billing portal invitation with non-existent customer ID', async () => {
const customerIDs = (await customersController.listCustomers({})).result
.map((value: CustomerResponse) => value.customer.id);
const customerIDs = (
await customersController.listCustomers({})
).result.map((value: CustomerResponse) => value.customer.id);
expect(customerIDs.includes(nonExistentCustomerId)).toBeFalsy();
const promise = billingPortalController.resendBillingPortalInvitation(nonExistentCustomerId);
const promise = billingPortalController.resendBillingPortalInvitation(
nonExistentCustomerId
);
expect(promise).rejects.toThrow();
await promise.catch((error) => {
expect(error.statusCode).toBe(404);
Expand All @@ -175,39 +218,46 @@ describe('Billing Portal Controller', () => {

describe('Revoke Billing Portal Invitation for Customer', () => {
test('should revoke a billing portal invitation:', async () => {
const customerIDs = (await customersController.listCustomers({})).result
.map((value: CustomerResponse) => value.customer.id);
const customerIDs = (
await customersController.listCustomers({ perPage })
).result.map((value: CustomerResponse) => value.customer.id);
expect(enabledCustomerIdPForBillingPortal).not.toBe(0);
expect(customerIDs.includes(enabledCustomerIdPForBillingPortal)).toBeTruthy();
const response = await billingPortalController.revokeBillingPortalAccess(enabledCustomerIdPForBillingPortal);
expect(
customerIDs.includes(enabledCustomerIdPForBillingPortal)
).toBeTruthy();
const response = await billingPortalController.revokeBillingPortalAccess(
enabledCustomerIdPForBillingPortal
);
expect(response.statusCode).toBe(200);
});

test('should thrown an error when revoke billing portal invitation with invalid customer ID', async () => {
const promise = billingPortalController.revokeBillingPortalAccess(invalidCustomerId);
const promise =
billingPortalController.revokeBillingPortalAccess(invalidCustomerId);
expect(promise).rejects.toThrow();
await promise.catch((error) => {
expect(error.statusCode).toBe(404);
});
});

test('should thrown an error when revoke billing portal invitation with unauthorized client', async () => {
const customerIDs = (await customersController.listCustomers({})).result
.map((value: CustomerResponse) => value.customer.id);
expect(enabledCustomerIdPForBillingPortal).not.toBe(0);
expect(customerIDs.includes(enabledCustomerIdPForBillingPortal)).toBeTruthy();
const promise = invalidBillingPortalController.revokeBillingPortalAccess(enabledCustomerIdPForBillingPortal);
const promise = invalidBillingPortalController.revokeBillingPortalAccess(
enabledCustomerIdPForBillingPortal
);
expect(promise).rejects.toThrow();
await promise.catch((error) => {
expect(error.statusCode).toBe(401);
});
});

test('should thrown an error when revoke billing portal invitation with non-existent customer ID', async () => {
const customerIDs = (await customersController.listCustomers({})).result
.map((value: CustomerResponse) => value.customer.id);
const customerIDs = (
await customersController.listCustomers({})
).result.map((value: CustomerResponse) => value.customer.id);
expect(customerIDs.includes(nonExistentCustomerId)).toBeFalsy();
const promise = billingPortalController.revokeBillingPortalAccess(nonExistentCustomerId);
const promise = billingPortalController.revokeBillingPortalAccess(
nonExistentCustomerId
);
expect(promise).rejects.toThrow();
await promise.catch((error) => {
expect(error.statusCode).toBe(404);
Expand Down
10 changes: 6 additions & 4 deletions e2e/src/couponsController.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { createClient, CONFIG } from './config';
import { product } from './mocks/products';
import { createMockSubscription } from './mocks/subscriptions';
import { uid } from 'uid';

const couponBody = {
coupon: {
Expand Down Expand Up @@ -61,7 +62,7 @@ describe('Coupons Controller', () => {
(
await productFamiliesController.createProductFamily({
productFamily: {
name: 'coupons-product-family ab',
name: `coupons-product-family-${uid()}`,
description: 'coupons product family a',
},
})
Expand Down Expand Up @@ -432,16 +433,17 @@ describe('Coupons Controller', () => {

describe('List Coupon Usages', () => {
test('should list the usages coupons ', async () => {
const handle = `${uid()}-coupon-handler`;
const productResponse = await productsController.createProduct(
productFamily?.id || 0,
{
product: { ...product, handle: 'coupon-handler' },
product: { ...product, handle },
}
);

const subscriptionBody = createMockSubscription({
productHandle: 'coupon-handler',
customerReference: 'coupon-reference',
productHandle: handle,
customerReference: uid(),
});

const subscriptionResponse = (
Expand Down
Loading

0 comments on commit 83260f7

Please sign in to comment.