1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import {
3 IFulfillmentModuleService,
4 IPricingModuleService,
5 } from "@medusajs/types"
6 import { ContainerRegistrationKeys, Modules } from "@medusajs/utils"
7 import {
8 adminHeaders,
9 createAdminUser,
10 } from "../../../helpers/create-admin-user"
11
12 jest.setTimeout(50000)
13
14 const env = {}
15
16 medusaIntegrationTestRunner({
17 env,
18 testSuite: ({ dbConnection, getContainer, api, dbUtils }) => {
19 describe("Region and Payment Providers", () => {
20 let appContainer
21 let fulfillmentModule: IFulfillmentModuleService
22 let pricingModule: IPricingModuleService
23 let remoteQuery
24
25 beforeAll(async () => {
26 appContainer = getContainer()
27 fulfillmentModule = appContainer.resolve(Modules.FULFILLMENT)
28 pricingModule = appContainer.resolve(Modules.PRICING)
29 remoteQuery = appContainer.resolve(
30 ContainerRegistrationKeys.REMOTE_QUERY
31 )
32 await createAdminUser(dbConnection, adminHeaders, getContainer())
33
34 await dbUtils.snapshot()
35 })
36
37 it("should query shipping option and price set link with remote query", async () => {
38 const shippingProfile = await fulfillmentModule.createShippingProfiles({
39 name: "Test",
40 type: "default",
41 })
42 const fulfillmentSet = await fulfillmentModule.createFulfillmentSets({
43 name: "Test",
44 type: "test-type",
45 })
46 const remoteLink = appContainer.resolve(
47 ContainerRegistrationKeys.REMOTE_LINK
48 )
49 const location = (
50 await api.post(
51 `/admin/stock-locations`,
52 { name: "Test location" },
53 adminHeaders
54 )
55 ).data.stock_location
56
57 await remoteLink.create([
58 {
59 [Modules.STOCK_LOCATION]: {
60 stock_location_id: location.id,
61 },
62 [Modules.FULFILLMENT]: {
63 fulfillment_set_id: fulfillmentSet.id,
64 },
65 },
66 ])
67
68 await api.post(
69 `/admin/stock-locations/${location.id}/fulfillment-providers`,
70 { add: ["manual_test-provider"] },
71 adminHeaders
72 )
73
74 const serviceZone = await fulfillmentModule.createServiceZones({
75 name: "Test",
76 fulfillment_set_id: fulfillmentSet.id,
77 geo_zones: [
78 {
79 type: "country",
80 country_code: "us",
81 },
82 ],
83 })
84
85 const shippingOption = await fulfillmentModule.createShippingOptions({
86 name: "Test shipping option",
87 service_zone_id: serviceZone.id,
88 shipping_profile_id: shippingProfile.id,
89 provider_id: "manual_test-provider",
90 price_type: "flat",
91 type: {
92 label: "Test type",
93 description: "Test description",
94 code: "test-code",
95 },
96 })
97
98 const priceSet = await pricingModule.createPriceSets({
99 prices: [
100 {
101 amount: 3000,
102 currency_code: "usd",
103 },
104 {
105 amount: 5000,
106 currency_code: "eur",
107 },
108 ],
109 })
110
111 await remoteLink.create([
112 {
113 [Modules.FULFILLMENT]: {
114 shipping_option_id: shippingOption.id,
115 },
116 [Modules.PRICING]: {
117 price_set_id: priceSet.id,
118 },
119 },
120 ])
121
122 const link = await remoteQuery({
123 shipping_option: {
124 fields: ["id"],
125 price_set_link: {
126 fields: ["id", "price_set_id", "shipping_option_id"],
127 },
128 prices: {
129 fields: ["amount", "currency_code"],
130 },
131 calculated_price: {
132 fields: ["calculated_amount", "currency_code"],
133 __args: {
134 context: {
135 currency_code: "eur",
136 },
137 },
138 },
139 },
140 })
141
142 expect(link).toHaveLength(1)
143 expect(link).toEqual(
144 expect.arrayContaining([
145 expect.objectContaining({
146 id: shippingOption.id,
147 price_set_link: expect.objectContaining({
148 price_set_id: priceSet.id,
149 shipping_option_id: shippingOption.id,
150 }),
151 prices: expect.arrayContaining([
152 expect.objectContaining({
153 amount: 5000,
154 currency_code: "eur",
155 }),
156 expect.objectContaining({
157 amount: 3000,
158 currency_code: "usd",
159 }),
160 ]),
161 calculated_price: expect.objectContaining({
162 calculated_amount: 5000,
163 currency_code: "eur",
164 }),
165 }),
166 ])
167 )
168 })
169 })
170 },
171 })