1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import {
3 ISalesChannelModuleService,
4 IStockLocationService,
5 } from "@medusajs/types"
6 import { Modules, remoteQueryObjectFromString } from "@medusajs/utils"
7
8 jest.setTimeout(50000)
9
10 const env = {}
11
12 medusaIntegrationTestRunner({
13 env,
14 testSuite: ({ getContainer }) => {
15 describe("Cart links", () => {
16 let appContainer
17 let scService: ISalesChannelModuleService
18 let locationService: IStockLocationService
19 let remoteQuery, remoteLink
20
21 beforeAll(async () => {
22 appContainer = getContainer()
23 scService = appContainer.resolve(Modules.SALES_CHANNEL)
24 locationService = appContainer.resolve(Modules.STOCK_LOCATION)
25 remoteQuery = appContainer.resolve("remoteQuery")
26 remoteLink = appContainer.resolve("remoteLink")
27 })
28
29 it("should query carts, sales channels, customers, regions with remote query", async () => {
30 const scWebshop = await scService.createSalesChannels({
31 name: "Webshop",
32 })
33
34 const scCphStore = await scService.createSalesChannels({
35 name: "CPH store",
36 })
37
38 const scNycStore = await scService.createSalesChannels({
39 name: "NYC store",
40 })
41
42 const euWarehouse = await locationService.createStockLocations({
43 name: "EU Warehouse",
44 })
45
46 const usWarehouse = await locationService.createStockLocations({
47 name: "US Warehouse",
48 })
49
50 await remoteLink.create([
51 {
52 [Modules.SALES_CHANNEL]: {
53 sales_channel_id: scWebshop.id,
54 },
55 [Modules.STOCK_LOCATION]: {
56 stock_location_id: euWarehouse.id,
57 },
58 },
59 {
60 [Modules.SALES_CHANNEL]: {
61 sales_channel_id: scCphStore.id,
62 },
63 [Modules.STOCK_LOCATION]: {
64 stock_location_id: euWarehouse.id,
65 },
66 },
67 {
68 [Modules.SALES_CHANNEL]: {
69 sales_channel_id: scNycStore.id,
70 },
71 [Modules.STOCK_LOCATION]: {
72 stock_location_id: usWarehouse.id,
73 },
74 },
75 ])
76
77 const euStockLocationChannelQuery = remoteQueryObjectFromString({
78 entryPoint: "stock_locations",
79 fields: ["id", "name", "sales_channels.id", "sales_channels.name"],
80 variables: { id: euWarehouse.id },
81 })
82
83 const usStockLocationChannelQuery = remoteQueryObjectFromString({
84 entryPoint: "stock_locations",
85 fields: ["id", "name", "sales_channels.id", "sales_channels.name"],
86 variables: { id: usWarehouse.id },
87 })
88
89 const euLocations = await remoteQuery(euStockLocationChannelQuery)
90 const usLocations = await remoteQuery(usStockLocationChannelQuery)
91
92 expect(euLocations.length).toBe(1)
93 expect(euLocations).toEqual([
94 expect.objectContaining({
95 id: euWarehouse.id,
96 sales_channels: [
97 expect.objectContaining({ id: scWebshop.id }),
98 expect.objectContaining({ id: scCphStore.id }),
99 ],
100 name: "EU Warehouse",
101 }),
102 ])
103
104 expect(usLocations.length).toBe(1)
105 expect(usLocations).toEqual([
106 expect.objectContaining({
107 id: usWarehouse.id,
108 sales_channels: [expect.objectContaining({ id: scNycStore.id })],
109 name: "US Warehouse",
110 }),
111 ])
112 })
113 })
114 },
115 })