Skip to content

Commit ca3dceb

Browse files
committed
W-20975340: Adding test cases
1 parent 924e7ba commit ca3dceb

File tree

1 file changed

+198
-10
lines changed

1 file changed

+198
-10
lines changed

packages/template-retail-react-app/app/pages/checkout/partials/sf-payments-sheet.test.js

Lines changed: 198 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,24 @@ jest.mock('@salesforce/retail-react-app/app/hooks/use-current-basket', () => ({
139139
useCurrentBasket: () => mockUseCurrentBasket()
140140
}))
141141

142-
jest.mock('@salesforce/retail-react-app/app/hooks/use-current-customer', () => ({
143-
useCurrentCustomer: () => ({
144-
data: {
145-
customerId: 'customer123',
146-
isGuest: false,
147-
isRegistered: true,
148-
email: 'test@example.com'
149-
}
150-
})
151-
}))
142+
const mockCustomer = {
143+
customerId: 'customer123',
144+
isGuest: false,
145+
isRegistered: true,
146+
email: 'test@example.com',
147+
paymentMethodReferences: []
148+
}
149+
150+
let mockUseCurrentCustomer
151+
152+
jest.mock('@salesforce/retail-react-app/app/hooks/use-current-customer', () => {
153+
mockUseCurrentCustomer = jest.fn(() => ({
154+
data: mockCustomer
155+
}))
156+
return {
157+
useCurrentCustomer: mockUseCurrentCustomer
158+
}
159+
})
152160

