1+ import CustomerModule from "@medusajs/customer"
2+ import ProductModule from "@medusajs/product"
13import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
24import { RemoteQueryFunction } from "@medusajs/types"
3- import { ContainerRegistrationKeys , defaultCurrencies } from "@medusajs/utils"
5+ import {
6+ ContainerRegistrationKeys ,
7+ defaultCurrencies ,
8+ defineLink ,
9+ } from "@medusajs/utils"
410import { setTimeout } from "timers/promises"
511import {
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) {
8289process . env . ENABLE_INDEX_MODULE = "true"
8390
8491medusaIntegrationTestRunner ( {
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} )
0 commit comments