|
1
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
2
|
import { MedusaContainer } from "@medusajs/types" |
|
3
|
import { Modules, ProductStatus } from "@medusajs/utils" |
|
4
|
import { |
|
5
|
createAdminUser, |
|
6
|
generatePublishableKey, |
|
7
|
generateStoreHeaders, |
|
8
|
} from "../../../../helpers/create-admin-user" |
|
9
|
|
|
10
|
jest.setTimeout(100000) |
|
11
|
|
|
12
|
process.env.MEDUSA_FF_TRANSLATION = "true" |
|
13
|
|
|
14
|
const adminHeaders = { headers: { "x-medusa-access-token": "test_token" } } |
|
15
|
|
|
16
|
const shippingAddressData = { |
|
17
|
address_1: "test address 1", |
|
18
|
address_2: "test address 2", |
|
19
|
city: "SF", |
|
20
|
country_code: "US", |
|
21
|
province: "CA", |
|
22
|
postal_code: "94016", |
|
23
|
} |
|
24
|
|
|
25
|
medusaIntegrationTestRunner({ |
|
26
|
testSuite: ({ dbConnection, getContainer, api, dbUtils }) => { |
|
27
|
describe("Store Cart Translation API", () => { |
|
28
|
let appContainer: MedusaContainer |
|
29
|
let storeHeaders: { headers: { [key: string]: string } } |
|
30
|
let region: { id: string } |
|
31
|
let product: { id: string; variants: { id: string; title: string }[] } |
|
32
|
let salesChannel: { id: string } |
|
33
|
let shippingProfile: { id: string } |
|
34
|
let taxRegion: { id: string } |
|
35
|
let taxRate: { id: string; name: string } |
|
36
|
|
|
37
|
beforeAll(async () => { |
|
38
|
appContainer = getContainer() |
|
39
|
}) |
|
40
|
|
|
41
|
afterAll(async () => { |
|
42
|
delete process.env.MEDUSA_FF_TRANSLATION |
|
43
|
}) |
|
44
|
|
|
45
|
beforeAll(async () => { |
|
46
|
await createAdminUser(dbConnection, adminHeaders, appContainer) |
|
47
|
|
|
48
|
const translationModule = appContainer.resolve(Modules.TRANSLATION) |
|
49
|
await translationModule.__hooks?.onApplicationStart?.().catch(() => {}) |
|
50
|
|
|
51
|
const publishableKey = await generatePublishableKey(appContainer) |
|
52
|
storeHeaders = generateStoreHeaders({ publishableKey }) |
|
53
|
|
|
54
|
salesChannel = ( |
|
55
|
await api.post( |
|
56
|
"/admin/sales-channels", |
|
57
|
{ name: "Webshop", description: "channel" }, |
|
58
|
adminHeaders |
|
59
|
) |
|
60
|
).data.sales_channel |
|
61
|
|
|
62
|
const storeModule = appContainer.resolve(Modules.STORE) |
|
63
|
const [defaultStore] = await storeModule.listStores( |
|
64
|
{}, |
|
65
|
{ |
|
66
|
select: ["id"], |
|
67
|
take: 1, |
|
68
|
} |
|
69
|
) |
|
70
|
await storeModule.updateStores(defaultStore.id, { |
|
71
|
supported_locales: [ |
|
72
|
{ locale_code: "en-US" }, |
|
73
|
{ locale_code: "fr-FR" }, |
|
74
|
{ locale_code: "de-DE" }, |
|
75
|
], |
|
76
|
}) |
|
77
|
|
|
78
|
shippingProfile = ( |
|
79
|
await api.post( |
|
80
|
`/admin/shipping-profiles`, |
|
81
|
{ name: "default", type: "default" }, |
|
82
|
adminHeaders |
|
83
|
) |
|
84
|
).data.shipping_profile |
|
85
|
|
|
86
|
region = ( |
|
87
|
await api.post( |
|
88
|
"/admin/regions", |
|
89
|
{ name: "US", currency_code: "usd", countries: ["us"] }, |
|
90
|
adminHeaders |
|
91
|
) |
|
92
|
).data.region |
|
93
|
|
|
94
|
// Create tax region and tax rate for tax line translations |
|
95
|
taxRegion = ( |
|
96
|
await api.post( |
|
97
|
"/admin/tax-regions", |
|
98
|
{ |
|
99
|
country_code: "us", |
|
100
|
provider_id: "tp_system", |
|
101
|
}, |
|
102
|
adminHeaders |
|
103
|
) |
|
104
|
).data.tax_region |
|
105
|
|
|
106
|
// Create the default tax rate |
|
107
|
taxRate = ( |
|
108
|
await api.post( |
|
109
|
"/admin/tax-rates", |
|
110
|
{ |
|
111
|
tax_region_id: taxRegion.id, |
|
112
|
name: "US Sales Tax", |
|
113
|
rate: 5, |
|
114
|
code: "US_SALES_TAX", |
|
115
|
is_default: true, |
|
116
|
}, |
|
117
|
adminHeaders |
|
118
|
) |
|
119
|
).data.tax_rate |
|
120
|
|
|
121
|
// Create translations for tax rate name |
|
122
|
await api.post( |
|
123
|
"/admin/translations/batch", |
|
124
|
{ |
|
125
|
create: [ |
|
126
|
{ |
|
127
|
reference_id: taxRate.id, |
|
128
|
reference: "tax_rate", |
|
129
|
locale_code: "fr-FR", |
|
130
|
translations: { |
|
131
|
name: "Taxe de vente US", |
|
132
|
}, |
|
133
|
}, |
|
134
|
{ |
|
135
|
reference_id: taxRate.id, |
|
136
|
reference: "tax_rate", |
|
137
|
locale_code: "de-DE", |
|
138
|
translations: { |
|
139
|
name: "US Umsatzsteuer", |
|
140
|
}, |
|
141
|
}, |
|
142
|
], |
|
143
|
}, |
|
144
|
adminHeaders |
|
145
|
) |
|
146
|
|
|
147
|
// Create product with description for translation |
|
148
|
product = ( |
|
149
|
await api.post( |
|
150
|
"/admin/products", |
|
151
|
{ |
|
152
|
title: "Medusa T-Shirt", |
|
153
|
description: "A comfortable cotton t-shirt", |
|
154
|
handle: "t-shirt", |
|
155
|
status: ProductStatus.PUBLISHED, |
|
156
|
shipping_profile_id: shippingProfile.id, |
|
157
|
options: [ |
|
158
|
{ |
|
159
|
title: "Size", |
|
160
|
values: ["S", "M"], |
|
161
|
}, |
|
162
|
], |
|
163
|
variants: [ |
|
164
|
{ |
|
165
|
title: "Small", |
|
166
|
sku: "SHIRT-S", |
|
167
|
options: { Size: "S" }, |
|
168
|
manage_inventory: false, |
|
169
|
prices: [{ amount: 1500, currency_code: "usd" }], |
|
170
|
}, |
|
171
|
{ |
|
172
|
title: "Medium", |
|
173
|
sku: "SHIRT-M", |
|
174
|
options: { Size: "M" }, |
|
175
|
manage_inventory: false, |
|
176
|
prices: [{ amount: 1500, currency_code: "usd" }], |
|
177
|
}, |
|
178
|
], |
|
179
|
}, |
|
180
|
adminHeaders |
|
181
|
) |
|
182
|
).data.product |
|
183
|
|
|
184
|
// Maintain predictable variants order |
|
185
|
const variantSmall = product.variants.find((v) => v.title === "Small") |
|
186
|
const variantMedium = product.variants.find((v) => v.title === "Medium") |
|
187
|
product.variants = [variantSmall!, variantMedium!] |
|
188
|
|
|
189
|
// Create translations for product and variants |
|
190
|
await api.post( |
|
191
|
"/admin/translations/batch", |
|
192
|
{ |
|
193
|
create: [ |
|
194
|
{ |
|
195
|
reference_id: product.id, |
|
196
|
reference: "product", |
|
197
|
locale_code: "fr-FR", |
|
198
|
translations: { |
|
199
|
title: "T-Shirt Medusa", |
|
200
|
description: "Un t-shirt en coton confortable", |
|
201
|
}, |
|
202
|
}, |
|
203
|
{ |
|
204
|
reference_id: product.id, |
|
205
|
reference: "product", |
|
206
|
locale_code: "de-DE", |
|
207
|
translations: { |
|
208
|
title: "Medusa T-Shirt DE", |
|
209
|
description: "Ein bequemes Baumwoll-T-Shirt", |
|
210
|
}, |
|
211
|
}, |
|
212
|
{ |
|
213
|
reference_id: product.variants[0].id, |
|
214
|
reference: "product_variant", |
|
215
|
locale_code: "fr-FR", |
|
216
|
translations: { |
|
217
|
title: "Petit", |
|
218
|
}, |
|
219
|
}, |
|
220
|
{ |
|
221
|
reference_id: product.variants[0].id, |
|
222
|
reference: "product_variant", |
|
223
|
locale_code: "de-DE", |
|
224
|
translations: { |
|
225
|
title: "Klein", |
|
226
|
}, |
|
227
|
}, |
|
228
|
{ |
|
229
|
reference_id: product.variants[1].id, |
|
230
|
reference: "product_variant", |
|
231
|
locale_code: "fr-FR", |
|
232
|
translations: { |
|
233
|
title: "Moyen", |
|
234
|
}, |
|
235
|
}, |
|
236
|
{ |
|
237
|
reference_id: product.variants[1].id, |
|
238
|
reference: "product_variant", |
|
239
|
locale_code: "de-DE", |
|
240
|
translations: { |
|
241
|
title: "Mittel", |
|
242
|
}, |
|
243
|
}, |
|
244
|
], |
|
245
|
}, |
|
246
|
adminHeaders |
|
247
|
) |
|
248
|
|
|
249
|
await dbUtils.snapshot() |
|
250
|
}) |
|
251
|
|
|
252
|
describe("POST /store/carts (create cart with locale)", () => { |
|
253
|
it("should create a cart with translated items when locale is provided", async () => { |
|
254
|
const response = await api.post( |
|
255
|
`/store/carts`, |
|
256
|
{ |
|
257
|
currency_code: "usd", |
|
258
|
sales_channel_id: salesChannel.id, |
|
259
|
region_id: region.id, |
|
260
|
locale: "fr-FR", |
|
261
|
shipping_address: shippingAddressData, |
|
262
|
items: [{ variant_id: product.variants[0].id, quantity: 1 }], |
|
263
|
}, |
|
264
|
storeHeaders |
|
265
|
) |
|
266
|
|
|
267
|
expect(response.status).toEqual(200) |
|
268
|
expect(response.data.cart).toEqual( |
|
269
|
expect.objectContaining({ |
|
270
|
items: expect.arrayContaining([ |
|
271
|
expect.objectContaining({ |
|
272
|
product_title: "T-Shirt Medusa", |
|
273
|
product_description: "Un t-shirt en coton confortable", |
|
274
|
variant_title: "Petit", |
|
275
|
}), |
|
276
|
]), |
|
277
|
}) |
|
278
|
) |
|
279
|
}) |
|
280
|
|
|
281
|
it("should create a cart with original values when no locale is provided", async () => { |
|
282
|
const response = await api.post( |
|
283
|
`/store/carts`, |
|
284
|
{ |
|
285
|
currency_code: "usd", |
|
286
|
sales_channel_id: salesChannel.id, |
|
287
|
region_id: region.id, |
|
288
|
shipping_address: shippingAddressData, |
|
289
|
items: [{ variant_id: product.variants[0].id, quantity: 1 }], |
|
290
|
}, |
|
291
|
storeHeaders |
|
292
|
) |
|
293
|
|
|
294
|
expect(response.status).toEqual(200) |
|
295
|
expect(response.data.cart.items[0]).toEqual( |
|
296
|
expect.objectContaining({ |
|
297
|
product_title: "Medusa T-Shirt", |
|
298
|
product_description: "A comfortable cotton t-shirt", |
|
299
|
variant_title: "Small", |
|
300
|
}) |
|
301
|
) |
|
302
|
}) |
|
303
|
}) |
|
304
|
|
|
305
|
describe("POST /store/carts/:id/line-items (add items to cart)", () => { |
|
306
|
it("should translate new items using the cart's locale", async () => { |
|
307
|
const cartResponse = await api.post( |
|
308
|
`/store/carts`, |
|
309
|
{ |
|
310
|
currency_code: "usd", |
|
311
|
sales_channel_id: salesChannel.id, |
|
312
|
region_id: region.id, |
|
313
|
locale: "fr-FR", |
|
314
|
shipping_address: shippingAddressData, |
|
315
|
}, |
|
316
|
storeHeaders |
|
317
|
) |
|
318
|
|
|
319
|
const cart = cartResponse.data.cart |
|
320
|
|
|
321
|
const addItemResponse = await api.post( |
|
322
|
`/store/carts/${cart.id}/line-items`, |
|
323
|
{ |
|
324
|
variant_id: product.variants[0].id, |
|
325
|
quantity: 1, |
|
326
|
}, |
|
327
|
storeHeaders |
|
328
|
) |
|
329
|
|
|
330
|
expect(addItemResponse.status).toEqual(200) |
|
331
|
expect(addItemResponse.data.cart.items[0]).toEqual( |
|
332
|
expect.objectContaining({ |
|
333
|
product_title: "T-Shirt Medusa", |
|
334
|
product_description: "Un t-shirt en coton confortable", |
|
335
|
variant_title: "Petit", |
|
336
|
}) |
|
337
|
) |
|
338
|
}) |
|
339
|
|
|
340
|
it("should translate multiple items added to cart", async () => { |
|
341
|
const cartResponse = await api.post( |
|
342
|
`/store/carts`, |
|
343
|
{ |
|
344
|
currency_code: "usd", |
|
345
|
sales_channel_id: salesChannel.id, |
|
346
|
region_id: region.id, |
|
347
|
locale: "fr-FR", |
|
348
|
shipping_address: shippingAddressData, |
|
349
|
}, |
|
350
|
storeHeaders |
|
351
|
) |
|
352
|
|
|
353
|
const cart = cartResponse.data.cart |
|
354
|
|
|
355
|
await api.post( |
|
356
|
`/store/carts/${cart.id}/line-items`, |
|
357
|
{ |
|
358
|
variant_id: product.variants[0].id, |
|
359
|
quantity: 1, |
|
360
|
}, |
|
361
|
storeHeaders |
|
362
|
) |
|
363
|
|
|
364
|
const response = await api.post( |
|
365
|
`/store/carts/${cart.id}/line-items`, |
|
366
|
{ |
|
367
|
variant_id: product.variants[1].id, |
|
368
|
quantity: 2, |
|
369
|
}, |
|
370
|
storeHeaders |
|
371
|
) |
|
372
|
|
|
373
|
expect(response.status).toEqual(200) |
|
374
|
expect(response.data.cart.items).toHaveLength(2) |
|
375
|
|
|
376
|
const smallItem = response.data.cart.items.find( |
|
377
|
(item) => item.variant_id === product.variants[0].id |
|
378
|
) |
|
379
|
const mediumItem = response.data.cart.items.find( |
|
380
|
(item) => item.variant_id === product.variants[1].id |
|
381
|
) |
|
382
|
|
|
383
|
expect(smallItem).toEqual( |
|
384
|
expect.objectContaining({ |
|
385
|
product_title: "T-Shirt Medusa", |
|
386
|
variant_title: "Petit", |
|
387
|
}) |
|
388
|
) |
|
389
|
expect(mediumItem).toEqual( |
|
390
|
expect.objectContaining({ |
|
391
|
product_title: "T-Shirt Medusa", |
|
392
|
variant_title: "Moyen", |
|
393
|
}) |
|
394
|
) |
|
395
|
}) |
|
396
|
}) |
|
397
|
|
|
398
|
describe("POST /store/carts/:id (update cart locale)", () => { |
|
399
|
it("should re-translate all items when locale is updated", async () => { |
|
400
|
const cartResponse = await api.post( |
|
401
|
`/store/carts`, |
|
402
|
{ |
|
403
|
currency_code: "usd", |
|
404
|
sales_channel_id: salesChannel.id, |
|
405
|
region_id: region.id, |
|
406
|
locale: "fr-FR", |
|
407
|
shipping_address: shippingAddressData, |
|
408
|
items: [ |
|
409
|
{ variant_id: product.variants[0].id, quantity: 1 }, |
|
410
|
{ variant_id: product.variants[1].id, quantity: 1 }, |
|
411
|
], |
|
412
|
}, |
|
413
|
storeHeaders |
|
414
|
) |
|
415
|
|
|
416
|
const cart = cartResponse.data.cart |
|
417
|
|
|
418
|
const frenchSmallItem = cart.items.find( |
|
419
|
(item) => item.variant_id === product.variants[0].id |
|
420
|
) |
|
421
|
expect(frenchSmallItem.variant_title).toEqual("Petit") |
|
422
|
|
|
423
|
const updateResponse = await api.post( |
|
424
|
`/store/carts/${cart.id}`, |
|
425
|
{ |
|
426
|
locale: "de-DE", |
|
427
|
}, |
|
428
|
storeHeaders |
|
429
|
) |
|
430
|
|
|
431
|
expect(updateResponse.status).toEqual(200) |
|
432
|
|
|
433
|
const updatedCartResponse = await api.get( |
|
434
|
`/store/carts/${cart.id}`, |
|
435
|
storeHeaders |
|
436
|
) |
|
437
|
|
|
438
|
const updatedCart = updatedCartResponse.data.cart |
|
439
|
|
|
440
|
const germanSmallItem = updatedCart.items.find( |
|
441
|
(item) => item.variant_id === product.variants[0].id |
|
442
|
) |
|
443
|
const germanMediumItem = updatedCart.items.find( |
|
444
|
(item) => item.variant_id === product.variants[1].id |
|
445
|
) |
|
446
|
|
|
447
|
expect(germanSmallItem).toEqual( |
|
448
|
expect.objectContaining({ |
|
449
|
product_title: "Medusa T-Shirt DE", |
|
450
|
product_description: "Ein bequemes Baumwoll-T-Shirt", |
|
451
|
variant_title: "Klein", |
|
452
|
}) |
|
453
|
) |
|
454
|
expect(germanMediumItem).toEqual( |
|
455
|
expect.objectContaining({ |
|
456
|
product_title: "Medusa T-Shirt DE", |
|
457
|
variant_title: "Mittel", |
|
458
|
}) |
|
459
|
) |
|
460
|
}) |
|
461
|
|
|
462
|
it("should not re-translate items when locale is not changed", async () => { |
|
463
|
const cartResponse = await api.post( |
|
464
|
`/store/carts`, |
|
465
|
{ |
|
466
|
currency_code: "usd", |
|
467
|
sales_channel_id: salesChannel.id, |
|
468
|
region_id: region.id, |
|
469
|
locale: "fr-FR", |
|
470
|
shipping_address: shippingAddressData, |
|
471
|
items: [{ variant_id: product.variants[0].id, quantity: 1 }], |
|
472
|
}, |
|
473
|
storeHeaders |
|
474
|
) |
|
475
|
|
|
476
|
const cart = cartResponse.data.cart |
|
477
|
|
|
478
|
const updateResponse = await api.post( |
|
479
|
`/store/carts/${cart.id}`, |
|
480
|
{ |
|
481
|
email: "test@example.com", |
|
482
|
}, |
|
483
|
storeHeaders |
|
484
|
) |
|
485
|
|
|
486
|
expect(updateResponse.status).toEqual(200) |
|
487
|
|
|
488
|
const updatedCartResponse = await api.get( |
|
489
|
`/store/carts/${cart.id}`, |
|
490
|
storeHeaders |
|
491
|
) |
|
492
|
|
|
493
|
const updatedCart = updatedCartResponse.data.cart |
|
494
|
expect(updatedCart.items[0]).toEqual( |
|
495
|
expect.objectContaining({ |
|
496
|
product_title: "T-Shirt Medusa", |
|
497
|
variant_title: "Petit", |
|
498
|
}) |
|
499
|
) |
|
500
|
}) |
|
501
|
|
|
502
|
it("should handle updating to a locale with no translations", async () => { |
|
503
|
const cartResponse = await api.post( |
|
504
|
`/store/carts`, |
|
505
|
{ |
|
506
|
currency_code: "usd", |
|
507
|
sales_channel_id: salesChannel.id, |
|
508
|
region_id: region.id, |
|
509
|
locale: "fr-FR", |
|
510
|
shipping_address: shippingAddressData, |
|
511
|
items: [{ variant_id: product.variants[0].id, quantity: 1 }], |
|
512
|
}, |
|
513
|
storeHeaders |
|
514
|
) |
|
515
|
|
|
516
|
const cart = cartResponse.data.cart |
|
517
|
|
|
518
|
const updateResponse = await api.post( |
|
519
|
`/store/carts/${cart.id}`, |
|
520
|
{ |
|
521
|
locale: "ja-JP", |
|
522
|
}, |
|
523
|
storeHeaders |
|
524
|
) |
|
525
|
|
|
526
|
expect(updateResponse.status).toEqual(200) |
|
527
|
|
|
528
|
// Fetch updated cart - should have original values since no Japanese translation exists |
|
529
|
const updatedCartResponse = await api.get( |
|
530
|
`/store/carts/${cart.id}`, |
|
531
|
storeHeaders |
|
532
|
) |
|
533
|
|
|
534
|
// no translation means it will revert to default values |
|
535
|
const updatedCart = updatedCartResponse.data.cart |
|
536
|
expect(updatedCart.items[0]).toEqual( |
|
537
|
expect.objectContaining({ |
|
538
|
product_title: "Medusa T-Shirt", |
|
539
|
variant_title: "Small", |
|
540
|
}) |
|
541
|
) |
|
542
|
}) |
|
543
|
}) |
|
544
|
|
|
545
|
describe("Cart with items and locale changes", () => { |
|
546
|
it("should maintain translations when adding items to a cart with existing locale", async () => { |
|
547
|
const cartResponse = await api.post( |
|
548
|
`/store/carts`, |
|
549
|
{ |
|
550
|
currency_code: "usd", |
|
551
|
sales_channel_id: salesChannel.id, |
|
552
|
region_id: region.id, |
|
553
|
locale: "fr-FR", |
|
554
|
shipping_address: shippingAddressData, |
|
555
|
items: [{ variant_id: product.variants[0].id, quantity: 1 }], |
|
556
|
}, |
|
557
|
storeHeaders |
|
558
|
) |
|
559
|
|
|
560
|
const cart = cartResponse.data.cart |
|
561
|
|
|
562
|
const addResponse = await api.post( |
|
563
|
`/store/carts/${cart.id}/line-items`, |
|
564
|
{ |
|
565
|
variant_id: product.variants[1].id, |
|
566
|
quantity: 1, |
|
567
|
}, |
|
568
|
storeHeaders |
|
569
|
) |
|
570
|
|
|
571
|
expect(addResponse.data.cart.items).toHaveLength(2) |
|
572
|
|
|
573
|
const allItemsTranslated = addResponse.data.cart.items.every( |
|
574
|
(item) => item.product_title === "T-Shirt Medusa" |
|
575
|
) |
|
576
|
expect(allItemsTranslated).toBe(true) |
|
577
|
}) |
|
578
|
}) |
|
579
|
|
|
580
|
describe("Tax line translations", () => { |
|
581
|
it("should translate tax line descriptions when creating a cart with locale", async () => { |
|
582
|
const response = await api.post( |
|
583
|
`/store/carts`, |
|
584
|
{ |
|
585
|
currency_code: "usd", |
|
586
|
sales_channel_id: salesChannel.id, |
|
587
|
region_id: region.id, |
|
588
|
locale: "fr-FR", |
|
589
|
shipping_address: shippingAddressData, |
|
590
|
items: [{ variant_id: product.variants[0].id, quantity: 1 }], |
|
591
|
}, |
|
592
|
storeHeaders |
|
593
|
) |
|
594
|
|
|
595
|
expect(response.status).toEqual(200) |
|
596
|
expect(response.data.cart.items[0]).toEqual( |
|
597
|
expect.objectContaining({ |
|
598
|
tax_lines: expect.arrayContaining([ |
|
599
|
expect.objectContaining({ |
|
600
|
description: "Taxe de vente US", |
|
601
|
rate: 5, |
|
602
|
}), |
|
603
|
]), |
|
604
|
}) |
|
605
|
) |
|
606
|
}) |
|
607
|
|
|
608
|
it("should use original tax line description when no locale is provided", async () => { |
|
609
|
const response = await api.post( |
|
610
|
`/store/carts`, |
|
611
|
{ |
|
612
|
currency_code: "usd", |
|
613
|
sales_channel_id: salesChannel.id, |
|
614
|
region_id: region.id, |
|
615
|
shipping_address: shippingAddressData, |
|
616
|
items: [{ variant_id: product.variants[0].id, quantity: 1 }], |
|
617
|
}, |
|
618
|
storeHeaders |
|
619
|
) |
|
620
|
|
|
621
|
expect(response.status).toEqual(200) |
|
622
|
expect(response.data.cart.items[0]).toEqual( |
|
623
|
expect.objectContaining({ |
|
624
|
tax_lines: expect.arrayContaining([ |
|
625
|
expect.objectContaining({ |
|
626
|
description: "US Sales Tax", |
|
627
|
rate: 5, |
|
628
|
}), |
|
629
|
]), |
|
630
|
}) |
|
631
|
) |
|
632
|
}) |
|
633
|
|
|
634
|
it("should translate tax line descriptions when adding items to cart with locale", async () => { |
|
635
|
const cartResponse = await api.post( |
|
636
|
`/store/carts`, |
|
637
|
{ |
|
638
|
currency_code: "usd", |
|
639
|
sales_channel_id: salesChannel.id, |
|
640
|
region_id: region.id, |
|
641
|
locale: "de-DE", |
|
642
|
shipping_address: shippingAddressData, |
|
643
|
}, |
|
644
|
storeHeaders |
|
645
|
) |
|
646
|
|
|
647
|
const cart = cartResponse.data.cart |
|
648
|
|
|
649
|
const addItemResponse = await api.post( |
|
650
|
`/store/carts/${cart.id}/line-items`, |
|
651
|
{ |
|
652
|
variant_id: product.variants[0].id, |
|
653
|
quantity: 1, |
|
654
|
}, |
|
655
|
storeHeaders |
|
656
|
) |
|
657
|
|
|
658
|
expect(addItemResponse.status).toEqual(200) |
|
659
|
expect(addItemResponse.data.cart.items[0]).toEqual( |
|
660
|
expect.objectContaining({ |
|
661
|
tax_lines: expect.arrayContaining([ |
|
662
|
expect.objectContaining({ |
|
663
|
description: "US Umsatzsteuer", |
|
664
|
rate: 5, |
|
665
|
}), |
|
666
|
]), |
|
667
|
}) |
|
668
|
) |
|
669
|
}) |
|
670
|
|
|
671
|
it("should re-translate tax line descriptions when cart locale is updated", async () => { |
|
672
|
const cartResponse = await api.post( |
|
673
|
`/store/carts`, |
|
674
|
{ |
|
675
|
currency_code: "usd", |
|
676
|
sales_channel_id: salesChannel.id, |
|
677
|
region_id: region.id, |
|
678
|
locale: "fr-FR", |
|
679
|
shipping_address: shippingAddressData, |
|
680
|
items: [{ variant_id: product.variants[0].id, quantity: 1 }], |
|
681
|
}, |
|
682
|
storeHeaders |
|
683
|
) |
|
684
|
|
|
685
|
const cart = cartResponse.data.cart |
|
686
|
|
|
687
|
// Verify French translation |
|
688
|
expect(cart.items[0].tax_lines[0].description).toEqual( |
|
689
|
"Taxe de vente US" |
|
690
|
) |
|
691
|
|
|
692
|
// Update locale to German |
|
693
|
const updateResponse = await api.post( |
|
694
|
`/store/carts/${cart.id}`, |
|
695
|
{ |
|
696
|
locale: "de-DE", |
|
697
|
}, |
|
698
|
storeHeaders |
|
699
|
) |
|
700
|
|
|
701
|
expect(updateResponse.status).toEqual(200) |
|
702
|
|
|
703
|
// Fetch updated cart |
|
704
|
const updatedCartResponse = await api.get( |
|
705
|
`/store/carts/${cart.id}`, |
|
706
|
storeHeaders |
|
707
|
) |
|
708
|
|
|
709
|
const updatedCart = updatedCartResponse.data.cart |
|
710
|
|
|
711
|
// Verify German translation |
|
712
|
expect(updatedCart.items[0]).toEqual( |
|
713
|
expect.objectContaining({ |
|
714
|
tax_lines: expect.arrayContaining([ |
|
715
|
expect.objectContaining({ |
|
716
|
description: "US Umsatzsteuer", |
|
717
|
rate: 5, |
|
718
|
}), |
|
719
|
]), |
|
720
|
}) |
|
721
|
) |
|
722
|
}) |
|
723
|
|
|
724
|
it("should revert to original tax line description when locale has no translation", async () => { |
|
725
|
const cartResponse = await api.post( |
|
726
|
`/store/carts`, |
|
727
|
{ |
|
728
|
currency_code: "usd", |
|
729
|
sales_channel_id: salesChannel.id, |
|
730
|
region_id: region.id, |
|
731
|
locale: "fr-FR", |
|
732
|
shipping_address: shippingAddressData, |
|
733
|
items: [{ variant_id: product.variants[0].id, quantity: 1 }], |
|
734
|
}, |
|
735
|
storeHeaders |
|
736
|
) |
|
737
|
|
|
738
|
const cart = cartResponse.data.cart |
|
739
|
|
|
740
|
// Verify French translation exists |
|
741
|
expect(cart.items[0].tax_lines[0].description).toEqual( |
|
742
|
"Taxe de vente US" |
|
743
|
) |
|
744
|
|
|
745
|
// Update to locale without translation |
|
746
|
const updateResponse = await api.post( |
|
747
|
`/store/carts/${cart.id}`, |
|
748
|
{ |
|
749
|
locale: "ja-JP", |
|
750
|
}, |
|
751
|
storeHeaders |
|
752
|
) |
|
753
|
|
|
754
|
expect(updateResponse.status).toEqual(200) |
|
755
|
|
|
756
|
// Fetch updated cart |
|
757
|
const updatedCartResponse = await api.get( |
|
758
|
`/store/carts/${cart.id}`, |
|
759
|
storeHeaders |
|
760
|
) |
|
761
|
|
|
762
|
const updatedCart = updatedCartResponse.data.cart |
|
763
|
|
|
764
|
// Should revert to original description |
|
765
|
expect(updatedCart.items[0]).toEqual( |
|
766
|
expect.objectContaining({ |
|
767
|
tax_lines: expect.arrayContaining([ |
|
768
|
expect.objectContaining({ |
|
769
|
description: "US Sales Tax", |
|
770
|
rate: 5, |
|
771
|
}), |
|
772
|
]), |
|
773
|
}) |
|
774
|
) |
|
775
|
}) |
|
776
|
|
|
777
|
it("should translate tax lines for all items when multiple items are added", async () => { |
|
778
|
const cartResponse = await api.post( |
|
779
|
`/store/carts`, |
|
780
|
{ |
|
781
|
currency_code: "usd", |
|
782
|
sales_channel_id: salesChannel.id, |
|
783
|
region_id: region.id, |
|
784
|
locale: "fr-FR", |
|
785
|
shipping_address: shippingAddressData, |
|
786
|
}, |
|
787
|
storeHeaders |
|
788
|
) |
|
789
|
|
|
790
|
const cart = cartResponse.data.cart |
|
791
|
|
|
792
|
// Add first item |
|
793
|
await api.post( |
|
794
|
`/store/carts/${cart.id}/line-items`, |
|
795
|
{ |
|
796
|
variant_id: product.variants[0].id, |
|
797
|
quantity: 1, |
|
798
|
}, |
|
799
|
storeHeaders |
|
800
|
) |
|
801
|
|
|
802
|
// Add second item |
|
803
|
const response = await api.post( |
|
804
|
`/store/carts/${cart.id}/line-items`, |
|
805
|
{ |
|
806
|
variant_id: product.variants[1].id, |
|
807
|
quantity: 1, |
|
808
|
}, |
|
809
|
storeHeaders |
|
810
|
) |
|
811
|
|
|
812
|
expect(response.status).toEqual(200) |
|
813
|
expect(response.data.cart.items).toHaveLength(2) |
|
814
|
|
|
815
|
// Both items should have translated tax lines |
|
816
|
response.data.cart.items.forEach((item: any) => { |
|
817
|
expect(item.tax_lines).toEqual( |
|
818
|
expect.arrayContaining([ |
|
819
|
expect.objectContaining({ |
|
820
|
description: "Taxe de vente US", |
|
821
|
rate: 5, |
|
822
|
}), |
|
823
|
]) |
|
824
|
) |
|
825
|
}) |
|
826
|
}) |
|
827
|
}) |
|
828
|
|
|
829
|
describe("POST /store/carts/:id/shipping-methods (shipping method translation)", () => { |
|
830
|
let shippingOption |
|
831
|
|
|
832
|
beforeEach(async () => { |
|
833
|
const stockLocation = ( |
|
834
|
await api.post( |
|
835
|
`/admin/stock-locations`, |
|
836
|
{ name: "translation test location" }, |
|
837
|
adminHeaders |
|
838
|
) |
|
839
|
).data.stock_location |
|
840
|
|
|
841
|
await api.post( |
|
842
|
`/admin/stock-locations/${stockLocation.id}/sales-channels`, |
|
843
|
{ add: [salesChannel.id] }, |
|
844
|
adminHeaders |
|
845
|
) |
|
846
|
|
|
847
|
const fulfillmentSets = ( |
|
848
|
await api.post( |
|
849
|
`/admin/stock-locations/${stockLocation.id}/fulfillment-sets?fields=*fulfillment_sets`, |
|
850
|
{ |
|
851
|
name: `Translation-Test-${shippingProfile.id}`, |
|
852
|
type: "test-type", |
|
853
|
}, |
|
854
|
adminHeaders |
|
855
|
) |
|
856
|
).data.stock_location.fulfillment_sets |
|
857
|
|
|
858
|
const fulfillmentSet = ( |
|
859
|
await api.post( |
|
860
|
`/admin/fulfillment-sets/${fulfillmentSets[0].id}/service-zones`, |
|
861
|
{ |
|
862
|
name: `Translation-Test-${shippingProfile.id}`, |
|
863
|
geo_zones: [{ type: "country", country_code: "US" }], |
|
864
|
}, |
|
865
|
adminHeaders |
|
866
|
) |
|
867
|
).data.fulfillment_set |
|
868
|
|
|
869
|
await api.post( |
|
870
|
`/admin/stock-locations/${stockLocation.id}/fulfillment-providers`, |
|
871
|
{ add: ["manual_test-provider"] }, |
|
872
|
adminHeaders |
|
873
|
) |
|
874
|
|
|
875
|
shippingOption = ( |
|
876
|
await api.post( |
|
877
|
`/admin/shipping-options`, |
|
878
|
{ |
|
879
|
name: "Standard Shipping", |
|
880
|
service_zone_id: fulfillmentSet.service_zones[0].id, |
|
881
|
shipping_profile_id: shippingProfile.id, |
|
882
|
provider_id: "manual_test-provider", |
|
883
|
price_type: "flat", |
|
884
|
type: { |
|
885
|
label: "Standard", |
|
886
|
description: "Standard shipping option", |
|
887
|
code: "standard", |
|
888
|
}, |
|
889
|
prices: [{ currency_code: "usd", amount: 1000 }], |
|
890
|
rules: [], |
|
891
|
}, |
|
892
|
adminHeaders |
|
893
|
) |
|
894
|
).data.shipping_option |
|
895
|
|
|
896
|
// Create translations for shipping option |
|
897
|
await api.post( |
|
898
|
"/admin/translations/batch", |
|
899
|
{ |
|
900
|
create: [ |
|
901
|
{ |
|
902
|
reference_id: shippingOption.id, |
|
903
|
reference: "shipping_option", |
|
904
|
locale_code: "fr-FR", |
|
905
|
translations: { |
|
906
|
name: "Expédition Standard", |
|
907
|
}, |
|
908
|
}, |
|
909
|
{ |
|
910
|
reference_id: shippingOption.id, |
|
911
|
reference: "shipping_option", |
|
912
|
locale_code: "de-DE", |
|
913
|
translations: { |
|
914
|
name: "Standardversand", |
|
915
|
}, |
|
916
|
}, |
|
917
|
], |
|
918
|
}, |
|
919
|
adminHeaders |
|
920
|
) |
|
921
|
}) |
|
922
|
|
|
923
|
it("should add shipping method with translated name when cart has locale", async () => { |
|
924
|
const cartResponse = await api.post( |
|
925
|
`/store/carts`, |
|
926
|
{ |
|
927
|
currency_code: "usd", |
|
928
|
sales_channel_id: salesChannel.id, |
|
929
|
region_id: region.id, |
|
930
|
locale: "fr-FR", |
|
931
|
shipping_address: shippingAddressData, |
|
932
|
items: [{ variant_id: product.variants[0].id, quantity: 1 }], |
|
933
|
}, |
|
934
|
storeHeaders |
|
935
|
) |
|
936
|
|
|
937
|
const cart = cartResponse.data.cart |
|
938
|
|
|
939
|
await api.post( |
|
940
|
`/store/carts/${cart.id}/shipping-methods`, |
|
941
|
{ option_id: shippingOption.id }, |
|
942
|
storeHeaders |
|
943
|
) |
|
944
|
|
|
945
|
const updatedCart = await api |
|
946
|
.get( |
|
947
|
`/store/carts/${cart.id}?fields=+shipping_methods.name`, |
|
948
|
storeHeaders |
|
949
|
) |
|
950
|
.then((res) => res.data.cart) |
|
951
|
|
|
952
|
expect(updatedCart.shipping_methods).toHaveLength(1) |
|
953
|
expect(updatedCart.shipping_methods[0]).toEqual( |
|
954
|
expect.objectContaining({ |
|
955
|
shipping_option_id: shippingOption.id, |
|
956
|
name: "Expédition Standard", |
|
957
|
}) |
|
958
|
) |
|
959
|
}) |
|
960
|
|
|
961
|
it("should update shipping method name when cart locale changes", async () => { |
|
962
|
const cartResponse = await api.post( |
|
963
|
`/store/carts`, |
|
964
|
{ |
|
965
|
currency_code: "usd", |
|
966
|
sales_channel_id: salesChannel.id, |
|
967
|
region_id: region.id, |
|
968
|
locale: "fr-FR", |
|
969
|
shipping_address: shippingAddressData, |
|
970
|
items: [{ variant_id: product.variants[0].id, quantity: 1 }], |
|
971
|
}, |
|
972
|
storeHeaders |
|
973
|
) |
|
974
|
|
|
975
|
const cart = cartResponse.data.cart |
|
976
|
|
|
977
|
// Add shipping method with French locale |
|
978
|
await api.post( |
|
979
|
`/store/carts/${cart.id}/shipping-methods`, |
|
980
|
{ option_id: shippingOption.id }, |
|
981
|
storeHeaders |
|
982
|
) |
|
983
|
|
|
984
|
// Verify French translation |
|
985
|
let cartResponseAfter = await api.get( |
|
986
|
`/store/carts/${cart.id}?fields=+shipping_methods.name`, |
|
987
|
storeHeaders |
|
988
|
) |
|
989
|
|
|
990
|
expect(cartResponseAfter.data.cart.shipping_methods[0].name).toEqual( |
|
991
|
"Expédition Standard" |
|
992
|
) |
|
993
|
|
|
994
|
// Update cart locale to German |
|
995
|
await api.post( |
|
996
|
`/store/carts/${cart.id}`, |
|
997
|
{ |
|
998
|
locale: "de-DE", |
|
999
|
}, |
|
1000
|
storeHeaders |
|
1001
|
) |
|
1002
|
|
|
1003
|
// Verify German translation |
|
1004
|
cartResponseAfter = await api.get( |
|
1005
|
`/store/carts/${cart.id}?fields=+shipping_methods.name`, |
|
1006
|
storeHeaders |
|
1007
|
) |
|
1008
|
|
|
1009
|
expect(cartResponseAfter.data.cart.shipping_methods[0].name).toEqual( |
|
1010
|
"Standardversand" |
|
1011
|
) |
|
1012
|
}) |
|
1013
|
|
|
1014
|
it("should use original shipping option name when no translation exists", async () => { |
|
1015
|
const cartResponse = await api.post( |
|
1016
|
`/store/carts`, |
|
1017
|
{ |
|
1018
|
currency_code: "usd", |
|
1019
|
sales_channel_id: salesChannel.id, |
|
1020
|
region_id: region.id, |
|
1021
|
locale: "ja-JP", |
|
1022
|
shipping_address: shippingAddressData, |
|
1023
|
items: [{ variant_id: product.variants[0].id, quantity: 1 }], |
|
1024
|
}, |
|
1025
|
storeHeaders |
|
1026
|
) |
|
1027
|
|
|
1028
|
const cart = cartResponse.data.cart |
|
1029
|
|
|
1030
|
await api.post( |
|
1031
|
`/store/carts/${cart.id}/shipping-methods`, |
|
1032
|
{ option_id: shippingOption.id }, |
|
1033
|
storeHeaders |
|
1034
|
) |
|
1035
|
|
|
1036
|
const updatedCart = await api |
|
1037
|
.get( |
|
1038
|
`/store/carts/${cart.id}?fields=+shipping_methods.name`, |
|
1039
|
storeHeaders |
|
1040
|
) |
|
1041
|
.then((res) => res.data.cart) |
|
1042
|
|
|
1043
|
expect(updatedCart.shipping_methods[0]).toEqual( |
|
1044
|
expect.objectContaining({ |
|
1045
|
shipping_option_id: shippingOption.id, |
|
1046
|
name: "Standard Shipping", |
|
1047
|
}) |
|
1048
|
) |
|
1049
|
}) |
|
1050
|
}) |
|
1051
|
}) |
|
1052
|
}, |
|
1053
|
}) |