import { getOrderTotalWeight } from '../features/orders/utils/orderWeight'; import type { Order } from '../types'; function makeOrder( items: any[], weightUnit = 'kg' ): Order { return { items, units: { weight: weightUnit, dimension: 'cm' }, } as unknown as Order; } function makeItem( weight: number | string | null, quantity = 1, name = 'Test Item', productId = 1 ) { return { name, product_id: productId, quantity, dimensions: { weight }, }; } describe( 'getOrderTotalWeight', () => { // ---- Positive tests ---- it( 'calculates total as weight × quantity', () => { const order = makeOrder( [ makeItem( 1.0, 3 ) ] ); expect( getOrderTotalWeight( order ).value ).toBeCloseTo( 3 ); } ); it( 'sums weight across multiple items', () => { const order = makeOrder( [ makeItem( 1.5, 2 ), makeItem( 0.5, 1 ), ] ); expect( getOrderTotalWeight( order ).value ).toBeCloseTo( 3.5 ); } ); it( 'formats total with the order weight unit', () => { const order = makeOrder( [ makeItem( 2, 1 ) ] ); const result = getOrderTotalWeight( order ); expect( result.formatted ).toContain( 'kg' ); expect( result.formatted ).toContain( '2' ); } ); it( 'reports hasNoWeight=false when all items have weight', () => { const order = makeOrder( [ makeItem( 1, 1 ), makeItem( 2, 1 ) ] ); expect( getOrderTotalWeight( order ).hasNoWeight ).toBe( false ); } ); it( 'returns empty missingWeightItems when all items have weight', () => { const order = makeOrder( [ makeItem( 1, 1 ) ] ); expect( getOrderTotalWeight( order ).missingWeightItems ).toHaveLength( 0 ); } ); // ---- Negative tests ---- it( 'reports hasNoWeight=true when all items have no weight', () => { const order = makeOrder( [ makeItem( null ), makeItem( 0 ) ] ); const result = getOrderTotalWeight( order ); expect( result.hasNoWeight ).toBe( true ); expect( result.value ).toBe( 0 ); expect( result.formatted ).toBeNull(); } ); it( 'reports hasPartialMissingWeight when some items are missing weight', () => { const order = makeOrder( [ makeItem( 1.0, 1 ), makeItem( null, 1, 'Missing Weight' ), ] ); const result = getOrderTotalWeight( order ); expect( result.hasPartialMissingWeight ).toBe( true ); expect( result.hasNoWeight ).toBe( false ); } ); it( 'collects names of items missing weight', () => { const order = makeOrder( [ makeItem( null, 1, 'Widget', 10 ), makeItem( null, 1, '', 20 ), ] ); const result = getOrderTotalWeight( order ); expect( result.missingWeightItems ).toContain( 'Widget' ); expect( result.missingWeightItems.some( ( n: string ) => n.includes( '20' ) ) ).toBe( true ); } ); it( 'returns zero and null formatted for an empty items array', () => { const order = makeOrder( [] ); const result = getOrderTotalWeight( order ); expect( result.value ).toBe( 0 ); expect( result.formatted ).toBeNull(); expect( result.hasNoWeight ).toBe( false ); } ); it( 'ignores items with quantity 0', () => { const order = makeOrder( [ makeItem( 5, 0 ) ] ); expect( getOrderTotalWeight( order ).value ).toBe( 0 ); } ); } );