Skip to content

Commit

Permalink
Added Tests for StripeService
Browse files Browse the repository at this point in the history
  • Loading branch information
asun555 committed Mar 29, 2024
1 parent 88b7c48 commit dff48de
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions server/src/business-layer/services/StripeService.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})

})

0 comments on commit dff48de

Please sign in to comment.