1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import {
3 createAdminUser,
4 generatePublishableKey,
5 generateStoreHeaders,
6 } from "../../../../helpers/create-admin-user"
7 import { Modules, ProductStatus } from "@medusajs/utils"
8
9 jest.setTimeout(50000)
10
11 process.env.MEDUSA_FF_TRANSLATION = "true"
12
13 const env = {}
14 const adminHeaders = { headers: { "x-medusa-access-token": "test_token" } }
15
16 medusaIntegrationTestRunner({
17 env,
18 testSuite: ({ dbConnection, getContainer, api, dbUtils }) => {
19 describe("Store: Shipping Option API (with translations)", () => {
20 let appContainer
21 let salesChannel
22 let region
23 let product
24 let stockLocation
25 let shippingProfile
26 let fulfillmentSet
27 let shippingOption
28 let storeHeaders
29
30 beforeAll(async () => {
31 appContainer = getContainer()
32 })
33
34 afterAll(async () => {
35 delete process.env.MEDUSA_FF_TRANSLATION
36 })
37
38 beforeAll(async () => {
39 const publishableKey = await generatePublishableKey(appContainer)
40 storeHeaders = generateStoreHeaders({ publishableKey })
41
42 await createAdminUser(dbConnection, adminHeaders, appContainer)
43
44 const translationModule = appContainer.resolve(Modules.TRANSLATION)
45 await translationModule.__hooks?.onApplicationStart?.().catch(() => {})
46
47 // Set up store locales
48 const storeModule = appContainer.resolve(Modules.STORE)
49 const [defaultStore] = await storeModule.listStores(
50 {},
51 {
52 select: ["id"],
53 take: 1,
54 }
55 )
56 await storeModule.updateStores(defaultStore.id, {
57 supported_locales: [
58 { locale_code: "en-US" },
59 { locale_code: "fr-FR" },
60 { locale_code: "de-DE" },
61 ],
62 })
63
64 region = (
65 await api.post(
66 "/admin/regions",
67 { name: "US", currency_code: "usd", countries: ["US"] },
68 adminHeaders
69 )
70 ).data.region
71
72 shippingProfile = (
73 await api.post(
74 `/admin/shipping-profiles`,
75 { name: "Test", type: "default" },
76 adminHeaders
77 )
78 ).data.shipping_profile
79
80 salesChannel = (
81 await api.post(
82 "/admin/sales-channels",
83 { name: "first channel", description: "channel" },
84 adminHeaders
85 )
86 ).data.sales_channel
87
88 product = (
89 await api.post(
90 "/admin/products",
91 {
92 title: "Test fixture",
93 status: ProductStatus.PUBLISHED,
94 options: [
95 { title: "size", values: ["large", "small"] },
96 { title: "color", values: ["green"] },
97 ],
98 shipping_profile_id: shippingProfile.id,
99 variants: [
100 {
101 title: "Test variant",
102 manage_inventory: false,
103 prices: [
104 {
105 currency_code: "usd",
106 amount: 100,
107 },
108 ],
109 options: {
110 size: "large",
111 color: "green",
112 },
113 },
114 ],
115 },
116 adminHeaders
117 )
118 ).data.product
119
120 stockLocation = (
121 await api.post(
122 `/admin/stock-locations`,
123 { name: "test location" },
124 adminHeaders
125 )
126 ).data.stock_location
127
128 await api.post(
129 `/admin/stock-locations/${stockLocation.id}/sales-channels`,
130 { add: [salesChannel.id] },
131 adminHeaders
132 )
133
134 const fulfillmentSets = (
135 await api.post(
136 `/admin/stock-locations/${stockLocation.id}/fulfillment-sets?fields=*fulfillment_sets`,
137 {
138 name: "Test",
139 type: "test-type",
140 },
141 adminHeaders
142 )
143 ).data.stock_location.fulfillment_sets
144
145 fulfillmentSet = (
146 await api.post(
147 `/admin/fulfillment-sets/${fulfillmentSets[0].id}/service-zones`,
148 {
149 name: "Test",
150 geo_zones: [
151 { type: "country", country_code: "us" },
152 { type: "country", country_code: "dk" },
153 ],
154 },
155 adminHeaders
156 )
157 ).data.fulfillment_set
158
159 await api.post(
160 `/admin/stock-locations/${stockLocation.id}/fulfillment-providers`,
161 { add: ["manual_test-provider"] },
162 adminHeaders
163 )
164
165 shippingOption = (
166 await api.post(
167 `/admin/shipping-options`,
168 {
169 name: "Test shipping option",
170 service_zone_id: fulfillmentSet.service_zones[0].id,
171 shipping_profile_id: shippingProfile.id,
172 provider_id: "manual_test-provider",
173 price_type: "flat",
174 type: {
175 label: "Test type",
176 description: "Test description",
177 code: "test-code",
178 },
179 prices: [
180 {
181 currency_code: "usd",
182 amount: 1000,
183 },
184 {
185 region_id: region.id,
186 amount: 1100,
187 },
188 ],
189 rules: [],
190 },
191 adminHeaders
192 )
193 ).data.shipping_option
194
195 // Create translations for shipping option
196 await api.post(
197 "/admin/translations/batch",
198 {
199 create: [
200 {
201 reference_id: shippingOption.id,
202 reference: "shipping_option",
203 locale_code: "fr-FR",
204 translations: {
205 name: "Option d'expédition test",
206 },
207 },
208 {
209 reference_id: shippingOption.id,
210 reference: "shipping_option",
211 locale_code: "de-DE",
212 translations: {
213 name: "Test-Versandoption",
214 },
215 },
216 ],
217 },
218 adminHeaders
219 )
220
221 await dbUtils.snapshot()
222 })
223
224 describe("GET /store/shipping-options?cart_id=", () => {
225 it("should return translated shipping options when cart has locale", async () => {
226 const cartResponse = await api.post(
227 `/store/carts`,
228 {
229 region_id: region.id,
230 sales_channel_id: salesChannel.id,
231 currency_code: "usd",
232 locale: "fr-FR",
233 email: "test@admin.com",
234 items: [
235 {
236 variant_id: product.variants[0].id,
237 quantity: 1,
238 },
239 ],
240 },
241 storeHeaders
242 )
243
244 const cart = cartResponse.data.cart
245
246 const shippingOptionsResponse = await api.get(
247 `/store/shipping-options?cart_id=${cart.id}`,
248 storeHeaders
249 )
250
251 expect(shippingOptionsResponse.status).toEqual(200)
252 expect(shippingOptionsResponse.data.shipping_options).toHaveLength(1)
253 expect(shippingOptionsResponse.data.shipping_options[0]).toEqual(
254 expect.objectContaining({
255 id: shippingOption.id,
256 name: "Option d'expédition test",
257 })
258 )
259 })
260
261 it("should return translated shipping options when locale is changed", async () => {
262 const cartResponse = await api.post(
263 `/store/carts`,
264 {
265 region_id: region.id,
266 sales_channel_id: salesChannel.id,
267 currency_code: "usd",
268 locale: "fr-FR",
269 email: "test@admin.com",
270 items: [
271 {
272 variant_id: product.variants[0].id,
273 quantity: 1,
274 },
275 ],
276 },
277 storeHeaders
278 )
279
280 const cart = cartResponse.data.cart
281
282 // Verify French translation
283 let shippingOptionsResponse = await api.get(
284 `/store/shipping-options?cart_id=${cart.id}`,
285 storeHeaders
286 )
287
288 expect(shippingOptionsResponse.data.shipping_options[0].name).toEqual(
289 "Option d'expédition test"
290 )
291
292 // Update cart locale to German
293 await api.post(
294 `/store/carts/${cart.id}`,
295 {
296 locale: "de-DE",
297 },
298 storeHeaders
299 )
300
301 // Verify German translation
302 shippingOptionsResponse = await api.get(
303 `/store/shipping-options?cart_id=${cart.id}`,
304 storeHeaders
305 )
306
307 expect(shippingOptionsResponse.data.shipping_options[0].name).toEqual(
308 "Test-Versandoption"
309 )
310 })
311
312 it("should return original shipping option name when no translation exists", async () => {
313 const cartResponse = await api.post(
314 `/store/carts`,
315 {
316 region_id: region.id,
317 sales_channel_id: salesChannel.id,
318 currency_code: "usd",
319 locale: "ja-JP",
320 email: "test@admin.com",
321 items: [
322 {
323 variant_id: product.variants[0].id,
324 quantity: 1,
325 },
326 ],
327 },
328 storeHeaders
329 )
330
331 const cart = cartResponse.data.cart
332
333 const shippingOptionsResponse = await api.get(
334 `/store/shipping-options?cart_id=${cart.id}`,
335 storeHeaders
336 )
337
338 expect(shippingOptionsResponse.status).toEqual(200)
339 expect(shippingOptionsResponse.data.shipping_options[0]).toEqual(
340 expect.objectContaining({
341 id: shippingOption.id,
342 name: "Test shipping option",
343 })
344 )
345 })
346 })
347 })
348 },
349 })