Skip to content

Commit b6754d3

Browse files
authored
test: add cases for handling tiny deficits in formatting and display (#216)
- Introduced tests to ensure that tiny percent, dollar, and count deficits that round to zero return null. - Updated the `formatDeficitText` function to return null for these cases, enhancing the accuracy of deficit display in the UI. - Added a test to verify that the `ProviderCard` component correctly hides deficit text that would round to zero. (cherry picked from commit f91d3ce)
1 parent e28f85c commit b6754d3

3 files changed

Lines changed: 67 additions & 4 deletions

File tree

src/components/provider-card.test.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,35 @@ describe("ProviderCard", () => {
558558
vi.useRealTimers()
559559
})
560560

561+
it("hides tiny positive deficit text that would round to zero", () => {
562+
vi.useFakeTimers()
563+
const now = new Date("2026-02-02T12:00:00.000Z")
564+
vi.setSystemTime(now)
565+
render(
566+
<ProviderCard
567+
name="Pace"
568+
displayMode="used"
569+
lines={[
570+
{
571+
type: "progress",
572+
label: "Behind",
573+
used: 50.3,
574+
limit: 100,
575+
format: { kind: "percent" },
576+
resetsAt: "2026-02-03T00:00:00.000Z",
577+
periodDurationMs: 24 * 60 * 60 * 1000,
578+
},
579+
]}
580+
/>
581+
)
582+
expect(screen.getByLabelText("Will run out")).toBeInTheDocument()
583+
expect(screen.getByText(/^Runs out in /)).toBeInTheDocument()
584+
expect(screen.queryByText("0% in deficit")).not.toBeInTheDocument()
585+
expect(screen.queryByText("0% short")).not.toBeInTheDocument()
586+
expect(screen.queryByText(/in deficit/i)).not.toBeInTheDocument()
587+
vi.useRealTimers()
588+
})
589+
561590
it("keeps status-only tooltip when pace projection is not yet available", () => {
562591
vi.useFakeTimers()
563592
const now = new Date("2026-02-02T00:45:00.000Z")

src/lib/pace-tooltip.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,4 +272,29 @@ describe("formatDeficitText", () => {
272272
it("rounds percent deficit", () => {
273273
expect(formatDeficitText(4.7, { kind: "percent" }, "used")).toBe("5% in deficit")
274274
})
275+
276+
it("returns null for tiny percent deficits that round to zero", () => {
277+
expect(formatDeficitText(0.3, { kind: "percent" }, "used")).toBeNull()
278+
expect(formatDeficitText(0.3, { kind: "percent" }, "left")).toBeNull()
279+
})
280+
281+
it("returns null for tiny dollar deficits that round to zero", () => {
282+
expect(formatDeficitText(0.004, { kind: "dollars" }, "used")).toBeNull()
283+
})
284+
285+
it("returns null for tiny count deficits that round to zero", () => {
286+
expect(formatDeficitText(0.004, { kind: "count", suffix: "requests" }, "used")).toBeNull()
287+
})
288+
289+
it("shows the first displayable percent deficit", () => {
290+
expect(formatDeficitText(0.5, { kind: "percent" }, "used")).toBe("1% in deficit")
291+
})
292+
293+
it("shows the first displayable dollar deficit", () => {
294+
expect(formatDeficitText(0.005, { kind: "dollars" }, "used")).toBe("$0.01 in deficit")
295+
})
296+
297+
it("shows the first displayable count deficit", () => {
298+
expect(formatDeficitText(0.005, { kind: "count", suffix: "requests" }, "used")).toBe("0.01 requests in deficit")
299+
})
275300
})

src/lib/pace-tooltip.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,18 @@ export function formatDeficitText(
104104
deficit: number,
105105
format: ProgressFormat,
106106
displayMode: DisplayMode
107-
): string {
107+
): string | null {
108+
if (!Number.isFinite(deficit) || deficit <= 0) return null
109+
108110
const suffix = displayMode === "left" ? "short" : "in deficit"
109-
if (format.kind === "percent") return `${Math.round(deficit)}% ${suffix}`
110-
if (format.kind === "dollars") return `$${formatFixedPrecisionNumber(deficit)} ${suffix}`
111-
return `${formatCountNumber(deficit)} ${format.suffix} ${suffix}`
111+
if (format.kind === "percent") {
112+
const roundedPercent = Math.round(deficit)
113+
return roundedPercent > 0 ? `${roundedPercent}% ${suffix}` : null
114+
}
115+
116+
const roundedToCents = Math.round(deficit * 100) / 100
117+
if (roundedToCents <= 0) return null
118+
119+
if (format.kind === "dollars") return `$${formatFixedPrecisionNumber(roundedToCents)} ${suffix}`
120+
return `${formatCountNumber(roundedToCents)} ${format.suffix} ${suffix}`
112121
}

0 commit comments

Comments
 (0)