Files
medusa-store/integration-tests/modules/__tests__/link-modules/publishable-key-sales-channel.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

91 lines
2.5 KiB
TypeScript

import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import {
IApiKeyModuleService,
ISalesChannelModuleService,
} from "@medusajs/types"
import { Modules, remoteQueryObjectFromString } from "@medusajs/utils"
jest.setTimeout(50000)
const env = {}
medusaIntegrationTestRunner({
env,
testSuite: ({ dbConnection, getContainer, api }) => {
describe("Publishable keys and sales channel link", () => {
let appContainer
let apiKeyModule: IApiKeyModuleService
let scModuleService: ISalesChannelModuleService
let remoteQuery
let remoteLink
beforeAll(async () => {
appContainer = getContainer()
apiKeyModule = appContainer.resolve(Modules.API_KEY)
scModuleService = appContainer.resolve(Modules.SALES_CHANNEL)
remoteQuery = appContainer.resolve("remoteQuery")
remoteLink = appContainer.resolve("remoteLink")
})
it("should query api key and sales channels link with remote query", async () => {
const salesChannel = await scModuleService.createSalesChannels({
name: "Webshop",
})
const apiKeys = await apiKeyModule.createApiKeys([
{
title: "Api key",
type: "publishable",
created_by: "test",
},
{
title: "Api key 2",
type: "publishable",
created_by: "test",
},
])
await remoteLink.create([
{
[Modules.API_KEY]: {
publishable_key_id: apiKeys[0].id,
},
[Modules.SALES_CHANNEL]: {
sales_channel_id: salesChannel.id,
},
},
{
[Modules.API_KEY]: {
publishable_key_id: apiKeys[1].id,
},
[Modules.SALES_CHANNEL]: {
sales_channel_id: salesChannel.id,
},
},
])
const queryObject = remoteQueryObjectFromString({
entryPoint: "api_key",
variables: {
filters: { token: apiKeys[0].token },
},
fields: ["id", "sales_channels.id"],
})
const keyLinks = await remoteQuery(queryObject)
expect(keyLinks).toHaveLength(1)
expect(keyLinks).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: apiKeys[0].id,
sales_channels: expect.arrayContaining([
expect.objectContaining({ id: salesChannel.id }),
]),
}),
])
)
})
})
},
})