-
Notifications
You must be signed in to change notification settings - Fork 212
W-20975340, W-21013345: SPM at checkout #3603
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
924e7ba
ca3dceb
9d905de
15d8ca8
9fa4e45
29e2e82
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -101,7 +101,14 @@ jest.mock('@salesforce/commerce-sdk-react', () => { | |
| defaultShippingMethodId: 'DefaultShippingMethod' | ||
| }, | ||
| refetch: mockRefetchShippingMethods | ||
| }) | ||
| }), | ||
| useCustomerId: () => 'customer123', | ||
| useCustomerType: () => ({ | ||
| isRegistered: true, | ||
| isGuest: false, | ||
| customerType: 'registered' | ||
| }), | ||
| useCustomer: jest.fn() | ||
| } | ||
| }) | ||
|
|
||
|
|
@@ -139,15 +146,21 @@ jest.mock('@salesforce/retail-react-app/app/hooks/use-current-basket', () => ({ | |
| useCurrentBasket: () => mockUseCurrentBasket() | ||
| })) | ||
|
|
||
| jest.mock('@salesforce/retail-react-app/app/hooks/use-current-customer', () => ({ | ||
| useCurrentCustomer: () => ({ | ||
| data: { | ||
| customerId: 'customer123', | ||
| isGuest: false, | ||
| isRegistered: true, | ||
| email: 'test@example.com' | ||
| } | ||
| }) | ||
| const mockCustomer = { | ||
| customerId: 'customer123', | ||
| isGuest: false, | ||
| isRegistered: true, | ||
| email: 'test@example.com', | ||
| paymentMethodReferences: [] | ||
| } | ||
|
|
||
| // Get the mocked useCustomer from commerce-sdk-react | ||
| // eslint-disable-next-line @typescript-eslint/no-var-requires | ||
| const mockUseCustomer = require('@salesforce/commerce-sdk-react').useCustomer | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is using require like this an established pattern in PWA?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are a few test modules ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Feels suspect having to disable lint rule here. It'd be good to at least try to find another solution here in case this doesn't jive well with the PWA reviewers when we merge to develop. Otherwise PR is looking good and shaping up! |
||
|
|
||
| // Set default implementation | ||
| mockUseCustomer.mockImplementation(() => ({ | ||
| data: mockCustomer | ||
| })) | ||
|
|
||
| jest.mock('@salesforce/retail-react-app/app/hooks/use-einstein', () => { | ||
|
|
@@ -995,6 +1008,187 @@ describe('SFPaymentsSheet', () => { | |
|
|
||
| expect(paymentIntent.setup_future_usage).toBeUndefined() | ||
| }) | ||
|
|
||
| test('confirmPayment sets setup_future_usage to off_session when futureUsageOffSession is true', async () => { | ||
| const ref = React.createRef() | ||
| setupConfirmPaymentMocks() | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-var-requires | ||
| const useShopperConfigurationModule = require('@salesforce/retail-react-app/app/hooks/use-shopper-configuration') | ||
| const originalMock = useShopperConfigurationModule.useShopperConfiguration | ||
|
|
||
| useShopperConfigurationModule.useShopperConfiguration = jest.fn((configId) => { | ||
| if (configId === 'futureUsageOffSession') return true | ||
| if (configId === 'cardCaptureAutomatic') return true | ||
| if (configId === 'zoneId') return 'default' | ||
| return undefined | ||
| }) | ||
|
|
||
| renderWithCheckoutContext( | ||
| <SFPaymentsSheet | ||
| ref={ref} | ||
| onCreateOrder={mockOnCreateOrder} | ||
| onError={mockOnError} | ||
| /> | ||
| ) | ||
|
|
||
| await waitFor(() => { | ||
| expect(ref.current).toBeDefined() | ||
| }) | ||
|
|
||
| await waitFor(() => { | ||
| expect(mockCheckout).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| const paymentElement = mockCheckout.mock.calls[0][4] | ||
|
|
||
| await act(async () => { | ||
| paymentElement.dispatchEvent( | ||
| new CustomEvent('sfp:paymentmethodselected', { | ||
| bubbles: true, | ||
| composed: true, | ||
| detail: { | ||
| selectedPaymentMethod: 'card', | ||
| savePaymentMethodForFutureUse: true | ||
| } | ||
| }) | ||
| ) | ||
| }) | ||
|
|
||
| await ref.current.confirmPayment() | ||
|
|
||
| await waitFor(() => { | ||
| expect(mockCheckoutConfirm).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| const confirmCall = mockCheckoutConfirm.mock.calls[0] | ||
| const paymentIntentFunction = confirmCall[0] | ||
| const paymentIntent = await paymentIntentFunction() | ||
|
|
||
| expect(paymentIntent.setup_future_usage).toBe('off_session') | ||
|
|
||
| useShopperConfigurationModule.useShopperConfiguration = originalMock | ||
| }) | ||
| }) | ||
|
|
||
| describe('SPM (Saved Payment Methods) Display', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks() | ||
| mockCustomer.paymentMethodReferences = [] | ||
| mockUseCustomer.mockImplementation(() => ({ | ||
| data: {...mockCustomer} | ||
| })) | ||
| }) | ||
|
|
||
| test('passes empty savedPaymentMethods to SDK when customer has no payment method references', async () => { | ||
| mockCustomer.paymentMethodReferences = [] | ||
|
|
||
| renderWithCheckoutContext( | ||
| <SFPaymentsSheet | ||
| ref={React.createRef()} | ||
| onCreateOrder={mockOnCreateOrder} | ||
| onError={mockOnError} | ||
| /> | ||
| ) | ||
|
|
||
| await waitFor(() => { | ||
| expect(mockCheckout).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| const checkoutCall = mockCheckout.mock.calls[0] | ||
| const config = checkoutCall[2] | ||
|
|
||
| expect(config.options.savedPaymentMethods).toEqual([]) | ||
| }) | ||
|
|
||
| test('passes empty savedPaymentMethods to SDK when paymentMethodReferences is null', async () => { | ||
| mockCustomer.paymentMethodReferences = null | ||
|
|
||
| renderWithCheckoutContext( | ||
| <SFPaymentsSheet | ||
| ref={React.createRef()} | ||
| onCreateOrder={mockOnCreateOrder} | ||
| onError={mockOnError} | ||
| /> | ||
| ) | ||
|
|
||
| await waitFor(() => { | ||
| expect(mockCheckout).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| const checkoutCall = mockCheckout.mock.calls[0] | ||
| const config = checkoutCall[2] | ||
|
|
||
| expect(config.options.savedPaymentMethods).toEqual([]) | ||
| }) | ||
|
|
||
| test('passes empty savedPaymentMethods to SDK when paymentMethodReferences is undefined', async () => { | ||
| mockCustomer.paymentMethodReferences = undefined | ||
|
|
||
| renderWithCheckoutContext( | ||
| <SFPaymentsSheet | ||
| ref={React.createRef()} | ||
| onCreateOrder={mockOnCreateOrder} | ||
| onError={mockOnError} | ||
| /> | ||
| ) | ||
|
|
||
| await waitFor(() => { | ||
| expect(mockCheckout).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| const checkoutCall = mockCheckout.mock.calls[0] | ||
| const config = checkoutCall[2] | ||
|
|
||
| expect(config.options.savedPaymentMethods).toEqual([]) | ||
| }) | ||
|
|
||
| test('passes empty savedPaymentMethods to SDK when paymentMethodSetAccounts is missing', async () => { | ||
| mockCustomer.paymentMethodReferences = [ | ||
| { | ||
| id: 'pm_123', | ||
| accountId: 'stripe-account-1', | ||
| type: 'card', | ||
| brand: 'visa', | ||
| last4: '4242' | ||
| } | ||
| ] | ||
|
|
||
| jest.spyOn( | ||
| // eslint-disable-next-line @typescript-eslint/no-var-requires | ||
| require('@salesforce/commerce-sdk-react'), | ||
| 'usePaymentConfiguration' | ||
| ).mockReturnValue({ | ||
| data: { | ||
| paymentMethods: [ | ||
| { | ||
| id: 'card', | ||
| name: 'Card', | ||
| paymentMethodType: 'card', | ||
| accountId: 'stripe-account-1' | ||
| } | ||
| ], | ||
| paymentMethodSetAccounts: null | ||
| } | ||
| }) | ||
|
|
||
| renderWithCheckoutContext( | ||
| <SFPaymentsSheet | ||
| ref={React.createRef()} | ||
| onCreateOrder={mockOnCreateOrder} | ||
| onError={mockOnError} | ||
| /> | ||
| ) | ||
|
|
||
| await waitFor(() => { | ||
| expect(mockCheckout).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| const checkoutCall = mockCheckout.mock.calls[0] | ||
| const config = checkoutCall[2] | ||
|
|
||
| expect(config.options.savedPaymentMethods).toEqual([]) | ||
| }) | ||
| }) | ||
|
|
||
| describe('lifecycle', () => { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.