Skip to content

Commit

Permalink
Tests continuation
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien2p committed Feb 15, 2023
1 parent 71eb7f5 commit 6e2a71b
Show file tree
Hide file tree
Showing 13 changed files with 506 additions and 14 deletions.
4 changes: 4 additions & 0 deletions packages/medusa-payment-stripe-processor/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dist
node_modules
.DS_store
yarn.lock
3 changes: 1 addition & 2 deletions packages/medusa-payment-stripe-processor/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {
// : [`default`].concat(useCoverage ? `jest-junit` : []),
globals: {
"ts-jest": {
tsConfig: "tsconfig.spec.json",
tsconfig: "tsconfig.spec.json",
isolatedModules: false,
},
},
Expand All @@ -20,5 +20,4 @@ module.exports = {
},
testEnvironment: `node`,
moduleFileExtensions: [`js`, `jsx`, `ts`, `tsx`, `json`],
setupFilesAfterEnv: ["<rootDir>/setupTests.js"],
}
7 changes: 3 additions & 4 deletions packages/medusa-payment-stripe-processor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@
"watch": "tsc --watch"
},
"devDependencies": {
"@medusajs/medusa": "^1.7.7",
"@types/stripe": "^8.0.417",
"cross-env": "^5.2.1",
"jest": "^25.5.4",
"medusa-interfaces": "^1.3.5",
"medusa-test-utils": "^1.1.37"
"jest": "^25.5.4"
},
"peerDependencies": {
"medusa-interfaces": "1.3.5"
"@medusajs/medusa": "^1.7.7"
},
"dependencies": {
"body-parser": "^1.19.0",
Expand Down
30 changes: 30 additions & 0 deletions packages/medusa-payment-stripe-processor/src/__fixtures__/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export const PaymentIntentDataByStatus = {
REQUIRE_PAYMENT_METHOD: {
id: "requires_payment_method",
status: "requires_payment_method"
},
REQUIRES_CONFIRMATION: {
id: "requires_confirmation",
status: "requires_confirmation"
},
PROCESSING: {
id: "processing",
status: "processing"
},
REQUIRES_ACTION: {
id: "requires_action",
status: "requires_action"
},
CANCELED: {
id: "canceled",
status: "canceled"
},
REQUIRES_CAPTURE: {
id: "requires_capture",
status: "requires_capture"
},
SUCCEEDED: {
id: "succeeded",
status: "succeeded"
},
}
216 changes: 216 additions & 0 deletions packages/medusa-payment-stripe-processor/src/__mocks__/cart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
import { IdMap } from "medusa-test-utils"

export const carts = {
emptyCart: {
id: IdMap.getId("emptyCart"),
items: [],
region_id: IdMap.getId("testRegion"),
customer_id: "test-customer",
payment_sessions: [],
shipping_options: [
{
id: IdMap.getId("freeShipping"),
profile_id: "default_profile",
data: {
some_data: "yes",
},
},
],
},
frCart: {
id: IdMap.getId("fr-cart"),
email: "[email protected]",
title: "test",
region_id: IdMap.getId("region-france"),
items: [
{
id: IdMap.getId("line"),
title: "merge line",
description: "This is a new line",
thumbnail: "test-img-yeah.com/thumb",

unit_price: 8,
variant: {
id: IdMap.getId("eur-8-us-10"),
},
product: {
id: IdMap.getId("product"),
},
// {
// unit_price: 10,
// variant: {
// id: IdMap.getId("eur-10-us-12"),
// },
// product: {
// id: IdMap.getId("product"),
// },
// quantity: 1,
// },
quantity: 10,
},
{
id: IdMap.getId("existingLine"),
title: "merge line",
description: "This is a new line",
thumbnail: "test-img-yeah.com/thumb",
unit_price: 10,
variant: {
id: IdMap.getId("eur-10-us-12"),
},
product: {
id: IdMap.getId("product"),
},
quantity: 1,
},
],
shipping_methods: [
{
id: IdMap.getId("freeShipping"),
profile_id: "default_profile",
},
],
shipping_options: [
{
id: IdMap.getId("freeShipping"),
profile_id: "default_profile",
},
],
payment_sessions: [
{
provider_id: "stripe",
data: {
id: "pi_123456789",
customer: IdMap.getId("not-lebron"),
},
},
],
payment_method: {
provider_id: "stripe",
data: {
id: "pi_123456789",
customer: IdMap.getId("not-lebron"),
},
},
region: { currency_code: "usd" },
total: 100,
shipping_address: {},
billing_address: {},
discounts: [],
customer_id: IdMap.getId("lebron"),
context: {}
},
frCartNoStripeCustomer: {
id: IdMap.getId("fr-cart-no-customer"),
title: "test",
region_id: IdMap.getId("region-france"),
items: [
{
id: IdMap.getId("line"),
title: "merge line",
description: "This is a new line",
thumbnail: "test-img-yeah.com/thumb",
content: [
{
unit_price: 8,
variant: {
id: IdMap.getId("eur-8-us-10"),
},
product: {
id: IdMap.getId("product"),
},
quantity: 1,
},
{
unit_price: 10,
variant: {
id: IdMap.getId("eur-10-us-12"),
},
product: {
id: IdMap.getId("product"),
},
quantity: 1,
},
],
quantity: 10,
},
{
id: IdMap.getId("existingLine"),
title: "merge line",
description: "This is a new line",
thumbnail: "test-img-yeah.com/thumb",
content: {
unit_price: 10,
variant: {
id: IdMap.getId("eur-10-us-12"),
},
product: {
id: IdMap.getId("product"),
},
quantity: 1,
},
quantity: 10,
},
],
shipping_methods: [
{
id: IdMap.getId("freeShipping"),
profile_id: "default_profile",
},
],
shipping_options: [
{
id: IdMap.getId("freeShipping"),
profile_id: "default_profile",
},
],
payment_sessions: [
{
provider_id: "stripe",
data: {
id: "pi_no",
customer: IdMap.getId("not-lebron"),
},
},
],
payment_method: {
provider_id: "stripe",
data: {
id: "pi_no",
customer: IdMap.getId("not-lebron"),
},
},
shipping_address: {},
billing_address: {},
discounts: [],
customer_id: IdMap.getId("vvd"),
},
}

export const CartServiceMock = {
withTransaction: function () {
return this
},
retrieve: jest.fn().mockImplementation((cartId) => {
if (cartId === IdMap.getId("fr-cart")) {
return Promise.resolve(carts.frCart)
}
if (cartId === IdMap.getId("fr-cart-no-customer")) {
return Promise.resolve(carts.frCartNoStripeCustomer)
}
if (cartId === IdMap.getId("emptyCart")) {
return Promise.resolve(carts.emptyCart)
}
return Promise.resolve(undefined)
}),
updatePaymentSession: jest
.fn()
.mockImplementation((cartId, stripe, paymentIntent) => {
return Promise.resolve()
}),
}

const mock = jest.fn().mockImplementation(() => {
return CartServiceMock
})

export default mock
39 changes: 39 additions & 0 deletions packages/medusa-payment-stripe-processor/src/__mocks__/customer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { IdMap } from "medusa-test-utils"

export const CustomerServiceMock = {
withTransaction: function () {
return this
},
retrieve: jest.fn().mockImplementation((id) => {
if (id === IdMap.getId("lebron")) {
return Promise.resolve({
_id: IdMap.getId("lebron"),
first_name: "LeBron",
last_name: "James",
email: "[email protected]",
password_hash: "1234",
metadata: {
stripe_id: "cus_123456789_new",
},
})
}
if (id === IdMap.getId("vvd")) {
return Promise.resolve({
_id: IdMap.getId("vvd"),
first_name: "Virgil",
last_name: "Van Dijk",
email: "[email protected]",
password_hash: "1234",
metadata: {},
})
}
return Promise.resolve(undefined)
}),
setMetadata: jest.fn().mockReturnValue(Promise.resolve()),
}

const mock = jest.fn().mockImplementation(() => {
return CustomerServiceMock
})

export default mock
10 changes: 10 additions & 0 deletions packages/medusa-payment-stripe-processor/src/__mocks__/eventbus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const EventBusServiceMock = {
emit: jest.fn(),
subscribe: jest.fn(),
}

const mock = jest.fn().mockImplementation(() => {
return EventBusServiceMock
})

export default mock
37 changes: 37 additions & 0 deletions packages/medusa-payment-stripe-processor/src/__mocks__/stripe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { PaymentIntentDataByStatus } from "../__fixtures__/data";

function buildPaymentIntent(data) {
const paymentIntentData = {}

return Object.assign(paymentIntentData, data)
}

export const StripeMock = {
retrieve: jest.fn().mockImplementation(async (paymentId) => {
if (paymentId === PaymentIntentDataByStatus.REQUIRE_PAYMENT_METHOD) {
return buildPaymentIntent({ status: PaymentIntentDataByStatus.REQUIRE_PAYMENT_METHOD })
}
if (paymentId === PaymentIntentDataByStatus.REQUIRES_CONFIRMATION) {
return buildPaymentIntent({ status: PaymentIntentDataByStatus.REQUIRES_CONFIRMATION })
}
if (paymentId === PaymentIntentDataByStatus.PROCESSING) {
return buildPaymentIntent({ status: PaymentIntentDataByStatus.PROCESSING })
}
if (paymentId === PaymentIntentDataByStatus.CANCELED) {
return buildPaymentIntent({ status: PaymentIntentDataByStatus.REQUIRES_ACTION })
}
if (paymentId === PaymentIntentDataByStatus.CANCELED) {
return buildPaymentIntent({ status: PaymentIntentDataByStatus.CANCELED })
}
if (paymentId === PaymentIntentDataByStatus.REQUIRES_CAPTURE) {
return buildPaymentIntent({ status: PaymentIntentDataByStatus.REQUIRES_CAPTURE })
}
if (paymentId === PaymentIntentDataByStatus.SUCCEEDED) {
return buildPaymentIntent({ status: PaymentIntentDataByStatus.SUCCEEDED })
}
})
}

const stripe = jest.fn(() => StripeMock)

export default stripe
12 changes: 12 additions & 0 deletions packages/medusa-payment-stripe-processor/src/__mocks__/totals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const TotalsServiceMock = {
withTransaction: function () {
return this
},
getTotal: jest.fn(),
}

const mock = jest.fn().mockImplementation(() => {
return TotalsServiceMock
})

export default mock
Loading

0 comments on commit 6e2a71b

Please sign in to comment.