This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 221
Add LocalPickupSelect
component
#8634
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
be6c747
Add LocalPickupSelect component
opr 0f4b7f8
Add tests for LocalPickupSelect component
opr e123670
Make title optional
opr 10b1dc9
Add packageCount as an option to LocalPickupSelect
opr f412ba8
Revert "Add packageCount as an option to LocalPickupSelect"
opr 7cf874b
Add ackage count to LocalPickupSelect
opr deaa8c3
Add package count and remove unused variable from renderPickupLocation
opr 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
57 changes: 57 additions & 0 deletions
57
assets/js/base/components/cart-checkout/local-pickup-select/index.tsx
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,57 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { RadioControlOption } from '@woocommerce/base-components/radio-control/types'; | ||
import { CartShippingPackageShippingRate } from '@woocommerce/types'; | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import RadioControl from '../../radio-control'; | ||
|
||
interface LocalPickupSelectProps { | ||
title?: string | undefined; | ||
setSelectedOption: ( value: string ) => void; | ||
selectedOption: string; | ||
pickupLocations: CartShippingPackageShippingRate[]; | ||
onSelectRate: ( value: string ) => void; | ||
renderPickupLocation: ( | ||
location: CartShippingPackageShippingRate, | ||
pickupLocationsCount: number | ||
) => RadioControlOption; | ||
packageCount: number; | ||
} | ||
/** | ||
* Local pickup select component, used to render a package title and local pickup options. | ||
*/ | ||
export const LocalPickupSelect = ( { | ||
title, | ||
setSelectedOption, | ||
selectedOption, | ||
pickupLocations, | ||
onSelectRate, | ||
renderPickupLocation, | ||
packageCount, | ||
}: LocalPickupSelectProps ) => { | ||
// Hacky way to check if there are multiple packages, this way is borrowed from `assets/js/base/components/cart-checkout/shipping-rates-control-package/index.tsx` | ||
// We have no built-in way of checking if other extensions have added packages. | ||
const multiplePackages = | ||
document.querySelectorAll( | ||
'.wc-block-components-local-pickup-select .wc-block-components-radio-control' | ||
).length > 1; | ||
return ( | ||
<div className="wc-block-components-local-pickup-select"> | ||
{ multiplePackages && title ? <div>{ title }</div> : false } | ||
<RadioControl | ||
onChange={ ( value ) => { | ||
setSelectedOption( value ); | ||
onSelectRate( value ); | ||
} } | ||
selected={ selectedOption } | ||
options={ pickupLocations.map( ( location ) => | ||
renderPickupLocation( location, packageCount ) | ||
) } | ||
/> | ||
</div> | ||
); | ||
}; | ||
export default LocalPickupSelect; |
121 changes: 121 additions & 0 deletions
121
assets/js/base/components/cart-checkout/local-pickup-select/test/index.tsx
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,121 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { render, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import LocalPickupSelect from '..'; | ||
|
||
describe( 'LocalPickupSelect', () => { | ||
const TestComponent = ( { | ||
selectedOptionOverride = null, | ||
onSelectRateOverride = null, | ||
}: { | ||
selectedOptionOverride?: null | ( ( value: string ) => void ); | ||
onSelectRateOverride?: null | ( ( value: string ) => void ); | ||
} ) => ( | ||
<LocalPickupSelect | ||
title="Package 1" | ||
setSelectedOption={ selectedOptionOverride || jest.fn() } | ||
selectedOption="" | ||
pickupLocations={ [ | ||
{ | ||
rate_id: '1', | ||
currency_code: 'USD', | ||
currency_decimal_separator: '.', | ||
currency_minor_unit: 2, | ||
currency_prefix: '$', | ||
currency_suffix: '', | ||
currency_thousand_separator: ',', | ||
currency_symbol: '$', | ||
name: 'Store 1', | ||
description: 'Store 1 description', | ||
delivery_time: '1 day', | ||
price: '0', | ||
taxes: '0', | ||
instance_id: 1, | ||
method_id: 'test_shipping:0', | ||
meta_data: [], | ||
selected: false, | ||
}, | ||
{ | ||
rate_id: '2', | ||
currency_code: 'USD', | ||
currency_decimal_separator: '.', | ||
currency_minor_unit: 2, | ||
currency_prefix: '$', | ||
currency_suffix: '', | ||
currency_thousand_separator: ',', | ||
currency_symbol: '$', | ||
name: 'Store 2', | ||
description: 'Store 2 description', | ||
delivery_time: '2 days', | ||
price: '0', | ||
taxes: '0', | ||
instance_id: 1, | ||
method_id: 'test_shipping:1', | ||
meta_data: [], | ||
selected: false, | ||
}, | ||
] } | ||
onSelectRate={ onSelectRateOverride || jest.fn() } | ||
packageCount={ 1 } | ||
renderPickupLocation={ ( location ) => { | ||
return { | ||
value: `${ location.rate_id }`, | ||
onChange: jest.fn(), | ||
label: `${ location.name }`, | ||
description: `${ location.description }`, | ||
}; | ||
} } | ||
/> | ||
); | ||
it( 'Does not render the title if only one package is present on the page', () => { | ||
render( <TestComponent /> ); | ||
expect( screen.queryByText( 'Package 1' ) ).not.toBeInTheDocument(); | ||
} ); | ||
it( 'Does render the title if more than one package is present on the page', () => { | ||
const { rerender } = render( | ||
<div className="wc-block-components-local-pickup-select"> | ||
<div className="wc-block-components-radio-control"></div> | ||
</div> | ||
); | ||
// Render twice so our component can check the DOM correctly. | ||
rerender( | ||
<> | ||
<div className="wc-block-components-local-pickup-select"> | ||
<div className="wc-block-components-radio-control"></div> | ||
</div> | ||
<TestComponent /> | ||
</> | ||
); | ||
rerender( | ||
<> | ||
<div className="wc-block-components-local-pickup-select"> | ||
<div className="wc-block-components-radio-control"></div> | ||
</div> | ||
<TestComponent /> | ||
</> | ||
); | ||
|
||
expect( screen.getByText( 'Package 1' ) ).toBeInTheDocument(); | ||
} ); | ||
it( 'Calls the correct functions when changing selected option', () => { | ||
const setSelectedOption = jest.fn(); | ||
const onSelectRate = jest.fn(); | ||
render( | ||
<TestComponent | ||
selectedOptionOverride={ setSelectedOption } | ||
onSelectRateOverride={ onSelectRate } | ||
/> | ||
); | ||
userEvent.click( screen.getByText( 'Store 2' ) ); | ||
expect( setSelectedOption ).toHaveBeenLastCalledWith( '2' ); | ||
expect( onSelectRate ).toHaveBeenLastCalledWith( '2' ); | ||
userEvent.click( screen.getByText( 'Store 1' ) ); | ||
expect( setSelectedOption ).toHaveBeenLastCalledWith( '1' ); | ||
expect( onSelectRate ).toHaveBeenLastCalledWith( '1' ); | ||
} ); | ||
} ); |
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.
We have a TS error here:
Property 'packageCount' is missing