From dff48de76f75ff44ddbdebaae71fb81b9740873d Mon Sep 17 00:00:00 2001 From: Albert Date: Fri, 29 Mar 2024 20:53:41 +1300 Subject: [PATCH] Added Tests for StripeService --- .../services/StripeService.test.ts | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 server/src/business-layer/services/StripeService.test.ts diff --git a/server/src/business-layer/services/StripeService.test.ts b/server/src/business-layer/services/StripeService.test.ts new file mode 100644 index 000000000..79e88f016 --- /dev/null +++ b/server/src/business-layer/services/StripeService.test.ts @@ -0,0 +1,54 @@ +import StripeService from "./StripeService" +import { productMock } from "test-config/mocks/Stripe.mock" + +jest.mock("stripe", () => { + const stripe = jest.requireActual("stripe") + jest + .spyOn(stripe.resources.Products.prototype, "search") + .mockImplementation(() => Promise.resolve([productMock])) + + jest + .spyOn(stripe.resources.Products.prototype, "list") + .mockImplementation((props) => { + const { limit = 10 } = props as { + limit?: number + starting_after?: string + } + const products = [] + for (let i=0; i < limit; ++i) { + products.push(productMock) + } + + }) + + jest + .spyOn(stripe.resources.Products.prototype, "retrieve") + .mockImplementation((id) => Promise.resolve({ ...productMock, id })) + return stripe +}) + +describe("Stripe service functionality", () => { + + it("should get a product with lookup key", async () => { + const result = await new StripeService().getProductsWithLookupKey( + "random_lookupKey" + ) + expect(result).toEqual(productMock) + }) + + it("should get all products with default length", async () => { + const result = await new StripeService().getAllProducts() + expect(result.length).toBe(10) + }) + + it("should get a product by id", async () => { + const result = await new StripeService().getProductById("random_id") + expect(result).toEqual(productMock) + }) + + it("should get a product by metadata", async () => { + const result = await new StripeService().getProductByMetadata("random_key", "random_value") + expect(result).toEqual(productMock) + }) + +})