1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2
3 import CurrencyModule from "@medusajs/currency"
4 import { MedusaModule } from "@medusajs/modules-sdk"
5 import ProductModule from "@medusajs/product"
6 import RegionModule from "@medusajs/region"
7 import { defineLink } from "@medusajs/utils"
8
9 jest.setTimeout(50000)
10
11 medusaIntegrationTestRunner({
12 testSuite: ({ getContainer }) => {
13 describe("defineLink", () => {
14 it("should generate a proper link definition", async () => {
15 const currencyLinks = CurrencyModule.linkable
16 const regionLinks = RegionModule.linkable
17
18 const link = defineLink(currencyLinks.currency, regionLinks.region)
19
20 const linkDefinition = MedusaModule.getCustomLinks()
21 .map((linkDefinition: any) => {
22 const definition = linkDefinition(
23 MedusaModule.getAllJoinerConfigs()
24 )
25 return definition.serviceName === link.serviceName && definition
26 })
27 .filter(Boolean)[0]
28
29 expect(link.serviceName).toEqual(linkDefinition.serviceName)
30 expect(link.entryPoint).toEqual(linkDefinition.alias[0].name[0])
31 expect(linkDefinition).toEqual({
32 serviceName: "CurrencyCurrencyRegionRegionLink",
33 isLink: true,
34 alias: [
35 {
36 name: ["currency_region"],
37 args: {
38 entity: "LinkCurrencyCurrencyRegionRegion",
39 },
40 },
41 ],
42 primaryKeys: ["id", "currency_code", "region_id"],
43 relationships: [
44 {
45 serviceName: "currency",
46 entity: "Currency",
47 primaryKey: "code",
48 foreignKey: "currency_code",
49 hasMany: false,
50 alias: "currency",
51 args: {
52 methodSuffix: "Currencies",
53 },
54 deleteCascade: false,
55 },
56 {
57 serviceName: "region",
58 entity: "Region",
59 primaryKey: "id",
60 foreignKey: "region_id",
61 alias: "region",
62 hasMany: false,
63 args: {
64 methodSuffix: "Regions",
65 },
66 deleteCascade: false,
67 },
68 ],
69 extends: [
70 {
71 serviceName: "currency",
72 entity: "Currency",
73 fieldAlias: {
74 region: {
75 path: "region_link.region",
76 isList: false,
77 forwardArgumentsOnPath: ["region_link.region"],
78 },
79 },
80 relationship: {
81 serviceName: "CurrencyCurrencyRegionRegionLink",
82 entity: "LinkCurrencyCurrencyRegionRegion",
83 primaryKey: "currency_code",
84 foreignKey: "code",
85 alias: "region_link",
86 isList: false,
87 },
88 },
89 {
90 serviceName: "region",
91 entity: "Region",
92 fieldAlias: {
93 currency: {
94 path: "currency_link.currency",
95 isList: false,
96 forwardArgumentsOnPath: ["currency_link.currency"],
97 },
98 },
99 relationship: {
100 serviceName: "CurrencyCurrencyRegionRegionLink",
101 entity: "LinkCurrencyCurrencyRegionRegion",
102 primaryKey: "region_id",
103 foreignKey: "id",
104 alias: "currency_link",
105 isList: false,
106 },
107 },
108 ],
109 })
110 })
111
112 it("should generate a proper link definition with multi parts entity name", async () => {
113 const productVariantLinks = ProductModule.linkable
114 const regionLinks = RegionModule.linkable
115
116 const link = defineLink(
117 productVariantLinks.productVariant,
118 regionLinks.region
119 )
120
121 const linkDefinition = MedusaModule.getCustomLinks()
122 .map((linkDefinition: any) => {
123 const definition = linkDefinition(
124 MedusaModule.getAllJoinerConfigs()
125 )
126 return definition.serviceName === link.serviceName && definition
127 })
128 .filter(Boolean)[0]
129
130 expect(link.serviceName).toEqual(linkDefinition.serviceName)
131 expect(link.entryPoint).toEqual(linkDefinition.alias[0].name[0])
132 expect(linkDefinition).toEqual({
133 serviceName: "ProductProductVariantRegionRegionLink",
134 isLink: true,
135 alias: [
136 {
137 name: ["product_variant_region"],
138 args: {
139 entity: "LinkProductProductVariantRegionRegion",
140 },
141 },
142 ],
143 primaryKeys: ["id", "product_variant_id", "region_id"],
144 relationships: [
145 {
146 serviceName: "product",
147 entity: "ProductVariant",
148 primaryKey: "id",
149 foreignKey: "product_variant_id",
150 hasMany: false,
151 alias: "product_variant",
152 args: {
153 methodSuffix: "ProductVariants",
154 },
155 deleteCascade: false,
156 },
157 {
158 serviceName: "region",
159 entity: "Region",
160 primaryKey: "id",
161 foreignKey: "region_id",
162 hasMany: false,
163 alias: "region",
164 args: {
165 methodSuffix: "Regions",
166 },
167 deleteCascade: false,
168 },
169 ],
170 extends: [
171 {
172 serviceName: "product",
173 entity: "ProductVariant",
174 fieldAlias: {
175 region: {
176 path: "region_link.region",
177 isList: false,
178 forwardArgumentsOnPath: ["region_link.region"],
179 },
180 },
181 relationship: {
182 serviceName: "ProductProductVariantRegionRegionLink",
183 entity: "LinkProductProductVariantRegionRegion",
184 primaryKey: "product_variant_id",
185 foreignKey: "id",
186 alias: "region_link",
187 isList: false,
188 },
189 },
190 {
191 serviceName: "region",
192 entity: "Region",
193 fieldAlias: {
194 product_variant: {
195 path: "product_variant_link.product_variant",
196 isList: false,
197 forwardArgumentsOnPath: [
198 "product_variant_link.product_variant",
199 ],
200 },
201 },
202 relationship: {
203 serviceName: "ProductProductVariantRegionRegionLink",
204 entity: "LinkProductProductVariantRegionRegion",
205 primaryKey: "region_id",
206 foreignKey: "id",
207 alias: "product_variant_link",
208 isList: false,
209 },
210 },
211 ],
212 })
213 })
214
215 it("should flag deleteCascade in the link definition", async () => {
216 const currencyLinks = CurrencyModule.linkable
217 const regionLinks = RegionModule.linkable
218
219 const link = defineLink(
220 {
221 linkable: currencyLinks.currency,
222 deleteCascade: true,
223 },
224 regionLinks.region
225 )
226
227 const linkDefinition = MedusaModule.getCustomLinks()
228 .map((linkDefinition: any) => {
229 const definition = linkDefinition(
230 MedusaModule.getAllJoinerConfigs()
231 )
232 return definition.serviceName === link.serviceName && definition
233 })
234 .filter(Boolean)[0]
235
236 expect(link.serviceName).toEqual(linkDefinition.serviceName)
237 expect(link.entryPoint).toEqual(linkDefinition.alias[0].name[0])
238 expect(linkDefinition).toEqual({
239 serviceName: "CurrencyCurrencyRegionRegionLink",
240 isLink: true,
241 alias: [
242 {
243 name: ["currency_region"],
244 args: {
245 entity: "LinkCurrencyCurrencyRegionRegion",
246 },
247 },
248 ],
249 primaryKeys: ["id", "currency_code", "region_id"],
250 relationships: [
251 {
252 serviceName: "currency",
253 entity: "Currency",
254 primaryKey: "code",
255 foreignKey: "currency_code",
256 hasMany: false,
257 alias: "currency",
258 args: {
259 methodSuffix: "Currencies",
260 },
261 deleteCascade: true,
262 },
263 {
264 serviceName: "region",
265 entity: "Region",
266 primaryKey: "id",
267 foreignKey: "region_id",
268 hasMany: false,
269 alias: "region",
270 args: {
271 methodSuffix: "Regions",
272 },
273 deleteCascade: false,
274 },
275 ],
276 extends: [
277 {
278 serviceName: "currency",
279 entity: "Currency",
280 fieldAlias: {
281 region: {
282 path: "region_link.region",
283 isList: false,
284 forwardArgumentsOnPath: ["region_link.region"],
285 },
286 },
287 relationship: {
288 serviceName: "CurrencyCurrencyRegionRegionLink",
289 entity: "LinkCurrencyCurrencyRegionRegion",
290 primaryKey: "currency_code",
291 foreignKey: "code",
292 alias: "region_link",
293 isList: false,
294 },
295 },
296 {
297 serviceName: "region",
298 entity: "Region",
299 fieldAlias: {
300 currency: {
301 path: "currency_link.currency",
302 isList: false,
303 forwardArgumentsOnPath: ["currency_link.currency"],
304 },
305 },
306 relationship: {
307 serviceName: "CurrencyCurrencyRegionRegionLink",
308 entity: "LinkCurrencyCurrencyRegionRegion",
309 primaryKey: "region_id",
310 foreignKey: "id",
311 alias: "currency_link",
312 isList: false,
313 },
314 },
315 ],
316 })
317 })
318
319 it("should generate a proper link definition passing an object as option", async () => {
320 const currencyLinks = CurrencyModule.linkable
321 const regionLinks = RegionModule.linkable
322
323 const link = defineLink(currencyLinks.currency, {
324 linkable: regionLinks.region,
325 isList: true,
326 })
327
328 const linkDefinition = MedusaModule.getCustomLinks()
329 .map((linkDefinition: any) => {
330 const definition = linkDefinition(
331 MedusaModule.getAllJoinerConfigs()
332 )
333 return definition.serviceName === link.serviceName && definition
334 })
335 .filter(Boolean)[0]
336
337 expect(link.serviceName).toEqual("CurrencyCurrencyRegionRegionLink")
338 expect(linkDefinition).toEqual({
339 serviceName: "CurrencyCurrencyRegionRegionLink",
340 isLink: true,
341 alias: [
342 {
343 name: ["currency_region"],
344 args: {
345 entity: "LinkCurrencyCurrencyRegionRegion",
346 },
347 },
348 ],
349 primaryKeys: ["id", "currency_code", "region_id"],
350 relationships: [
351 {
352 serviceName: "currency",
353 entity: "Currency",
354 primaryKey: "code",
355 foreignKey: "currency_code",
356 hasMany: false,
357 alias: "currency",
358 args: {
359 methodSuffix: "Currencies",
360 },
361 deleteCascade: false,
362 },
363 {
364 serviceName: "region",
365 entity: "Region",
366 primaryKey: "id",
367 foreignKey: "region_id",
368 hasMany: true,
369 alias: "region",
370 args: {
371 methodSuffix: "Regions",
372 },
373 deleteCascade: false,
374 },
375 ],
376 extends: [
377 {
378 serviceName: "currency",
379 entity: "Currency",
380 fieldAlias: {
381 regions: {
382 path: "region_link.region",
383 isList: true,
384 forwardArgumentsOnPath: ["region_link.region"],
385 },
386 },
387 relationship: {
388 serviceName: "CurrencyCurrencyRegionRegionLink",
389 entity: "LinkCurrencyCurrencyRegionRegion",
390 primaryKey: "currency_code",
391 foreignKey: "code",
392 alias: "region_link",
393 isList: true,
394 },
395 },
396 {
397 serviceName: "region",
398 entity: "Region",
399 fieldAlias: {
400 currency: {
401 path: "currency_link.currency",
402 isList: false,
403 forwardArgumentsOnPath: ["currency_link.currency"],
404 },
405 },
406 relationship: {
407 serviceName: "CurrencyCurrencyRegionRegionLink",
408 entity: "LinkCurrencyCurrencyRegionRegion",
409 primaryKey: "region_id",
410 foreignKey: "id",
411 alias: "currency_link",
412 isList: false,
413 },
414 },
415 ],
416 })
417 })
418
419 it("should generate a proper link with both sides using explicit isList=true", async () => {
420 const currencyLinks = CurrencyModule.linkable
421 const regionLinks = RegionModule.linkable
422
423 const link = defineLink(
424 {
425 linkable: currencyLinks.currency,
426 isList: true,
427 },
428 {
429 linkable: regionLinks.region,
430 isList: true,
431 }
432 )
433
434 const linkDefinition = MedusaModule.getCustomLinks()
435 .map((linkDefinition: any) => {
436 const definition = linkDefinition(
437 MedusaModule.getAllJoinerConfigs()
438 )
439 return definition.serviceName === link.serviceName && definition
440 })
441 .filter(Boolean)[0]
442
443 expect(link.serviceName).toEqual("CurrencyCurrencyRegionRegionLink")
444 expect(linkDefinition).toEqual({
445 serviceName: "CurrencyCurrencyRegionRegionLink",
446 isLink: true,
447 alias: [
448 {
449 name: ["currency_region"],
450 args: {
451 entity: "LinkCurrencyCurrencyRegionRegion",
452 },
453 },
454 ],
455 primaryKeys: ["id", "currency_code", "region_id"],
456 relationships: [
457 {
458 serviceName: "currency",
459 entity: "Currency",
460 primaryKey: "code",
461 foreignKey: "currency_code",
462 hasMany: true,
463 alias: "currency",
464 args: {
465 methodSuffix: "Currencies",
466 },
467 deleteCascade: false,
468 },
469 {
470 serviceName: "region",
471 entity: "Region",
472 primaryKey: "id",
473 foreignKey: "region_id",
474 hasMany: true,
475 alias: "region",
476 args: {
477 methodSuffix: "Regions",
478 },
479 deleteCascade: false,
480 },
481 ],
482 extends: [
483 {
484 serviceName: "currency",
485 entity: "Currency",
486 fieldAlias: {
487 regions: {
488 path: "region_link.region",
489 isList: true,
490 forwardArgumentsOnPath: ["region_link.region"],
491 },
492 },
493 relationship: {
494 serviceName: "CurrencyCurrencyRegionRegionLink",
495 entity: "LinkCurrencyCurrencyRegionRegion",
496 primaryKey: "currency_code",
497 foreignKey: "code",
498 alias: "region_link",
499 isList: true,
500 },
501 },
502 {
503 serviceName: "region",
504 entity: "Region",
505 fieldAlias: {
506 currencies: {
507 path: "currency_link.currency",
508 isList: true,
509 forwardArgumentsOnPath: ["currency_link.currency"],
510 },
511 },
512 relationship: {
513 serviceName: "CurrencyCurrencyRegionRegionLink",
514 entity: "LinkCurrencyCurrencyRegionRegion",
515 primaryKey: "region_id",
516 foreignKey: "id",
517 alias: "currency_link",
518 isList: true,
519 },
520 },
521 ],
522 })
523 })
524
525 it("should generate a proper link with both sides using explicit isList=false", async () => {
526 const currencyLinks = CurrencyModule.linkable
527 const regionLinks = RegionModule.linkable
528
529 const link = defineLink(
530 {
531 linkable: currencyLinks.currency,
532 isList: false,
533 },
534 {
535 linkable: regionLinks.region,
536 isList: false,
537 }
538 )
539
540 const linkDefinition = MedusaModule.getCustomLinks()
541 .map((linkDefinition: any) => {
542 const definition = linkDefinition(
543 MedusaModule.getAllJoinerConfigs()
544 )
545 return definition.serviceName === link.serviceName && definition
546 })
547 .filter(Boolean)[0]
548
549 expect(link.serviceName).toEqual("CurrencyCurrencyRegionRegionLink")
550 expect(linkDefinition).toEqual({
551 serviceName: "CurrencyCurrencyRegionRegionLink",
552 isLink: true,
553 alias: [
554 {
555 name: ["currency_region"],
556 args: {
557 entity: "LinkCurrencyCurrencyRegionRegion",
558 },
559 },
560 ],
561 primaryKeys: ["id", "currency_code", "region_id"],
562 relationships: [
563 {
564 serviceName: "currency",
565 entity: "Currency",
566 primaryKey: "code",
567 foreignKey: "currency_code",
568 hasMany: false,
569 alias: "currency",
570 args: {
571 methodSuffix: "Currencies",
572 },
573 deleteCascade: false,
574 },
575 {
576 serviceName: "region",
577 entity: "Region",
578 primaryKey: "id",
579 foreignKey: "region_id",
580 hasMany: false,
581 alias: "region",
582 args: {
583 methodSuffix: "Regions",
584 },
585 deleteCascade: false,
586 },
587 ],
588 extends: [
589 {
590 serviceName: "currency",
591 entity: "Currency",
592 fieldAlias: {
593 region: {
594 path: "region_link.region",
595 isList: false,
596 forwardArgumentsOnPath: ["region_link.region"],
597 },
598 },
599 relationship: {
600 serviceName: "CurrencyCurrencyRegionRegionLink",
601 entity: "LinkCurrencyCurrencyRegionRegion",
602 primaryKey: "currency_code",
603 foreignKey: "code",
604 alias: "region_link",
605 isList: false,
606 },
607 },
608 {
609 serviceName: "region",
610 entity: "Region",
611 fieldAlias: {
612 currency: {
613 path: "currency_link.currency",
614 isList: false,
615 forwardArgumentsOnPath: ["currency_link.currency"],
616 },
617 },
618 relationship: {
619 serviceName: "CurrencyCurrencyRegionRegionLink",
620 entity: "LinkCurrencyCurrencyRegionRegion",
621 primaryKey: "region_id",
622 foreignKey: "id",
623 alias: "currency_link",
624 isList: false,
625 },
626 },
627 ],
628 })
629 })
630
631 it("should generate a read-only link definition with pluralized alias when isList is true", async () => {
632 const currencyLinks = CurrencyModule.linkable
633 const regionLinks = RegionModule.linkable
634
635 defineLink(
636 {
637 linkable: currencyLinks.currency,
638 field: "region_id",
639 },
640 {
641 linkable: regionLinks.region,
642 },
643 {
644 readOnly: true,
645 isList: true,
646 }
647 )
648
649 const linkDefinition = MedusaModule.getCustomLinks()
650 .map((linkDefinition: any) => {
651 const definition = linkDefinition(
652 MedusaModule.getAllJoinerConfigs()
653 )
654 return definition.isReadOnlyLink && definition
655 })
656 .filter(Boolean)[0]
657
658 expect(linkDefinition).toEqual({
659 isLink: true,
660 isReadOnlyLink: true,
661 extends: [
662 {
663 serviceName: "currency",
664 entity: "Currency",
665 fieldAlias: undefined,
666 relationship: {
667 serviceName: "region",
668 entity: "Region",
669 primaryKey: "id",
670 foreignKey: "region_id",
671 alias: "region",
672 isList: true,
673 },
674 },
675 {
676 serviceName: "currency",
677 entity: "Currency",
678 fieldAlias: undefined,
679 relationship: {
680 serviceName: "region",
681 entity: "Region",
682 primaryKey: "id",
683 foreignKey: "region_id",
684 alias: "regions",
685 isList: true,
686 },
687 },
688 ],
689 })
690 })
691
692 it("should use a custom alias when provided in InputOptions for a regular link", async () => {
693 const currencyLinks = CurrencyModule.linkable
694 const regionLinks = RegionModule.linkable
695
696 const link = defineLink(currencyLinks.currency, {
697 linkable: regionLinks.region,
698 alias: "primary_region",
699 })
700
701 const linkDefinition = MedusaModule.getCustomLinks()
702 .map((linkDefinition: any) => {
703 const definition = linkDefinition(
704 MedusaModule.getAllJoinerConfigs()
705 )
706 return definition.serviceName === link.serviceName && definition
707 })
708 .filter(Boolean)[0]
709
710 expect(link.serviceName).toEqual(
711 "CurrencyCurrencyRegionPrimaryRegionLink"
712 )
713 expect(link.entryPoint).toEqual("currency_primary_region")
714 expect(linkDefinition.relationships[1].alias).toEqual("primary_region")
715 expect(linkDefinition.extends[0].fieldAlias).toHaveProperty(
716 "primary_region"
717 )
718 expect(linkDefinition.extends[0].relationship.alias).toEqual(
719 "primary_region_link"
720 )
721 })
722
723 it("should allow two read-only links to the same module using distinct aliases", async () => {
724 const currencyLinks = CurrencyModule.linkable
725 const regionLinks = RegionModule.linkable
726
727 defineLink(
728 {
729 linkable: currencyLinks.currency,
730 field: "primary_region_id",
731 },
732 {
733 linkable: regionLinks.region,
734 alias: "primary_region",
735 },
736 { readOnly: true }
737 )
738
739 defineLink(
740 {
741 linkable: currencyLinks.currency,
742 field: "secondary_region_id",
743 },
744 {
745 linkable: regionLinks.region,
746 alias: "secondary_region",
747 },
748 { readOnly: true }
749 )
750
751 const readOnlyLinks = MedusaModule.getCustomLinks()
752 .map((linkDefinition: any) => {
753 const definition = linkDefinition(
754 MedusaModule.getAllJoinerConfigs()
755 )
756 return definition.isReadOnlyLink && definition
757 })
758 .filter(Boolean)
759
760 const primaryLink = readOnlyLinks.find((d: any) =>
761 d.extends.some(
762 (e: any) => e.relationship?.alias === "primary_region"
763 )
764 )
765 const secondaryLink = readOnlyLinks.find((d: any) =>
766 d.extends.some(
767 (e: any) => e.relationship?.alias === "secondary_region"
768 )
769 )
770
771 expect(primaryLink).toBeDefined()
772 expect(secondaryLink).toBeDefined()
773
774 expect(primaryLink.extends[0].relationship).toMatchObject({
775 alias: "primary_region",
776 foreignKey: "primary_region_id",
777 serviceName: "region",
778 })
779 expect(secondaryLink.extends[0].relationship).toMatchObject({
780 alias: "secondary_region",
781 foreignKey: "secondary_region_id",
782 serviceName: "region",
783 })
784 })
785 })
786 },
787 })