@ordergroove/offers 2.47.2-alpha-PR-1352-2.29 → 2.47.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,821 @@
1
+ import * as constants from '../../../core/constants';
2
+ import { optedin } from '../../shopifyReducer';
3
+ import { DEFAULT_PAY_AS_YOU_GO_GROUP_NAME } from '../../utils';
4
+
5
+ describe('optedin', () => {
6
+ it('should return optins given action SETUP_CART', () => {
7
+ const actual = optedin([], {
8
+ type: constants.SETUP_CART,
9
+ payload: {
10
+ items: [
11
+ {
12
+ key: 'yum item key 1',
13
+ selling_plan_allocation: {
14
+ selling_plan: {
15
+ id: 'yum selling plan id 1',
16
+ options: [
17
+ {
18
+ name: 'Deliver every',
19
+ value: '2 months'
20
+ }
21
+ ]
22
+ }
23
+ }
24
+ },
25
+ {
26
+ key: 'yum item key 2',
27
+ selling_plan_allocation: {
28
+ selling_plan: {
29
+ id: 'yum selling plan id 2',
30
+ options: [
31
+ {
32
+ name: 'Deliver every',
33
+ value: '2 months'
34
+ }
35
+ ]
36
+ }
37
+ }
38
+ },
39
+ {
40
+ key: 'prepaid yum item',
41
+ selling_plan_allocation: {
42
+ selling_plan: {
43
+ id: 'prepaid yum selling plan id',
44
+ options: [
45
+ {
46
+ name: 'Deliver every',
47
+ value: '1 month'
48
+ },
49
+ {
50
+ name: 'Shipment amount',
51
+ value: '3 shipments'
52
+ }
53
+ ]
54
+ }
55
+ }
56
+ }
57
+ ]
58
+ }
59
+ });
60
+
61
+ expect(actual).toEqual([
62
+ {
63
+ id: 'yum item key 1',
64
+ frequency: 'yum selling plan id 1'
65
+ },
66
+ {
67
+ id: 'yum item key 2',
68
+ frequency: 'yum selling plan id 2'
69
+ },
70
+ {
71
+ id: 'prepaid yum item',
72
+ frequency: 'prepaid yum selling plan id',
73
+ prepaidShipments: 3
74
+ }
75
+ ]);
76
+ });
77
+
78
+ describe('given action RECEIVE_OFFER', () => {
79
+ describe('given optin already in state', () => {
80
+ describe('given optin does not have selling plan', () => {
81
+ it('should set frequency to the selling plan given there is a matching selling plan', () => {
82
+ const actual = optedin(
83
+ [
84
+ {
85
+ id: 'yum product id 1',
86
+ frequency: '1_2'
87
+ }
88
+ ],
89
+ {
90
+ type: constants.RECEIVE_OFFER,
91
+ payload: {
92
+ offer: {},
93
+ frequencyConfig: {
94
+ frequencies: ['yum selling plan id 1', 'yum selling plan id 2'],
95
+ frequenciesEveryPeriod: ['1_1', '1_2']
96
+ }
97
+ }
98
+ }
99
+ );
100
+
101
+ expect(actual).toEqual([
102
+ {
103
+ id: 'yum product id 1',
104
+ frequency: 'yum selling plan id 2'
105
+ }
106
+ ]);
107
+ });
108
+
109
+ it('should set frequency to default selling plan given there is not a matching selling plan but there is a matching default sellin gplan', () => {
110
+ const actual = optedin(
111
+ [
112
+ {
113
+ id: 'yum product id 1',
114
+ frequency: '1_2'
115
+ }
116
+ ],
117
+ {
118
+ type: constants.RECEIVE_OFFER,
119
+ payload: {
120
+ offer: {
121
+ defaultFrequency: '3_2'
122
+ },
123
+ frequencyConfig: {
124
+ frequencies: ['yum selling plan id 1', 'yum selling plan id 2'],
125
+ frequenciesEveryPeriod: ['3_1', '3_2']
126
+ }
127
+ }
128
+ }
129
+ );
130
+
131
+ expect(actual).toEqual([
132
+ {
133
+ id: 'yum product id 1',
134
+ frequency: 'yum selling plan id 2'
135
+ }
136
+ ]);
137
+ });
138
+
139
+ it('should set frequency to first selling plan given there is not a matching selling plan or default selling plan', () => {
140
+ const actual = optedin(
141
+ [
142
+ {
143
+ id: 'yum product id 1',
144
+ frequency: '1_2'
145
+ }
146
+ ],
147
+ {
148
+ type: constants.RECEIVE_OFFER,
149
+ payload: {
150
+ offer: {},
151
+ frequencyConfig: {
152
+ frequencies: ['yum selling plan id 1', 'yum selling plan id 2'],
153
+ frequenciesEveryPeriod: ['3_1', '3_2']
154
+ }
155
+ }
156
+ }
157
+ );
158
+
159
+ expect(actual).toEqual([
160
+ {
161
+ id: 'yum product id 1',
162
+ frequency: 'yum selling plan id 1'
163
+ }
164
+ ]);
165
+ });
166
+ });
167
+
168
+ it('should not change frequency given optin already has a selling plan', () => {
169
+ const actual = optedin(
170
+ [
171
+ {
172
+ id: 'yum product id 1',
173
+ frequency: 'yum selling plan id 1'
174
+ }
175
+ ],
176
+ {
177
+ type: constants.RECEIVE_OFFER,
178
+ payload: {
179
+ offer: {},
180
+ frequencyConfig: {
181
+ frequencies: ['yum selling plan id 1', 'yum selling plan id 2'],
182
+ frequenciesEveryPeriod: ['1_1', '1_2']
183
+ }
184
+ }
185
+ }
186
+ );
187
+
188
+ expect(actual).toEqual([
189
+ {
190
+ id: 'yum product id 1',
191
+ frequency: 'yum selling plan id 1'
192
+ }
193
+ ]);
194
+ });
195
+ });
196
+
197
+ describe('given optin not already in state', () => {
198
+ describe('given offer is eligible for autoship by default', () => {
199
+ describe('given offer has PSDF', () => {
200
+ describe('given selling plans are set', () => {
201
+ it('should set frequency to the selling plan given there is a matching selling plan', () => {
202
+ const actual = optedin([], {
203
+ type: constants.RECEIVE_OFFER,
204
+ payload: {
205
+ autoship: {
206
+ 'yum product id 1': true
207
+ },
208
+ autoship_by_default: {
209
+ 'yum product id 1': true
210
+ },
211
+ default_frequencies: {
212
+ 'yum product id 1': {
213
+ every: 1,
214
+ every_period: 2
215
+ }
216
+ },
217
+ in_stock: {
218
+ 'yum product id 1': true
219
+ },
220
+ offer: {
221
+ defaultFrequency: 'yum offer default frequency'
222
+ },
223
+ frequencyConfig: {
224
+ frequencies: ['yum selling plan id 1', 'yum selling plan id 2'],
225
+ frequenciesEveryPeriod: ['1_1', '1_2']
226
+ }
227
+ }
228
+ });
229
+ expect(actual).toEqual([
230
+ {
231
+ id: 'yum product id 1',
232
+ frequency: 'yum selling plan id 2'
233
+ }
234
+ ]);
235
+ });
236
+
237
+ describe('given there is no matching selling plan for PSDF', () => {
238
+ it('should set frequency to offer element default frequency given offer element default frequency is a selling plan', () => {
239
+ const actual = optedin([], {
240
+ type: constants.RECEIVE_OFFER,
241
+ payload: {
242
+ autoship: {
243
+ 'yum product id 1': true
244
+ },
245
+ autoship_by_default: {
246
+ 'yum product id 1': true
247
+ },
248
+ default_frequencies: {
249
+ 'yum product id 1': {
250
+ every: 1,
251
+ every_period: 2
252
+ }
253
+ },
254
+ in_stock: {
255
+ 'yum product id 1': true
256
+ },
257
+ offer: {
258
+ defaultFrequency: 'yum offer default selling plan'
259
+ },
260
+ frequencyConfig: {
261
+ frequencies: ['yum selling plan id 1'],
262
+ frequenciesEveryPeriod: ['1_1']
263
+ }
264
+ }
265
+ });
266
+ expect(actual).toEqual([
267
+ {
268
+ id: 'yum product id 1',
269
+ frequency: 'yum offer default selling plan'
270
+ }
271
+ ]);
272
+ });
273
+
274
+ it('should set frequency to the selling plan of the offer element default frequency given the offer element default frequency has a matching selling plan', () => {
275
+ const actual = optedin([], {
276
+ type: constants.RECEIVE_OFFER,
277
+ payload: {
278
+ autoship: {
279
+ 'yum product id 1': true
280
+ },
281
+ autoship_by_default: {
282
+ 'yum product id 1': true
283
+ },
284
+ default_frequencies: {
285
+ 'yum product id 1': {
286
+ every: 1,
287
+ every_period: 2
288
+ }
289
+ },
290
+ in_stock: {
291
+ 'yum product id 1': true
292
+ },
293
+ offer: {
294
+ defaultFrequency: '1_3'
295
+ },
296
+ frequencyConfig: {
297
+ frequencies: ['yum selling plan id 1', 'yum selling plan id 3'],
298
+ frequenciesEveryPeriod: ['1_1', '1_3']
299
+ }
300
+ }
301
+ });
302
+ expect(actual).toEqual([
303
+ {
304
+ id: 'yum product id 1',
305
+ frequency: 'yum selling plan id 3'
306
+ }
307
+ ]);
308
+ });
309
+
310
+ it('should set frequency to the first selling plan given the offer element default frequency is not a selling plan and does not have a matching selling plan', () => {
311
+ const actual = optedin([], {
312
+ type: constants.RECEIVE_OFFER,
313
+ payload: {
314
+ autoship: {
315
+ 'yum product id 1': true
316
+ },
317
+ autoship_by_default: {
318
+ 'yum product id 1': true
319
+ },
320
+ default_frequencies: {
321
+ 'yum product id 1': {
322
+ every: 1,
323
+ every_period: 2
324
+ }
325
+ },
326
+ in_stock: {
327
+ 'yum product id 1': true
328
+ },
329
+ offer: {
330
+ defaultFrequency: '1_3'
331
+ },
332
+ frequencyConfig: {
333
+ frequencies: ['yum selling plan id 1', 'yum selling plan id 4'],
334
+ frequenciesEveryPeriod: ['1_1', '1_4']
335
+ }
336
+ }
337
+ });
338
+ expect(actual).toEqual([
339
+ {
340
+ id: 'yum product id 1',
341
+ frequency: 'yum selling plan id 1'
342
+ }
343
+ ]);
344
+ });
345
+ });
346
+ });
347
+
348
+ it('should set frequency to every_everyPeriod psdf given selling plans are not set', () => {
349
+ const actual = optedin([], {
350
+ type: constants.RECEIVE_OFFER,
351
+ payload: {
352
+ autoship: {
353
+ 'yum product id 1': true
354
+ },
355
+ autoship_by_default: {
356
+ 'yum product id 1': true
357
+ },
358
+ default_frequencies: {
359
+ 'yum product id 1': {
360
+ every: 1,
361
+ every_period: 2
362
+ }
363
+ },
364
+ in_stock: {
365
+ 'yum product id 1': true
366
+ },
367
+ offer: {
368
+ defaultFrequency: 'yum offer default frequency'
369
+ },
370
+ frequencyConfig: {}
371
+ }
372
+ });
373
+ expect(actual).toEqual([
374
+ {
375
+ id: 'yum product id 1',
376
+ frequency: '1_2'
377
+ }
378
+ ]);
379
+ });
380
+ });
381
+
382
+ describe('given offer does not have PSDF', () => {
383
+ it('should set frequency to offer element default frequency given offer element default frequency is selling plan', () => {
384
+ const actual = optedin([], {
385
+ type: constants.RECEIVE_OFFER,
386
+ payload: {
387
+ autoship: {
388
+ 'yum product id 1': true
389
+ },
390
+ autoship_by_default: {
391
+ 'yum product id 1': true
392
+ },
393
+ in_stock: {
394
+ 'yum product id 1': true
395
+ },
396
+ offer: {
397
+ defaultFrequency: 'yum offer default selling plan'
398
+ },
399
+ frequencyConfig: {
400
+ frequencies: ['yum selling plan id 1'],
401
+ frequenciesEveryPeriod: ['1_1']
402
+ }
403
+ }
404
+ });
405
+ expect(actual).toEqual([
406
+ {
407
+ id: 'yum product id 1',
408
+ frequency: 'yum offer default selling plan'
409
+ }
410
+ ]);
411
+ });
412
+
413
+ describe('given selling plans are set', () => {
414
+ it('should set frequency to the selling plan of offer element default frequency given there is a matching selling plan for offer element default frequency', () => {
415
+ const actual = optedin([], {
416
+ type: constants.RECEIVE_OFFER,
417
+ payload: {
418
+ autoship: {
419
+ 'yum product id 1': true
420
+ },
421
+ autoship_by_default: {
422
+ 'yum product id 1': true
423
+ },
424
+ in_stock: {
425
+ 'yum product id 1': true
426
+ },
427
+ offer: {
428
+ defaultFrequency: '1_2'
429
+ },
430
+ frequencyConfig: {
431
+ frequencies: ['yum selling plan id 1', 'yum selling plan id 2'],
432
+ frequenciesEveryPeriod: ['1_1', '1_2']
433
+ }
434
+ }
435
+ });
436
+ expect(actual).toEqual([
437
+ {
438
+ id: 'yum product id 1',
439
+ frequency: 'yum selling plan id 2'
440
+ }
441
+ ]);
442
+ });
443
+
444
+ describe('given there is no matching selling plan for every_everyPeriod default frequency of offer element', () => {
445
+ it('should set frequency to offer element default frequency given offer element default frequency is a selling plan', () => {
446
+ const actual = optedin([], {
447
+ type: constants.RECEIVE_OFFER,
448
+ payload: {
449
+ autoship: {
450
+ 'yum product id 1': true
451
+ },
452
+ autoship_by_default: {
453
+ 'yum product id 1': true
454
+ },
455
+ in_stock: {
456
+ 'yum product id 1': true
457
+ },
458
+ offer: {
459
+ defaultFrequency: 'yum offer default selling plan'
460
+ },
461
+ frequencyConfig: {
462
+ frequencies: ['yum selling plan id 1'],
463
+ frequenciesEveryPeriod: ['1_1']
464
+ }
465
+ }
466
+ });
467
+ expect(actual).toEqual([
468
+ {
469
+ id: 'yum product id 1',
470
+ frequency: 'yum offer default selling plan'
471
+ }
472
+ ]);
473
+ });
474
+
475
+ it('should set frequency to the selling plan of the offer element default frequency given the offer element default frequency has a matching selling plan', () => {
476
+ const actual = optedin([], {
477
+ type: constants.RECEIVE_OFFER,
478
+ payload: {
479
+ autoship: {
480
+ 'yum product id 1': true
481
+ },
482
+ autoship_by_default: {
483
+ 'yum product id 1': true
484
+ },
485
+ in_stock: {
486
+ 'yum product id 1': true
487
+ },
488
+ offer: {
489
+ defaultFrequency: '1_3'
490
+ },
491
+ frequencyConfig: {
492
+ frequencies: ['yum selling plan id 1', 'yum selling plan id 3'],
493
+ frequenciesEveryPeriod: ['1_1', '1_3']
494
+ }
495
+ }
496
+ });
497
+ expect(actual).toEqual([
498
+ {
499
+ id: 'yum product id 1',
500
+ frequency: 'yum selling plan id 3'
501
+ }
502
+ ]);
503
+ });
504
+
505
+ it('should set frequency to the first selling plan given the offer element default frequency is not a selling plan and does not have a matching selling plan', () => {
506
+ const actual = optedin([], {
507
+ type: constants.RECEIVE_OFFER,
508
+ payload: {
509
+ autoship: {
510
+ 'yum product id 1': true
511
+ },
512
+ autoship_by_default: {
513
+ 'yum product id 1': true
514
+ },
515
+ in_stock: {
516
+ 'yum product id 1': true
517
+ },
518
+ offer: {
519
+ defaultFrequency: '1_3'
520
+ },
521
+ frequencyConfig: {
522
+ frequencies: ['yum selling plan id 1', 'yum selling plan id 4'],
523
+ frequenciesEveryPeriod: ['1_1', '1_4']
524
+ }
525
+ }
526
+ });
527
+ expect(actual).toEqual([
528
+ {
529
+ id: 'yum product id 1',
530
+ frequency: 'yum selling plan id 1'
531
+ }
532
+ ]);
533
+ });
534
+ });
535
+ });
536
+ });
537
+
538
+ it('should map frequency to selling plan given selling plan exists', () => {
539
+ const actual = optedin(
540
+ [
541
+ {
542
+ id: 'yum product id 1',
543
+ frequency: '1_2'
544
+ }
545
+ ],
546
+ {
547
+ type: constants.RECEIVE_OFFER,
548
+ payload: {
549
+ offer: {},
550
+ frequencyConfig: {
551
+ frequencies: ['yum selling plan id 1', 'yum selling plan id 2'],
552
+ frequenciesEveryPeriod: ['1_1', '1_2']
553
+ }
554
+ }
555
+ }
556
+ );
557
+ expect(actual).toEqual([
558
+ {
559
+ id: 'yum product id 1',
560
+ frequency: 'yum selling plan id 2'
561
+ }
562
+ ]);
563
+ });
564
+
565
+ it('should return unmodified state given state already contains optin with a selling plan', () => {
566
+ const actual = optedin(
567
+ [
568
+ {
569
+ id: 'yum product id 1',
570
+ frequency: 'yum existing frequency'
571
+ }
572
+ ],
573
+ {
574
+ type: constants.RECEIVE_OFFER,
575
+ payload: {}
576
+ }
577
+ );
578
+ expect(actual).toEqual([
579
+ {
580
+ id: 'yum product id 1',
581
+ frequency: 'yum existing frequency'
582
+ }
583
+ ]);
584
+ });
585
+ });
586
+ });
587
+ });
588
+
589
+ describe('given action is SETUP_PRODUCT', () => {
590
+ it('should return unmodified state given frequeny is already a selling plan', () => {
591
+ const actual = optedin(
592
+ [
593
+ {
594
+ id: 'yum product id 1',
595
+ frequency: 'yum selling plan id 1'
596
+ }
597
+ ],
598
+ {
599
+ type: constants.SETUP_PRODUCT,
600
+ payload: {
601
+ product: {
602
+ selling_plan_groups: [
603
+ {
604
+ name: DEFAULT_PAY_AS_YOU_GO_GROUP_NAME,
605
+ selling_plans: [
606
+ {
607
+ id: 'yum selling plan id 1',
608
+ options: [
609
+ {
610
+ value: '1 day'
611
+ }
612
+ ]
613
+ },
614
+ {
615
+ id: 'yum selling plan id 2',
616
+ options: [
617
+ {
618
+ value: '1 week1'
619
+ }
620
+ ]
621
+ }
622
+ ]
623
+ }
624
+ ]
625
+ }
626
+ }
627
+ }
628
+ );
629
+
630
+ expect(actual).toEqual([
631
+ {
632
+ id: 'yum product id 1',
633
+ frequency: 'yum selling plan id 1'
634
+ }
635
+ ]);
636
+ });
637
+
638
+ it('should set frequency to selling plan given matching selling plan', () => {
639
+ const actual = optedin(
640
+ [
641
+ {
642
+ id: 'yum product id 1',
643
+ frequency: '1_2'
644
+ }
645
+ ],
646
+ {
647
+ type: constants.SETUP_PRODUCT,
648
+ payload: {
649
+ product: {
650
+ selling_plan_groups: [
651
+ {
652
+ name: DEFAULT_PAY_AS_YOU_GO_GROUP_NAME,
653
+ selling_plans: [
654
+ {
655
+ id: 'yum selling plan id 1',
656
+ options: [
657
+ {
658
+ value: '1 day'
659
+ }
660
+ ]
661
+ },
662
+ {
663
+ id: 'yum selling plan id 2',
664
+ options: [
665
+ {
666
+ value: '1 week'
667
+ }
668
+ ]
669
+ }
670
+ ]
671
+ }
672
+ ]
673
+ }
674
+ }
675
+ }
676
+ );
677
+
678
+ expect(actual).toEqual([
679
+ {
680
+ id: 'yum product id 1',
681
+ frequency: 'yum selling plan id 2'
682
+ }
683
+ ]);
684
+ });
685
+
686
+ it('should set frequency to first selling plan given no matching selling plan', () => {
687
+ const actual = optedin(
688
+ [
689
+ {
690
+ id: 'yum product id 1',
691
+ frequency: '1_3'
692
+ }
693
+ ],
694
+ {
695
+ type: constants.SETUP_PRODUCT,
696
+ payload: {
697
+ product: {
698
+ selling_plan_groups: [
699
+ {
700
+ name: DEFAULT_PAY_AS_YOU_GO_GROUP_NAME,
701
+ selling_plans: [
702
+ {
703
+ id: 'yum selling plan id 1',
704
+ options: [
705
+ {
706
+ value: '1 day'
707
+ }
708
+ ]
709
+ },
710
+ {
711
+ id: 'yum selling plan id 2',
712
+ options: [
713
+ {
714
+ value: '1 week'
715
+ }
716
+ ]
717
+ }
718
+ ]
719
+ }
720
+ ]
721
+ }
722
+ }
723
+ }
724
+ );
725
+
726
+ expect(actual).toEqual([
727
+ {
728
+ id: 'yum product id 1',
729
+ frequency: 'yum selling plan id 1'
730
+ }
731
+ ]);
732
+ });
733
+
734
+ it('should not error and not change state if there are no selling plan groups', () => {
735
+ const actual = optedin(
736
+ [
737
+ {
738
+ id: 'yum product id 1',
739
+ frequency: '1_3'
740
+ }
741
+ ],
742
+ {
743
+ type: constants.SETUP_PRODUCT,
744
+ payload: {
745
+ product: {
746
+ selling_plan_groups: []
747
+ }
748
+ }
749
+ }
750
+ );
751
+
752
+ expect(actual).toEqual([
753
+ {
754
+ id: 'yum product id 1',
755
+ frequency: '1_3'
756
+ }
757
+ ]);
758
+ });
759
+ });
760
+
761
+ describe('given action is PRODUCT_CHANGE_PREPAID_SHIPMENTS', () => {
762
+ function getPayload(prepaidShipments, frequency) {
763
+ return {
764
+ product: {
765
+ id: 'yum prepaid id'
766
+ },
767
+ prepaidShipments: prepaidShipments,
768
+ offer: {},
769
+ frequency
770
+ };
771
+ }
772
+
773
+ it('opts into the given frequency and shipments', () => {
774
+ const actual = optedin([], {
775
+ type: constants.PRODUCT_CHANGE_PREPAID_SHIPMENTS,
776
+ payload: getPayload(4, 'yum prepaid selling plan id 2')
777
+ });
778
+ expect(actual).toEqual([
779
+ {
780
+ id: 'yum prepaid id',
781
+ frequency: 'yum prepaid selling plan id 2',
782
+ prepaidShipments: 4
783
+ }
784
+ ]);
785
+ });
786
+
787
+ it('updates existing opted in state', () => {
788
+ const actual = optedin(
789
+ [
790
+ {
791
+ id: 'yum prepaid id',
792
+ frequency: 'yum selling plan id 1'
793
+ }
794
+ ],
795
+ {
796
+ type: constants.PRODUCT_CHANGE_PREPAID_SHIPMENTS,
797
+ payload: getPayload(4, 'yum prepaid selling plan id 2')
798
+ }
799
+ );
800
+ expect(actual).toEqual([
801
+ {
802
+ id: 'yum prepaid id',
803
+ frequency: 'yum prepaid selling plan id 2',
804
+ prepaidShipments: 4
805
+ }
806
+ ]);
807
+ });
808
+ });
809
+
810
+ it('should return unmodified state given unsupported action', () => {
811
+ const actual = optedin(
812
+ { 'yum existing key': 'yum existing value' },
813
+ {
814
+ type: 'yum unsupported action',
815
+ payload: {}
816
+ }
817
+ );
818
+
819
+ expect(actual).toEqual({ 'yum existing key': 'yum existing value' });
820
+ });
821
+ });