-
Notifications
You must be signed in to change notification settings - Fork 264
/
Copy pathclient-checkout-integration-test.js
595 lines (526 loc) · 19.8 KB
/
client-checkout-integration-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
import assert from 'assert';
import Client from '../src/client';
suite('client-checkout-integration-test', () => {
const domain = 'graphql.myshopify.com';
const config = {
storefrontAccessToken: '595005d0c565f6969eece280de85edb5',
domain,
apiVersion: '2025-01'
};
let client;
setup(() => {
client = Client.buildClient(config);
});
teardown(() => {
client = null;
});
suite('create', () => {
test('it resolves with an empty checkout', () => {
return client.checkout.create({}).then((checkout) => {
assert.ok((typeof checkout.webUrl === 'string'));
});
});
test('it resolves with a checkout created with custom attributes', () => {
const input = {
customAttributes: [
{
key: 'my-key',
value: 'my-value'
}
]
};
return client.checkout.create(input).then((checkout) => {
assert.strictEqual(checkout.customAttributes.length, 1);
assert.strictEqual(checkout.customAttributes[0].value, 'my-value');
assert.strictEqual(checkout.customAttributes[0].key, 'my-key');
});
});
test('it resolves with a checkout created with an email', () => {
const input = {
email: 'sdk@shopify.com'
};
return client.checkout.create(input).then((checkout) => {
assert.strictEqual(checkout.email, input.email);
});
});
test('it resolves with a checkout created with a line item', () => {
const input = {
lineItems: [
{
variantId: 'gid://shopify/ProductVariant/50850334310456',
quantity: 1
}
]
};
return client.checkout.create(input).then((checkout) => {
assert.strictEqual(checkout.lineItems.length, 1);
});
});
test('It resolves with a checkout created with multiple line items', () => {
const input = {
lineItems: [
{
variantId: 'gid://shopify/ProductVariant/50850334310456',
quantity: 1
},
{
variantId: 'gid://shopify/ProductVariant/50850336211000',
quantity: 1
}
]
};
return client.checkout.create(input).then((checkout) => {
assert.strictEqual(checkout.lineItems.length, 2);
});
});
test('it resolves with a checkout created with a shippingAddress', () => {
const input = {
shippingAddress: {
firstName: 'John',
lastName: 'Doe',
phone: '+16135551111',
address1: '123 Oak St',
address2: 'Unit 2',
city: 'Ottawa',
company: 'Shopify',
country: 'Canada',
province: 'ON',
zip: '123 ABC'
}
};
return client.checkout.create(input).then((checkout) => {
assert.strictEqual(checkout.shippingAddress.address1, input.shippingAddress.address1);
assert.strictEqual(checkout.shippingAddress.city, input.shippingAddress.city);
assert.strictEqual(checkout.shippingAddress.province, input.shippingAddress.province);
assert.strictEqual(checkout.shippingAddress.country, input.shippingAddress.country);
assert.strictEqual(checkout.shippingAddress.zip, input.shippingAddress.zip);
});
});
test('it resolves with a checkout created with a note', () => {
const input = {
note: 'This is a note!'
};
return client.checkout.create(input).then((checkout) => {
assert.strictEqual(checkout.note, input.note);
});
});
test('it rejects the promise if there is an error with the input', () => {
const input = {
lineItems: [
{
variantId: 'gid://shopify/ProductVariant/50850334310456',
quantity: 99999999
},
{
variantId: 'gid://shopify/ProductVariant/50850336211000',
quantity: 1
}
]
};
return client.checkout.create(input).catch((error) => {
assert.deepStrictEqual(error, [
{
code: 'INVALID',
field: [
'input',
'lines',
'0',
'quantity'
],
message: 'The quantity for merchandise with id gid://shopify/ProductVariant/50850334310456 must be greater than zero but less than 1000000.'
}
]);
});
});
test('it resolves a localized non-empty checkout created with buyerIdentity.countryCode', () => {
const input = {
buyerIdentity: {
countryCode: 'US'
// NOTE: if we don't pass an item, the cart won't localize setting currencyCode to XXX
},
lineItems: [
{
variantId: 'gid://shopify/ProductVariant/50850336211000',
quantity: 1
}
]
};
return client.checkout.create(input).then((checkout) => {
assert.strictEqual(checkout.lineItems.length, 1);
assert.strictEqual(checkout.buyerIdentity.countryCode, 'US');
assert.strictEqual(checkout.currencyCode, 'USD');
});
});
});
suite('create / not supported', () => {
test('it resolves with a checkout ignoring presentmentCurrencyCode', () => {
const input = {
presentmentCurrencyCode: 'EUR'
};
return client.checkout.create(input).then((checkout) => {
assert.ok(checkout.paymentDueV2.currencyCode !== input.presentmentCurrencyCode);
});
});
// NOTE: if we don't pass an item, the updatedCheckout won't respect the countryCode and set the currencyCode to XXX
// this is a known Cart limitation tracked as Sev3
test("a localized empty checkout created with buyerIdentity.countryCode will default to the shop's currency", () => {
const input = {
buyerIdentity: {
countryCode: 'ES'
}
};
return client.checkout.create(input).then((checkout) => {
assert.strictEqual(checkout.paymentDueV2.currencyCode, 'CAD');
});
});
});
suite('fetch', () => {
test('it returns a null checkout for an invalid checkoutId', () => {
return client.checkout.fetch('gid://shopify/Cart/invalid').then((checkout) => {
assert.strictEqual(checkout, null);
});
});
test('it fetches a checkout by id', () => {
return client.checkout.create({}).then((checkout) => {
return client.checkout.fetch(checkout.id).then((updatedCheckout) => {
assert.ok(typeof updatedCheckout.checkoutUrl === 'undefined');
assert.strictEqual(updatedCheckout.webUrl, checkout.webUrl);
});
});
});
});
suite('payload fields verification', () => {
test('it returns all expected fields including price fields in a checkout with items', () => {
const input = {
lineItems: [
{
variantId: 'gid://shopify/ProductVariant/50850334310456',
quantity: 1
}
]
};
return client.checkout.create(input).then((checkout) => {
assert.ok(checkout.lineItemsSubtotalPrice, 'lineItemsSubtotalPrice exists');
assert.ok(checkout.lineItemsSubtotalPrice.amount, 'lineItemsSubtotalPrice.amount exists');
assert.ok(checkout.lineItemsSubtotalPrice.currencyCode, 'lineItemsSubtotalPrice.currencyCode exists');
assert.ok(checkout.subtotalPrice, 'subtotalPrice exists');
assert.ok(checkout.subtotalPriceV2, 'subtotalPriceV2 exists');
assert.strictEqual(checkout.subtotalPrice.amount, checkout.subtotalPriceV2.amount, 'subtotalPrice amount matches V2');
assert.strictEqual(checkout.subtotalPrice.currencyCode, checkout.subtotalPriceV2.currencyCode, 'subtotalPrice currency matches V2');
assert.ok(checkout.totalPrice, 'totalPrice exists');
assert.ok(checkout.totalPriceV2, 'totalPriceV2 exists');
assert.strictEqual(checkout.totalPrice.amount, checkout.totalPriceV2.amount, 'totalPrice amount matches V2');
assert.strictEqual(checkout.totalPrice.currencyCode, checkout.totalPriceV2.currencyCode, 'totalPrice currency matches V2');
assert.ok(checkout.totalTax, 'totalTax exists');
assert.ok(checkout.totalTaxV2, 'totalTaxV2 exists');
assert.strictEqual(checkout.totalTax.amount, checkout.totalTaxV2.amount, 'totalTax amount matches V2');
assert.strictEqual(checkout.totalTax.currencyCode, checkout.totalTaxV2.currencyCode, 'totalTax currency matches V2');
// Verify the UNSUPPORTED_FIELDS have expected values
assert.strictEqual(checkout.completedAt, null, 'completedAt is null');
assert.strictEqual(checkout.order, null, 'order is null');
assert.strictEqual(checkout.orderStatusUrl, null, 'orderStatusUrl is null');
assert.strictEqual(checkout.ready, false, 'ready is false');
assert.strictEqual(checkout.requiresShipping, true, 'requiresShipping is true');
assert.strictEqual(checkout.shippingLine, null, 'shippingLine is null');
assert.strictEqual(checkout.taxExempt, false, 'taxExempt is false');
assert.strictEqual(checkout.taxesIncluded, false, 'taxesIncluded is false');
});
});
});
suite('updateAttributes', () => {
test('it updates a checkout note via updateAttributes', () => {
const input = {
note: 'This is a note!'
};
return client.checkout.create({}).then((checkout) => {
return client.checkout.updateAttributes(checkout.id, input).then((updatedCheckout) => {
assert.strictEqual(updatedCheckout.note, input.note);
});
});
});
test('it updates a checkout with a single custom attribute', () => {
const input = {
customAttributes: [
{
key: 'my-key',
value: 'my-value'
}
]
};
const output = {
customAttributes: [
{
key: 'my-key',
value: 'my-value'
}
]
};
return client.checkout.create({}).then((checkout) => {
return client.checkout.updateAttributes(checkout.id, input).then((updatedCheckout) => {
assert.strictEqual(updatedCheckout.customAttributes[0].key, output.customAttributes[0].key);
assert.strictEqual(updatedCheckout.customAttributes[0].value, output.customAttributes[0].value);
});
});
});
test('it updates a checkout with multiple custom attributes and note', () => {
const input = {
customAttributes: [
{
key: 'my-key',
value: 'my-value'
},
{
key: 'my-key2',
value: 'my-value2'
}
],
note: 'This is a note!'
};
const output = {
customAttributes: [
{
key: 'my-key',
value: 'my-value'
},
{
key: 'my-key2',
value: 'my-value2'
}
],
note: 'This is a note!'
};
return client.checkout.create({}).then((checkout) => {
return client.checkout.updateAttributes(checkout.id, input).then((updatedCheckout) => {
assert.strictEqual(updatedCheckout.customAttributes[0].value, output.customAttributes[0].value);
assert.strictEqual(updatedCheckout.customAttributes[1].value, output.customAttributes[1].value);
assert.strictEqual(updatedCheckout.note, input.note);
});
});
});
});
suite('updateAttributes / not supported', () => {
test('it resolves with a checkout ignoring allowPartialAddresses', () => {
const input = {
allowPartialAddresses: true
};
return client.checkout.create({}).then((checkout) => {
return client.checkout.updateAttributes(checkout.id, input).then((updatedCheckout) => {
assert.strictEqual(updatedCheckout.id, checkout.id);
});
});
});
});
suite('updateEmail', () => {
test('it updates a checkout email via updateEmail', () => {
const email = 'sdk@shopify.com';
return client.checkout.create({}).then((checkout) => {
return client.checkout.updateEmail(checkout.id, email).then((updatedCheckout) => {
assert.strictEqual(updatedCheckout.email, email);
});
});
});
});
suite('addLineItems', () => {
test('it adds a line item to a checkout via addLineItems', () => {
const input = {
lineItems:
{
variantId: 'gid://shopify/ProductVariant/50850334310456',
quantity: 1
}
};
return client.checkout.create({}).then((checkout) => {
return client.checkout.addLineItems(checkout.id, input.lineItems).then((updatedCheckout) => {
assert.strictEqual(updatedCheckout.lineItems.length, 1);
});
});
});
test('it adds multiple line items to a checkout via addLineItems', () => {
const input = {
lineItems: [
{
variantId: 'gid://shopify/ProductVariant/50850334310456',
quantity: 1
},
{
variantId: 'gid://shopify/ProductVariant/50850336211000',
quantity: 1
}
]
};
return client.checkout.create({}).then((checkout) => {
return client.checkout.addLineItems(checkout.id, input.lineItems).then((updatedCheckout) => {
assert.strictEqual(updatedCheckout.lineItems.length, 2);
});
});
});
});
suite('removeLineItems', () => {
test('it remotes a line item from a checkout via removeLineItems', () => {
const input = {
lineItems: [
{
variantId: 'gid://shopify/ProductVariant/50850334310456',
quantity: 1
}
]
};
return client.checkout.create(input).then((checkout) => {
return client.checkout.removeLineItems(checkout.id, checkout.lineItems[0].id).then((updatedCheckout) => {
assert.strictEqual(updatedCheckout.lineItems.length, 0);
});
});
});
test('it removes multiple line items from a checkout via removeLineItems', () => {
const input = {
lineItems: [
{
variantId: 'gid://shopify/ProductVariant/50850334310456',
quantity: 1
},
{
variantId: 'gid://shopify/ProductVariant/50850336211000',
quantity: 1
}
]
};
return client.checkout.create(input).then((checkout) => {
return client.checkout.removeLineItems(checkout.id, [checkout.lineItems[0].id, checkout.lineItems[1].id]).then((updatedCheckout) => {
assert.strictEqual(updatedCheckout.lineItems.length, 0);
});
});
});
});
suite('replaceLineItems', () => {
test('it replaces a line item in a checkout via replaceLineItems', () => {
const input = {
lineItems: [
{
variantId: 'gid://shopify/ProductVariant/50850334310456',
quantity: 1
}
]
};
const replacement = {
lineItems: [
{
variantId: 'gid://shopify/ProductVariant/50850336211000',
quantity: 1
}
]
};
return client.checkout.create(input).then((checkout) => {
return client.checkout.replaceLineItems(checkout.id, replacement.lineItems).then((updatedCheckout) => {
assert.strictEqual(updatedCheckout.lineItems[0].variant.id, replacement.lineItems[0].variantId);
});
});
}).timeout(3000);
});
suite('updateLineItems', () => {
test('it updates a line item quantity in a checkout via updateLineItems', () => {
const input = {
lineItems: [
{
variantId: 'gid://shopify/ProductVariant/50850334310456',
quantity: 1
}
]
};
const updatedQuantity = 2;
return client.checkout.create(input).then((checkout) => {
return client.checkout.updateLineItems(checkout.id, [{id: checkout.lineItems[0].id, quantity: updatedQuantity}]).then((updatedCheckout) => {
assert.strictEqual(updatedCheckout.lineItems[0].quantity, updatedQuantity);
});
});
});
test('it updates multiple line items attributes in a checkout via updateLineItems', () => {
const input = {
lineItems: [
{
variantId: 'gid://shopify/ProductVariant/50850334310456'
},
{
variantId: 'gid://shopify/ProductVariant/50850336211000'
}
]
};
return client.checkout.create(input).then((checkout) => {
const customAttributes = [{key: 'my-key', value: 'my-value'}];
const updateLines = checkout.lineItems.map((lineItem) => {
return {id: lineItem.id, customAttributes};
});
return client.checkout.updateLineItems(checkout.id, updateLines).then((updatedCheckout) => {
assert.strictEqual(updatedCheckout.lineItems[0].quantity, 1);
assert.strictEqual(updatedCheckout.lineItems[1].quantity, 1);
assert.strictEqual(updatedCheckout.lineItems[0].customAttributes[0].key, customAttributes[0].key);
assert.strictEqual(updatedCheckout.lineItems[1].customAttributes[0].value, customAttributes[0].value);
});
});
});
});
suite('updateShippingAddress', () => {
test('it updates the shipping address of a checkout via updateShippingAddress', () => {
const input = {
shippingAddress: {
address1: '123 Oak St',
address2: 'Unit 2',
city: 'Ottawa',
company: 'Shopify',
country: 'Canada',
firstName: 'John',
lastName: 'Doe',
phone: '+16135551111',
province: 'ON',
zip: '123 ABC'
}
};
return client.checkout.create({}).then((checkout) => {
return client.checkout.updateShippingAddress(checkout.id, input.shippingAddress).then((updatedCheckout) => {
assert.strictEqual(updatedCheckout.shippingAddress.address1, input.shippingAddress.address1);
assert.strictEqual(updatedCheckout.shippingAddress.address2, input.shippingAddress.address2);
assert.strictEqual(updatedCheckout.shippingAddress.city, input.shippingAddress.city);
assert.strictEqual(updatedCheckout.shippingAddress.company, input.shippingAddress.company);
assert.strictEqual(updatedCheckout.shippingAddress.country, input.shippingAddress.country);
assert.strictEqual(updatedCheckout.shippingAddress.firstName, input.shippingAddress.firstName);
assert.strictEqual(updatedCheckout.shippingAddress.lastName, input.shippingAddress.lastName);
assert.strictEqual(updatedCheckout.shippingAddress.phone, input.shippingAddress.phone);
assert.strictEqual(updatedCheckout.shippingAddress.province, input.shippingAddress.province);
assert.strictEqual(updatedCheckout.shippingAddress.zip, input.shippingAddress.zip);
});
});
});
test('it returns any user errors', () => {
const inputWithHtmlTags = {
shippingAddress: {
address1: '<html>123 Oak St</html>',
address2: '<script>Unit 2</script>',
city: '<script>Ottawa</script>',
company: '<script>Shopify</script>',
country: '<script>Canada</script>',
firstName: 'John',
lastName: 'Doe',
phone: '+16135551111',
province: 'ON',
zip: '123 ABC'
}
};
return client.checkout.create({}).then((checkout) => {
return client.checkout.updateShippingAddress(checkout.id, inputWithHtmlTags.shippingAddress).then((updatedCheckout) => {
assert.deepStrictEqual(updatedCheckout.userErrors, [
{
code: 'INVALID',
field: [
'buyerIdentity',
'deliveryAddressPreferences',
'0',
'deliveryAddress',
'country'
],
message: 'invalid value'
}
]);
});
});
});
});
});