Skip to content

fix: preserve valid falsy custom names (0, "") in tooltips#7175

Merged
PavelVanecek merged 1 commit into
recharts:mainfrom
vamsi2246:fix/preserve-falsy-tooltip-names
Mar 27, 2026
Merged

fix: preserve valid falsy custom names (0, "") in tooltips#7175
PavelVanecek merged 1 commit into
recharts:mainfrom
vamsi2246:fix/preserve-falsy-tooltip-names

Conversation

@vamsi2246

@vamsi2246 vamsi2246 commented Mar 27, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes a bug where providing name={0} or name="" to a graphical component (like <Bar>, <Line>, etc.) would be silently ignored, causing the tooltip and legend to fall back to the dataKey instead.

This occurred because getTooltipNameProp in src/util/ChartUtils.ts used a falsy check (if (nameFromItem)) which incorrectly drops the number 0 and empty strings. Changed this to a nullish check (!= null).

Proposed Changes

  • Change falsy check to != null check in getTooltipNameProp.
  • Add comprehensive unit tests for getTooltipNameProp.

Checklist

  • I have read the CONTRIBUTING doc
  • Lint and unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works

Summary by CodeRabbit

  • Bug Fixes

    • Fixed tooltip display to correctly show numeric zero and empty string values instead of incorrectly treating them as missing data and falling back to alternative names.
  • Tests

    • Added comprehensive test coverage for tooltip name handling across various input scenarios, including numeric values and empty strings.

@coderabbitai

coderabbitai Bot commented Mar 27, 2026

Copy link
Copy Markdown
Contributor

Walkthrough

The PR updates the getTooltipNameProp utility function to properly handle falsy values (such as 0 and empty strings) as valid inputs instead of falling back to the dataKey parameter. Corresponding test coverage is added to validate this behavior across multiple scenarios.

Changes

Cohort / File(s) Summary
Utility Function Implementation
src/util/ChartUtils.ts
Modified getTooltipNameProp to return String(nameFromItem) for any non-null/undefined value, enabling proper handling of falsy values like 0 and empty strings as legitimate tooltip names.
Test Coverage
test/util/ChartUtils.spec.tsx
Added comprehensive test suite for getTooltipNameProp covering string names, numeric conversion, falsy value preservation, undefined fallback behavior, and edge cases with function-type dataKey.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Suggested reviewers

  • PavelVanecek
  • ckifer
🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: preserving falsy values (0 and empty string) in tooltip names by fixing the validation logic.
Description check ✅ Passed The description covers the bug, its cause, the fix, and testing approach. While it doesn't strictly follow all template sections, it provides sufficient information about the problem and solution.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
test/util/ChartUtils.spec.tsx (1)

439-467: Nice regression coverage; add explicit null case for completeness.

The suite is strong for 0/'' preservation and fallback paths. Consider adding a null input assertion to fully lock in the nullish contract introduced in Line [705].

Suggested test addition
 describe('getTooltipNameProp', () => {
@@
   it('should fall back to string dataKey when name is undefined', () => {
     expect(getTooltipNameProp(undefined, 'revenue')).toBe('revenue');
   });
+
+  it('should fall back to string dataKey when name is null', () => {
+    expect(getTooltipNameProp(null, 'revenue')).toBe('revenue');
+  });
@@
 });

As per coding guidelines: test/**/*.{test,spec}.{ts,tsx}: Aim for 100% unit test code coverage when writing new code.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@test/util/ChartUtils.spec.tsx` around lines 439 - 467, Add explicit tests
covering name === null for getTooltipNameProp: verify that
getTooltipNameProp(null, 'revenue') falls back to the string dataKey
('revenue'), getTooltipNameProp(null, (e) => e) returns undefined when dataKey
is a function, and getTooltipNameProp(null, null) returns undefined; add these
assertions to the existing describe('getTooltipNameProp') block in
ChartUtils.spec.tsx so the null branch of the nullish contract (introduced
around line 705) is exercised.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@test/util/ChartUtils.spec.tsx`:
- Around line 439-467: Add explicit tests covering name === null for
getTooltipNameProp: verify that getTooltipNameProp(null, 'revenue') falls back
to the string dataKey ('revenue'), getTooltipNameProp(null, (e) => e) returns
undefined when dataKey is a function, and getTooltipNameProp(null, null) returns
undefined; add these assertions to the existing describe('getTooltipNameProp')
block in ChartUtils.spec.tsx so the null branch of the nullish contract
(introduced around line 705) is exercised.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ec916c43-7f60-4678-961a-e46b57e9c9fe

📥 Commits

Reviewing files that changed from the base of the PR and between ad7e3f3 and 98b8b36.

📒 Files selected for processing (2)
  • src/util/ChartUtils.ts
  • test/util/ChartUtils.spec.tsx

@codecov

codecov Bot commented Mar 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.68%. Comparing base (ad7e3f3) to head (98b8b36).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7175   +/-   ##
=======================================
  Coverage   89.68%   89.68%           
=======================================
  Files         537      537           
  Lines       40674    40674           
  Branches     5547     5548    +1     
=======================================
  Hits        36477    36477           
  Misses       4189     4189           
  Partials        8        8           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codecov

codecov Bot commented Mar 27, 2026

Copy link
Copy Markdown

Bundle Report

Changes will increase total bundle size by 38 bytes (0.0%) ⬆️. This is within the configured threshold ✅

Detailed changes
Bundle name Size Change
recharts/bundle-cjs 1.3MB 8 bytes (0.0%) ⬆️
recharts/bundle-es6 1.13MB 8 bytes (0.0%) ⬆️
recharts/bundle-umd 547.75kB 6 bytes (0.0%) ⬆️
recharts/bundle-treeshaking-cartesian 643.64kB 8 bytes (0.0%) ⬆️
recharts/bundle-treeshaking-polar 448.07kB 8 bytes (0.0%) ⬆️

Affected Assets, Files, and Routes:

view changes for bundle: recharts/bundle-treeshaking-polar

Assets Changed:

Asset Name Size Change Total Size Change (%)
bundle.js 8 bytes 448.07kB 0.0%
view changes for bundle: recharts/bundle-cjs

Assets Changed:

Asset Name Size Change Total Size Change (%)
util/ChartUtils.js 8 bytes 19.99kB 0.04%
view changes for bundle: recharts/bundle-umd

Assets Changed:

Asset Name Size Change Total Size Change (%)
Recharts.js 6 bytes 547.75kB 0.0%
view changes for bundle: recharts/bundle-treeshaking-cartesian

Assets Changed:

Asset Name Size Change Total Size Change (%)
bundle.js 8 bytes 643.64kB 0.0%
view changes for bundle: recharts/bundle-es6

Assets Changed:

Asset Name Size Change Total Size Change (%)
util/ChartUtils.js 8 bytes 17.98kB 0.04%

@PavelVanecek
PavelVanecek merged commit 32ec3c7 into recharts:main Mar 27, 2026
98 of 99 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants