2023-02-03 16:00:24 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
hasCollectableRate,
|
|
|
|
isPackageRateCollectable,
|
|
|
|
} from '@woocommerce/base-utils';
|
2023-05-22 08:48:12 +00:00
|
|
|
import {
|
|
|
|
CartShippingRate,
|
|
|
|
CartShippingPackageShippingRate,
|
|
|
|
} from '@woocommerce/type-defs/cart';
|
2023-03-13 03:45:48 +00:00
|
|
|
import * as blockSettings from '@woocommerce/block-settings';
|
2023-02-03 16:00:24 +00:00
|
|
|
|
2023-05-22 08:48:12 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
getLocalPickupPrices,
|
|
|
|
getShippingPrices,
|
|
|
|
} from '../../../blocks/checkout/inner-blocks/checkout-shipping-method-block/shared/helpers';
|
|
|
|
|
2023-02-03 16:00:24 +00:00
|
|
|
jest.mock( '@woocommerce/settings', () => {
|
|
|
|
return {
|
2023-03-13 03:45:48 +00:00
|
|
|
__esModule: true,
|
2023-02-03 16:00:24 +00:00
|
|
|
...jest.requireActual( '@woocommerce/settings' ),
|
2023-03-13 03:45:48 +00:00
|
|
|
getSetting: jest.fn().mockImplementation( ( setting: string ) => {
|
2023-02-03 16:00:24 +00:00
|
|
|
if ( setting === 'collectableMethodIds' ) {
|
|
|
|
return [ 'local_pickup' ];
|
|
|
|
}
|
|
|
|
return jest
|
|
|
|
.requireActual( '@woocommerce/settings' )
|
|
|
|
.getSetting( setting );
|
2023-03-13 03:45:48 +00:00
|
|
|
} ),
|
2023-02-03 16:00:24 +00:00
|
|
|
};
|
|
|
|
} );
|
2023-03-13 03:45:48 +00:00
|
|
|
jest.mock( '@woocommerce/block-settings', () => ( {
|
|
|
|
__esModule: true,
|
|
|
|
...jest.requireActual( '@woocommerce/block-settings' ),
|
|
|
|
LOCAL_PICKUP_ENABLED: true,
|
|
|
|
} ) );
|
2023-05-22 08:48:12 +00:00
|
|
|
|
|
|
|
// Returns a rate object with the given values
|
|
|
|
const generateRate = (
|
|
|
|
rateId: string,
|
|
|
|
name: string,
|
|
|
|
price: string,
|
|
|
|
instanceID: number,
|
|
|
|
selected = false
|
2023-12-12 23:05:20 +00:00
|
|
|
): ( typeof testPackage.shipping_rates )[ 0 ] => {
|
2023-05-22 08:48:12 +00:00
|
|
|
return {
|
|
|
|
rate_id: rateId,
|
|
|
|
name,
|
|
|
|
description: '',
|
|
|
|
delivery_time: '',
|
|
|
|
price,
|
|
|
|
taxes: '0',
|
|
|
|
instance_id: instanceID,
|
|
|
|
method_id: name.toLowerCase().split( ' ' ).join( '_' ),
|
|
|
|
meta_data: [],
|
|
|
|
selected,
|
|
|
|
currency_code: 'USD',
|
|
|
|
currency_symbol: '$',
|
|
|
|
currency_minor_unit: 2,
|
|
|
|
currency_decimal_separator: '.',
|
|
|
|
currency_thousand_separator: ',',
|
|
|
|
currency_prefix: '$',
|
|
|
|
currency_suffix: '',
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// A test package with 5 shipping rates
|
|
|
|
const testPackage: CartShippingRate = {
|
|
|
|
package_id: 0,
|
|
|
|
name: 'Shipping',
|
|
|
|
destination: {
|
|
|
|
address_1: '',
|
|
|
|
address_2: '',
|
|
|
|
city: '',
|
|
|
|
state: '',
|
|
|
|
postcode: '',
|
|
|
|
country: '',
|
|
|
|
},
|
|
|
|
items: [],
|
|
|
|
shipping_rates: [
|
|
|
|
generateRate( 'flat_rate:1', 'Flat rate', '10', 1 ),
|
|
|
|
generateRate( 'local_pickup:1', 'Local pickup', '0', 2 ),
|
|
|
|
generateRate( 'local_pickup:2', 'Local pickup', '10', 3 ),
|
|
|
|
generateRate( 'local_pickup:3', 'Local pickup', '50', 4 ),
|
|
|
|
generateRate( 'flat_rate:2', 'Flat rate', '50', 5 ),
|
|
|
|
],
|
|
|
|
};
|
|
|
|
describe( 'Test Min and Max rates', () => {
|
|
|
|
it( 'returns the lowest and highest rates when local pickup method is used', () => {
|
|
|
|
expect( getLocalPickupPrices( testPackage.shipping_rates ) ).toEqual( {
|
|
|
|
min: generateRate( 'local_pickup:1', 'Local pickup', '0', 2 ),
|
|
|
|
|
|
|
|
max: generateRate( 'local_pickup:3', 'Local pickup', '50', 4 ),
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
it( 'returns the lowest and highest rates when flat rate shipping method is used', () => {
|
|
|
|
expect( getShippingPrices( testPackage.shipping_rates ) ).toEqual( {
|
|
|
|
min: generateRate( 'flat_rate:1', 'Flat rate', '10', 1 ),
|
|
|
|
max: generateRate( 'flat_rate:2', 'Flat rate', '50', 5 ),
|
|
|
|
} );
|
2023-02-03 16:00:24 +00:00
|
|
|
} );
|
2023-05-22 08:48:12 +00:00
|
|
|
it( 'returns undefined as lowest and highest rates when shipping rates are not available', () => {
|
|
|
|
const testEmptyShippingRates: CartShippingPackageShippingRate[] = [];
|
|
|
|
expect( getLocalPickupPrices( testEmptyShippingRates ) ).toEqual( {
|
|
|
|
min: undefined,
|
|
|
|
max: undefined,
|
|
|
|
} );
|
|
|
|
expect( getShippingPrices( testEmptyShippingRates ) ).toEqual( {
|
|
|
|
min: undefined,
|
|
|
|
max: undefined,
|
|
|
|
} );
|
2023-03-13 03:45:48 +00:00
|
|
|
} );
|
2023-02-03 16:00:24 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
describe( 'isPackageRateCollectable', () => {
|
|
|
|
it( 'correctly identifies if a package rate is collectable or not', () => {
|
|
|
|
expect(
|
|
|
|
isPackageRateCollectable( testPackage.shipping_rates[ 0 ] )
|
|
|
|
).toBe( false );
|
|
|
|
expect(
|
|
|
|
isPackageRateCollectable( testPackage.shipping_rates[ 1 ] )
|
|
|
|
).toBe( true );
|
|
|
|
} );
|
2023-05-22 08:48:12 +00:00
|
|
|
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 );
|
|
|
|
} );
|
|
|
|
it( 'returns false for all rates if local pickup is disabled', () => {
|
|
|
|
// Attempt to assign to const or readonly variable error on next line is OK because it is mocked by jest
|
|
|
|
blockSettings.LOCAL_PICKUP_ENABLED = false;
|
|
|
|
const ratesToTest = [ 'flat_rate', 'local_pickup' ];
|
|
|
|
expect( hasCollectableRate( ratesToTest ) ).toBe( false );
|
|
|
|
} );
|
|
|
|
} );
|
2023-02-03 16:00:24 +00:00
|
|
|
} );
|