-
Notifications
You must be signed in to change notification settings - Fork 68
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
fix: ECE initialization when no shipping rates are provided #10169
Open
frosso
wants to merge
4
commits into
develop
Choose a base branch
from
fix/tokenized-ece-with-no-address-on-initialization
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+238
−38
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1975975
fix: ECE initialization when no shipping rates are provided
frosso 7d2eb28
Merge branch 'develop' into fix/tokenized-ece-with-no-address-on-init…
frosso 1e72c2c
Merge branch 'develop' into fix/tokenized-ece-with-no-address-on-init…
frosso dc87218
Merge branch 'develop' into fix/tokenized-ece-with-no-address-on-init…
frosso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
changelog/fix-tokenized-ece-with-no-address-on-initialization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: patch | ||
Type: fix | ||
|
||
fix: avoid ECE error when no address is provided on initialization |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
client/tokenized-express-checkout/blocks/hooks/__tests__/use-express-checkout.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { useExpressCheckout } from '../use-express-checkout'; | ||
|
||
jest.mock( '@stripe/react-stripe-js', () => ( { | ||
useElements: jest.fn(), | ||
useStripe: jest.fn(), | ||
} ) ); | ||
jest.mock( 'tracks', () => ( { | ||
recordUserEvent: jest.fn(), | ||
} ) ); | ||
|
||
const jQueryMock = ( selector ) => { | ||
if ( typeof selector === 'function' ) { | ||
return selector( jQueryMock ); | ||
} | ||
|
||
return { | ||
on: () => null, | ||
val: () => null, | ||
is: () => null, | ||
remove: () => null, | ||
}; | ||
}; | ||
jQueryMock.blockUI = () => null; | ||
|
||
window.wcpayExpressCheckoutParams = {}; | ||
window.wcpayExpressCheckoutParams.checkout = {}; | ||
|
||
describe( 'useExpressCheckout', () => { | ||
beforeEach( () => { | ||
global.$ = jQueryMock; | ||
global.jQuery = jQueryMock; | ||
} ); | ||
|
||
it( 'should provide no shipping rates when not required on click', () => { | ||
const onClickMock = jest.fn(); | ||
const event = { resolve: jest.fn() }; | ||
const { result } = renderHook( () => | ||
useExpressCheckout( { | ||
billing: { | ||
cartTotalItems: [], | ||
}, | ||
shippingData: { | ||
needsShipping: false, | ||
shippingRates: [], | ||
}, | ||
onClick: onClickMock, | ||
onClose: {}, | ||
setExpressPaymentError: {}, | ||
} ) | ||
); | ||
|
||
expect( onClickMock ).not.toHaveBeenCalled(); | ||
|
||
result.current.onButtonClick( event ); | ||
|
||
expect( event.resolve ).toHaveBeenCalledWith( | ||
expect.objectContaining( { | ||
shippingRates: undefined, | ||
shippingAddressRequired: false, | ||
} ) | ||
); | ||
expect( onClickMock ).toHaveBeenCalled(); | ||
} ); | ||
|
||
it( 'should provide the shipping rates on click', () => { | ||
const event = { resolve: jest.fn() }; | ||
const { result } = renderHook( () => | ||
useExpressCheckout( { | ||
billing: { | ||
cartTotalItems: [], | ||
}, | ||
shippingData: { | ||
needsShipping: true, | ||
shippingRates: [ | ||
{ | ||
shipping_rates: [ | ||
{ | ||
rate_id: '1', | ||
price: '10', | ||
name: 'Fake shipping rate', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
onClick: jest.fn(), | ||
onClose: {}, | ||
setExpressPaymentError: {}, | ||
} ) | ||
); | ||
|
||
result.current.onButtonClick( event ); | ||
|
||
expect( event.resolve ).toHaveBeenCalledWith( | ||
expect.objectContaining( { | ||
shippingRates: expect.arrayContaining( [ | ||
{ | ||
id: '1', | ||
displayName: 'Fake shipping rate', | ||
amount: 10, | ||
}, | ||
] ), | ||
shippingAddressRequired: true, | ||
} ) | ||
); | ||
} ); | ||
|
||
it( 'should provide the shipping rates with fallback on click', () => { | ||
const event = { resolve: jest.fn() }; | ||
const { result } = renderHook( () => | ||
useExpressCheckout( { | ||
billing: { | ||
cartTotalItems: [], | ||
}, | ||
shippingData: { | ||
needsShipping: true, | ||
shippingRates: [], | ||
}, | ||
onClick: jest.fn(), | ||
onClose: {}, | ||
setExpressPaymentError: {}, | ||
} ) | ||
); | ||
|
||
result.current.onButtonClick( event ); | ||
|
||
expect( event.resolve ).toHaveBeenCalledWith( | ||
expect.objectContaining( { | ||
shippingRates: expect.arrayContaining( [ | ||
{ | ||
id: 'pending', | ||
displayName: 'Pending', | ||
amount: 0, | ||
}, | ||
] ), | ||
shippingAddressRequired: true, | ||
} ) | ||
); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,29 +65,18 @@ const fetchNewCartData = async () => { | |
}; | ||
|
||
const getServerSideExpressCheckoutProductData = () => { | ||
const requestShipping = | ||
getExpressCheckoutData( 'product' )?.needs_shipping ?? false; | ||
const displayItems = ( | ||
getExpressCheckoutData( 'product' )?.displayItems ?? [] | ||
).map( ( { label, amount } ) => ( { | ||
name: label, | ||
amount, | ||
} ) ); | ||
const shippingRates = requestShipping | ||
? [ | ||
{ | ||
id: 'pending', | ||
displayName: __( 'Pending', 'woocommerce-payments' ), | ||
amount: 0, | ||
}, | ||
] | ||
: undefined; | ||
Comment on lines
-76
to
-84
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. This "Pending" rate is now consistently added on initialization. Before, it was added only on the product pages. |
||
|
||
return { | ||
total: getExpressCheckoutData( 'product' )?.total.amount, | ||
currency: getExpressCheckoutData( 'product' )?.currency, | ||
requestShipping, | ||
shippingRates, | ||
requestShipping: | ||
getExpressCheckoutData( 'product' )?.needs_shipping ?? false, | ||
requestPhone: | ||
getExpressCheckoutData( 'checkout' )?.needs_payer_phone ?? false, | ||
displayItems, | ||
|
@@ -258,6 +247,22 @@ jQuery( ( $ ) => { | |
} ); | ||
} | ||
|
||
const shippingOptionsWithFallback = | ||
! options.shippingRates || // server-side data on the product page initialization doesn't provide any shipping rates. | ||
options.shippingRates.length === 0 // but it can also happen that there are no rates in the array. | ||
? [ | ||
// fallback for initialization (and initialization _only_), before an address is provided by the ECE. | ||
{ | ||
id: 'pending', | ||
displayName: __( | ||
'Pending', | ||
'woocommerce-payments' | ||
), | ||
amount: 0, | ||
}, | ||
] | ||
: options.shippingRates; | ||
|
||
const clickOptions = { | ||
// `options.displayItems`, `options.requestShipping`, `options.requestPhone`, `options.shippingRates`, | ||
// are all coming from prior of the initialization. | ||
|
@@ -268,7 +273,9 @@ jQuery( ( $ ) => { | |
emailRequired: true, | ||
shippingAddressRequired: options.requestShipping, | ||
phoneNumberRequired: options.requestPhone, | ||
shippingRates: options.shippingRates, | ||
shippingRates: options.requestShipping | ||
? shippingOptionsWithFallback | ||
: undefined, | ||
allowedShippingCountries: getExpressCheckoutData( | ||
'checkout' | ||
).allowed_shipping_countries, | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved above, so that the newly selected address is saved regardless of the error.
This makes the behavior consistent between block-based and shortcode-based checkout.