153161
jest.mock('@salesforce/retail-react-app/app/hooks/use-einstein', () => {
154162
return jest.fn(() => ({
@@ -995,6 +1003,186 @@ describe('SFPaymentsSheet', () => {
9951003

9961004
expect(paymentIntent.setup_future_usage).toBeUndefined()
9971005
})
1006+
1007+
test('confirmPayment sets setup_future_usage to off_session when futureUsageOffSession is true', async () => {
1008+
const ref = React.createRef()
1009+
setupConfirmPaymentMocks()
1010+
1011+
const useShopperConfigurationModule = require('@salesforce/retail-react-app/app/hooks/use-shopper-configuration')
1012+
const originalMock = useShopperConfigurationModule.useShopperConfiguration
1013+
1014+
useShopperConfigurationModule.useShopperConfiguration = jest.fn((configId) => {
1015+
if (configId === 'futureUsageOffSession') return true
1016+
if (configId === 'cardCaptureAutomatic') return true
1017+
if (configId === 'zoneId') return 'default'
1018+
return undefined
1019+
})
1020+
1021+
renderWithCheckoutContext(
1022+
<SFPaymentsSheet
1023+
ref={ref}
1024+
onCreateOrder={mockOnCreateOrder}
1025+
onError={mockOnError}
1026+
/>
1027+
)
1028+
1029+
await waitFor(() => {
1030+
expect(ref.current).toBeDefined()
1031+
})
1032+
1033+
await waitFor(() => {
1034+
expect(mockCheckout).toHaveBeenCalled()
1035+
})
1036+
1037+
const paymentElement = mockCheckout.mock.calls[0][4]
1038+
1039+
await act(async () => {
1040+
paymentElement.dispatchEvent(
1041+
new CustomEvent('sfp:paymentmethodselected', {
1042+
bubbles: true,
1043+
composed: true,
1044+
detail: {
1045+
selectedPaymentMethod: 'card',
1046+
savePaymentMethodForFutureUse: true
1047+
}
1048+
})
1049+
)
1050+
})
1051+
1052+
await ref.current.confirmPayment()
1053+
1054+
await waitFor(() => {
1055+
expect(mockCheckoutConfirm).toHaveBeenCalled()
1056+
})
1057+
1058+
const confirmCall = mockCheckoutConfirm.mock.calls[0]
1059+
const paymentIntentFunction = confirmCall[0]
1060+
const paymentIntent = await paymentIntentFunction()
1061+
1062+
expect(paymentIntent.setup_future_usage).toBe('off_session')
1063+
1064+
useShopperConfigurationModule.useShopperConfiguration = originalMock
1065+
})
1066+
})
1067+
1068+
describe('SPM (Saved Payment Methods) Display', () => {
1069+
beforeEach(() => {
1070+
jest.clearAllMocks()
1071+
mockCustomer.paymentMethodReferences = []
1072+
mockUseCurrentCustomer.mockImplementation(() => ({
1073+
data: {...mockCustomer}
1074+
}))
1075+
})
1076+
1077+
test('passes empty savedPaymentMethods to SDK when customer has no payment method references', async () => {
1078+
mockCustomer.paymentMethodReferences = []
1079+
1080+
renderWithCheckoutContext(
1081+
<SFPaymentsSheet
1082+
ref={React.createRef()}
1083+
onCreateOrder={mockOnCreateOrder}
1084+
onError={mockOnError}
1085+
/>
1086+
)
1087+
1088+
await waitFor(() => {
1089+
expect(mockCheckout).toHaveBeenCalled()
1090+
})
1091+
1092+
const checkoutCall = mockCheckout.mock.calls[0]
1093+
const config = checkoutCall[2]
1094+
1095+
expect(config.options.savedPaymentMethods).toEqual([])
1096+
})
1097+
1098+
test('passes empty savedPaymentMethods to SDK when paymentMethodReferences is null', async () => {
1099+
mockCustomer.paymentMethodReferences = null
1100+
1101+
renderWithCheckoutContext(
1102+
<SFPaymentsSheet
1103+
ref={React.createRef()}
1104+
onCreateOrder={mockOnCreateOrder}
1105+
onError={mockOnError}
1106+
/>
1107+
)
1108+
1109+
await waitFor(() => {
1110+
expect(mockCheckout).toHaveBeenCalled()
1111+
})
1112+
1113+
const checkoutCall = mockCheckout.mock.calls[0]
1114+
const config = checkoutCall[2]
1115+
1116+
expect(config.options.savedPaymentMethods).toEqual([])
1117+
})
1118+
1119+
test('passes empty savedPaymentMethods to SDK when paymentMethodReferences is undefined', async () => {
1120+
mockCustomer.paymentMethodReferences = undefined
1121+
1122+
renderWithCheckoutContext(
1123+
<SFPaymentsSheet
1124+
ref={React.createRef()}
1125+
onCreateOrder={mockOnCreateOrder}
1126+
onError={mockOnError}
1127+
/>
1128+
)
1129+
1130+
await waitFor(() => {
1131+
expect(mockCheckout).toHaveBeenCalled()
1132+
})
1133+
1134+
const checkoutCall = mockCheckout.mock.calls[0]
1135+
const config = checkoutCall[2]
1136+
1137+
expect(config.options.savedPaymentMethods).toEqual([])
1138+
})
1139+
1140+
test('passes empty savedPaymentMethods to SDK when paymentMethodSetAccounts is missing', async () => {
1141+
mockCustomer.paymentMethodReferences = [
1142+
{
1143+
id: 'pm_123',
1144+
accountId: 'stripe-account-1',
1145+
type: 'card',
1146+
brand: 'visa',
1147+
last4: '4242'
1148+
}
1149+
]
1150+
1151+
jest.spyOn(
1152+
require('@salesforce/commerce-sdk-react'),
1153+
'usePaymentConfiguration'
1154+
).mockReturnValue({
1155+
data: {
1156+
paymentMethods: [
1157+
{
1158+
id: 'card',
1159+
name: 'Card',
1160+
paymentMethodType: 'card',
1161+
accountId: 'stripe-account-1'
1162+
}
1163+
],
1164+
paymentMethodSetAccounts: null
1165+
}
1166+
})
1167+
1168+
renderWithCheckoutContext(
1169+
<SFPaymentsSheet
1170+
ref={React.createRef()}
1171+
onCreateOrder={mockOnCreateOrder}
1172+
onError={mockOnError}
1173+
/>
1174+
)
1175+
1176+
await waitFor(() => {
1177+
expect(mockCheckout).toHaveBeenCalled()
1178+
})
1179+
1180+
const checkoutCall = mockCheckout.mock.calls[0]
1181+
const config = checkoutCall[2]
1182+
1183+
expect(config.options.savedPaymentMethods).toEqual([])
1184+
})
1185+
9981186
})
9991187

10001188
describe('lifecycle', () => {

0 commit comments

Comments
 (0)