woocommerce/plugins/woocommerce-blocks/assets/js/base/utils/test/shipping-rates.ts

95 lines
2.3 KiB
TypeScript
Raw Normal View History

Allow third party methods to appear in local pickup area (https://github.com/woocommerce/woocommerce-blocks/pull/8256) * Add get_collectible_method_ids function * Add collectibleMethodIds to asset data registry * Check whether method id is pickup_location/in collectibleMethodIds * Allow selectShippingRate to be called without a package id * Prevent collectible methods showing in the main shipping area * Remove unnecessary pluck and add pickup_location to returned array * No longer insert pickup_location in collectibleMethodIds * Allow third party methods to influence low/high collection price * Update useShippingData to consider any collectible method * Add hasSelectedLocalPickup to shipping types * Add dependency to selectShippingRate in useShippingData * Register collectibleMethodIds as a callback This is so the shipping methods get change to register before this is called. Passing a callback to `add` means it won't be called until just before it is output. * Update supports key to 'local_pickup' * Rename utils/shipping-rates to TS * Convert to TS, add isPackageRateCollectible & hasCollectableRate * Add tests for hasCollectableRate and isPackageRateCollectible * Update shipping controller to output only method names * Make PickupLocation shipping method support local_pickup * Set prefersCollection based on rate ID being collectible * Remove need to retrieve settings and use helper function instead * rename hasCollectableRate to hasCollectibleRate * Use array_reduce and update comments in get_local_pickup_method_ids * Switch order of array_unique and array_values * Remove unneeded dependency * Hyphenate local-pickup so it follows the same format as other features * Update use of collectible to collectable * Change supports feature to be hyphenated
2023-02-03 16:00:24 +00:00
/**
* External dependencies
*/
import {
hasCollectableRate,
isPackageRateCollectable,
} from '@woocommerce/base-utils';
import { CartShippingRate } from '@woocommerce/type-defs/cart';
jest.mock( '@woocommerce/settings', () => {
return {
...jest.requireActual( '@woocommerce/settings' ),
getSetting: ( setting: string ) => {
if ( setting === 'collectableMethodIds' ) {
return [ 'local_pickup' ];
}
return jest
.requireActual( '@woocommerce/settings' )
.getSetting( setting );
},
};
} );
describe( 'hasCollectableRate', () => {
it( 'correctly identifies if an array contains a collectable rate', () => {
const ratesToTest = [ 'flat_rate', 'local_pickup' ];
expect( hasCollectableRate( ratesToTest ) ).toBe( true );
const ratesToTest2 = [ 'flat_rate', 'free_shipping' ];
expect( hasCollectableRate( ratesToTest2 ) ).toBe( false );
} );
} );
describe( 'isPackageRateCollectable', () => {
it( 'correctly identifies if a package rate is collectable or not', () => {
const testPackage: CartShippingRate = {
package_id: 0,
name: 'Shipping',
destination: {
address_1: '',
address_2: '',
city: '',
state: '',
postcode: '',
country: '',
},
items: [],
shipping_rates: [
{
rate_id: 'flat_rate:1',
name: 'Flat rate',
description: '',
delivery_time: '',
price: '10',
taxes: '0',
instance_id: 1,
method_id: 'flat_rate',
meta_data: [],
selected: true,
currency_code: 'USD',
currency_symbol: '$',
currency_minor_unit: 2,
currency_decimal_separator: '.',
currency_thousand_separator: ',',
currency_prefix: '$',
currency_suffix: '',
},
{
rate_id: 'local_pickup:2',
name: 'Local pickup',
description: '',
delivery_time: '',
price: '0',
taxes: '0',
instance_id: 2,
method_id: 'local_pickup',
meta_data: [],
selected: false,
currency_code: 'USD',
currency_symbol: '$',
currency_minor_unit: 2,
currency_decimal_separator: '.',
currency_thousand_separator: ',',
currency_prefix: '$',
currency_suffix: '',
},
],
};
expect(
isPackageRateCollectable( testPackage.shipping_rates[ 0 ] )
).toBe( false );
expect(
isPackageRateCollectable( testPackage.shipping_rates[ 1 ] )
).toBe( true );
} );
} );