1
+ import { createAction } from '@bigcommerce/data-store' ;
1
2
import { createFormPoster , FormPoster } from '@bigcommerce/form-poster' ;
3
+ import { createRequestSender } from '@bigcommerce/request-sender' ;
2
4
import { getScriptLoader } from '@bigcommerce/script-loader' ;
3
5
import { EventEmitter } from 'events' ;
4
6
import { merge } from 'lodash' ;
7
+ import { from } from 'rxjs' ;
5
8
6
- import { createCheckoutStore , CheckoutStore } from '../../../checkout' ;
7
- import { getCheckoutStoreState } from '../../../checkout/checkouts.mock' ;
9
+ import { createCheckoutStore , CheckoutActionCreator , CheckoutActionType , CheckoutRequestSender , CheckoutStore } from '../../../checkout' ;
10
+ import { getCheckout , getCheckoutStoreState } from '../../../checkout/checkouts.mock' ;
8
11
import { MissingDataError } from '../../../common/error/errors' ;
12
+ import { ConfigActionCreator , ConfigRequestSender } from '../../../config' ;
9
13
import { getPaypalExpress } from '../../../payment/payment-methods.mock' ;
10
14
import { PaypalActions , PaypalButtonOptions , PaypalScriptLoader , PaypalSDK } from '../../../payment/strategies/paypal' ;
11
15
import { getPaypalMock } from '../../../payment/strategies/paypal/paypal.mock' ;
@@ -16,6 +20,8 @@ import { PaypalButtonInitializeOptions } from './paypal-button-options';
16
20
import PaypalButtonStrategy from './paypal-button-strategy' ;
17
21
18
22
describe ( 'PaypalButtonStrategy' , ( ) => {
23
+ let actionsMock : PaypalActions ;
24
+ let checkoutActionCreator : CheckoutActionCreator ;
19
25
let eventEmitter : EventEmitter ;
20
26
let formPoster : FormPoster ;
21
27
let options : CheckoutButtonInitializeOptions ;
@@ -24,10 +30,13 @@ describe('PaypalButtonStrategy', () => {
24
30
let paypalScriptLoader : PaypalScriptLoader ;
25
31
let store : CheckoutStore ;
26
32
let strategy : PaypalButtonStrategy ;
27
- let actionsMock : PaypalActions ;
28
33
29
34
beforeEach ( ( ) => {
30
35
store = createCheckoutStore ( getCheckoutStoreState ( ) ) ;
36
+ checkoutActionCreator = new CheckoutActionCreator (
37
+ new CheckoutRequestSender ( createRequestSender ( ) ) ,
38
+ new ConfigActionCreator ( new ConfigRequestSender ( createRequestSender ( ) ) )
39
+ ) ;
31
40
formPoster = createFormPoster ( ) ;
32
41
paypalScriptLoader = new PaypalScriptLoader ( getScriptLoader ( ) ) ;
33
42
@@ -63,7 +72,11 @@ describe('PaypalButtonStrategy', () => {
63
72
jest . spyOn ( paypal . Button , 'render' )
64
73
. mockImplementation ( ( options : PaypalButtonOptions ) => {
65
74
eventEmitter . on ( 'payment' , ( ) => {
66
- options . payment ( ) . catch ( ( ) => { } ) ;
75
+ options . payment ( {
76
+ payerId : 'PAYER_ID' ,
77
+ paymentID : 'PAYMENT_ID' ,
78
+ payerID : 'PAYER_ID' ,
79
+ } , actionsMock ) . catch ( ( ) => { } ) ;
67
80
} ) ;
68
81
69
82
eventEmitter . on ( 'authorize' , ( ) => {
@@ -75,6 +88,12 @@ describe('PaypalButtonStrategy', () => {
75
88
} ) ;
76
89
} ) ;
77
90
91
+ jest . spyOn ( checkoutActionCreator , 'loadDefaultCheckout' )
92
+ . mockReturnValue ( ( ) => from ( [
93
+ createAction ( CheckoutActionType . LoadCheckoutRequested ) ,
94
+ createAction ( CheckoutActionType . LoadCheckoutSucceeded , getCheckout ( ) ) ,
95
+ ] ) ) ;
96
+
78
97
jest . spyOn ( paypalScriptLoader , 'loadPaypal' )
79
98
. mockReturnValue ( Promise . resolve ( paypal ) ) ;
80
99
@@ -83,6 +102,7 @@ describe('PaypalButtonStrategy', () => {
83
102
84
103
strategy = new PaypalButtonStrategy (
85
104
store ,
105
+ checkoutActionCreator ,
86
106
paypalScriptLoader ,
87
107
formPoster
88
108
) ;
@@ -93,6 +113,7 @@ describe('PaypalButtonStrategy', () => {
93
113
store = createCheckoutStore ( ) ;
94
114
strategy = new PaypalButtonStrategy (
95
115
store ,
116
+ checkoutActionCreator ,
96
117
paypalScriptLoader ,
97
118
formPoster
98
119
) ;
@@ -193,6 +214,7 @@ describe('PaypalButtonStrategy', () => {
193
214
194
215
strategy = new PaypalButtonStrategy (
195
216
store ,
217
+ checkoutActionCreator ,
196
218
paypalScriptLoader ,
197
219
formPoster
198
220
) ;
@@ -251,4 +273,42 @@ describe('PaypalButtonStrategy', () => {
251
273
} , 'checkout-button' ) ;
252
274
} ) ;
253
275
} ) ;
276
+
277
+ it ( 'sends create payment requests to the relative url by default' , async ( ) => {
278
+ await strategy . initialize ( options ) ;
279
+
280
+ eventEmitter . emit ( 'payment' ) ;
281
+
282
+ await new Promise ( resolve => process . nextTick ( resolve ) ) ;
283
+
284
+ const expectedBody = { cartId : 'b20deef40f9699e48671bbc3fef6ca44dc80e3c7' , merchantId : 'h3hxn44tdd8wxkzd' } ;
285
+
286
+ expect ( actionsMock . request . post )
287
+ . toHaveBeenCalledWith ( '/api/storefront/payment/paypalexpress' , expectedBody , expect . any ( Object ) ) ;
288
+ } ) ;
289
+
290
+ describe ( 'with a supplied host' , ( ) => {
291
+ beforeEach ( ( ) => {
292
+ strategy = new PaypalButtonStrategy (
293
+ store ,
294
+ checkoutActionCreator ,
295
+ paypalScriptLoader ,
296
+ formPoster ,
297
+ 'https://example.com'
298
+ ) ;
299
+ } ) ;
300
+
301
+ it ( 'sends create payment requests to the supplied host' , async ( ) => {
302
+ await strategy . initialize ( options ) ;
303
+
304
+ eventEmitter . emit ( 'payment' ) ;
305
+
306
+ await new Promise ( resolve => process . nextTick ( resolve ) ) ;
307
+
308
+ const expectedBody = { cartId : 'b20deef40f9699e48671bbc3fef6ca44dc80e3c7' , merchantId : 'h3hxn44tdd8wxkzd' } ;
309
+
310
+ expect ( actionsMock . request . post )
311
+ . toHaveBeenCalledWith ( 'https://example.com/api/storefront/payment/paypalexpress' , expectedBody , expect . any ( Object ) ) ;
312
+ } ) ;
313
+ } ) ;
254
314
} ) ;
0 commit comments