From 1514c97630066cfc8fe9f06897364f8cad82d33d Mon Sep 17 00:00:00 2001 From: Paulo Henriques Date: Thu, 23 Apr 2026 12:40:53 +0100 Subject: [PATCH 1/4] Fix: Improve Tiers Display Format --- src/tiers/compute-cumulative-value.test.ts | 36 +++++++++++----------- src/tiers/compute-cumulative-value.ts | 8 +++++ src/tiers/utils.test.ts | 10 +++--- src/tiers/utils.ts | 4 +-- 4 files changed, 33 insertions(+), 25 deletions(-) diff --git a/src/tiers/compute-cumulative-value.test.ts b/src/tiers/compute-cumulative-value.test.ts index 3b83a680..d4436ffc 100644 --- a/src/tiers/compute-cumulative-value.test.ts +++ b/src/tiers/compute-cumulative-value.test.ts @@ -55,25 +55,25 @@ const onRequestTiers: PriceTier[] = [ describe('computeCumulativeValue', () => { it.each` tiers | quantityToSelectTier | unit | locale | currency | isTaxInclusive | showOnRequest | showStartsAt | tax | expected - ${baseTiersUnitAmount} | ${1} | ${'kWh'} | ${undefined} | ${'EUR'} | ${true} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '10,00\xa0€', totalWithPrecision: '10,00\xa0€', average: '10,00\xa0€/kWh', subtotal: '8,40\xa0€', subtotalWithPrecision: '8,403361344538\xa0€', subAverage: '8,40\xa0€/kWh', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '10,00\xa0€' }] }} - ${baseTiersUnitAmount} | ${1} | ${'kWh'} | ${undefined} | ${'EUR'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '10,00\xa0€', totalWithPrecision: '10,00\xa0€', average: '10,00\xa0€/kWh', subtotal: '8,40\xa0€', subtotalWithPrecision: '8,403361344538\xa0€', subAverage: '8,40\xa0€/kWh', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '10,00\xa0€' }] }} - ${baseTiersUnitAmount} | ${1} | ${'kWh'} | ${undefined} | ${'EUR'} | ${false} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '11,90\xa0€', totalWithPrecision: '11,90\xa0€', average: '11,90\xa0€/kWh', subtotal: '10,00\xa0€', subtotalWithPrecision: '10,00\xa0€', subAverage: '10,00\xa0€/kWh', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '10,00\xa0€' }] }} - ${baseTiersUnitAmount} | ${1} | ${'kWh'} | ${undefined} | ${'EUR'} | ${false} | ${undefined} | ${undefined} | ${undefined} | ${{ total: '10,00\xa0€', totalWithPrecision: '10,00\xa0€', average: '10,00\xa0€/kWh', subtotal: '10,00\xa0€', subtotalWithPrecision: '10,00\xa0€', subAverage: '10,00\xa0€/kWh', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '10,00\xa0€' }] }} - ${baseTiersUnitAmount} | ${2} | ${'kWh'} | ${undefined} | ${'EUR'} | ${true} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '20,00\xa0€', totalWithPrecision: '20,00\xa0€', average: '10,00\xa0€/kWh', subtotal: '16,81\xa0€', subtotalWithPrecision: '16,806722689076\xa0€', subAverage: '8,40\xa0€/kWh', breakdown: [{ quantityUsed: '2 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '20,00\xa0€' }] }} - ${baseTiersUnitAmount} | ${2} | ${'kWh'} | ${undefined} | ${'EUR'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '20,00\xa0€', totalWithPrecision: '20,00\xa0€', average: '10,00\xa0€/kWh', subtotal: '16,81\xa0€', subtotalWithPrecision: '16,806722689076\xa0€', subAverage: '8,40\xa0€/kWh', breakdown: [{ quantityUsed: '2 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '20,00\xa0€' }] }} - ${baseTiersUnitAmount} | ${2} | ${'kWh'} | ${undefined} | ${'EUR'} | ${false} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '23,80\xa0€', totalWithPrecision: '23,80\xa0€', average: '11,90\xa0€/kWh', subtotal: '20,00\xa0€', subtotalWithPrecision: '20,00\xa0€', subAverage: '10,00\xa0€/kWh', breakdown: [{ quantityUsed: '2 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '20,00\xa0€' }] }} - ${baseTiersUnitAmount} | ${2} | ${'kWh'} | ${undefined} | ${'EUR'} | ${false} | ${undefined} | ${undefined} | ${undefined} | ${{ total: '20,00\xa0€', totalWithPrecision: '20,00\xa0€', average: '10,00\xa0€/kWh', subtotal: '20,00\xa0€', subtotalWithPrecision: '20,00\xa0€', subAverage: '10,00\xa0€/kWh', breakdown: [{ quantityUsed: '2 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '20,00\xa0€' }] }} - ${baseTiersUnitAmount} | ${5} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '€50.00', totalWithPrecision: '€50.00', average: '€10.00/kWh', subAverage: '€8.40/kWh', subtotal: '€42.02', subtotalWithPrecision: '€42.016806722689', breakdown: [{ quantityUsed: '5 kWh', tierAmountDecimal: '€10.00/kWh', totalAmountDecimal: '€50.00' }] }} - ${baseTiersUnitAmount} | ${15} | ${'banana'} | ${'en'} | ${'EUR'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '€145.00', totalWithPrecision: '€145.00', average: '€9.67/banana', subAverage: '€8.12/banana', subtotal: '€121.85', subtotalWithPrecision: '€121.848739495798', breakdown: [{ quantityUsed: '10 banana', tierAmountDecimal: '€10.00/banana', totalAmountDecimal: '€100.00' }, { quantityUsed: '5 banana', tierAmountDecimal: '€9.00/banana', totalAmountDecimal: '€45.00' }] }} - ${baseTiersUnitAmount} | ${30} | ${'kWh'} | ${'en'} | ${'USD'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '$270.00', totalWithPrecision: '$270.00', average: '$9.00/kWh', subAverage: '$7.56/kWh', subtotal: '$226.89', subtotalWithPrecision: '$226.890756302521', breakdown: [{ quantityUsed: '10 kWh', tierAmountDecimal: '$10.00/kWh', totalAmountDecimal: '$100.00' }, { quantityUsed: '10 kWh', tierAmountDecimal: '$9.00/kWh', totalAmountDecimal: '$90.00' }, { quantityUsed: '10 kWh', tierAmountDecimal: '$8.00/kWh', totalAmountDecimal: '$80.00' }] }} - ${baseTiersUnitAmount} | ${0} | ${'kWh'} | ${'en'} | ${'USD'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '$0.00', totalWithPrecision: '$0.00', average: '$0.00/kWh', subAverage: '$0.00/kWh', subtotal: '$0.00', subtotalWithPrecision: '$0.00', breakdown: [{ quantityUsed: '0 kWh', tierAmountDecimal: '$10.00/kWh', totalAmountDecimal: '$0.00' }] }} - ${subunitTiers} | ${5} | ${'kWh'} | ${'en'} | ${'USD'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '5.00 cents', totalWithPrecision: '5.00 cents', average: '1.00 cent/kWh', subAverage: '0.84 cents/kWh', subtotal: '4.20 cents', subtotalWithPrecision: '4.201681 cents', breakdown: [{ quantityUsed: '5 kWh', tierAmountDecimal: '1.00 cent/kWh', totalAmountDecimal: '5.00 cents' }] }} - ${zeroTiers} | ${1} | ${'kWh'} | ${'en'} | ${'USD'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '$0.00', totalWithPrecision: '$0.00', average: '$0.00/kWh', subAverage: '$0.00/kWh', subtotal: '$0.00', subtotalWithPrecision: '$0.00', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '$0.00/kWh', totalAmountDecimal: '$0.00' }] }} - ${onRequestTiers} | ${5} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${true} | ${undefined} | ${{ rate: 19 }} | ${{ total: '€50.00', totalWithPrecision: '€50.00', average: '€10.00/kWh', subAverage: '€8.40/kWh', subtotal: '€42.02', subtotalWithPrecision: '€42.016806722689', breakdown: [{ quantityUsed: '5 kWh', tierAmountDecimal: '€10.00/kWh', totalAmountDecimal: '€50.00' }] }} + ${baseTiersUnitAmount} | ${1} | ${'kWh'} | ${undefined} | ${'EUR'} | ${true} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '10,00\xa0€', totalWithPrecision: '10,00\xa0€', average: '10,00\xa0€/kWh', averageWithPrecision: '10,00\xa0€/kWh', subtotal: '8,40\xa0€', subtotalWithPrecision: '8,403361344538\xa0€', subAverage: '8,40\xa0€/kWh', subAverageWithPrecision: '8,403361344538\xa0€/kWh', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '10,00\xa0€' }] }} + ${baseTiersUnitAmount} | ${1} | ${'kWh'} | ${undefined} | ${'EUR'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '10,00\xa0€', totalWithPrecision: '10,00\xa0€', average: '10,00\xa0€/kWh', averageWithPrecision: '10,00\xa0€/kWh', subtotal: '8,40\xa0€', subtotalWithPrecision: '8,403361344538\xa0€', subAverage: '8,40\xa0€/kWh', subAverageWithPrecision: '8,403361344538\xa0€/kWh', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '10,00\xa0€' }] }} + ${baseTiersUnitAmount} | ${1} | ${'kWh'} | ${undefined} | ${'EUR'} | ${false} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '11,90\xa0€', totalWithPrecision: '11,90\xa0€', average: '11,90\xa0€/kWh', averageWithPrecision: '11,90\xa0€/kWh', subtotal: '10,00\xa0€', subtotalWithPrecision: '10,00\xa0€', subAverage: '10,00\xa0€/kWh', subAverageWithPrecision: '10,00\xa0€/kWh', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '10,00\xa0€' }] }} + ${baseTiersUnitAmount} | ${1} | ${'kWh'} | ${undefined} | ${'EUR'} | ${false} | ${undefined} | ${undefined} | ${undefined} | ${{ total: '10,00\xa0€', totalWithPrecision: '10,00\xa0€', average: '10,00\xa0€/kWh', averageWithPrecision: '10,00\xa0€/kWh', subtotal: '10,00\xa0€', subtotalWithPrecision: '10,00\xa0€', subAverage: '10,00\xa0€/kWh', subAverageWithPrecision: '10,00\xa0€/kWh', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '10,00\xa0€' }] }} + ${baseTiersUnitAmount} | ${2} | ${'kWh'} | ${undefined} | ${'EUR'} | ${true} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '20,00\xa0€', totalWithPrecision: '20,00\xa0€', average: '10,00\xa0€/kWh', averageWithPrecision: '10,00\xa0€/kWh', subtotal: '16,81\xa0€', subtotalWithPrecision: '16,806722689076\xa0€', subAverage: '8,40\xa0€/kWh', subAverageWithPrecision: '8,403361344538\xa0€/kWh', breakdown: [{ quantityUsed: '2 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '20,00\xa0€' }] }} + ${baseTiersUnitAmount} | ${2} | ${'kWh'} | ${undefined} | ${'EUR'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '20,00\xa0€', totalWithPrecision: '20,00\xa0€', average: '10,00\xa0€/kWh', averageWithPrecision: '10,00\xa0€/kWh', subtotal: '16,81\xa0€', subtotalWithPrecision: '16,806722689076\xa0€', subAverage: '8,40\xa0€/kWh', subAverageWithPrecision: '8,403361344538\xa0€/kWh', breakdown: [{ quantityUsed: '2 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '20,00\xa0€' }] }} + ${baseTiersUnitAmount} | ${2} | ${'kWh'} | ${undefined} | ${'EUR'} | ${false} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '23,80\xa0€', totalWithPrecision: '23,80\xa0€', average: '11,90\xa0€/kWh', averageWithPrecision: '11,90\xa0€/kWh', subtotal: '20,00\xa0€', subtotalWithPrecision: '20,00\xa0€', subAverage: '10,00\xa0€/kWh', subAverageWithPrecision: '10,00\xa0€/kWh', breakdown: [{ quantityUsed: '2 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '20,00\xa0€' }] }} + ${baseTiersUnitAmount} | ${2} | ${'kWh'} | ${undefined} | ${'EUR'} | ${false} | ${undefined} | ${undefined} | ${undefined} | ${{ total: '20,00\xa0€', totalWithPrecision: '20,00\xa0€', average: '10,00\xa0€/kWh', averageWithPrecision: '10,00\xa0€/kWh', subtotal: '20,00\xa0€', subtotalWithPrecision: '20,00\xa0€', subAverage: '10,00\xa0€/kWh', subAverageWithPrecision: '10,00\xa0€/kWh', breakdown: [{ quantityUsed: '2 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '20,00\xa0€' }] }} + ${baseTiersUnitAmount} | ${5} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '€50.00', totalWithPrecision: '€50.00', average: '€10.00/kWh', averageWithPrecision: '€10.00/kWh', subAverage: '€8.40/kWh', subAverageWithPrecision: '€8.403361344538/kWh', subtotal: '€42.02', subtotalWithPrecision: '€42.016806722689', breakdown: [{ quantityUsed: '5 kWh', tierAmountDecimal: '€10.00/kWh', totalAmountDecimal: '€50.00' }] }} + ${baseTiersUnitAmount} | ${15} | ${'banana'} | ${'en'} | ${'EUR'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '€145.00', totalWithPrecision: '€145.00', average: '€9.67/banana', averageWithPrecision: '€9.666666666667/banana', subAverage: '€8.12/banana', subAverageWithPrecision: '€8.12324929972/banana', subtotal: '€121.85', subtotalWithPrecision: '€121.848739495798', breakdown: [{ quantityUsed: '10 banana', tierAmountDecimal: '€10.00/banana', totalAmountDecimal: '€100.00' }, { quantityUsed: '5 banana', tierAmountDecimal: '€9.00/banana', totalAmountDecimal: '€45.00' }] }} + ${baseTiersUnitAmount} | ${30} | ${'kWh'} | ${'en'} | ${'USD'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '$270.00', totalWithPrecision: '$270.00', average: '$9.00/kWh', averageWithPrecision: '$9.00/kWh', subAverage: '$7.56/kWh', subAverageWithPrecision: '$7.563025210084/kWh', subtotal: '$226.89', subtotalWithPrecision: '$226.890756302521', breakdown: [{ quantityUsed: '10 kWh', tierAmountDecimal: '$10.00/kWh', totalAmountDecimal: '$100.00' }, { quantityUsed: '10 kWh', tierAmountDecimal: '$9.00/kWh', totalAmountDecimal: '$90.00' }, { quantityUsed: '10 kWh', tierAmountDecimal: '$8.00/kWh', totalAmountDecimal: '$80.00' }] }} + ${baseTiersUnitAmount} | ${0} | ${'kWh'} | ${'en'} | ${'USD'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '$0.00', totalWithPrecision: '$0.00', average: '$0.00/kWh', averageWithPrecision: '$0.00/kWh', subAverage: '$0.00/kWh', subAverageWithPrecision: '$0.00/kWh', subtotal: '$0.00', subtotalWithPrecision: '$0.00', breakdown: [{ quantityUsed: '0 kWh', tierAmountDecimal: '$10.00/kWh', totalAmountDecimal: '$0.00' }] }} + ${subunitTiers} | ${5} | ${'kWh'} | ${'en'} | ${'USD'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '5.00 cents', totalWithPrecision: '5.00 cents', average: '1.00 cent/kWh', averageWithPrecision: '1.00 cent/kWh', subAverage: '0.84 cents/kWh', subAverageWithPrecision: '0.840336 cents/kWh', subtotal: '4.20 cents', subtotalWithPrecision: '4.201681 cents', breakdown: [{ quantityUsed: '5 kWh', tierAmountDecimal: '1.00 cent/kWh', totalAmountDecimal: '5.00 cents' }] }} + ${zeroTiers} | ${1} | ${'kWh'} | ${'en'} | ${'USD'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '$0.00', totalWithPrecision: '$0.00', average: '$0.00/kWh', averageWithPrecision: '$0.00/kWh', subAverage: '$0.00/kWh', subAverageWithPrecision: '$0.00/kWh', subtotal: '$0.00', subtotalWithPrecision: '$0.00', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '$0.00/kWh', totalAmountDecimal: '$0.00' }] }} + ${onRequestTiers} | ${5} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${true} | ${undefined} | ${{ rate: 19 }} | ${{ total: '€50.00', totalWithPrecision: '€50.00', average: '€10.00/kWh', averageWithPrecision: '€10.00/kWh', subAverage: '€8.40/kWh', subAverageWithPrecision: '€8.403361344538/kWh', subtotal: '€42.02', subtotalWithPrecision: '€42.016806722689', breakdown: [{ quantityUsed: '5 kWh', tierAmountDecimal: '€10.00/kWh', totalAmountDecimal: '€50.00' }] }} ${onRequestTiers} | ${15} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${true} | ${undefined} | ${{ rate: 19 }} | ${'Price on request'} - ${onRequestTiers} | ${15} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${false} | ${undefined} | ${{ rate: 19 }} | ${{ total: '€145.00', totalWithPrecision: '€145.00', average: '€9.67/kWh', subAverage: '€8.12/kWh', subtotal: '€121.85', subtotalWithPrecision: '€121.848739495798', breakdown: [{ quantityUsed: '10 kWh', tierAmountDecimal: '€10.00/kWh', totalAmountDecimal: '€100.00' }, { quantityUsed: '5 kWh', tierAmountDecimal: '€9.00/kWh', totalAmountDecimal: '€45.00' }] }} - ${decimalTiers} | ${7} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${false} | ${undefined} | ${{ rate: 19 }} | ${{ total: '38.59 cents', totalWithPrecision: '38.5915 cents', average: '5.51 cents/kWh', subAverage: '4.63 cents/kWh', subtotal: '32.43 cents', subtotalWithPrecision: '32.429832 cents', breakdown: [{ quantityUsed: '5 kWh', tierAmountDecimal: '5.5451 cents/kWh', totalAmountDecimal: '27.7255 cents' }, { quantityUsed: '2 kWh', tierAmountDecimal: '5.433 cents/kWh', totalAmountDecimal: '10.866 cents' }] }} - ${decimalTiers} | ${7} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${false} | ${true} | ${{ rate: 19 }} | ${{ total: 'Starts at 38.59 cents', totalWithPrecision: 'Starts at 38.5915 cents', average: '5.51 cents/kWh', subAverage: '4.63 cents/kWh', subtotal: 'Starts at 32.43 cents', subtotalWithPrecision: 'Starts at 32.429832 cents', breakdown: [{ quantityUsed: '5 kWh', tierAmountDecimal: '5.5451 cents/kWh', totalAmountDecimal: '27.7255 cents' }, { quantityUsed: '2 kWh', tierAmountDecimal: '5.433 cents/kWh', totalAmountDecimal: '10.866 cents' }] }} + ${onRequestTiers} | ${15} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${false} | ${undefined} | ${{ rate: 19 }} | ${{ total: '€145.00', totalWithPrecision: '€145.00', average: '€9.67/kWh', averageWithPrecision: '€9.666666666667/kWh', subAverage: '€8.12/kWh', subAverageWithPrecision: '€8.12324929972/kWh', subtotal: '€121.85', subtotalWithPrecision: '€121.848739495798', breakdown: [{ quantityUsed: '10 kWh', tierAmountDecimal: '€10.00/kWh', totalAmountDecimal: '€100.00' }, { quantityUsed: '5 kWh', tierAmountDecimal: '€9.00/kWh', totalAmountDecimal: '€45.00' }] }} + ${decimalTiers} | ${7} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${false} | ${undefined} | ${{ rate: 19 }} | ${{ total: '38.59 cents', totalWithPrecision: '38.5915 cents', average: '5.51 cents/kWh', averageWithPrecision: '5.513071 cents/kWh', subAverage: '4.63 cents/kWh', subAverageWithPrecision: '4.632833 cents/kWh', subtotal: '32.43 cents', subtotalWithPrecision: '32.429832 cents', breakdown: [{ quantityUsed: '5 kWh', tierAmountDecimal: '5.5451 cents/kWh', totalAmountDecimal: '27.7255 cents' }, { quantityUsed: '2 kWh', tierAmountDecimal: '5.433 cents/kWh', totalAmountDecimal: '10.866 cents' }] }} + ${decimalTiers} | ${7} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${false} | ${true} | ${{ rate: 19 }} | ${{ total: 'Starts at 38.59 cents', totalWithPrecision: 'Starts at 38.5915 cents', average: '5.51 cents/kWh', averageWithPrecision: '5.513071 cents/kWh', subAverage: '4.63 cents/kWh', subAverageWithPrecision: '4.632833 cents/kWh', subtotal: 'Starts at 32.43 cents', subtotalWithPrecision: 'Starts at 32.429832 cents', breakdown: [{ quantityUsed: '5 kWh', tierAmountDecimal: '5.5451 cents/kWh', totalAmountDecimal: '27.7255 cents' }, { quantityUsed: '2 kWh', tierAmountDecimal: '5.433 cents/kWh', totalAmountDecimal: '10.866 cents' }] }} `( 'should compute cumulative value correctly when quantityToSelectTier=$quantityToSelectTier, isTaxInclusive=$isTaxInclusive and tax=$tax', ({ diff --git a/src/tiers/compute-cumulative-value.ts b/src/tiers/compute-cumulative-value.ts index f852041e..eb0b2ad2 100644 --- a/src/tiers/compute-cumulative-value.ts +++ b/src/tiers/compute-cumulative-value.ts @@ -148,6 +148,10 @@ export const computeCumulativeValue = ( precision: 2, useRealPrecision: false, })}${formattedUnit ? `/${formattedUnit}` : ''}`, + averageWithPrecision: `${formatAmountFromString({ + decimalAmount: addSeparatorToDineroString(average), + ...formatOptions, + })}${formattedUnit ? `/${formattedUnit}` : ''}`, subtotal: (startsAt ? `${startsAt} ` : '') + formatAmountFromString({ @@ -168,6 +172,10 @@ export const computeCumulativeValue = ( precision: 2, useRealPrecision: false, })}${formattedUnit ? `/${formattedUnit}` : ''}`, + subAverageWithPrecision: `${formatAmountFromString({ + decimalAmount: addSeparatorToDineroString(subAverage!), + ...formatOptions, + })}${formattedUnit ? `/${formattedUnit}` : ''}`, breakdown, }; }; diff --git a/src/tiers/utils.test.ts b/src/tiers/utils.test.ts index 3c5ddf8d..e2cb38ca 100644 --- a/src/tiers/utils.test.ts +++ b/src/tiers/utils.test.ts @@ -204,7 +204,7 @@ describe('getTierDescription', () => { ${PricingModel.tieredVolume} | ${tierWithUnitAmount} | ${'kWh'} | ${'de'} | ${'EUR'} | ${mockedTranslationFn} | ${true} | ${false} | ${undefined} | ${undefined} | ${'Starts at 10,00 €/kWh'} ${PricingModel.tieredVolume} | ${tierWithUnitAmount} | ${'kWh'} | ${'en'} | ${'USD'} | ${mockedTranslationFn} | ${true} | ${false} | ${undefined} | ${undefined} | ${'Starts at $10.00/kWh'} ${PricingModel.tieredVolume} | ${tierWithUnitAmount} | ${'kWh'} | ${'en'} | ${'EUR'} | ${mockedTranslationFn} | ${true} | ${true} | ${undefined} | ${undefined} | ${'Starts at €10.00/kWh'} - ${PricingModel.tieredVolume} | ${tierWithSubunitAmount} | ${'kWh'} | ${'en'} | ${'EUR'} | ${mockedTranslationFn} | ${true} | ${true} | ${undefined} | ${undefined} | ${'Starts at 5.12 cents/kWh'} + ${PricingModel.tieredVolume} | ${tierWithSubunitAmount} | ${'kWh'} | ${'en'} | ${'EUR'} | ${mockedTranslationFn} | ${true} | ${true} | ${undefined} | ${undefined} | ${'Starts at 5.123412 cents/kWh'} ${PricingModel.tieredVolume} | ${tierWithSubunitAmount} | ${'kWh'} | ${'en'} | ${'EUR'} | ${mockedTranslationFn} | ${true} | ${true} | ${undefined} | ${4} | ${'Starts at 5.1234 cents/kWh'} ${PricingModel.tieredVolume} | ${tierWithUnitAmount} | ${undefined} | ${'en'} | ${'EUR'} | ${mockedTranslationFn} | ${true} | ${false} | ${undefined} | ${undefined} | ${'Starts at €10.00'} ${PricingModel.tieredVolume} | ${tierWithUnitAmount} | ${'unit'} | ${'en'} | ${'EUR'} | ${mockedTranslationFn} | ${true} | ${false} | ${undefined} | ${undefined} | ${'Starts at €10.00'} @@ -261,10 +261,10 @@ describe('getTierDescription', () => { it.each` pricingModel | tier | unit | locale | currency | t | showStartsAt | enableSubunitDisplay | shouldDisplayOnRequest | tax | expected - ${PricingModel.tieredGraduated} | ${tierWithUnitAmount} | ${'kWh'} | ${'en'} | ${'EUR'} | ${mockedTranslationFn} | ${true} | ${false} | ${undefined} | ${{ isInclusive: true, rate: 10 }} | ${'Starts at €9.09/kWh'} - ${PricingModel.tieredVolume} | ${tierWithUnitAmount} | ${'kWh'} | ${'en'} | ${'EUR'} | ${mockedTranslationFn} | ${true} | ${false} | ${undefined} | ${{ isInclusive: true, rate: 10 }} | ${'Starts at €9.09/kWh'} - ${PricingModel.tieredVolume} | ${tierWithSubunitAmount} | ${'kWh'} | ${'en'} | ${'EUR'} | ${mockedTranslationFn} | ${true} | ${true} | ${undefined} | ${{ isInclusive: true, rate: 10 }} | ${'Starts at 4.66 cents/kWh'} - ${PricingModel.tieredFlatFee} | ${tierWithFlatFeeAmount} | ${'kWh'} | ${'en'} | ${'EUR'} | ${mockedTranslationFn} | ${true} | ${false} | ${undefined} | ${{ isInclusive: true, rate: 10 }} | ${'Starts at €9.09'} + ${PricingModel.tieredGraduated} | ${tierWithUnitAmount} | ${'kWh'} | ${'en'} | ${'EUR'} | ${mockedTranslationFn} | ${true} | ${false} | ${undefined} | ${{ isInclusive: true, rate: 10 }} | ${'Starts at €9.090909090909/kWh'} + ${PricingModel.tieredVolume} | ${tierWithUnitAmount} | ${'kWh'} | ${'en'} | ${'EUR'} | ${mockedTranslationFn} | ${true} | ${false} | ${undefined} | ${{ isInclusive: true, rate: 10 }} | ${'Starts at €9.090909090909/kWh'} + ${PricingModel.tieredVolume} | ${tierWithSubunitAmount} | ${'kWh'} | ${'en'} | ${'EUR'} | ${mockedTranslationFn} | ${true} | ${true} | ${undefined} | ${{ isInclusive: true, rate: 10 }} | ${'Starts at 4.657647 cents/kWh'} + ${PricingModel.tieredFlatFee} | ${tierWithFlatFeeAmount} | ${'kWh'} | ${'en'} | ${'EUR'} | ${mockedTranslationFn} | ${true} | ${false} | ${undefined} | ${{ isInclusive: true, rate: 10 }} | ${'Starts at €9.090909090909'} `( 'should return correct net values for the tier, when pricingModel=$pricingModel, unit=$unit, locale=$locale, currency=$currency, showStartsAt=$showStartsAt', ({ diff --git a/src/tiers/utils.ts b/src/tiers/utils.ts index 71d8b3da..b8558d34 100644 --- a/src/tiers/utils.ts +++ b/src/tiers/utils.ts @@ -144,7 +144,7 @@ export function getTierDescription( return; } - const { showStartsAt = true, enableSubunitDisplay = false, precision = 2 } = options; + const { showStartsAt = true, enableSubunitDisplay = false, precision } = options; const showUnitAmount = (pricingModel === PricingModel.tieredGraduated || pricingModel === PricingModel.tieredVolume) && typeof tier.unit_amount === 'number'; @@ -161,7 +161,7 @@ export function getTierDescription( currency, locale, enableSubunitDisplay, - precision, + ...(precision !== undefined ? { precision } : { useRealPrecision: true }), }; const unitAmountDecimal = From 7ac90042bcbf76d97c1c6dd4dc466f26da2b5388 Mon Sep 17 00:00:00 2001 From: Paulo Henriques Date: Thu, 23 Apr 2026 14:05:18 +0100 Subject: [PATCH 2/4] Remove unecessary for the current iteration --- src/tiers/compute-cumulative-value.test.ts | 36 +++++++++++----------- src/tiers/compute-cumulative-value.ts | 4 --- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/tiers/compute-cumulative-value.test.ts b/src/tiers/compute-cumulative-value.test.ts index d4436ffc..607d8e49 100644 --- a/src/tiers/compute-cumulative-value.test.ts +++ b/src/tiers/compute-cumulative-value.test.ts @@ -55,25 +55,25 @@ const onRequestTiers: PriceTier[] = [ describe('computeCumulativeValue', () => { it.each` tiers | quantityToSelectTier | unit | locale | currency | isTaxInclusive | showOnRequest | showStartsAt | tax | expected - ${baseTiersUnitAmount} | ${1} | ${'kWh'} | ${undefined} | ${'EUR'} | ${true} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '10,00\xa0€', totalWithPrecision: '10,00\xa0€', average: '10,00\xa0€/kWh', averageWithPrecision: '10,00\xa0€/kWh', subtotal: '8,40\xa0€', subtotalWithPrecision: '8,403361344538\xa0€', subAverage: '8,40\xa0€/kWh', subAverageWithPrecision: '8,403361344538\xa0€/kWh', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '10,00\xa0€' }] }} - ${baseTiersUnitAmount} | ${1} | ${'kWh'} | ${undefined} | ${'EUR'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '10,00\xa0€', totalWithPrecision: '10,00\xa0€', average: '10,00\xa0€/kWh', averageWithPrecision: '10,00\xa0€/kWh', subtotal: '8,40\xa0€', subtotalWithPrecision: '8,403361344538\xa0€', subAverage: '8,40\xa0€/kWh', subAverageWithPrecision: '8,403361344538\xa0€/kWh', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '10,00\xa0€' }] }} - ${baseTiersUnitAmount} | ${1} | ${'kWh'} | ${undefined} | ${'EUR'} | ${false} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '11,90\xa0€', totalWithPrecision: '11,90\xa0€', average: '11,90\xa0€/kWh', averageWithPrecision: '11,90\xa0€/kWh', subtotal: '10,00\xa0€', subtotalWithPrecision: '10,00\xa0€', subAverage: '10,00\xa0€/kWh', subAverageWithPrecision: '10,00\xa0€/kWh', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '10,00\xa0€' }] }} - ${baseTiersUnitAmount} | ${1} | ${'kWh'} | ${undefined} | ${'EUR'} | ${false} | ${undefined} | ${undefined} | ${undefined} | ${{ total: '10,00\xa0€', totalWithPrecision: '10,00\xa0€', average: '10,00\xa0€/kWh', averageWithPrecision: '10,00\xa0€/kWh', subtotal: '10,00\xa0€', subtotalWithPrecision: '10,00\xa0€', subAverage: '10,00\xa0€/kWh', subAverageWithPrecision: '10,00\xa0€/kWh', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '10,00\xa0€' }] }} - ${baseTiersUnitAmount} | ${2} | ${'kWh'} | ${undefined} | ${'EUR'} | ${true} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '20,00\xa0€', totalWithPrecision: '20,00\xa0€', average: '10,00\xa0€/kWh', averageWithPrecision: '10,00\xa0€/kWh', subtotal: '16,81\xa0€', subtotalWithPrecision: '16,806722689076\xa0€', subAverage: '8,40\xa0€/kWh', subAverageWithPrecision: '8,403361344538\xa0€/kWh', breakdown: [{ quantityUsed: '2 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '20,00\xa0€' }] }} - ${baseTiersUnitAmount} | ${2} | ${'kWh'} | ${undefined} | ${'EUR'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '20,00\xa0€', totalWithPrecision: '20,00\xa0€', average: '10,00\xa0€/kWh', averageWithPrecision: '10,00\xa0€/kWh', subtotal: '16,81\xa0€', subtotalWithPrecision: '16,806722689076\xa0€', subAverage: '8,40\xa0€/kWh', subAverageWithPrecision: '8,403361344538\xa0€/kWh', breakdown: [{ quantityUsed: '2 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '20,00\xa0€' }] }} - ${baseTiersUnitAmount} | ${2} | ${'kWh'} | ${undefined} | ${'EUR'} | ${false} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '23,80\xa0€', totalWithPrecision: '23,80\xa0€', average: '11,90\xa0€/kWh', averageWithPrecision: '11,90\xa0€/kWh', subtotal: '20,00\xa0€', subtotalWithPrecision: '20,00\xa0€', subAverage: '10,00\xa0€/kWh', subAverageWithPrecision: '10,00\xa0€/kWh', breakdown: [{ quantityUsed: '2 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '20,00\xa0€' }] }} - ${baseTiersUnitAmount} | ${2} | ${'kWh'} | ${undefined} | ${'EUR'} | ${false} | ${undefined} | ${undefined} | ${undefined} | ${{ total: '20,00\xa0€', totalWithPrecision: '20,00\xa0€', average: '10,00\xa0€/kWh', averageWithPrecision: '10,00\xa0€/kWh', subtotal: '20,00\xa0€', subtotalWithPrecision: '20,00\xa0€', subAverage: '10,00\xa0€/kWh', subAverageWithPrecision: '10,00\xa0€/kWh', breakdown: [{ quantityUsed: '2 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '20,00\xa0€' }] }} - ${baseTiersUnitAmount} | ${5} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '€50.00', totalWithPrecision: '€50.00', average: '€10.00/kWh', averageWithPrecision: '€10.00/kWh', subAverage: '€8.40/kWh', subAverageWithPrecision: '€8.403361344538/kWh', subtotal: '€42.02', subtotalWithPrecision: '€42.016806722689', breakdown: [{ quantityUsed: '5 kWh', tierAmountDecimal: '€10.00/kWh', totalAmountDecimal: '€50.00' }] }} - ${baseTiersUnitAmount} | ${15} | ${'banana'} | ${'en'} | ${'EUR'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '€145.00', totalWithPrecision: '€145.00', average: '€9.67/banana', averageWithPrecision: '€9.666666666667/banana', subAverage: '€8.12/banana', subAverageWithPrecision: '€8.12324929972/banana', subtotal: '€121.85', subtotalWithPrecision: '€121.848739495798', breakdown: [{ quantityUsed: '10 banana', tierAmountDecimal: '€10.00/banana', totalAmountDecimal: '€100.00' }, { quantityUsed: '5 banana', tierAmountDecimal: '€9.00/banana', totalAmountDecimal: '€45.00' }] }} - ${baseTiersUnitAmount} | ${30} | ${'kWh'} | ${'en'} | ${'USD'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '$270.00', totalWithPrecision: '$270.00', average: '$9.00/kWh', averageWithPrecision: '$9.00/kWh', subAverage: '$7.56/kWh', subAverageWithPrecision: '$7.563025210084/kWh', subtotal: '$226.89', subtotalWithPrecision: '$226.890756302521', breakdown: [{ quantityUsed: '10 kWh', tierAmountDecimal: '$10.00/kWh', totalAmountDecimal: '$100.00' }, { quantityUsed: '10 kWh', tierAmountDecimal: '$9.00/kWh', totalAmountDecimal: '$90.00' }, { quantityUsed: '10 kWh', tierAmountDecimal: '$8.00/kWh', totalAmountDecimal: '$80.00' }] }} - ${baseTiersUnitAmount} | ${0} | ${'kWh'} | ${'en'} | ${'USD'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '$0.00', totalWithPrecision: '$0.00', average: '$0.00/kWh', averageWithPrecision: '$0.00/kWh', subAverage: '$0.00/kWh', subAverageWithPrecision: '$0.00/kWh', subtotal: '$0.00', subtotalWithPrecision: '$0.00', breakdown: [{ quantityUsed: '0 kWh', tierAmountDecimal: '$10.00/kWh', totalAmountDecimal: '$0.00' }] }} - ${subunitTiers} | ${5} | ${'kWh'} | ${'en'} | ${'USD'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '5.00 cents', totalWithPrecision: '5.00 cents', average: '1.00 cent/kWh', averageWithPrecision: '1.00 cent/kWh', subAverage: '0.84 cents/kWh', subAverageWithPrecision: '0.840336 cents/kWh', subtotal: '4.20 cents', subtotalWithPrecision: '4.201681 cents', breakdown: [{ quantityUsed: '5 kWh', tierAmountDecimal: '1.00 cent/kWh', totalAmountDecimal: '5.00 cents' }] }} - ${zeroTiers} | ${1} | ${'kWh'} | ${'en'} | ${'USD'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '$0.00', totalWithPrecision: '$0.00', average: '$0.00/kWh', averageWithPrecision: '$0.00/kWh', subAverage: '$0.00/kWh', subAverageWithPrecision: '$0.00/kWh', subtotal: '$0.00', subtotalWithPrecision: '$0.00', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '$0.00/kWh', totalAmountDecimal: '$0.00' }] }} - ${onRequestTiers} | ${5} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${true} | ${undefined} | ${{ rate: 19 }} | ${{ total: '€50.00', totalWithPrecision: '€50.00', average: '€10.00/kWh', averageWithPrecision: '€10.00/kWh', subAverage: '€8.40/kWh', subAverageWithPrecision: '€8.403361344538/kWh', subtotal: '€42.02', subtotalWithPrecision: '€42.016806722689', breakdown: [{ quantityUsed: '5 kWh', tierAmountDecimal: '€10.00/kWh', totalAmountDecimal: '€50.00' }] }} + ${baseTiersUnitAmount} | ${1} | ${'kWh'} | ${undefined} | ${'EUR'} | ${true} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '10,00\xa0€', totalWithPrecision: '10,00\xa0€', average: '10,00\xa0€/kWh', averageWithPrecision: '10,00\xa0€/kWh', subtotal: '8,40\xa0€', subtotalWithPrecision: '8,403361344538\xa0€', subAverage: '8,40\xa0€/kWh', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '10,00\xa0€' }] }} + ${baseTiersUnitAmount} | ${1} | ${'kWh'} | ${undefined} | ${'EUR'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '10,00\xa0€', totalWithPrecision: '10,00\xa0€', average: '10,00\xa0€/kWh', averageWithPrecision: '10,00\xa0€/kWh', subtotal: '8,40\xa0€', subtotalWithPrecision: '8,403361344538\xa0€', subAverage: '8,40\xa0€/kWh', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '10,00\xa0€' }] }} + ${baseTiersUnitAmount} | ${1} | ${'kWh'} | ${undefined} | ${'EUR'} | ${false} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '11,90\xa0€', totalWithPrecision: '11,90\xa0€', average: '11,90\xa0€/kWh', averageWithPrecision: '11,90\xa0€/kWh', subtotal: '10,00\xa0€', subtotalWithPrecision: '10,00\xa0€', subAverage: '10,00\xa0€/kWh', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '10,00\xa0€' }] }} + ${baseTiersUnitAmount} | ${1} | ${'kWh'} | ${undefined} | ${'EUR'} | ${false} | ${undefined} | ${undefined} | ${undefined} | ${{ total: '10,00\xa0€', totalWithPrecision: '10,00\xa0€', average: '10,00\xa0€/kWh', averageWithPrecision: '10,00\xa0€/kWh', subtotal: '10,00\xa0€', subtotalWithPrecision: '10,00\xa0€', subAverage: '10,00\xa0€/kWh', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '10,00\xa0€' }] }} + ${baseTiersUnitAmount} | ${2} | ${'kWh'} | ${undefined} | ${'EUR'} | ${true} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '20,00\xa0€', totalWithPrecision: '20,00\xa0€', average: '10,00\xa0€/kWh', averageWithPrecision: '10,00\xa0€/kWh', subtotal: '16,81\xa0€', subtotalWithPrecision: '16,806722689076\xa0€', subAverage: '8,40\xa0€/kWh', breakdown: [{ quantityUsed: '2 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '20,00\xa0€' }] }} + ${baseTiersUnitAmount} | ${2} | ${'kWh'} | ${undefined} | ${'EUR'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '20,00\xa0€', totalWithPrecision: '20,00\xa0€', average: '10,00\xa0€/kWh', averageWithPrecision: '10,00\xa0€/kWh', subtotal: '16,81\xa0€', subtotalWithPrecision: '16,806722689076\xa0€', subAverage: '8,40\xa0€/kWh', breakdown: [{ quantityUsed: '2 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '20,00\xa0€' }] }} + ${baseTiersUnitAmount} | ${2} | ${'kWh'} | ${undefined} | ${'EUR'} | ${false} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '23,80\xa0€', totalWithPrecision: '23,80\xa0€', average: '11,90\xa0€/kWh', averageWithPrecision: '11,90\xa0€/kWh', subtotal: '20,00\xa0€', subtotalWithPrecision: '20,00\xa0€', subAverage: '10,00\xa0€/kWh', breakdown: [{ quantityUsed: '2 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '20,00\xa0€' }] }} + ${baseTiersUnitAmount} | ${2} | ${'kWh'} | ${undefined} | ${'EUR'} | ${false} | ${undefined} | ${undefined} | ${undefined} | ${{ total: '20,00\xa0€', totalWithPrecision: '20,00\xa0€', average: '10,00\xa0€/kWh', averageWithPrecision: '10,00\xa0€/kWh', subtotal: '20,00\xa0€', subtotalWithPrecision: '20,00\xa0€', subAverage: '10,00\xa0€/kWh', breakdown: [{ quantityUsed: '2 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '20,00\xa0€' }] }} + ${baseTiersUnitAmount} | ${5} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '€50.00', totalWithPrecision: '€50.00', average: '€10.00/kWh', averageWithPrecision: '€10.00/kWh', subAverage: '€8.40/kWh', subtotal: '€42.02', subtotalWithPrecision: '€42.016806722689', breakdown: [{ quantityUsed: '5 kWh', tierAmountDecimal: '€10.00/kWh', totalAmountDecimal: '€50.00' }] }} + ${baseTiersUnitAmount} | ${15} | ${'banana'} | ${'en'} | ${'EUR'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '€145.00', totalWithPrecision: '€145.00', average: '€9.67/banana', averageWithPrecision: '€9.666666666667/banana', subAverage: '€8.12/banana', subtotal: '€121.85', subtotalWithPrecision: '€121.848739495798', breakdown: [{ quantityUsed: '10 banana', tierAmountDecimal: '€10.00/banana', totalAmountDecimal: '€100.00' }, { quantityUsed: '5 banana', tierAmountDecimal: '€9.00/banana', totalAmountDecimal: '€45.00' }] }} + ${baseTiersUnitAmount} | ${30} | ${'kWh'} | ${'en'} | ${'USD'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '$270.00', totalWithPrecision: '$270.00', average: '$9.00/kWh', averageWithPrecision: '$9.00/kWh', subAverage: '$7.56/kWh', subtotal: '$226.89', subtotalWithPrecision: '$226.890756302521', breakdown: [{ quantityUsed: '10 kWh', tierAmountDecimal: '$10.00/kWh', totalAmountDecimal: '$100.00' }, { quantityUsed: '10 kWh', tierAmountDecimal: '$9.00/kWh', totalAmountDecimal: '$90.00' }, { quantityUsed: '10 kWh', tierAmountDecimal: '$8.00/kWh', totalAmountDecimal: '$80.00' }] }} + ${baseTiersUnitAmount} | ${0} | ${'kWh'} | ${'en'} | ${'USD'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '$0.00', totalWithPrecision: '$0.00', average: '$0.00/kWh', averageWithPrecision: '$0.00/kWh', subAverage: '$0.00/kWh', subtotal: '$0.00', subtotalWithPrecision: '$0.00', breakdown: [{ quantityUsed: '0 kWh', tierAmountDecimal: '$10.00/kWh', totalAmountDecimal: '$0.00' }] }} + ${subunitTiers} | ${5} | ${'kWh'} | ${'en'} | ${'USD'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '5.00 cents', totalWithPrecision: '5.00 cents', average: '1.00 cent/kWh', averageWithPrecision: '1.00 cent/kWh', subAverage: '0.84 cents/kWh', subtotal: '4.20 cents', subtotalWithPrecision: '4.201681 cents', breakdown: [{ quantityUsed: '5 kWh', tierAmountDecimal: '1.00 cent/kWh', totalAmountDecimal: '5.00 cents' }] }} + ${zeroTiers} | ${1} | ${'kWh'} | ${'en'} | ${'USD'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '$0.00', totalWithPrecision: '$0.00', average: '$0.00/kWh', averageWithPrecision: '$0.00/kWh', subAverage: '$0.00/kWh', subtotal: '$0.00', subtotalWithPrecision: '$0.00', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '$0.00/kWh', totalAmountDecimal: '$0.00' }] }} + ${onRequestTiers} | ${5} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${true} | ${undefined} | ${{ rate: 19 }} | ${{ total: '€50.00', totalWithPrecision: '€50.00', average: '€10.00/kWh', averageWithPrecision: '€10.00/kWh', subAverage: '€8.40/kWh', subtotal: '€42.02', subtotalWithPrecision: '€42.016806722689', breakdown: [{ quantityUsed: '5 kWh', tierAmountDecimal: '€10.00/kWh', totalAmountDecimal: '€50.00' }] }} ${onRequestTiers} | ${15} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${true} | ${undefined} | ${{ rate: 19 }} | ${'Price on request'} - ${onRequestTiers} | ${15} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${false} | ${undefined} | ${{ rate: 19 }} | ${{ total: '€145.00', totalWithPrecision: '€145.00', average: '€9.67/kWh', averageWithPrecision: '€9.666666666667/kWh', subAverage: '€8.12/kWh', subAverageWithPrecision: '€8.12324929972/kWh', subtotal: '€121.85', subtotalWithPrecision: '€121.848739495798', breakdown: [{ quantityUsed: '10 kWh', tierAmountDecimal: '€10.00/kWh', totalAmountDecimal: '€100.00' }, { quantityUsed: '5 kWh', tierAmountDecimal: '€9.00/kWh', totalAmountDecimal: '€45.00' }] }} - ${decimalTiers} | ${7} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${false} | ${undefined} | ${{ rate: 19 }} | ${{ total: '38.59 cents', totalWithPrecision: '38.5915 cents', average: '5.51 cents/kWh', averageWithPrecision: '5.513071 cents/kWh', subAverage: '4.63 cents/kWh', subAverageWithPrecision: '4.632833 cents/kWh', subtotal: '32.43 cents', subtotalWithPrecision: '32.429832 cents', breakdown: [{ quantityUsed: '5 kWh', tierAmountDecimal: '5.5451 cents/kWh', totalAmountDecimal: '27.7255 cents' }, { quantityUsed: '2 kWh', tierAmountDecimal: '5.433 cents/kWh', totalAmountDecimal: '10.866 cents' }] }} - ${decimalTiers} | ${7} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${false} | ${true} | ${{ rate: 19 }} | ${{ total: 'Starts at 38.59 cents', totalWithPrecision: 'Starts at 38.5915 cents', average: '5.51 cents/kWh', averageWithPrecision: '5.513071 cents/kWh', subAverage: '4.63 cents/kWh', subAverageWithPrecision: '4.632833 cents/kWh', subtotal: 'Starts at 32.43 cents', subtotalWithPrecision: 'Starts at 32.429832 cents', breakdown: [{ quantityUsed: '5 kWh', tierAmountDecimal: '5.5451 cents/kWh', totalAmountDecimal: '27.7255 cents' }, { quantityUsed: '2 kWh', tierAmountDecimal: '5.433 cents/kWh', totalAmountDecimal: '10.866 cents' }] }} + ${onRequestTiers} | ${15} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${false} | ${undefined} | ${{ rate: 19 }} | ${{ total: '€145.00', totalWithPrecision: '€145.00', average: '€9.67/kWh', averageWithPrecision: '€9.666666666667/kWh', subAverage: '€8.12/kWh', subtotal: '€121.85', subtotalWithPrecision: '€121.848739495798', breakdown: [{ quantityUsed: '10 kWh', tierAmountDecimal: '€10.00/kWh', totalAmountDecimal: '€100.00' }, { quantityUsed: '5 kWh', tierAmountDecimal: '€9.00/kWh', totalAmountDecimal: '€45.00' }] }} + ${decimalTiers} | ${7} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${false} | ${undefined} | ${{ rate: 19 }} | ${{ total: '38.59 cents', totalWithPrecision: '38.5915 cents', average: '5.51 cents/kWh', averageWithPrecision: '5.513071 cents/kWh', subAverage: '4.63 cents/kWh', subtotal: '32.43 cents', subtotalWithPrecision: '32.429832 cents', breakdown: [{ quantityUsed: '5 kWh', tierAmountDecimal: '5.5451 cents/kWh', totalAmountDecimal: '27.7255 cents' }, { quantityUsed: '2 kWh', tierAmountDecimal: '5.433 cents/kWh', totalAmountDecimal: '10.866 cents' }] }} + ${decimalTiers} | ${7} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${false} | ${true} | ${{ rate: 19 }} | ${{ total: 'Starts at 38.59 cents', totalWithPrecision: 'Starts at 38.5915 cents', average: '5.51 cents/kWh', averageWithPrecision: '5.513071 cents/kWh', subAverage: '4.63 cents/kWh', subtotal: 'Starts at 32.43 cents', subtotalWithPrecision: 'Starts at 32.429832 cents', breakdown: [{ quantityUsed: '5 kWh', tierAmountDecimal: '5.5451 cents/kWh', totalAmountDecimal: '27.7255 cents' }, { quantityUsed: '2 kWh', tierAmountDecimal: '5.433 cents/kWh', totalAmountDecimal: '10.866 cents' }] }} `( 'should compute cumulative value correctly when quantityToSelectTier=$quantityToSelectTier, isTaxInclusive=$isTaxInclusive and tax=$tax', ({ diff --git a/src/tiers/compute-cumulative-value.ts b/src/tiers/compute-cumulative-value.ts index eb0b2ad2..ebece2e6 100644 --- a/src/tiers/compute-cumulative-value.ts +++ b/src/tiers/compute-cumulative-value.ts @@ -172,10 +172,6 @@ export const computeCumulativeValue = ( precision: 2, useRealPrecision: false, })}${formattedUnit ? `/${formattedUnit}` : ''}`, - subAverageWithPrecision: `${formatAmountFromString({ - decimalAmount: addSeparatorToDineroString(subAverage!), - ...formatOptions, - })}${formattedUnit ? `/${formattedUnit}` : ''}`, breakdown, }; }; From 7b543769d838e72b07da0e392461b23dfee4ceca Mon Sep 17 00:00:00 2001 From: Paulo Henriques Date: Thu, 23 Apr 2026 15:52:04 +0100 Subject: [PATCH 3/4] Revert averageWithPrecision from compute-cumulative-value Deferred for a future iteration. Co-Authored-By: Claude Sonnet 4.6 --- src/tiers/compute-cumulative-value.test.ts | 36 +++++++++++----------- src/tiers/compute-cumulative-value.ts | 4 --- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/tiers/compute-cumulative-value.test.ts b/src/tiers/compute-cumulative-value.test.ts index 607d8e49..3b83a680 100644 --- a/src/tiers/compute-cumulative-value.test.ts +++ b/src/tiers/compute-cumulative-value.test.ts @@ -55,25 +55,25 @@ const onRequestTiers: PriceTier[] = [ describe('computeCumulativeValue', () => { it.each` tiers | quantityToSelectTier | unit | locale | currency | isTaxInclusive | showOnRequest | showStartsAt | tax | expected - ${baseTiersUnitAmount} | ${1} | ${'kWh'} | ${undefined} | ${'EUR'} | ${true} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '10,00\xa0€', totalWithPrecision: '10,00\xa0€', average: '10,00\xa0€/kWh', averageWithPrecision: '10,00\xa0€/kWh', subtotal: '8,40\xa0€', subtotalWithPrecision: '8,403361344538\xa0€', subAverage: '8,40\xa0€/kWh', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '10,00\xa0€' }] }} - ${baseTiersUnitAmount} | ${1} | ${'kWh'} | ${undefined} | ${'EUR'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '10,00\xa0€', totalWithPrecision: '10,00\xa0€', average: '10,00\xa0€/kWh', averageWithPrecision: '10,00\xa0€/kWh', subtotal: '8,40\xa0€', subtotalWithPrecision: '8,403361344538\xa0€', subAverage: '8,40\xa0€/kWh', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '10,00\xa0€' }] }} - ${baseTiersUnitAmount} | ${1} | ${'kWh'} | ${undefined} | ${'EUR'} | ${false} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '11,90\xa0€', totalWithPrecision: '11,90\xa0€', average: '11,90\xa0€/kWh', averageWithPrecision: '11,90\xa0€/kWh', subtotal: '10,00\xa0€', subtotalWithPrecision: '10,00\xa0€', subAverage: '10,00\xa0€/kWh', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '10,00\xa0€' }] }} - ${baseTiersUnitAmount} | ${1} | ${'kWh'} | ${undefined} | ${'EUR'} | ${false} | ${undefined} | ${undefined} | ${undefined} | ${{ total: '10,00\xa0€', totalWithPrecision: '10,00\xa0€', average: '10,00\xa0€/kWh', averageWithPrecision: '10,00\xa0€/kWh', subtotal: '10,00\xa0€', subtotalWithPrecision: '10,00\xa0€', subAverage: '10,00\xa0€/kWh', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '10,00\xa0€' }] }} - ${baseTiersUnitAmount} | ${2} | ${'kWh'} | ${undefined} | ${'EUR'} | ${true} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '20,00\xa0€', totalWithPrecision: '20,00\xa0€', average: '10,00\xa0€/kWh', averageWithPrecision: '10,00\xa0€/kWh', subtotal: '16,81\xa0€', subtotalWithPrecision: '16,806722689076\xa0€', subAverage: '8,40\xa0€/kWh', breakdown: [{ quantityUsed: '2 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '20,00\xa0€' }] }} - ${baseTiersUnitAmount} | ${2} | ${'kWh'} | ${undefined} | ${'EUR'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '20,00\xa0€', totalWithPrecision: '20,00\xa0€', average: '10,00\xa0€/kWh', averageWithPrecision: '10,00\xa0€/kWh', subtotal: '16,81\xa0€', subtotalWithPrecision: '16,806722689076\xa0€', subAverage: '8,40\xa0€/kWh', breakdown: [{ quantityUsed: '2 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '20,00\xa0€' }] }} - ${baseTiersUnitAmount} | ${2} | ${'kWh'} | ${undefined} | ${'EUR'} | ${false} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '23,80\xa0€', totalWithPrecision: '23,80\xa0€', average: '11,90\xa0€/kWh', averageWithPrecision: '11,90\xa0€/kWh', subtotal: '20,00\xa0€', subtotalWithPrecision: '20,00\xa0€', subAverage: '10,00\xa0€/kWh', breakdown: [{ quantityUsed: '2 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '20,00\xa0€' }] }} - ${baseTiersUnitAmount} | ${2} | ${'kWh'} | ${undefined} | ${'EUR'} | ${false} | ${undefined} | ${undefined} | ${undefined} | ${{ total: '20,00\xa0€', totalWithPrecision: '20,00\xa0€', average: '10,00\xa0€/kWh', averageWithPrecision: '10,00\xa0€/kWh', subtotal: '20,00\xa0€', subtotalWithPrecision: '20,00\xa0€', subAverage: '10,00\xa0€/kWh', breakdown: [{ quantityUsed: '2 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '20,00\xa0€' }] }} - ${baseTiersUnitAmount} | ${5} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '€50.00', totalWithPrecision: '€50.00', average: '€10.00/kWh', averageWithPrecision: '€10.00/kWh', subAverage: '€8.40/kWh', subtotal: '€42.02', subtotalWithPrecision: '€42.016806722689', breakdown: [{ quantityUsed: '5 kWh', tierAmountDecimal: '€10.00/kWh', totalAmountDecimal: '€50.00' }] }} - ${baseTiersUnitAmount} | ${15} | ${'banana'} | ${'en'} | ${'EUR'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '€145.00', totalWithPrecision: '€145.00', average: '€9.67/banana', averageWithPrecision: '€9.666666666667/banana', subAverage: '€8.12/banana', subtotal: '€121.85', subtotalWithPrecision: '€121.848739495798', breakdown: [{ quantityUsed: '10 banana', tierAmountDecimal: '€10.00/banana', totalAmountDecimal: '€100.00' }, { quantityUsed: '5 banana', tierAmountDecimal: '€9.00/banana', totalAmountDecimal: '€45.00' }] }} - ${baseTiersUnitAmount} | ${30} | ${'kWh'} | ${'en'} | ${'USD'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '$270.00', totalWithPrecision: '$270.00', average: '$9.00/kWh', averageWithPrecision: '$9.00/kWh', subAverage: '$7.56/kWh', subtotal: '$226.89', subtotalWithPrecision: '$226.890756302521', breakdown: [{ quantityUsed: '10 kWh', tierAmountDecimal: '$10.00/kWh', totalAmountDecimal: '$100.00' }, { quantityUsed: '10 kWh', tierAmountDecimal: '$9.00/kWh', totalAmountDecimal: '$90.00' }, { quantityUsed: '10 kWh', tierAmountDecimal: '$8.00/kWh', totalAmountDecimal: '$80.00' }] }} - ${baseTiersUnitAmount} | ${0} | ${'kWh'} | ${'en'} | ${'USD'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '$0.00', totalWithPrecision: '$0.00', average: '$0.00/kWh', averageWithPrecision: '$0.00/kWh', subAverage: '$0.00/kWh', subtotal: '$0.00', subtotalWithPrecision: '$0.00', breakdown: [{ quantityUsed: '0 kWh', tierAmountDecimal: '$10.00/kWh', totalAmountDecimal: '$0.00' }] }} - ${subunitTiers} | ${5} | ${'kWh'} | ${'en'} | ${'USD'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '5.00 cents', totalWithPrecision: '5.00 cents', average: '1.00 cent/kWh', averageWithPrecision: '1.00 cent/kWh', subAverage: '0.84 cents/kWh', subtotal: '4.20 cents', subtotalWithPrecision: '4.201681 cents', breakdown: [{ quantityUsed: '5 kWh', tierAmountDecimal: '1.00 cent/kWh', totalAmountDecimal: '5.00 cents' }] }} - ${zeroTiers} | ${1} | ${'kWh'} | ${'en'} | ${'USD'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '$0.00', totalWithPrecision: '$0.00', average: '$0.00/kWh', averageWithPrecision: '$0.00/kWh', subAverage: '$0.00/kWh', subtotal: '$0.00', subtotalWithPrecision: '$0.00', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '$0.00/kWh', totalAmountDecimal: '$0.00' }] }} - ${onRequestTiers} | ${5} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${true} | ${undefined} | ${{ rate: 19 }} | ${{ total: '€50.00', totalWithPrecision: '€50.00', average: '€10.00/kWh', averageWithPrecision: '€10.00/kWh', subAverage: '€8.40/kWh', subtotal: '€42.02', subtotalWithPrecision: '€42.016806722689', breakdown: [{ quantityUsed: '5 kWh', tierAmountDecimal: '€10.00/kWh', totalAmountDecimal: '€50.00' }] }} + ${baseTiersUnitAmount} | ${1} | ${'kWh'} | ${undefined} | ${'EUR'} | ${true} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '10,00\xa0€', totalWithPrecision: '10,00\xa0€', average: '10,00\xa0€/kWh', subtotal: '8,40\xa0€', subtotalWithPrecision: '8,403361344538\xa0€', subAverage: '8,40\xa0€/kWh', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '10,00\xa0€' }] }} + ${baseTiersUnitAmount} | ${1} | ${'kWh'} | ${undefined} | ${'EUR'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '10,00\xa0€', totalWithPrecision: '10,00\xa0€', average: '10,00\xa0€/kWh', subtotal: '8,40\xa0€', subtotalWithPrecision: '8,403361344538\xa0€', subAverage: '8,40\xa0€/kWh', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '10,00\xa0€' }] }} + ${baseTiersUnitAmount} | ${1} | ${'kWh'} | ${undefined} | ${'EUR'} | ${false} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '11,90\xa0€', totalWithPrecision: '11,90\xa0€', average: '11,90\xa0€/kWh', subtotal: '10,00\xa0€', subtotalWithPrecision: '10,00\xa0€', subAverage: '10,00\xa0€/kWh', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '10,00\xa0€' }] }} + ${baseTiersUnitAmount} | ${1} | ${'kWh'} | ${undefined} | ${'EUR'} | ${false} | ${undefined} | ${undefined} | ${undefined} | ${{ total: '10,00\xa0€', totalWithPrecision: '10,00\xa0€', average: '10,00\xa0€/kWh', subtotal: '10,00\xa0€', subtotalWithPrecision: '10,00\xa0€', subAverage: '10,00\xa0€/kWh', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '10,00\xa0€' }] }} + ${baseTiersUnitAmount} | ${2} | ${'kWh'} | ${undefined} | ${'EUR'} | ${true} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '20,00\xa0€', totalWithPrecision: '20,00\xa0€', average: '10,00\xa0€/kWh', subtotal: '16,81\xa0€', subtotalWithPrecision: '16,806722689076\xa0€', subAverage: '8,40\xa0€/kWh', breakdown: [{ quantityUsed: '2 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '20,00\xa0€' }] }} + ${baseTiersUnitAmount} | ${2} | ${'kWh'} | ${undefined} | ${'EUR'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '20,00\xa0€', totalWithPrecision: '20,00\xa0€', average: '10,00\xa0€/kWh', subtotal: '16,81\xa0€', subtotalWithPrecision: '16,806722689076\xa0€', subAverage: '8,40\xa0€/kWh', breakdown: [{ quantityUsed: '2 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '20,00\xa0€' }] }} + ${baseTiersUnitAmount} | ${2} | ${'kWh'} | ${undefined} | ${'EUR'} | ${false} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '23,80\xa0€', totalWithPrecision: '23,80\xa0€', average: '11,90\xa0€/kWh', subtotal: '20,00\xa0€', subtotalWithPrecision: '20,00\xa0€', subAverage: '10,00\xa0€/kWh', breakdown: [{ quantityUsed: '2 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '20,00\xa0€' }] }} + ${baseTiersUnitAmount} | ${2} | ${'kWh'} | ${undefined} | ${'EUR'} | ${false} | ${undefined} | ${undefined} | ${undefined} | ${{ total: '20,00\xa0€', totalWithPrecision: '20,00\xa0€', average: '10,00\xa0€/kWh', subtotal: '20,00\xa0€', subtotalWithPrecision: '20,00\xa0€', subAverage: '10,00\xa0€/kWh', breakdown: [{ quantityUsed: '2 kWh', tierAmountDecimal: '10,00\xa0€/kWh', totalAmountDecimal: '20,00\xa0€' }] }} + ${baseTiersUnitAmount} | ${5} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '€50.00', totalWithPrecision: '€50.00', average: '€10.00/kWh', subAverage: '€8.40/kWh', subtotal: '€42.02', subtotalWithPrecision: '€42.016806722689', breakdown: [{ quantityUsed: '5 kWh', tierAmountDecimal: '€10.00/kWh', totalAmountDecimal: '€50.00' }] }} + ${baseTiersUnitAmount} | ${15} | ${'banana'} | ${'en'} | ${'EUR'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '€145.00', totalWithPrecision: '€145.00', average: '€9.67/banana', subAverage: '€8.12/banana', subtotal: '€121.85', subtotalWithPrecision: '€121.848739495798', breakdown: [{ quantityUsed: '10 banana', tierAmountDecimal: '€10.00/banana', totalAmountDecimal: '€100.00' }, { quantityUsed: '5 banana', tierAmountDecimal: '€9.00/banana', totalAmountDecimal: '€45.00' }] }} + ${baseTiersUnitAmount} | ${30} | ${'kWh'} | ${'en'} | ${'USD'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '$270.00', totalWithPrecision: '$270.00', average: '$9.00/kWh', subAverage: '$7.56/kWh', subtotal: '$226.89', subtotalWithPrecision: '$226.890756302521', breakdown: [{ quantityUsed: '10 kWh', tierAmountDecimal: '$10.00/kWh', totalAmountDecimal: '$100.00' }, { quantityUsed: '10 kWh', tierAmountDecimal: '$9.00/kWh', totalAmountDecimal: '$90.00' }, { quantityUsed: '10 kWh', tierAmountDecimal: '$8.00/kWh', totalAmountDecimal: '$80.00' }] }} + ${baseTiersUnitAmount} | ${0} | ${'kWh'} | ${'en'} | ${'USD'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '$0.00', totalWithPrecision: '$0.00', average: '$0.00/kWh', subAverage: '$0.00/kWh', subtotal: '$0.00', subtotalWithPrecision: '$0.00', breakdown: [{ quantityUsed: '0 kWh', tierAmountDecimal: '$10.00/kWh', totalAmountDecimal: '$0.00' }] }} + ${subunitTiers} | ${5} | ${'kWh'} | ${'en'} | ${'USD'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '5.00 cents', totalWithPrecision: '5.00 cents', average: '1.00 cent/kWh', subAverage: '0.84 cents/kWh', subtotal: '4.20 cents', subtotalWithPrecision: '4.201681 cents', breakdown: [{ quantityUsed: '5 kWh', tierAmountDecimal: '1.00 cent/kWh', totalAmountDecimal: '5.00 cents' }] }} + ${zeroTiers} | ${1} | ${'kWh'} | ${'en'} | ${'USD'} | ${undefined} | ${undefined} | ${undefined} | ${{ rate: 19 }} | ${{ total: '$0.00', totalWithPrecision: '$0.00', average: '$0.00/kWh', subAverage: '$0.00/kWh', subtotal: '$0.00', subtotalWithPrecision: '$0.00', breakdown: [{ quantityUsed: '1 kWh', tierAmountDecimal: '$0.00/kWh', totalAmountDecimal: '$0.00' }] }} + ${onRequestTiers} | ${5} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${true} | ${undefined} | ${{ rate: 19 }} | ${{ total: '€50.00', totalWithPrecision: '€50.00', average: '€10.00/kWh', subAverage: '€8.40/kWh', subtotal: '€42.02', subtotalWithPrecision: '€42.016806722689', breakdown: [{ quantityUsed: '5 kWh', tierAmountDecimal: '€10.00/kWh', totalAmountDecimal: '€50.00' }] }} ${onRequestTiers} | ${15} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${true} | ${undefined} | ${{ rate: 19 }} | ${'Price on request'} - ${onRequestTiers} | ${15} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${false} | ${undefined} | ${{ rate: 19 }} | ${{ total: '€145.00', totalWithPrecision: '€145.00', average: '€9.67/kWh', averageWithPrecision: '€9.666666666667/kWh', subAverage: '€8.12/kWh', subtotal: '€121.85', subtotalWithPrecision: '€121.848739495798', breakdown: [{ quantityUsed: '10 kWh', tierAmountDecimal: '€10.00/kWh', totalAmountDecimal: '€100.00' }, { quantityUsed: '5 kWh', tierAmountDecimal: '€9.00/kWh', totalAmountDecimal: '€45.00' }] }} - ${decimalTiers} | ${7} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${false} | ${undefined} | ${{ rate: 19 }} | ${{ total: '38.59 cents', totalWithPrecision: '38.5915 cents', average: '5.51 cents/kWh', averageWithPrecision: '5.513071 cents/kWh', subAverage: '4.63 cents/kWh', subtotal: '32.43 cents', subtotalWithPrecision: '32.429832 cents', breakdown: [{ quantityUsed: '5 kWh', tierAmountDecimal: '5.5451 cents/kWh', totalAmountDecimal: '27.7255 cents' }, { quantityUsed: '2 kWh', tierAmountDecimal: '5.433 cents/kWh', totalAmountDecimal: '10.866 cents' }] }} - ${decimalTiers} | ${7} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${false} | ${true} | ${{ rate: 19 }} | ${{ total: 'Starts at 38.59 cents', totalWithPrecision: 'Starts at 38.5915 cents', average: '5.51 cents/kWh', averageWithPrecision: '5.513071 cents/kWh', subAverage: '4.63 cents/kWh', subtotal: 'Starts at 32.43 cents', subtotalWithPrecision: 'Starts at 32.429832 cents', breakdown: [{ quantityUsed: '5 kWh', tierAmountDecimal: '5.5451 cents/kWh', totalAmountDecimal: '27.7255 cents' }, { quantityUsed: '2 kWh', tierAmountDecimal: '5.433 cents/kWh', totalAmountDecimal: '10.866 cents' }] }} + ${onRequestTiers} | ${15} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${false} | ${undefined} | ${{ rate: 19 }} | ${{ total: '€145.00', totalWithPrecision: '€145.00', average: '€9.67/kWh', subAverage: '€8.12/kWh', subtotal: '€121.85', subtotalWithPrecision: '€121.848739495798', breakdown: [{ quantityUsed: '10 kWh', tierAmountDecimal: '€10.00/kWh', totalAmountDecimal: '€100.00' }, { quantityUsed: '5 kWh', tierAmountDecimal: '€9.00/kWh', totalAmountDecimal: '€45.00' }] }} + ${decimalTiers} | ${7} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${false} | ${undefined} | ${{ rate: 19 }} | ${{ total: '38.59 cents', totalWithPrecision: '38.5915 cents', average: '5.51 cents/kWh', subAverage: '4.63 cents/kWh', subtotal: '32.43 cents', subtotalWithPrecision: '32.429832 cents', breakdown: [{ quantityUsed: '5 kWh', tierAmountDecimal: '5.5451 cents/kWh', totalAmountDecimal: '27.7255 cents' }, { quantityUsed: '2 kWh', tierAmountDecimal: '5.433 cents/kWh', totalAmountDecimal: '10.866 cents' }] }} + ${decimalTiers} | ${7} | ${'kWh'} | ${'en'} | ${'EUR'} | ${undefined} | ${false} | ${true} | ${{ rate: 19 }} | ${{ total: 'Starts at 38.59 cents', totalWithPrecision: 'Starts at 38.5915 cents', average: '5.51 cents/kWh', subAverage: '4.63 cents/kWh', subtotal: 'Starts at 32.43 cents', subtotalWithPrecision: 'Starts at 32.429832 cents', breakdown: [{ quantityUsed: '5 kWh', tierAmountDecimal: '5.5451 cents/kWh', totalAmountDecimal: '27.7255 cents' }, { quantityUsed: '2 kWh', tierAmountDecimal: '5.433 cents/kWh', totalAmountDecimal: '10.866 cents' }] }} `( 'should compute cumulative value correctly when quantityToSelectTier=$quantityToSelectTier, isTaxInclusive=$isTaxInclusive and tax=$tax', ({ diff --git a/src/tiers/compute-cumulative-value.ts b/src/tiers/compute-cumulative-value.ts index ebece2e6..f852041e 100644 --- a/src/tiers/compute-cumulative-value.ts +++ b/src/tiers/compute-cumulative-value.ts @@ -148,10 +148,6 @@ export const computeCumulativeValue = ( precision: 2, useRealPrecision: false, })}${formattedUnit ? `/${formattedUnit}` : ''}`, - averageWithPrecision: `${formatAmountFromString({ - decimalAmount: addSeparatorToDineroString(average), - ...formatOptions, - })}${formattedUnit ? `/${formattedUnit}` : ''}`, subtotal: (startsAt ? `${startsAt} ` : '') + formatAmountFromString({ From eaa6cb349bc58bf839fa6c3e2a78ad78e548dab1 Mon Sep 17 00:00:00 2001 From: Paulo Henriques Date: Thu, 23 Apr 2026 15:58:50 +0100 Subject: [PATCH 4/4] Add changeset --- .changeset/flat-moments-flash.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/flat-moments-flash.md diff --git a/.changeset/flat-moments-flash.md b/.changeset/flat-moments-flash.md new file mode 100644 index 00000000..edd834aa --- /dev/null +++ b/.changeset/flat-moments-flash.md @@ -0,0 +1,5 @@ +--- +'@epilot/pricing': patch +--- + +Improve tier display string