@@ -2,56 +2,73 @@ import {
2
2
CheckoutSelectors ,
3
3
CheckoutService ,
4
4
createCheckoutService ,
5
+ createLanguageService ,
6
+ PaymentInitializeOptions ,
5
7
} from '@bigcommerce/checkout-sdk' ;
8
+ import { act } from '@testing-library/react' ;
6
9
import { mount , ReactWrapper } from 'enzyme' ;
7
10
import { Formik } from 'formik' ;
8
11
import { noop } from 'lodash' ;
9
12
import React , { FunctionComponent } from 'react' ;
10
- import { act } from 'react-dom/test-utils' ;
11
13
12
- import { createLocaleContext , LocaleContext , LocaleContextType } from '@bigcommerce/checkout/locale' ;
13
- import { CheckoutProvider } from '@bigcommerce/checkout/payment-integration-api' ;
14
-
15
- import { getCart } from '../../cart/carts.mock' ;
16
- import { getStoreConfig } from '../../config/config.mock' ;
17
- import { getCustomer } from '../../customer/customers.mock' ;
18
- import { Modal , ModalProps } from '../../ui/modal' ;
19
- import { getPaymentMethod } from '../payment-methods.mock' ;
20
- import PaymentContext , { PaymentContextProps } from '../PaymentContext' ;
21
-
22
- import BlueSnapV2PaymentMethod , { BlueSnapV2PaymentMethodProps } from './BlueSnapV2PaymentMethod' ;
23
- import HostedPaymentMethod from './HostedPaymentMethod' ;
24
- import PaymentMethodId from './PaymentMethodId' ;
14
+ import {
15
+ createLocaleContext ,
16
+ LocaleContext ,
17
+ LocaleContextType ,
18
+ } from '@bigcommerce/checkout/locale' ;
19
+ import {
20
+ CheckoutProvider ,
21
+ PaymentFormContext ,
22
+ PaymentFormService ,
23
+ PaymentMethodId ,
24
+ PaymentMethodProps ,
25
+ } from '@bigcommerce/checkout/payment-integration-api' ;
26
+ import {
27
+ getCart ,
28
+ getCustomer ,
29
+ getPaymentFormServiceMock ,
30
+ getPaymentMethod ,
31
+ getStoreConfig ,
32
+ } from '@bigcommerce/checkout/test-mocks' ;
33
+ import { Modal } from '@bigcommerce/checkout/ui' ;
34
+ import HostedPaymentMethodComponent from 'packages/hosted-payment-integration/src/HostedPaymentComponent' ;
35
+ import { ModalProps } from 'packages/ui/src/modal' ;
36
+
37
+ import BlueSnapV2PaymentMethod from './BlueSnapV2PaymentMethod' ;
25
38
26
39
describe ( 'when using BlueSnapV2 payment' , ( ) => {
27
- let defaultProps : BlueSnapV2PaymentMethodProps ;
40
+ let defaultProps : PaymentMethodProps ;
28
41
let checkoutService : CheckoutService ;
29
42
let checkoutState : CheckoutSelectors ;
30
43
let localeContext : LocaleContextType ;
31
- let paymentContext : PaymentContextProps ;
32
44
let BlueSnapV2PaymentMethodTest : FunctionComponent ;
45
+ let paymentForm : PaymentFormService ;
46
+ let initializePayment : jest . SpyInstance <
47
+ Promise < CheckoutSelectors > ,
48
+ [ options : PaymentInitializeOptions ]
49
+ > ;
33
50
34
51
beforeEach ( ( ) => {
52
+ checkoutService = createCheckoutService ( ) ;
53
+ checkoutState = checkoutService . getState ( ) ;
54
+ localeContext = createLocaleContext ( getStoreConfig ( ) ) ;
55
+ paymentForm = getPaymentFormServiceMock ( ) ;
56
+ initializePayment = jest
57
+ . spyOn ( checkoutService , 'initializePayment' )
58
+ . mockResolvedValue ( checkoutState ) ;
35
59
defaultProps = {
36
- initializePayment : jest . fn ( ) ,
37
- deinitializePayment : jest . fn ( ) ,
60
+ checkoutService,
61
+ checkoutState,
62
+ paymentForm : getPaymentFormServiceMock ( ) ,
63
+ language : createLanguageService ( ) ,
64
+ onUnhandledError : jest . fn ( ) ,
38
65
method : {
39
66
...getPaymentMethod ( ) ,
40
67
id : 'mastercard' ,
41
68
gateway : PaymentMethodId . BlueSnapV2 ,
42
69
} ,
43
70
} ;
44
-
45
- checkoutService = createCheckoutService ( ) ;
46
- checkoutState = checkoutService . getState ( ) ;
47
- localeContext = createLocaleContext ( getStoreConfig ( ) ) ;
48
-
49
- paymentContext = {
50
- disableSubmit : jest . fn ( ) ,
51
- setSubmit : jest . fn ( ) ,
52
- setValidationSchema : jest . fn ( ) ,
53
- hidePaymentSubmitButton : jest . fn ( ) ,
54
- } ;
71
+ jest . spyOn ( checkoutService , 'initializePayment' ) . mockResolvedValue ( checkoutState ) ;
55
72
56
73
jest . spyOn ( checkoutState . data , 'getCart' ) . mockReturnValue ( getCart ( ) ) ;
57
74
@@ -61,27 +78,27 @@ describe('when using BlueSnapV2 payment', () => {
61
78
62
79
BlueSnapV2PaymentMethodTest = ( props ) => (
63
80
< CheckoutProvider checkoutService = { checkoutService } >
64
- < PaymentContext . Provider value = { paymentContext } >
81
+ < PaymentFormContext . Provider value = { { paymentForm } } >
65
82
< LocaleContext . Provider value = { localeContext } >
66
83
< Formik initialValues = { { } } onSubmit = { noop } >
67
84
< BlueSnapV2PaymentMethod { ...defaultProps } { ...props } />
68
85
</ Formik >
69
86
</ LocaleContext . Provider >
70
- </ PaymentContext . Provider >
87
+ </ PaymentFormContext . Provider >
71
88
</ CheckoutProvider >
72
89
) ;
73
90
} ) ;
74
91
75
92
it ( 'renders as hosted payment method' , ( ) => {
76
93
const container = mount ( < BlueSnapV2PaymentMethodTest /> ) ;
77
94
78
- expect ( container . find ( HostedPaymentMethod ) ) . toHaveLength ( 1 ) ;
95
+ expect ( container . find ( HostedPaymentMethodComponent ) ) . toHaveLength ( 1 ) ;
79
96
} ) ;
80
97
81
98
it ( 'initializes method with required config' , ( ) => {
82
99
mount ( < BlueSnapV2PaymentMethodTest /> ) ;
83
100
84
- expect ( defaultProps . initializePayment ) . toHaveBeenCalledWith (
101
+ expect ( defaultProps . checkoutService . initializePayment ) . toHaveBeenCalledWith (
85
102
expect . objectContaining ( {
86
103
methodId : 'mastercard' ,
87
104
gatewayId : 'bluesnapv2' ,
@@ -99,10 +116,10 @@ describe('when using BlueSnapV2 payment', () => {
99
116
100
117
it ( 'renders modal that hosts bluesnap payment page' , async ( ) => {
101
118
const component = mount ( < BlueSnapV2PaymentMethodTest /> ) ;
102
- const initializeOptions = ( defaultProps . initializePayment as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ;
119
+ const initializeOptions = initializePayment . mock . calls [ 0 ] [ 0 ] ;
103
120
104
121
act ( ( ) => {
105
- initializeOptions . bluesnapv2 . onLoad ( document . createElement ( 'iframe' ) , jest . fn ( ) ) ;
122
+ initializeOptions . bluesnapv2 ? .onLoad ( document . createElement ( 'iframe' ) , jest . fn ( ) ) ;
106
123
} ) ;
107
124
108
125
await new Promise ( ( resolve ) => process . nextTick ( resolve ) ) ;
@@ -116,10 +133,10 @@ describe('when using BlueSnapV2 payment', () => {
116
133
117
134
it ( 'renders modal and appends bluesnap payment page' , async ( ) => {
118
135
const component = mount ( < BlueSnapV2PaymentMethodTest /> ) ;
119
- const initializeOptions = ( defaultProps . initializePayment as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ;
136
+ const initializeOptions = initializePayment . mock . calls [ 0 ] [ 0 ] ;
120
137
const iframe = document . createElement ( 'iframe' ) ;
121
138
122
- act ( ( ) => initializeOptions . bluesnapv2 . onLoad ( iframe , jest . fn ( ) ) ) ;
139
+ act ( ( ) => initializeOptions . bluesnapv2 ? .onLoad ( iframe , jest . fn ( ) ) ) ;
123
140
124
141
await new Promise ( ( resolve ) => process . nextTick ( resolve ) ) ;
125
142
@@ -136,10 +153,10 @@ describe('when using BlueSnapV2 payment', () => {
136
153
137
154
it ( 'renders modal but does not append bluesnap payment page because is empty' , async ( ) => {
138
155
const component = mount ( < BlueSnapV2PaymentMethodTest /> ) ;
139
- const initializeOptions = ( defaultProps . initializePayment as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ;
140
-
156
+ const initializeOptions = initializePayment . mock . calls [ 0 ] [ 0 ] ;
157
+
141
158
act ( ( ) => {
142
- initializeOptions . bluesnapv2 . onLoad ( undefined , jest . fn ( ) ) ;
159
+ initializeOptions . bluesnapv2 ? .onLoad ( undefined , jest . fn ( ) ) ;
143
160
} ) ;
144
161
145
162
await new Promise ( ( resolve ) => process . nextTick ( resolve ) ) ;
@@ -158,10 +175,10 @@ describe('when using BlueSnapV2 payment', () => {
158
175
it ( 'cancels payment flow if user chooses to close modal' , async ( ) => {
159
176
const cancelBlueSnapV2Payment = jest . fn ( ) ;
160
177
const component = mount ( < BlueSnapV2PaymentMethodTest /> ) ;
161
- const initializeOptions = ( defaultProps . initializePayment as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ;
178
+ const initializeOptions = initializePayment . mock . calls [ 0 ] [ 0 ] ;
162
179
163
180
act ( ( ) => {
164
- initializeOptions . bluesnapv2 . onLoad (
181
+ initializeOptions . bluesnapv2 ? .onLoad (
165
182
document . createElement ( 'iframe' ) ,
166
183
cancelBlueSnapV2Payment ,
167
184
) ;
@@ -176,9 +193,9 @@ describe('when using BlueSnapV2 payment', () => {
176
193
const modal : ReactWrapper < ModalProps > = component . find ( Modal ) ;
177
194
178
195
act ( ( ) => {
179
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
196
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument, @typescript-eslint/consistent-type-assertions
180
197
modal . prop ( 'onRequestClose' ) ! ( new MouseEvent ( 'click' ) as any ) ;
181
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
198
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-unsafe-argument, @typescript-eslint/consistent-type-assertions, @typescript-eslint/no-explicit-any
182
199
modal . prop ( 'onRequestClose' ) ! ( new MouseEvent ( 'click' ) as any ) ;
183
200
} ) ;
184
201
0 commit comments