From 700fbc9405c8b4ee2a9ef0a9aab20a9fb7ae8525 Mon Sep 17 00:00:00 2001 From: Raquel Smith Date: Tue, 17 Oct 2023 10:38:44 -0700 Subject: [PATCH] fix: tiers show incorrect volumes if denominator differs (#18041) * use correct denom for previous tier and show correct number * test the func --- .../src/scenes/billing/billing-utils.spec.ts | 18 +++++++++++++++++- frontend/src/scenes/billing/billing-utils.ts | 10 ++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/frontend/src/scenes/billing/billing-utils.spec.ts b/frontend/src/scenes/billing/billing-utils.spec.ts index da715a0422904..9680f47886738 100644 --- a/frontend/src/scenes/billing/billing-utils.spec.ts +++ b/frontend/src/scenes/billing/billing-utils.spec.ts @@ -1,4 +1,10 @@ -import { convertAmountToUsage, convertUsageToAmount, projectUsage, summarizeUsage } from './billing-utils' +import { + convertAmountToUsage, + convertLargeNumberToWords, + convertUsageToAmount, + projectUsage, + summarizeUsage, +} from './billing-utils' import tk from 'timekeeper' import { dayjs } from 'lib/dayjs' import billingJson from '~/mocks/fixtures/_billing_v2.json' @@ -159,3 +165,13 @@ describe('convertAmountToUsageWithPercentDiscount', () => { } ) }) + +describe('convertLargeNumberToWords', () => { + it('should convert large numbers to words', () => { + expect(convertLargeNumberToWords(250, null, true, 'survey')).toEqual('First 250 surveys/mo') + expect(convertLargeNumberToWords(500, 250, true, 'survey')).toEqual('251-500') + expect(convertLargeNumberToWords(1000, 500, true, 'survey')).toEqual('501-1k') + expect(convertLargeNumberToWords(10000, 1000, true, 'survey')).toEqual('1-10k') + expect(convertLargeNumberToWords(10000000, 1000000, true, 'survey')).toEqual('1-10 million') + }) +}) diff --git a/frontend/src/scenes/billing/billing-utils.ts b/frontend/src/scenes/billing/billing-utils.ts index d0bfaca0a3cb3..fd4c0d25bcd56 100644 --- a/frontend/src/scenes/billing/billing-utils.ts +++ b/frontend/src/scenes/billing/billing-utils.ts @@ -185,14 +185,20 @@ export const convertLargeNumberToWords = ( } let denominator = 1 - if (num >= 1000000) { denominator = 1000000 } else if (num >= 1000) { denominator = 1000 } - return `${previousNum ? `${(previousNum / denominator).toFixed(0)}-` : multipleTiers ? 'First ' : ''}${( + let prevDenominator = 1 + if (previousNum && previousNum >= 1000000) { + prevDenominator = 1000000 + } else if (previousNum && previousNum >= 1000) { + prevDenominator = 1000 + } + + return `${previousNum ? `${((previousNum + 1) / prevDenominator).toFixed(0)}-` : multipleTiers ? 'First ' : ''}${( num / denominator ).toFixed(0)}${denominator === 1000000 ? ' million' : denominator === 1000 ? 'k' : ''}${ !previousNum && multipleTiers ? ` ${productType}s/mo` : ''