forked from vendure-ecommerce/vendure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shop-order.e2e-spec.ts
918 lines (820 loc) · 35.9 KB
/
shop-order.e2e-spec.ts
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
/* tslint:disable:no-non-null-assertion */
import path from 'path';
import { PaymentMethodHandler } from '../src/config/payment-method/payment-method-handler';
import { TEST_SETUP_TIMEOUT_MS } from './config/test-config';
import {
CreateAddressInput,
GetCountryList,
GetCustomer,
GetCustomerList,
LanguageCode,
UpdateCountry,
} from './graphql/generated-e2e-admin-types';
import {
AddItemToOrder,
AddPaymentToOrder,
AdjustItemQuantity,
GetActiveOrder,
GetActiveOrderPayments,
GetAvailableCountries,
GetCustomerAddresses,
GetNextOrderStates,
GetOrderByCode,
GetShippingMethods,
RemoveItemFromOrder,
SetCustomerForOrder,
SetShippingAddress,
SetShippingMethod,
TransitionToState,
} from './graphql/generated-e2e-shop-types';
import {
GET_COUNTRY_LIST,
GET_CUSTOMER,
GET_CUSTOMER_LIST,
UPDATE_COUNTRY,
} from './graphql/shared-definitions';
import {
ADD_ITEM_TO_ORDER,
ADD_PAYMENT,
ADJUST_ITEM_QUANTITY,
GET_ACTIVE_ORDER,
GET_ACTIVE_ORDER_ADDRESSES,
GET_ACTIVE_ORDER_PAYMENTS,
GET_AVAILABLE_COUNTRIES,
GET_ELIGIBLE_SHIPPING_METHODS,
GET_NEXT_STATES,
GET_ORDER_BY_CODE,
REMOVE_ITEM_FROM_ORDER,
SET_CUSTOMER,
SET_SHIPPING_ADDRESS,
SET_SHIPPING_METHOD,
TRANSITION_TO_STATE,
} from './graphql/shop-definitions';
import { TestAdminClient, TestShopClient } from './test-client';
import { TestServer } from './test-server';
import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
describe('Shop orders', () => {
const adminClient = new TestAdminClient();
const shopClient = new TestShopClient();
const server = new TestServer();
beforeAll(async () => {
const token = await server.init(
{
productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
customerCount: 2,
},
{
paymentOptions: {
paymentMethodHandlers: [
testPaymentMethod,
testFailingPaymentMethod,
testErrorPaymentMethod,
],
},
orderOptions: {
orderItemsLimit: 99,
},
},
);
await shopClient.init();
await adminClient.init();
}, TEST_SETUP_TIMEOUT_MS);
afterAll(async () => {
await server.destroy();
});
it('availableCountries returns enabled countries', async () => {
// disable Austria
const { countries } = await adminClient.query<GetCountryList.Query>(GET_COUNTRY_LIST, {});
const AT = countries.items.find(c => c.code === 'AT')!;
await adminClient.query<UpdateCountry.Mutation, UpdateCountry.Variables>(UPDATE_COUNTRY, {
input: {
id: AT.id,
enabled: false,
},
});
const result = await shopClient.query<GetAvailableCountries.Query>(GET_AVAILABLE_COUNTRIES);
expect(result.availableCountries.length).toBe(countries.items.length - 1);
expect(result.availableCountries.find(c => c.id === AT.id)).toBeUndefined();
});
describe('ordering as anonymous user', () => {
let firstOrderLineId: string;
let createdCustomerId: string;
let orderCode: string;
it('addItemToOrder starts with no session token', () => {
expect(shopClient.getAuthToken()).toBeFalsy();
});
it('activeOrder returns null before any items have been added', async () => {
const result = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
expect(result.activeOrder).toBeNull();
});
it('activeOrder creates an anonymous session', () => {
expect(shopClient.getAuthToken()).not.toBe('');
});
it('addItemToOrder creates a new Order with an item', async () => {
const { addItemToOrder } = await shopClient.query<
AddItemToOrder.Mutation,
AddItemToOrder.Variables
>(ADD_ITEM_TO_ORDER, {
productVariantId: 'T_1',
quantity: 1,
});
expect(addItemToOrder!.lines.length).toBe(1);
expect(addItemToOrder!.lines[0].quantity).toBe(1);
expect(addItemToOrder!.lines[0].productVariant.id).toBe('T_1');
expect(addItemToOrder!.lines[0].id).toBe('T_1');
firstOrderLineId = addItemToOrder!.lines[0].id;
orderCode = addItemToOrder!.code;
});
it(
'addItemToOrder errors with an invalid productVariantId',
assertThrowsWithMessage(
() =>
shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(ADD_ITEM_TO_ORDER, {
productVariantId: 'T_999',
quantity: 1,
}),
`No ProductVariant with the id '999' could be found`,
),
);
it(
'addItemToOrder errors with a negative quantity',
assertThrowsWithMessage(
() =>
shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(ADD_ITEM_TO_ORDER, {
productVariantId: 'T_999',
quantity: -3,
}),
`-3 is not a valid quantity for an OrderItem`,
),
);
it('addItemToOrder with an existing productVariantId adds quantity to the existing OrderLine', async () => {
const { addItemToOrder } = await shopClient.query<
AddItemToOrder.Mutation,
AddItemToOrder.Variables
>(ADD_ITEM_TO_ORDER, {
productVariantId: 'T_1',
quantity: 2,
});
expect(addItemToOrder!.lines.length).toBe(1);
expect(addItemToOrder!.lines[0].quantity).toBe(3);
});
it(
'addItemToOrder errors when going beyond orderItemsLimit',
assertThrowsWithMessage(async () => {
await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(ADD_ITEM_TO_ORDER, {
productVariantId: 'T_1',
quantity: 100,
});
}, 'Cannot add items. An order may consist of a maximum of 99 items'),
);
it('adjustOrderLine adjusts the quantity', async () => {
const { adjustOrderLine } = await shopClient.query<
AdjustItemQuantity.Mutation,
AdjustItemQuantity.Variables
>(ADJUST_ITEM_QUANTITY, {
orderLineId: firstOrderLineId,
quantity: 50,
});
expect(adjustOrderLine!.lines.length).toBe(1);
expect(adjustOrderLine!.lines[0].quantity).toBe(50);
});
it(
'adjustOrderLine errors when going beyond orderItemsLimit',
assertThrowsWithMessage(async () => {
await shopClient.query<AdjustItemQuantity.Mutation, AdjustItemQuantity.Variables>(
ADJUST_ITEM_QUANTITY,
{
orderLineId: firstOrderLineId,
quantity: 100,
},
);
}, 'Cannot add items. An order may consist of a maximum of 99 items'),
);
it(
'adjustOrderLine errors with a negative quantity',
assertThrowsWithMessage(
() =>
shopClient.query<AdjustItemQuantity.Mutation, AdjustItemQuantity.Variables>(
ADJUST_ITEM_QUANTITY,
{
orderLineId: firstOrderLineId,
quantity: -3,
},
),
`-3 is not a valid quantity for an OrderItem`,
),
);
it(
'adjustOrderLine errors with an invalid orderLineId',
assertThrowsWithMessage(
() =>
shopClient.query<AdjustItemQuantity.Mutation, AdjustItemQuantity.Variables>(
ADJUST_ITEM_QUANTITY,
{
orderLineId: 'T_999',
quantity: 5,
},
),
`This order does not contain an OrderLine with the id 999`,
),
);
it('removeItemFromOrder removes the correct item', async () => {
const { addItemToOrder } = await shopClient.query<
AddItemToOrder.Mutation,
AddItemToOrder.Variables
>(ADD_ITEM_TO_ORDER, {
productVariantId: 'T_3',
quantity: 3,
});
expect(addItemToOrder!.lines.length).toBe(2);
expect(addItemToOrder!.lines.map(i => i.productVariant.id)).toEqual(['T_1', 'T_3']);
const { removeOrderLine } = await shopClient.query<
RemoveItemFromOrder.Mutation,
RemoveItemFromOrder.Variables
>(REMOVE_ITEM_FROM_ORDER, {
orderLineId: firstOrderLineId,
});
expect(removeOrderLine!.lines.length).toBe(1);
expect(removeOrderLine!.lines.map(i => i.productVariant.id)).toEqual(['T_3']);
});
it(
'removeItemFromOrder errors with an invalid orderItemId',
assertThrowsWithMessage(
() =>
shopClient.query<RemoveItemFromOrder.Mutation, RemoveItemFromOrder.Variables>(
REMOVE_ITEM_FROM_ORDER,
{
orderLineId: 'T_999',
},
),
`This order does not contain an OrderLine with the id 999`,
),
);
it('nextOrderStates returns next valid states', async () => {
const result = await shopClient.query<GetNextOrderStates.Query>(GET_NEXT_STATES);
expect(result.nextOrderStates).toEqual(['ArrangingPayment', 'Cancelled']);
});
it(
'transitionOrderToState throws for an invalid state',
assertThrowsWithMessage(
() =>
shopClient.query<TransitionToState.Mutation, TransitionToState.Variables>(
TRANSITION_TO_STATE,
{ state: 'Completed' },
),
`Cannot transition Order from "AddingItems" to "Completed"`,
),
);
it(
'attempting to transition to ArrangingPayment throws when Order has no Customer',
assertThrowsWithMessage(
() =>
shopClient.query<TransitionToState.Mutation, TransitionToState.Variables>(
TRANSITION_TO_STATE,
{ state: 'ArrangingPayment' },
),
`Cannot transition Order to the "ArrangingPayment" state without Customer details`,
),
);
it('setCustomerForOrder creates a new Customer and associates it with the Order', async () => {
const { setCustomerForOrder } = await shopClient.query<
SetCustomerForOrder.Mutation,
SetCustomerForOrder.Variables
>(SET_CUSTOMER, {
input: {
emailAddress: '[email protected]',
firstName: 'Test',
lastName: 'Person',
},
});
const customer = setCustomerForOrder!.customer!;
expect(customer.firstName).toBe('Test');
expect(customer.lastName).toBe('Person');
expect(customer.emailAddress).toBe('[email protected]');
createdCustomerId = customer.id;
});
it('setCustomerForOrder updates the existing customer if Customer already set', async () => {
const { setCustomerForOrder } = await shopClient.query<
SetCustomerForOrder.Mutation,
SetCustomerForOrder.Variables
>(SET_CUSTOMER, {
input: {
emailAddress: '[email protected]',
firstName: 'Changed',
lastName: 'Person',
},
});
const customer = setCustomerForOrder!.customer!;
expect(customer.firstName).toBe('Changed');
expect(customer.lastName).toBe('Person');
expect(customer.emailAddress).toBe('[email protected]');
expect(customer.id).toBe(createdCustomerId);
});
it('setOrderShippingAddress sets shipping address', async () => {
const address: CreateAddressInput = {
fullName: 'name',
company: 'company',
streetLine1: '12 the street',
streetLine2: null,
city: 'foo',
province: 'bar',
postalCode: '123456',
countryCode: 'US',
phoneNumber: '4444444',
};
const { setOrderShippingAddress } = await shopClient.query<
SetShippingAddress.Mutation,
SetShippingAddress.Variables
>(SET_SHIPPING_ADDRESS, {
input: address,
});
expect(setOrderShippingAddress!.shippingAddress).toEqual({
fullName: 'name',
company: 'company',
streetLine1: '12 the street',
streetLine2: null,
city: 'foo',
province: 'bar',
postalCode: '123456',
country: 'United States of America',
phoneNumber: '4444444',
});
});
it('customer default Addresses are not updated before payment', async () => {
// TODO: will need to be reworked for https://github.com/vendure-ecommerce/vendure/issues/98
const result = await shopClient.query<GetCustomerAddresses.Query>(GET_ACTIVE_ORDER_ADDRESSES);
expect(result.activeOrder!.customer!.addresses).toEqual([]);
});
it('can transition to ArrangingPayment once Customer has been set', async () => {
const result = await shopClient.query<TransitionToState.Mutation, TransitionToState.Variables>(
TRANSITION_TO_STATE,
{ state: 'ArrangingPayment' },
);
expect(result.transitionOrderToState).toEqual({ id: 'T_1', state: 'ArrangingPayment' });
});
it('adds a successful payment and transitions Order state', async () => {
const { addPaymentToOrder } = await shopClient.query<
AddPaymentToOrder.Mutation,
AddPaymentToOrder.Variables
>(ADD_PAYMENT, {
input: {
method: testPaymentMethod.code,
metadata: {},
},
});
const payment = addPaymentToOrder!.payments![0];
expect(addPaymentToOrder!.state).toBe('PaymentSettled');
expect(addPaymentToOrder!.active).toBe(false);
expect(addPaymentToOrder!.payments!.length).toBe(1);
expect(payment.method).toBe(testPaymentMethod.code);
expect(payment.state).toBe('Settled');
});
it('activeOrder is null after payment', async () => {
const result = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
expect(result.activeOrder).toBeNull();
});
it('customer default Addresses are updated after payment', async () => {
// TODO: will need to be reworked for https://github.com/vendure-ecommerce/vendure/issues/98
const result = await adminClient.query<GetCustomer.Query, GetCustomer.Variables>(GET_CUSTOMER, {
id: createdCustomerId,
});
// tslint:disable-next-line:no-non-null-assertion
const address = result.customer!.addresses![0];
expect(address.streetLine1).toBe('12 the street');
expect(address.postalCode).toBe('123456');
expect(address.defaultBillingAddress).toBe(true);
expect(address.defaultShippingAddress).toBe(true);
});
});
describe('ordering as authenticated user', () => {
let firstOrderLineId: string;
let activeOrder: AddItemToOrder.AddItemToOrder;
let authenticatedUserEmailAddress: string;
let customers: GetCustomerList.Items[];
const password = 'test';
beforeAll(async () => {
await adminClient.asSuperAdmin();
const result = await adminClient.query<GetCustomerList.Query, GetCustomerList.Variables>(
GET_CUSTOMER_LIST,
{
options: {
take: 2,
},
},
);
customers = result.customers.items;
authenticatedUserEmailAddress = customers[0].emailAddress;
await shopClient.asUserWithCredentials(authenticatedUserEmailAddress, password);
});
it('activeOrder returns null before any items have been added', async () => {
const result = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
expect(result.activeOrder).toBeNull();
});
it('addItemToOrder creates a new Order with an item', async () => {
const { addItemToOrder } = await shopClient.query<
AddItemToOrder.Mutation,
AddItemToOrder.Variables
>(ADD_ITEM_TO_ORDER, {
productVariantId: 'T_1',
quantity: 1,
});
expect(addItemToOrder!.lines.length).toBe(1);
expect(addItemToOrder!.lines[0].quantity).toBe(1);
expect(addItemToOrder!.lines[0].productVariant.id).toBe('T_1');
activeOrder = addItemToOrder!;
firstOrderLineId = addItemToOrder!.lines[0].id;
});
it('activeOrder returns order after item has been added', async () => {
const result = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
expect(result.activeOrder!.id).toBe(activeOrder.id);
expect(result.activeOrder!.state).toBe('AddingItems');
});
it('addItemToOrder with an existing productVariantId adds quantity to the existing OrderLine', async () => {
const { addItemToOrder } = await shopClient.query<
AddItemToOrder.Mutation,
AddItemToOrder.Variables
>(ADD_ITEM_TO_ORDER, {
productVariantId: 'T_1',
quantity: 2,
});
expect(addItemToOrder!.lines.length).toBe(1);
expect(addItemToOrder!.lines[0].quantity).toBe(3);
});
it('adjustOrderLine adjusts the quantity', async () => {
const { adjustOrderLine } = await shopClient.query<
AdjustItemQuantity.Mutation,
AdjustItemQuantity.Variables
>(ADJUST_ITEM_QUANTITY, {
orderLineId: firstOrderLineId,
quantity: 50,
});
expect(adjustOrderLine!.lines.length).toBe(1);
expect(adjustOrderLine!.lines[0].quantity).toBe(50);
});
it('removeItemFromOrder removes the correct item', async () => {
const { addItemToOrder } = await shopClient.query<
AddItemToOrder.Mutation,
AddItemToOrder.Variables
>(ADD_ITEM_TO_ORDER, {
productVariantId: 'T_3',
quantity: 3,
});
expect(addItemToOrder!.lines.length).toBe(2);
expect(addItemToOrder!.lines.map(i => i.productVariant.id)).toEqual(['T_1', 'T_3']);
const { removeOrderLine } = await shopClient.query<
RemoveItemFromOrder.Mutation,
RemoveItemFromOrder.Variables
>(REMOVE_ITEM_FROM_ORDER, {
orderLineId: firstOrderLineId,
});
expect(removeOrderLine!.lines.length).toBe(1);
expect(removeOrderLine!.lines.map(i => i.productVariant.id)).toEqual(['T_3']);
});
it('nextOrderStates returns next valid states', async () => {
const result = await shopClient.query<GetNextOrderStates.Query>(GET_NEXT_STATES);
expect(result.nextOrderStates).toEqual(['ArrangingPayment', 'Cancelled']);
});
it('logging out and back in again resumes the last active order', async () => {
await shopClient.asAnonymousUser();
const result1 = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
expect(result1.activeOrder).toBeNull();
await shopClient.asUserWithCredentials(authenticatedUserEmailAddress, password);
const result2 = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
expect(result2.activeOrder!.id).toBe(activeOrder.id);
});
describe('shipping', () => {
let shippingMethods: GetShippingMethods.EligibleShippingMethods[];
it(
'setOrderShippingAddress throws with invalid countryCode',
assertThrowsWithMessage(() => {
const address: CreateAddressInput = {
streetLine1: '12 the street',
countryCode: 'INVALID',
};
return shopClient.query<SetShippingAddress.Mutation, SetShippingAddress.Variables>(
SET_SHIPPING_ADDRESS,
{
input: address,
},
);
}, `The countryCode "INVALID" was not recognized`),
);
it('setOrderShippingAddress sets shipping address', async () => {
const address: CreateAddressInput = {
fullName: 'name',
company: 'company',
streetLine1: '12 the street',
streetLine2: null,
city: 'foo',
province: 'bar',
postalCode: '123456',
countryCode: 'US',
phoneNumber: '4444444',
};
const { setOrderShippingAddress } = await shopClient.query<
SetShippingAddress.Mutation,
SetShippingAddress.Variables
>(SET_SHIPPING_ADDRESS, {
input: address,
});
expect(setOrderShippingAddress!.shippingAddress).toEqual({
fullName: 'name',
company: 'company',
streetLine1: '12 the street',
streetLine2: null,
city: 'foo',
province: 'bar',
postalCode: '123456',
country: 'United States of America',
phoneNumber: '4444444',
});
});
it('eligibleShippingMethods lists shipping methods', async () => {
const result = await shopClient.query<GetShippingMethods.Query>(
GET_ELIGIBLE_SHIPPING_METHODS,
);
shippingMethods = result.eligibleShippingMethods;
expect(shippingMethods).toEqual([
{ id: 'T_1', price: 500, description: 'Standard Shipping' },
{ id: 'T_2', price: 1000, description: 'Express Shipping' },
]);
});
it('shipping is initially unset', async () => {
const result = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
expect(result.activeOrder!.shipping).toEqual(0);
expect(result.activeOrder!.shippingMethod).toEqual(null);
});
it('setOrderShippingMethod sets the shipping method', async () => {
const result = await shopClient.query<
SetShippingMethod.Mutation,
SetShippingMethod.Variables
>(SET_SHIPPING_METHOD, {
id: shippingMethods[1].id,
});
const activeOrderResult = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
const order = activeOrderResult.activeOrder!;
expect(order.shipping).toBe(shippingMethods[1].price);
expect(order.shippingMethod!.id).toBe(shippingMethods[1].id);
expect(order.shippingMethod!.description).toBe(shippingMethods[1].description);
});
it('shipping method is preserved after adjustOrderLine', async () => {
const activeOrderResult = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
activeOrder = activeOrderResult.activeOrder!;
const { adjustOrderLine } = await shopClient.query<
AdjustItemQuantity.Mutation,
AdjustItemQuantity.Variables
>(ADJUST_ITEM_QUANTITY, {
orderLineId: activeOrder.lines[0].id,
quantity: 10,
});
expect(adjustOrderLine!.shipping).toBe(shippingMethods[1].price);
expect(adjustOrderLine!.shippingMethod!.id).toBe(shippingMethods[1].id);
expect(adjustOrderLine!.shippingMethod!.description).toBe(shippingMethods[1].description);
});
});
describe('payment', () => {
it(
'attempting add a Payment throws error when in AddingItems state',
assertThrowsWithMessage(
() =>
shopClient.query<AddPaymentToOrder.Mutation, AddPaymentToOrder.Variables>(
ADD_PAYMENT,
{
input: {
method: testPaymentMethod.code,
metadata: {},
},
},
),
`A Payment may only be added when Order is in "ArrangingPayment" state`,
),
);
it('transitions to the ArrangingPayment state', async () => {
const result = await shopClient.query<
TransitionToState.Mutation,
TransitionToState.Variables
>(TRANSITION_TO_STATE, { state: 'ArrangingPayment' });
expect(result.transitionOrderToState).toEqual({
id: activeOrder.id,
state: 'ArrangingPayment',
});
});
it(
'attempting to add an item throws error when in ArrangingPayment state',
assertThrowsWithMessage(
() =>
shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(
ADD_ITEM_TO_ORDER,
{
productVariantId: 'T_4',
quantity: 1,
},
),
`Order contents may only be modified when in the "AddingItems" state`,
),
);
it(
'attempting to modify item quantity throws error when in ArrangingPayment state',
assertThrowsWithMessage(
() =>
shopClient.query<AdjustItemQuantity.Mutation, AdjustItemQuantity.Variables>(
ADJUST_ITEM_QUANTITY,
{
orderLineId: activeOrder.lines[0].id,
quantity: 12,
},
),
`Order contents may only be modified when in the "AddingItems" state`,
),
);
it(
'attempting to remove an item throws error when in ArrangingPayment state',
assertThrowsWithMessage(
() =>
shopClient.query<RemoveItemFromOrder.Mutation, RemoveItemFromOrder.Variables>(
REMOVE_ITEM_FROM_ORDER,
{
orderLineId: activeOrder.lines[0].id,
},
),
`Order contents may only be modified when in the "AddingItems" state`,
),
);
it(
'attempting to setOrderShippingMethod throws error when in ArrangingPayment state',
assertThrowsWithMessage(async () => {
const shippingMethodsResult = await shopClient.query<GetShippingMethods.Query>(
GET_ELIGIBLE_SHIPPING_METHODS,
);
const shippingMethods = shippingMethodsResult.eligibleShippingMethods;
return shopClient.query<SetShippingMethod.Mutation, SetShippingMethod.Variables>(
SET_SHIPPING_METHOD,
{
id: shippingMethods[0].id,
},
);
}, `Order contents may only be modified when in the "AddingItems" state`),
);
it('adds a declined payment', async () => {
const { addPaymentToOrder } = await shopClient.query<
AddPaymentToOrder.Mutation,
AddPaymentToOrder.Variables
>(ADD_PAYMENT, {
input: {
method: testFailingPaymentMethod.code,
metadata: {
foo: 'bar',
},
},
});
const payment = addPaymentToOrder!.payments![0];
expect(addPaymentToOrder!.payments!.length).toBe(1);
expect(payment.method).toBe(testFailingPaymentMethod.code);
expect(payment.state).toBe('Declined');
expect(payment.transactionId).toBe(null);
expect(payment.metadata).toEqual({
foo: 'bar',
});
});
it('adds an error payment and returns error response', async () => {
try {
await shopClient.query<AddPaymentToOrder.Mutation, AddPaymentToOrder.Variables>(
ADD_PAYMENT,
{
input: {
method: testErrorPaymentMethod.code,
metadata: {
foo: 'bar',
},
},
},
);
fail('should have thrown');
} catch (err) {
expect(err.message).toEqual('Something went horribly wrong');
}
const result = await shopClient.query<GetActiveOrderPayments.Query>(
GET_ACTIVE_ORDER_PAYMENTS,
);
const payment = result.activeOrder!.payments![1];
expect(result.activeOrder!.payments!.length).toBe(2);
expect(payment.method).toBe(testErrorPaymentMethod.code);
expect(payment.state).toBe('Error');
expect(payment.errorMessage).toBe('Something went horribly wrong');
});
it('adds a successful payment and transitions Order state', async () => {
const { addPaymentToOrder } = await shopClient.query<
AddPaymentToOrder.Mutation,
AddPaymentToOrder.Variables
>(ADD_PAYMENT, {
input: {
method: testPaymentMethod.code,
metadata: {
baz: 'quux',
},
},
});
const payment = addPaymentToOrder!.payments![2];
expect(addPaymentToOrder!.state).toBe('PaymentSettled');
expect(addPaymentToOrder!.active).toBe(false);
expect(addPaymentToOrder!.payments!.length).toBe(3);
expect(payment.method).toBe(testPaymentMethod.code);
expect(payment.state).toBe('Settled');
expect(payment.transactionId).toBe('12345');
expect(payment.metadata).toEqual({
baz: 'quux',
});
});
it('does not create new address when Customer already has address', async () => {
const { customer } = await adminClient.query<GetCustomer.Query, GetCustomer.Variables>(
GET_CUSTOMER,
{ id: customers[0].id },
);
expect(customer!.addresses!.length).toBe(1);
});
});
describe('orderByCode', () => {
describe('immediately after Order is placed', () => {
it('works when authenticated', async () => {
const result = await shopClient.query<GetOrderByCode.Query, GetOrderByCode.Variables>(
GET_ORDER_BY_CODE,
{
code: activeOrder.code,
},
);
expect(result.orderByCode!.id).toBe(activeOrder.id);
});
it('works when anonymous', async () => {
await shopClient.asAnonymousUser();
const result = await shopClient.query<GetOrderByCode.Query, GetOrderByCode.Variables>(
GET_ORDER_BY_CODE,
{
code: activeOrder.code,
},
);
expect(result.orderByCode!.id).toBe(activeOrder.id);
});
it(
`throws error for another user's Order`,
assertThrowsWithMessage(async () => {
authenticatedUserEmailAddress = customers[1].emailAddress;
await shopClient.asUserWithCredentials(authenticatedUserEmailAddress, password);
return shopClient.query<GetOrderByCode.Query, GetOrderByCode.Variables>(
GET_ORDER_BY_CODE,
{
code: activeOrder.code,
},
);
}, `You are not currently authorized to perform this action`),
);
});
});
});
});
const testPaymentMethod = new PaymentMethodHandler({
code: 'test-payment-method',
description: [{ languageCode: LanguageCode.en, value: 'Test Payment Method' }],
args: {},
createPayment: (order, args, metadata) => {
return {
amount: order.total,
state: 'Settled',
transactionId: '12345',
metadata,
};
},
settlePayment: order => ({
success: true,
}),
});
const testFailingPaymentMethod = new PaymentMethodHandler({
code: 'test-failing-payment-method',
description: [{ languageCode: LanguageCode.en, value: 'Test Failing Payment Method' }],
args: {},
createPayment: (order, args, metadata) => {
return {
amount: order.total,
state: 'Declined',
metadata,
};
},
settlePayment: order => ({
success: true,
}),
});
const testErrorPaymentMethod = new PaymentMethodHandler({
code: 'test-error-payment-method',
description: [{ languageCode: LanguageCode.en, value: 'Test Error Payment Method' }],
args: {},
createPayment: (order, args, metadata) => {
return {
amount: order.total,
state: 'Error',
errorMessage: 'Something went horribly wrong',
metadata,
};
},
settlePayment: order => ({
success: true,
}),
});