|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Helper function to get the tax destination address based on delivery method. |
| 5 | + * This is the core logic used in useUpdateOrder to determine which address |
| 6 | + * to send for tax calculation. |
| 7 | + * |
| 8 | + * For pickup orders: Always use the pickup location address |
| 9 | + * For shipping orders: Let backend use the saved shipping address (undefined) |
| 10 | + */ |
| 11 | +function getTaxDestinationAddress( |
| 12 | + deliveryMethod: string | undefined, |
| 13 | + pickupLocationId: string | undefined, |
| 14 | + locations: Array<{ id: string; address: Record<string, unknown> }> | undefined |
| 15 | +): Record<string, unknown> | undefined { |
| 16 | + const isPickup = deliveryMethod === 'PICKUP'; |
| 17 | + |
| 18 | + if (isPickup) { |
| 19 | + const pickupLocationAddress = locations?.find( |
| 20 | + loc => loc.id === pickupLocationId |
| 21 | + )?.address; |
| 22 | + return pickupLocationAddress; |
| 23 | + } |
| 24 | + |
| 25 | + // For shipping, return undefined to let backend use saved shipping address |
| 26 | + return undefined; |
| 27 | +} |
| 28 | + |
| 29 | +describe('useUpdateOrder - Tax calculation address logic', () => { |
| 30 | + const mockLocations = [ |
| 31 | + { |
| 32 | + id: 'location-1', |
| 33 | + address: { |
| 34 | + addressLine1: '599 Stegall Dr', |
| 35 | + adminArea1: 'GA', |
| 36 | + adminArea2: 'Jasper', |
| 37 | + countryCode: 'US', |
| 38 | + postalCode: '30143', |
| 39 | + }, |
| 40 | + }, |
| 41 | + { |
| 42 | + id: 'location-2', |
| 43 | + address: { |
| 44 | + addressLine1: '123 Main St', |
| 45 | + adminArea1: 'NY', |
| 46 | + adminArea2: 'New York', |
| 47 | + countryCode: 'US', |
| 48 | + postalCode: '10001', |
| 49 | + }, |
| 50 | + }, |
| 51 | + ]; |
| 52 | + |
| 53 | + describe('Pickup orders', () => { |
| 54 | + it('should return pickup location address for PICKUP delivery method', () => { |
| 55 | + const result = getTaxDestinationAddress( |
| 56 | + 'PICKUP', |
| 57 | + 'location-1', |
| 58 | + mockLocations |
| 59 | + ); |
| 60 | + |
| 61 | + expect(result).toEqual({ |
| 62 | + addressLine1: '599 Stegall Dr', |
| 63 | + adminArea1: 'GA', |
| 64 | + adminArea2: 'Jasper', |
| 65 | + countryCode: 'US', |
| 66 | + postalCode: '30143', |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should return correct location when multiple locations exist', () => { |
| 71 | + const result = getTaxDestinationAddress( |
| 72 | + 'PICKUP', |
| 73 | + 'location-2', |
| 74 | + mockLocations |
| 75 | + ); |
| 76 | + |
| 77 | + expect(result).toEqual({ |
| 78 | + addressLine1: '123 Main St', |
| 79 | + adminArea1: 'NY', |
| 80 | + adminArea2: 'New York', |
| 81 | + countryCode: 'US', |
| 82 | + postalCode: '10001', |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should return undefined if pickup location not found', () => { |
| 87 | + const result = getTaxDestinationAddress( |
| 88 | + 'PICKUP', |
| 89 | + 'non-existent-location', |
| 90 | + mockLocations |
| 91 | + ); |
| 92 | + |
| 93 | + expect(result).toBeUndefined(); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should return undefined if locations array is empty', () => { |
| 97 | + const result = getTaxDestinationAddress('PICKUP', 'location-1', []); |
| 98 | + |
| 99 | + expect(result).toBeUndefined(); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should return undefined if locations is undefined', () => { |
| 103 | + const result = getTaxDestinationAddress( |
| 104 | + 'PICKUP', |
| 105 | + 'location-1', |
| 106 | + undefined |
| 107 | + ); |
| 108 | + |
| 109 | + expect(result).toBeUndefined(); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + describe('Shipping orders', () => { |
| 114 | + it('should return undefined for SHIP delivery method', () => { |
| 115 | + const result = getTaxDestinationAddress( |
| 116 | + 'SHIP', |
| 117 | + 'location-1', |
| 118 | + mockLocations |
| 119 | + ); |
| 120 | + |
| 121 | + expect(result).toBeUndefined(); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should return undefined when delivery method is undefined', () => { |
| 125 | + const result = getTaxDestinationAddress( |
| 126 | + undefined, |
| 127 | + 'location-1', |
| 128 | + mockLocations |
| 129 | + ); |
| 130 | + |
| 131 | + expect(result).toBeUndefined(); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + describe('Edge cases', () => { |
| 136 | + it('should handle pickupLocationId being undefined for pickup', () => { |
| 137 | + const result = getTaxDestinationAddress('PICKUP', undefined, mockLocations); |
| 138 | + |
| 139 | + expect(result).toBeUndefined(); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should handle empty string pickupLocationId for pickup', () => { |
| 143 | + const result = getTaxDestinationAddress('PICKUP', '', mockLocations); |
| 144 | + |
| 145 | + expect(result).toBeUndefined(); |
| 146 | + }); |
| 147 | + }); |
| 148 | +}); |
0 commit comments