Files
medusa-store/integration-tests/modules/__tests__/link-modules/sales-channel-location.spec.ts
Carlos R. L. Rodrigues e413cfefc2 feat(utils): define file config (#13283)
** What
 - Allow auto-loaded Medusa files to export a config object.
 - Currently supports isDisabled to control loading.
 - new instance `FeatureFlag` exported by `@medusajs/framework/utils`
 - `feature-flags` is now a supported folder for medusa projects, modules, providers and plugins. They will be loaded and added to `FeatureFlag`

** Why
 - Enables conditional loading of routes, migrations, jobs, subscribers, workflows, and other files based on feature flags.

```ts
// /src/feature-flags

import { FlagSettings } from "@medusajs/framework/feature-flags"

const CustomFeatureFlag: FlagSettings = {
  key: "custom_feature",
  default_val: false,
  env_key: "FF_MY_CUSTOM_FEATURE",
  description: "Enable xyz",
}

export default CustomFeatureFlag
```

```ts
// /src/modules/my-custom-module/migration/Migration20250822135845.ts

import { FeatureFlag } from "@medusajs/framework/utils"

export class Migration20250822135845 extends Migration {
  override async up(){ }
  override async down(){ }
}

defineFileConfig({
  isDisabled: () => !FeatureFlag.isFeatureEnabled("custom_feature")
})
```
2025-08-26 12:22:30 +00:00

116 lines
3.5 KiB
TypeScript

import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import {
ISalesChannelModuleService,
IStockLocationService,
} from "@medusajs/types"
import { Modules, remoteQueryObjectFromString } from "@medusajs/utils"
jest.setTimeout(50000)
const env = {}
medusaIntegrationTestRunner({
env,
testSuite: ({ getContainer }) => {
describe("Cart links", () => {
let appContainer
let scService: ISalesChannelModuleService
let locationService: IStockLocationService
let remoteQuery, remoteLink
beforeAll(async () => {
appContainer = getContainer()
scService = appContainer.resolve(Modules.SALES_CHANNEL)
locationService = appContainer.resolve(Modules.STOCK_LOCATION)
remoteQuery = appContainer.resolve("remoteQuery")
remoteLink = appContainer.resolve("remoteLink")
})
it("should query carts, sales channels, customers, regions with remote query", async () => {
const scWebshop = await scService.createSalesChannels({
name: "Webshop",
})
const scCphStore = await scService.createSalesChannels({
name: "CPH store",
})
const scNycStore = await scService.createSalesChannels({
name: "NYC store",
})
const euWarehouse = await locationService.createStockLocations({
name: "EU Warehouse",
})
const usWarehouse = await locationService.createStockLocations({
name: "US Warehouse",
})
await remoteLink.create([
{
[Modules.SALES_CHANNEL]: {
sales_channel_id: scWebshop.id,
},
[Modules.STOCK_LOCATION]: {
stock_location_id: euWarehouse.id,
},
},
{
[Modules.SALES_CHANNEL]: {
sales_channel_id: scCphStore.id,
},
[Modules.STOCK_LOCATION]: {
stock_location_id: euWarehouse.id,
},
},
{
[Modules.SALES_CHANNEL]: {
sales_channel_id: scNycStore.id,
},
[Modules.STOCK_LOCATION]: {
stock_location_id: usWarehouse.id,
},
},
])
const euStockLocationChannelQuery = remoteQueryObjectFromString({
entryPoint: "stock_locations",
fields: ["id", "name", "sales_channels.id", "sales_channels.name"],
variables: { id: euWarehouse.id },
})
const usStockLocationChannelQuery = remoteQueryObjectFromString({
entryPoint: "stock_locations",
fields: ["id", "name", "sales_channels.id", "sales_channels.name"],
variables: { id: usWarehouse.id },
})
const euLocations = await remoteQuery(euStockLocationChannelQuery)
const usLocations = await remoteQuery(usStockLocationChannelQuery)
expect(euLocations.length).toBe(1)
expect(euLocations).toEqual([
expect.objectContaining({
id: euWarehouse.id,
sales_channels: [
expect.objectContaining({ id: scWebshop.id }),
expect.objectContaining({ id: scCphStore.id }),
],
name: "EU Warehouse",
}),
])
expect(usLocations.length).toBe(1)
expect(usLocations).toEqual([
expect.objectContaining({
id: usWarehouse.id,
sales_channels: [expect.objectContaining({ id: scNycStore.id })],
name: "US Warehouse",
}),
])
})
})
},
})