feat(link-modules): Cart, Payment Collection link definition (#6508)

* Add cart payment collection joiner confg

* fix migration

* chore: Exclude inventory tests

* remove jest ignore

* add back payment module
This commit is contained in:
Oli Juhl
2024-02-29 19:33:16 +01:00
committed by GitHub
parent 4d38eb3bf8
commit cdb01e073b
8 changed files with 129 additions and 148 deletions

View File

@@ -1,7 +1,8 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ModuleRegistrationName, Modules } from "@medusajs/modules-sdk"
import {
ICartModuleService,
ICustomerModuleService,
IPaymentModuleService,
IRegionModuleService,
ISalesChannelModuleService,
} from "@medusajs/types"
@@ -22,7 +23,8 @@ describe("Cart links", () => {
let regionModule: IRegionModuleService
let customerModule: ICustomerModuleService
let scModuleService: ISalesChannelModuleService
let remoteQuery
let paymentModuleService: IPaymentModuleService
let remoteQuery, remoteLink
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."))
@@ -34,7 +36,9 @@ describe("Cart links", () => {
customerModule = appContainer.resolve(ModuleRegistrationName.CUSTOMER)
scModuleService = appContainer.resolve(ModuleRegistrationName.SALES_CHANNEL)
regionModule = appContainer.resolve(ModuleRegistrationName.REGION)
paymentModuleService = appContainer.resolve(ModuleRegistrationName.PAYMENT)
remoteQuery = appContainer.resolve("remoteQuery")
remoteLink = appContainer.resolve("remoteLink")
})
afterAll(async () => {
@@ -75,6 +79,24 @@ describe("Cart links", () => {
customer_id: customer.id,
})
const paymentCollection =
await paymentModuleService.createPaymentCollections({
currency_code: "usd",
region_id: region.id,
amount: 1000,
})
await remoteLink.create([
{
[Modules.CART]: {
cart_id: cart.id,
},
[Modules.PAYMENT]: {
payment_collection_id: paymentCollection.id,
},
},
])
const carts = await remoteQuery({
cart: {
fields: ["id"],
@@ -87,6 +109,9 @@ describe("Cart links", () => {
sales_channel: {
fields: ["id"],
},
payment_collection: {
fields: ["id"],
},
},
})
@@ -117,6 +142,15 @@ describe("Cart links", () => {
},
})
const paymentCollections = await remoteQuery({
payment: {
fields: ["id"],
cart: {
fields: ["id"],
},
},
})
expect(carts).toEqual(
expect.arrayContaining([
expect.objectContaining({
@@ -124,6 +158,9 @@ describe("Cart links", () => {
customer: expect.objectContaining({ id: customer.id }),
sales_channel: expect.objectContaining({ id: salesChannel.id }),
region: expect.objectContaining({ id: region.id }),
payment_collection: expect.objectContaining({
id: paymentCollection.id,
}),
}),
])
)
@@ -160,5 +197,14 @@ describe("Cart links", () => {
}),
])
)
expect(paymentCollections).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: paymentCollection.id,
cart: expect.objectContaining({ id: cart.id }),
}),
])
)
})
})

View File

@@ -1,3 +1,8 @@
import { PricingModuleService } from "@medusajs/pricing"
import { ProductModuleService } from "@medusajs/product"
import { AxiosInstance } from "axios"
import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
@@ -5,13 +10,8 @@ import {
simpleProductFactory,
simpleRegionFactory,
} from "../../../../factories"
import { AxiosInstance } from "axios"
import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import adminSeeder from "../../../../helpers/admin-seeder"
import { createDefaultRuleTypes } from "../../../helpers/create-default-rule-types"
import { ProductModuleService } from "@medusajs/product"
import { PricingModuleService } from "@medusajs/pricing"
jest.setTimeout(50000)

View File

@@ -1,139 +0,0 @@
import path from "path"
import { startBootstrapApp } from "../../../environment-helpers/bootstrap-app"
import { getContainer } from "../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../environment-helpers/use-db"
jest.setTimeout(30000)
describe("product", () => {
let medusaContainer
let productService
let shutdownServer
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."))
await initDb({ cwd })
shutdownServer = shutdownServer = await startBootstrapApp({ cwd })
medusaContainer = getContainer()
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
afterEach(async () => {
jest.clearAllMocks()
const db = useDb()
await db.teardown()
})
describe("product service", () => {
it("should create variant prices correctly in service creation", async () => {
productService = medusaContainer.resolve("productService")
const payload = {
title: "test-product",
handle: "test-product",
options: [{ title: "test-option" }],
variants: [
{
title: "test-variant",
inventory_quantity: 10,
sku: "test",
options: [{ value: "large", title: "test-option" }],
prices: [{ amount: "100", currency_code: "usd" }],
},
],
}
const { id } = await productService.create(payload)
const result = await productService.retrieve(id, {
relations: ["variants", "variants.prices", "variants.options"],
})
expect(result).toEqual(
expect.objectContaining({
variants: [
expect.objectContaining({
options: [expect.objectContaining({ value: "large" })],
prices: [
expect.objectContaining({ amount: 100, currency_code: "usd" }),
],
}),
],
})
)
})
it("should fail to create a variant without options on for a product with options", async () => {
const payload = {
title: "test-product",
handle: "test-product",
options: [{ title: "test-option" }],
variants: [
{
title: "test-variant",
inventory_quantity: 10,
sku: "test",
prices: [{ amount: "100", currency_code: "usd" }],
},
],
}
let error
try {
await productService.create(payload)
} catch (err) {
error = err
}
expect(error.message).toEqual(
"Product options length does not match variant options length. Product has 1 and variant has 0."
)
})
it("should create a product and variant without options", async () => {
const payload = {
title: "test-product",
handle: "test-product",
variants: [
{
title: "test-variant",
inventory_quantity: 10,
sku: "test",
prices: [{ amount: "100", currency_code: "usd" }],
},
],
}
const { id } = await productService.create(payload)
const result = await productService.retrieve(id, {
relations: [
"options",
"variants",
"variants.prices",
"variants.options",
],
})
expect(result).toEqual(
expect.objectContaining({
options: [],
variants: [
expect.objectContaining({
prices: [
expect.objectContaining({ amount: 100, currency_code: "usd" }),
],
}),
],
})
)
})
})
})

View File

@@ -62,5 +62,6 @@ module.exports = {
[Modules.STORE]: true,
[Modules.TAX]: true,
[Modules.CURRENCY]: true,
[Modules.PAYMENT]: true,
},
}