fix: normalize path before consuming it while loading models (#8194)
* fix: normalize path before consuming it while loading models * Make it so that models derived from the service if present * fix linkable tests that now match what has been passed to the medusa service if there is no custom joiner config * fix linkable tests that now match what has been passed to the medusa service if there is no custom joiner config * fix linkable tests that now match what has been passed to the medusa service if there is no custom joiner config
This commit is contained in:
committed by
GitHub
parent
2f56030101
commit
566300d54b
@@ -5,7 +5,7 @@ import {
|
||||
PropertyType,
|
||||
} from "@medusajs/types"
|
||||
import * as path from "path"
|
||||
import { dirname, join } from "path"
|
||||
import { dirname, join, normalize } from "path"
|
||||
import {
|
||||
camelToSnakeCase,
|
||||
deduplicate,
|
||||
@@ -77,11 +77,16 @@ export function defineJoinerConfig(
|
||||
break
|
||||
}
|
||||
|
||||
fullPath = normalize(fullPath)
|
||||
const integrationTestPotentialPath = normalize(
|
||||
"integration-tests/__tests__"
|
||||
)
|
||||
|
||||
/**
|
||||
* Handle integration-tests/__tests__ path based on conventional naming
|
||||
*/
|
||||
if (fullPath.includes("integration-tests/__tests__")) {
|
||||
const sourcePath = fullPath.split("integration-tests/__tests__")[0]
|
||||
if (fullPath.includes(integrationTestPotentialPath)) {
|
||||
const sourcePath = fullPath.split(integrationTestPotentialPath)[0]
|
||||
fullPath = path.join(sourcePath, "src")
|
||||
}
|
||||
|
||||
@@ -90,7 +95,8 @@ export function defineJoinerConfig(
|
||||
|
||||
let basePath = splitPath[0] + srcDir
|
||||
|
||||
const isMedusaProject = fullPath.includes(`${srcDir}/modules/`)
|
||||
const potentialModulesDirPathSegment = normalize(`${srcDir}/modules/`)
|
||||
const isMedusaProject = fullPath.includes(potentialModulesDirPathSegment)
|
||||
if (isMedusaProject) {
|
||||
basePath = dirname(fullPath)
|
||||
}
|
||||
|
||||
@@ -33,11 +33,15 @@ export function Module<
|
||||
): ModuleExports<Service> & {
|
||||
linkable: Linkable
|
||||
} {
|
||||
const defaultJoinerConfig = defineJoinerConfig(serviceName)
|
||||
service.prototype.__joinerConfig ??= () => defaultJoinerConfig
|
||||
|
||||
const modelObjects = service[MedusaServiceModelObjectsSymbol] ?? {}
|
||||
|
||||
const defaultJoinerConfig = defineJoinerConfig(serviceName, {
|
||||
models: Object.keys(modelObjects).length
|
||||
? Object.values(modelObjects)
|
||||
: undefined,
|
||||
})
|
||||
service.prototype.__joinerConfig ??= () => defaultJoinerConfig
|
||||
|
||||
let linkable = {} as Linkable
|
||||
|
||||
if (Object.keys(modelObjects)?.length) {
|
||||
|
||||
@@ -16,16 +16,14 @@ moduleIntegrationTestRunner<ICartModuleService>({
|
||||
}).linkable
|
||||
|
||||
expect(Object.keys(linkable)).toEqual([
|
||||
"address",
|
||||
"adjustmentLine",
|
||||
"cart",
|
||||
"address",
|
||||
"lineItem",
|
||||
"lineItemAdjustment",
|
||||
"lineItemTaxLine",
|
||||
"lineItem",
|
||||
"shippingMethod",
|
||||
"shippingMethodAdjustment",
|
||||
"shippingMethodTaxLine",
|
||||
"shippingMethod",
|
||||
"taxLine",
|
||||
])
|
||||
|
||||
Object.keys(linkable).forEach((key) => {
|
||||
@@ -33,6 +31,14 @@ moduleIntegrationTestRunner<ICartModuleService>({
|
||||
})
|
||||
|
||||
expect(linkable).toEqual({
|
||||
cart: {
|
||||
id: {
|
||||
linkable: "cart_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "cart",
|
||||
field: "cart",
|
||||
},
|
||||
},
|
||||
address: {
|
||||
id: {
|
||||
linkable: "address_id",
|
||||
@@ -41,20 +47,12 @@ moduleIntegrationTestRunner<ICartModuleService>({
|
||||
field: "address",
|
||||
},
|
||||
},
|
||||
adjustmentLine: {
|
||||
lineItem: {
|
||||
id: {
|
||||
linkable: "adjustment_line_id",
|
||||
linkable: "line_item_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "cart",
|
||||
field: "adjustmentLine",
|
||||
},
|
||||
},
|
||||
cart: {
|
||||
id: {
|
||||
linkable: "cart_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "cart",
|
||||
field: "cart",
|
||||
field: "lineItem",
|
||||
},
|
||||
},
|
||||
lineItemAdjustment: {
|
||||
@@ -73,12 +71,12 @@ moduleIntegrationTestRunner<ICartModuleService>({
|
||||
field: "lineItemTaxLine",
|
||||
},
|
||||
},
|
||||
lineItem: {
|
||||
shippingMethod: {
|
||||
id: {
|
||||
linkable: "line_item_id",
|
||||
linkable: "shipping_method_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "cart",
|
||||
field: "lineItem",
|
||||
field: "shippingMethod",
|
||||
},
|
||||
},
|
||||
shippingMethodAdjustment: {
|
||||
@@ -97,22 +95,6 @@ moduleIntegrationTestRunner<ICartModuleService>({
|
||||
field: "shippingMethodTaxLine",
|
||||
},
|
||||
},
|
||||
shippingMethod: {
|
||||
id: {
|
||||
linkable: "shipping_method_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "cart",
|
||||
field: "shippingMethod",
|
||||
},
|
||||
},
|
||||
taxLine: {
|
||||
id: {
|
||||
linkable: "tax_line_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "cart",
|
||||
field: "taxLine",
|
||||
},
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -13,30 +13,25 @@ moduleIntegrationTestRunner<IOrderModuleService>({
|
||||
}).linkable
|
||||
|
||||
expect(Object.keys(linkable)).toEqual([
|
||||
"order",
|
||||
"address",
|
||||
"adjustmentLine",
|
||||
"claimItemImage",
|
||||
"orderClaimItem",
|
||||
"orderClaim",
|
||||
"orderExchangeItem",
|
||||
"orderExchange",
|
||||
"lineItem",
|
||||
"lineItemAdjustment",
|
||||
"lineItemTaxLine",
|
||||
"lineItem",
|
||||
"orderChangeAction",
|
||||
"orderChange",
|
||||
"orderItem",
|
||||
"orderShippingMethod",
|
||||
"orderSummary",
|
||||
"order",
|
||||
"returnItem",
|
||||
"returnReason",
|
||||
"return",
|
||||
"shippingMethod",
|
||||
"shippingMethodAdjustment",
|
||||
"shippingMethodTaxLine",
|
||||
"shippingMethod",
|
||||
"taxLine",
|
||||
"transaction",
|
||||
"orderChange",
|
||||
"orderChangeAction",
|
||||
"orderItem",
|
||||
"orderSummary",
|
||||
"orderShippingMethod",
|
||||
"returnReason",
|
||||
"return",
|
||||
"returnItem",
|
||||
"orderClaim",
|
||||
"orderExchange",
|
||||
])
|
||||
|
||||
Object.keys(linkable).forEach((key) => {
|
||||
@@ -44,6 +39,14 @@ moduleIntegrationTestRunner<IOrderModuleService>({
|
||||
})
|
||||
|
||||
expect(linkable).toEqual({
|
||||
order: {
|
||||
id: {
|
||||
linkable: "order_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "order",
|
||||
field: "order",
|
||||
},
|
||||
},
|
||||
address: {
|
||||
id: {
|
||||
linkable: "address_id",
|
||||
@@ -52,52 +55,12 @@ moduleIntegrationTestRunner<IOrderModuleService>({
|
||||
field: "address",
|
||||
},
|
||||
},
|
||||
adjustmentLine: {
|
||||
lineItem: {
|
||||
id: {
|
||||
linkable: "adjustment_line_id",
|
||||
linkable: "line_item_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "order",
|
||||
field: "adjustmentLine",
|
||||
},
|
||||
},
|
||||
claimItemImage: {
|
||||
id: {
|
||||
linkable: "claim_item_image_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "order",
|
||||
field: "claimItemImage",
|
||||
},
|
||||
},
|
||||
orderClaimItem: {
|
||||
id: {
|
||||
linkable: "order_claim_item_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "order",
|
||||
field: "orderClaimItem",
|
||||
},
|
||||
},
|
||||
orderClaim: {
|
||||
id: {
|
||||
linkable: "order_claim_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "order",
|
||||
field: "orderClaim",
|
||||
},
|
||||
},
|
||||
orderExchangeItem: {
|
||||
id: {
|
||||
linkable: "order_exchange_item_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "order",
|
||||
field: "orderExchangeItem",
|
||||
},
|
||||
},
|
||||
orderExchange: {
|
||||
id: {
|
||||
linkable: "order_exchange_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "order",
|
||||
field: "orderExchange",
|
||||
field: "lineItem",
|
||||
},
|
||||
},
|
||||
lineItemAdjustment: {
|
||||
@@ -116,84 +79,12 @@ moduleIntegrationTestRunner<IOrderModuleService>({
|
||||
field: "lineItemTaxLine",
|
||||
},
|
||||
},
|
||||
lineItem: {
|
||||
shippingMethod: {
|
||||
id: {
|
||||
linkable: "line_item_id",
|
||||
linkable: "shipping_method_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "order",
|
||||
field: "lineItem",
|
||||
},
|
||||
},
|
||||
orderChangeAction: {
|
||||
id: {
|
||||
linkable: "order_change_action_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "order",
|
||||
field: "orderChangeAction",
|
||||
},
|
||||
},
|
||||
orderChange: {
|
||||
id: {
|
||||
linkable: "order_change_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "order",
|
||||
field: "orderChange",
|
||||
},
|
||||
},
|
||||
orderItem: {
|
||||
id: {
|
||||
linkable: "order_item_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "order",
|
||||
field: "orderItem",
|
||||
},
|
||||
},
|
||||
orderShippingMethod: {
|
||||
id: {
|
||||
linkable: "order_shipping_method_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "order",
|
||||
field: "orderShippingMethod",
|
||||
},
|
||||
},
|
||||
orderSummary: {
|
||||
id: {
|
||||
linkable: "order_summary_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "order",
|
||||
field: "orderSummary",
|
||||
},
|
||||
},
|
||||
order: {
|
||||
id: {
|
||||
linkable: "order_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "order",
|
||||
field: "order",
|
||||
},
|
||||
},
|
||||
returnItem: {
|
||||
id: {
|
||||
linkable: "return_item_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "order",
|
||||
field: "returnItem",
|
||||
},
|
||||
},
|
||||
returnReason: {
|
||||
id: {
|
||||
linkable: "return_reason_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "order",
|
||||
field: "returnReason",
|
||||
},
|
||||
},
|
||||
return: {
|
||||
id: {
|
||||
linkable: "return_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "order",
|
||||
field: "return",
|
||||
field: "shippingMethod",
|
||||
},
|
||||
},
|
||||
shippingMethodAdjustment: {
|
||||
@@ -212,22 +103,6 @@ moduleIntegrationTestRunner<IOrderModuleService>({
|
||||
field: "shippingMethodTaxLine",
|
||||
},
|
||||
},
|
||||
shippingMethod: {
|
||||
id: {
|
||||
linkable: "shipping_method_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "order",
|
||||
field: "shippingMethod",
|
||||
},
|
||||
},
|
||||
taxLine: {
|
||||
id: {
|
||||
linkable: "tax_line_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "order",
|
||||
field: "taxLine",
|
||||
},
|
||||
},
|
||||
transaction: {
|
||||
id: {
|
||||
linkable: "transaction_id",
|
||||
@@ -236,6 +111,86 @@ moduleIntegrationTestRunner<IOrderModuleService>({
|
||||
field: "transaction",
|
||||
},
|
||||
},
|
||||
orderChange: {
|
||||
id: {
|
||||
linkable: "order_change_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "order",
|
||||
field: "orderChange",
|
||||
},
|
||||
},
|
||||
orderChangeAction: {
|
||||
id: {
|
||||
linkable: "order_change_action_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "order",
|
||||
field: "orderChangeAction",
|
||||
},
|
||||
},
|
||||
orderItem: {
|
||||
id: {
|
||||
linkable: "order_item_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "order",
|
||||
field: "orderItem",
|
||||
},
|
||||
},
|
||||
orderSummary: {
|
||||
id: {
|
||||
linkable: "order_summary_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "order",
|
||||
field: "orderSummary",
|
||||
},
|
||||
},
|
||||
orderShippingMethod: {
|
||||
id: {
|
||||
linkable: "order_shipping_method_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "order",
|
||||
field: "orderShippingMethod",
|
||||
},
|
||||
},
|
||||
returnReason: {
|
||||
id: {
|
||||
linkable: "return_reason_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "order",
|
||||
field: "returnReason",
|
||||
},
|
||||
},
|
||||
return: {
|
||||
id: {
|
||||
linkable: "return_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "order",
|
||||
field: "return",
|
||||
},
|
||||
},
|
||||
returnItem: {
|
||||
id: {
|
||||
linkable: "return_item_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "order",
|
||||
field: "returnItem",
|
||||
},
|
||||
},
|
||||
orderClaim: {
|
||||
id: {
|
||||
linkable: "order_claim_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "order",
|
||||
field: "orderClaim",
|
||||
},
|
||||
},
|
||||
orderExchange: {
|
||||
id: {
|
||||
linkable: "order_exchange_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "order",
|
||||
field: "orderExchange",
|
||||
},
|
||||
},
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -15,21 +15,13 @@ moduleIntegrationTestRunner<IStoreModuleService>({
|
||||
service: StoreModuleService,
|
||||
}).linkable
|
||||
|
||||
expect(Object.keys(linkable)).toEqual(["storeCurrency", "store"])
|
||||
expect(Object.keys(linkable)).toEqual(["store", "storeCurrency"])
|
||||
|
||||
Object.keys(linkable).forEach((key) => {
|
||||
delete linkable[key].toJSON
|
||||
})
|
||||
|
||||
expect(linkable).toEqual({
|
||||
storeCurrency: {
|
||||
id: {
|
||||
linkable: "store_currency_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "store",
|
||||
field: "storeCurrency",
|
||||
},
|
||||
},
|
||||
store: {
|
||||
id: {
|
||||
linkable: "store_id",
|
||||
@@ -38,6 +30,14 @@ moduleIntegrationTestRunner<IStoreModuleService>({
|
||||
field: "store",
|
||||
},
|
||||
},
|
||||
storeCurrency: {
|
||||
id: {
|
||||
linkable: "store_currency_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "store",
|
||||
field: "storeCurrency",
|
||||
},
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -16,10 +16,10 @@ moduleIntegrationTestRunner<ITaxModuleService>({
|
||||
}).linkable
|
||||
|
||||
expect(Object.keys(linkable)).toEqual([
|
||||
"taxProvider",
|
||||
"taxRateRule",
|
||||
"taxRate",
|
||||
"taxRegion",
|
||||
"taxRateRule",
|
||||
"taxProvider",
|
||||
])
|
||||
|
||||
Object.keys(linkable).forEach((key) => {
|
||||
@@ -27,22 +27,6 @@ moduleIntegrationTestRunner<ITaxModuleService>({
|
||||
})
|
||||
|
||||
expect(linkable).toEqual({
|
||||
taxProvider: {
|
||||
id: {
|
||||
linkable: "tax_provider_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "tax",
|
||||
field: "taxProvider",
|
||||
},
|
||||
},
|
||||
taxRateRule: {
|
||||
id: {
|
||||
linkable: "tax_rate_rule_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "tax",
|
||||
field: "taxRateRule",
|
||||
},
|
||||
},
|
||||
taxRate: {
|
||||
id: {
|
||||
linkable: "tax_rate_id",
|
||||
@@ -59,6 +43,22 @@ moduleIntegrationTestRunner<ITaxModuleService>({
|
||||
field: "taxRegion",
|
||||
},
|
||||
},
|
||||
taxRateRule: {
|
||||
id: {
|
||||
linkable: "tax_rate_rule_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "tax",
|
||||
field: "taxRateRule",
|
||||
},
|
||||
},
|
||||
taxProvider: {
|
||||
id: {
|
||||
linkable: "tax_provider_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "tax",
|
||||
field: "taxProvider",
|
||||
},
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
@@ -214,15 +214,19 @@ moduleIntegrationTestRunner<ITaxModuleService>({
|
||||
},
|
||||
})
|
||||
|
||||
const error = await service.createTaxRegions({
|
||||
country_code: "US",
|
||||
default_tax_rate: {
|
||||
name: "Test Rate",
|
||||
rate: 0.2,
|
||||
},
|
||||
}).catch(e => e)
|
||||
const error = await service
|
||||
.createTaxRegions({
|
||||
country_code: "US",
|
||||
default_tax_rate: {
|
||||
name: "Test Rate",
|
||||
rate: 0.2,
|
||||
},
|
||||
})
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.message).toEqual("Tax region with country_code: us, already exists.")
|
||||
expect(error.message).toEqual(
|
||||
"Tax region with country_code: us, already exists."
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw when creating a tax region with a country code and province code of an existing region", async () => {
|
||||
@@ -235,18 +239,22 @@ moduleIntegrationTestRunner<ITaxModuleService>({
|
||||
},
|
||||
})
|
||||
|
||||
const error = await service.createTaxRegions({
|
||||
country_code: "US",
|
||||
province_code: "CA",
|
||||
default_tax_rate: {
|
||||
name: "Test Rate",
|
||||
rate: 0.2,
|
||||
},
|
||||
}).catch(e => e)
|
||||
const error = await service
|
||||
.createTaxRegions({
|
||||
country_code: "US",
|
||||
province_code: "CA",
|
||||
default_tax_rate: {
|
||||
name: "Test Rate",
|
||||
rate: 0.2,
|
||||
},
|
||||
})
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.message).toEqual("Tax region with country_code: us, province_code: ca, already exists.")
|
||||
expect(error.message).toEqual(
|
||||
"Tax region with country_code: us, province_code: ca, already exists."
|
||||
)
|
||||
})
|
||||
|
||||
|
||||
it("should create tax rates and update them", async () => {
|
||||
const region = await service.createTaxRegions({
|
||||
country_code: "US",
|
||||
|
||||
@@ -33,21 +33,13 @@ moduleIntegrationTestRunner<IUserModuleService>({
|
||||
service: UserModuleService,
|
||||
}).linkable
|
||||
|
||||
expect(Object.keys(linkable)).toEqual(["invite", "user"])
|
||||
expect(Object.keys(linkable)).toEqual(["user", "invite"])
|
||||
|
||||
Object.keys(linkable).forEach((key) => {
|
||||
delete linkable[key].toJSON
|
||||
})
|
||||
|
||||
expect(linkable).toEqual({
|
||||
invite: {
|
||||
id: {
|
||||
linkable: "invite_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "user",
|
||||
field: "invite",
|
||||
},
|
||||
},
|
||||
user: {
|
||||
id: {
|
||||
linkable: "user_id",
|
||||
@@ -56,6 +48,14 @@ moduleIntegrationTestRunner<IUserModuleService>({
|
||||
field: "user",
|
||||
},
|
||||
},
|
||||
invite: {
|
||||
id: {
|
||||
linkable: "invite_id",
|
||||
primaryKey: "id",
|
||||
serviceName: "user",
|
||||
field: "invite",
|
||||
},
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user