Skip to content

Commit fa6d871

Browse files
authored
Merge branch 'develop' into fix/order-constraints
2 parents 7b5946b + fa76f85 commit fa6d871

4 files changed

Lines changed: 66 additions & 3 deletions

File tree

.changeset/tough-horses-return.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@medusajs/test-utils": patch
3+
"@medusajs/index": patch
4+
---
5+
6+
fix(index): merge filterable fields with default fields

integration-tests/modules/__tests__/index/query-index.spec.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
import CustomerModule from "@medusajs/customer"
2+
import ProductModule from "@medusajs/product"
13
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
24
import { RemoteQueryFunction } from "@medusajs/types"
3-
import { ContainerRegistrationKeys, defaultCurrencies } from "@medusajs/utils"
5+
import {
6+
ContainerRegistrationKeys,
7+
defaultCurrencies,
8+
defineLink,
9+
} from "@medusajs/utils"
410
import { setTimeout } from "timers/promises"
511
import {
612
adminHeaders,
@@ -26,6 +32,7 @@ async function populateData(api: any) {
2632
title: "Test Product",
2733
status: "published",
2834
description: "test-product-description",
35+
origin_country: "USA",
2936
shipping_profile_id: shippingProfile.id,
3037
options: [{ title: "Denominations", values: ["100"] }],
3138
variants: [
@@ -82,6 +89,17 @@ async function populateData(api: any) {
8289
process.env.ENABLE_INDEX_MODULE = "true"
8390

8491
medusaIntegrationTestRunner({
92+
hooks: {
93+
beforeServerStart: async () => {
94+
const customer = CustomerModule.linkable.customer
95+
const product = ProductModule.linkable.product
96+
97+
defineLink(customer, {
98+
linkable: product,
99+
filterable: ["origin_country"],
100+
})
101+
},
102+
},
85103
testSuite: ({ getContainer, dbConnection, api, dbConfig }) => {
86104
let appContainer
87105

@@ -417,6 +435,33 @@ medusaIntegrationTestRunner({
417435

418436
expect(resultset.data.length).toEqual(2)
419437
})
438+
439+
it("should query by custom linkable field and default field using query.index", async () => {
440+
await populateData(api)
441+
442+
const query = appContainer.resolve(
443+
ContainerRegistrationKeys.QUERY
444+
) as RemoteQueryFunction
445+
446+
const resultset = await fetchAndRetry(
447+
async () =>
448+
await query.index({
449+
entity: "product",
450+
fields: ["id", "origin_country"],
451+
filters: {
452+
origin_country: ["USA"],
453+
},
454+
}),
455+
({ data }) => data.length > 0,
456+
{
457+
retries: 3,
458+
waitSeconds: 3,
459+
}
460+
)
461+
462+
expect(resultset.data.length).toEqual(1)
463+
expect(resultset.data[0].origin_country).toEqual("USA")
464+
})
420465
})
421466
},
422467
})

packages/medusa-test-utils/src/medusa-test-runner.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { logger } from "@medusajs/framework/logger"
12
import { MedusaAppOutput } from "@medusajs/framework/modules-sdk"
23
import { MedusaContainer } from "@medusajs/framework/types"
34
import {
@@ -7,7 +8,6 @@ import {
78
mergePluginModules,
89
} from "@medusajs/framework/utils"
910
import { asValue } from "awilix"
10-
import { logger } from "@medusajs/framework/logger"
1111
import { dbTestUtilFactory, getDatabaseURL } from "./database"
1212
import {
1313
applyEnvVarsToProcess,
@@ -48,6 +48,9 @@ interface TestRunnerConfig {
4848
schema?: string
4949
debug?: boolean
5050
inApp?: boolean
51+
hooks?: {
52+
beforeServerStart?: (container: MedusaContainer) => Promise<void>
53+
}
5154
}
5255

5356
class MedusaTestRunner {
@@ -72,6 +75,7 @@ class MedusaTestRunner {
7275
private loadedApplication: any = null
7376
private shutdown: () => Promise<void> = async () => void 0
7477
private isFirstTime = true
78+
private hooks: TestRunnerConfig["hooks"] = {}
7579

7680
constructor(config: TestRunnerConfig) {
7781
const tempName = parseInt(process.env.JEST_WORKER_ID || "1")
@@ -93,6 +97,7 @@ class MedusaTestRunner {
9397
schema: this.schema,
9498
debug: this.debug,
9599
}
100+
this.hooks = config.hooks ?? {}
96101

97102
this.setupProcessHandlers()
98103
}
@@ -158,6 +163,10 @@ class MedusaTestRunner {
158163
[ContainerRegistrationKeys.LOGGER]: asValue(logger),
159164
})
160165

166+
if (this.hooks?.beforeServerStart) {
167+
await this.hooks.beforeServerStart(container)
168+
}
169+
161170
await this.initializeDatabase()
162171

163172
logger.info(
@@ -309,6 +318,7 @@ export function medusaIntegrationTestRunner({
309318
debug = false,
310319
inApp = false,
311320
testSuite,
321+
hooks,
312322
}: {
313323
moduleName?: string
314324
env?: Record<string, any>
@@ -318,6 +328,7 @@ export function medusaIntegrationTestRunner({
318328
debug?: boolean
319329
inApp?: boolean
320330
testSuite: (options: MedusaSuiteOptions) => void
331+
hooks?: TestRunnerConfig["hooks"]
321332
}) {
322333
const runner = new MedusaTestRunner({
323334
moduleName,
@@ -327,6 +338,7 @@ export function medusaIntegrationTestRunner({
327338
env,
328339
debug,
329340
inApp,
341+
hooks,
330342
})
331343

332344
return describe("", () => {

packages/modules/index/src/utils/build-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,7 @@ function buildSchemaFromFilterableLinks(
12061206
})
12071207
.join("\n")
12081208

1209-
return `type ${entity} ${events} {
1209+
return `extend type ${entity} ${events} {
12101210
${fieldDefinitions}
12111211
}`
12121212
})

0 commit comments

Comments
 (0)