1
1
/// <reference path="./square-form.d.ts" />
2
-
3
2
import { createClient as createPaymentClient } from '@bigcommerce/bigpay-client' ;
4
3
import { createAction , Action } from '@bigcommerce/data-store' ;
5
4
import { createScriptLoader } from '@bigcommerce/script-loader' ;
6
5
import { Observable } from 'rxjs' ;
7
6
7
+ import { PaymentActionCreator , PaymentRequestSender } from '../..' ;
8
8
import { createCheckoutClient , createCheckoutStore , CheckoutClient , CheckoutStore } from '../../../checkout' ;
9
+ import { MissingDataError , TimeoutError } from '../../../common/error/errors' ;
9
10
import { OrderActionCreator } from '../../../order' ;
10
11
import { SUBMIT_ORDER_REQUESTED } from '../../../order/order-action-types' ;
11
12
import { getSquare } from '../../../payment/payment-methods.mock' ;
13
+ import { SUBMIT_PAYMENT_REQUESTED } from '../../payment-action-types' ;
12
14
import PaymentMethod from '../../payment-method' ;
13
15
14
16
import SquarePaymentStrategy from './square-payment-strategy' ;
15
17
import SquareScriptLoader from './square-script-loader' ;
16
18
19
+
17
20
describe ( 'SquarePaymentStrategy' , ( ) => {
18
21
let client : CheckoutClient ;
19
22
let scriptLoader : SquareScriptLoader ;
20
23
let store : CheckoutStore ;
21
24
let strategy : SquarePaymentStrategy ;
22
25
let orderActionCreator : OrderActionCreator ;
26
+ let paymentActionCreator : PaymentActionCreator ;
23
27
let paymentMethod : PaymentMethod ;
24
28
let callbacks : Square . FormCallbacks ;
25
29
let submitOrderAction : Observable < Action > ;
30
+ let submitPaymentAction : Observable < Action > ;
26
31
27
32
const formFactory = options => {
28
33
callbacks = options . callbacks ;
@@ -39,13 +44,21 @@ describe('SquarePaymentStrategy', () => {
39
44
store = createCheckoutStore ( ) ;
40
45
paymentMethod = getSquare ( ) ;
41
46
orderActionCreator = new OrderActionCreator ( createCheckoutClient ( ) ) ;
47
+ paymentActionCreator = new PaymentActionCreator (
48
+ new PaymentRequestSender ( createPaymentClient ( ) ) ,
49
+ orderActionCreator
50
+ ) ;
42
51
scriptLoader = new SquareScriptLoader ( createScriptLoader ( ) ) ;
43
- strategy = new SquarePaymentStrategy ( store , orderActionCreator , scriptLoader ) ;
44
- submitOrderAction = Observable . of ( createAction ( SUBMIT_ORDER_REQUESTED ) ;
52
+ strategy = new SquarePaymentStrategy ( store , orderActionCreator , paymentActionCreator , scriptLoader ) ;
53
+ submitOrderAction = Observable . of ( createAction ( SUBMIT_ORDER_REQUESTED ) ) ;
54
+ submitPaymentAction = Observable . of ( createAction ( SUBMIT_PAYMENT_REQUESTED ) ) ;
45
55
46
56
jest . spyOn ( orderActionCreator , 'submitOrder' )
47
57
. mockReturnValue ( submitOrderAction ) ;
48
58
59
+ jest . spyOn ( paymentActionCreator , 'submitPayment' )
60
+ . mockReturnValue ( submitPaymentAction ) ;
61
+
49
62
jest . spyOn ( store , 'dispatch' ) ;
50
63
jest . spyOn ( store , 'getState' )
51
64
. mockReturnValue ( {
@@ -119,9 +132,15 @@ describe('SquarePaymentStrategy', () => {
119
132
} ) ;
120
133
121
134
describe ( '#execute()' , ( ) => {
135
+ const payload = {
136
+ payment : {
137
+ name : 'foo' ,
138
+ } ,
139
+ } ;
140
+
122
141
describe ( 'when form has not been initialized' , ( ) => {
123
142
it ( 'rejects the promise' , ( ) => {
124
- strategy . execute ( )
143
+ strategy . execute ( payload )
125
144
. catch ( e => expect ( e . type ) . toEqual ( 'not_initialized' ) ) ;
126
145
127
146
expect ( squareForm . requestCardNonce ) . toHaveBeenCalledTimes ( 0 ) ;
@@ -140,47 +159,64 @@ describe('SquarePaymentStrategy', () => {
140
159
await strategy . initialize ( initOptions ) ;
141
160
} ) ;
142
161
162
+ it ( 'fails if payment name is not passed' , ( ) => {
163
+ try {
164
+ strategy . execute ( { } ) ;
165
+ } catch ( error ) {
166
+ expect ( error ) . toBeInstanceOf ( MissingDataError ) ;
167
+ expect ( squareForm . requestCardNonce ) . toHaveBeenCalledTimes ( 0 ) ;
168
+ }
169
+ } ) ;
170
+
143
171
it ( 'requests the nonce' , ( ) => {
144
- strategy . execute ( { } ) ;
172
+ strategy . execute ( payload ) ;
145
173
expect ( squareForm . requestCardNonce ) . toHaveBeenCalledTimes ( 1 ) ;
146
174
} ) ;
147
175
148
- it ( 'cancels the first request' , async ( ) => {
149
- strategy . execute ( { } )
150
- . catch ( e => expect ( e . type ) . toEqual ( 'timeout' ) ) ;
176
+ it ( 'cancels the first request when a newer is made' , async ( ) => {
177
+ strategy . execute ( payload ) . catch ( e => expect ( e ) . toBeInstanceOf ( TimeoutError ) ) ;
151
178
152
179
setTimeout ( ( ) => {
153
180
callbacks . cardNonceResponseReceived ( null , 'nonce' ) ;
154
181
} , 0 ) ;
155
182
156
- await strategy . execute ( { } ) ;
183
+ await strategy . execute ( payload ) ;
157
184
} ) ;
158
185
159
186
describe ( 'when the nonce is received' , ( ) => {
160
187
let promise ;
161
188
162
189
beforeEach ( ( ) => {
163
- promise = strategy . execute ( { payment : '' , x : 'y' } , { b : 'f' } ) ;
190
+ promise = strategy . execute ( { payment : { name : 'square' } , useStoreCredit : true , x : 'y' } , { b : 'f' } ) ;
164
191
callbacks . cardNonceResponseReceived ( null , 'nonce' ) ;
165
192
} ) ;
166
193
167
194
it ( 'places the order with the right arguments' , ( ) => {
168
- expect ( orderActionCreator . submitOrder ) . toHaveBeenCalledWith ( { x : 'y' } , true , { b : 'f' } ) ;
195
+ expect ( orderActionCreator . submitOrder ) . toHaveBeenCalledWith ( { x : 'y' , useStoreCredit : true } , true , { b : 'f' } ) ;
169
196
expect ( store . dispatch ) . toHaveBeenCalledWith ( submitOrderAction ) ;
170
197
} ) ;
171
198
172
- it ( 'resolves to what is returned by submitOrder ' , async ( ) => {
199
+ it ( 'resolves to what is returned by submitPayment ' , async ( ) => {
173
200
const value = await promise ;
174
201
175
202
expect ( value ) . toEqual ( store . getState ( ) ) ;
176
203
} ) ;
204
+
205
+ it ( 'submits the payment with the right arguments' , ( ) => {
206
+ expect ( paymentActionCreator . submitPayment ) . toHaveBeenCalledWith ( {
207
+ name : 'square' ,
208
+ paymentData : {
209
+ nonce : 'nonce' ,
210
+ } ,
211
+ } ) ;
212
+ } ) ;
177
213
} ) ;
178
214
179
215
describe ( 'when a failure happens receiving the nonce' , ( ) => {
180
216
let promise ;
181
217
182
218
beforeEach ( ( ) => {
183
- promise = strategy . execute ( { } ) ;
219
+ promise = strategy . execute ( payload ) ;
184
220
callbacks . cardNonceResponseReceived ( { } , 'nonce' ) ;
185
221
} ) ;
186
222
@@ -189,6 +225,10 @@ describe('SquarePaymentStrategy', () => {
189
225
expect ( store . dispatch ) . not . toHaveBeenCalledWith ( submitOrderAction ) ;
190
226
} ) ;
191
227
228
+ it ( 'does not submit payment' , ( ) => {
229
+ expect ( paymentActionCreator . submitPayment ) . toHaveBeenCalledTimes ( 0 ) ;
230
+ } ) ;
231
+
192
232
it ( 'rejects the promise' , async ( ) => {
193
233
await promise . catch ( error => expect ( error ) . toBeTruthy ( ) ) ;
194
234
} ) ;
0 commit comments