From 90e57bca11b38184f3805fdd91326c5da0a92d41 Mon Sep 17 00:00:00 2001 From: Tarun Vijwani Date: Mon, 22 May 2023 12:48:12 +0400 Subject: [PATCH] Add unit test cases for Min and Max price for shipping methods (https://github.com/woocommerce/woocommerce-blocks/pull/9336) * Add unit test cases for Min and Max price for shipping methods * Move Test for Min and Max rates to shipping-rates - Move Test for Min and Max rates to shipping-rates. - Use single shippingRates object throughout the test cases. * Update assets/js/base/utils/test/shipping-rates.ts Co-authored-by: Niels Lange * Update assets/js/base/utils/test/shipping-rates.ts Co-authored-by: Niels Lange * Add helper function to generate shipping rates * Add a test for empty shipping rates * Remove redundant attribute values from generateRate function --------- Co-authored-by: Niels Lange --- .../js/base/utils/test/shipping-rates.ts | 165 +++++++++++------- 1 file changed, 100 insertions(+), 65 deletions(-) diff --git a/plugins/woocommerce-blocks/assets/js/base/utils/test/shipping-rates.ts b/plugins/woocommerce-blocks/assets/js/base/utils/test/shipping-rates.ts index 6121834f6aa..ac66c1c7afe 100644 --- a/plugins/woocommerce-blocks/assets/js/base/utils/test/shipping-rates.ts +++ b/plugins/woocommerce-blocks/assets/js/base/utils/test/shipping-rates.ts @@ -5,9 +5,20 @@ import { hasCollectableRate, isPackageRateCollectable, } from '@woocommerce/base-utils'; -import { CartShippingRate } from '@woocommerce/type-defs/cart'; +import { + CartShippingRate, + CartShippingPackageShippingRate, +} from '@woocommerce/type-defs/cart'; import * as blockSettings from '@woocommerce/block-settings'; +/** + * Internal dependencies + */ +import { + getLocalPickupPrices, + getShippingPrices, +} from '../../../blocks/checkout/inner-blocks/checkout-shipping-method-block/shared/helpers'; + jest.mock( '@woocommerce/settings', () => { return { __esModule: true, @@ -27,76 +38,86 @@ jest.mock( '@woocommerce/block-settings', () => ( { ...jest.requireActual( '@woocommerce/block-settings' ), LOCAL_PICKUP_ENABLED: true, } ) ); -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 ); + +// Returns a rate object with the given values +const generateRate = ( + rateId: string, + name: string, + price: string, + instanceID: number, + selected = false +): typeof testPackage.shipping_rates[ 0 ] => { + 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 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 ); + 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 ), + } ); + } ); + 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, + } ); } ); } ); 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 ); @@ -104,4 +125,18 @@ describe( 'isPackageRateCollectable', () => { isPackageRateCollectable( testPackage.shipping_rates[ 1 ] ) ).toBe( true ); } ); + 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 ); + } ); + } ); } );