Repository navigation
Tooltip: guard against missing text at runtime - #8814
rickyzhangca wants to merge 1 commit into
Conversation
#8740 called `text.trim()` unconditionally during render, which throws when a caller passes an undefined `text` value at runtime (such as incomplete test mocks in github/github-ui). Check `typeof text === 'string'` before trimming so missing values remain inert instead of crashing render. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
🦋 Changeset detectedLatest commit: cf37458 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
There was a problem hiding this comment.
🟡 Changes recommended
The tests do not cover explicitly undefined text, leaving the crash fix without regression coverage for successful rendering, absent tooltip ARIA association, and no tooltip opening on focus.
1 open issue
What changed in this PR
This follow-up to #8740 prevents Primer React’s Tooltip from throwing when text is missing at runtime, while preserving its required-string TypeScript API.
Changes:
- Checks that
textis a string before trimming, keeping missing-text tooltips inert unless a keybinding hint provides content. - Adds a patch changeset for
@primer/react.
| File | Description |
|---|---|
packages/react/src/TooltipV2/Tooltip.tsx |
Guards the text-content check against missing runtime values. |
.changeset/tooltip-missing-text.md |
Records the runtime fix for a patch release. |
Review effort: Balanced
| // Normalize keybindingHint to an array for uniform rendering | ||
| const keybindingHints = Array.isArray(keybindingHint) ? keybindingHint : [keybindingHint] | ||
| const hasText = Boolean(text.trim()) | ||
| const hasText = typeof text === 'string' && text.trim().length > 0 |
|
Integration test results from github/github-ui PR:
All checks passed! |

Closes #
Follows up on #8740.
#8740added an unconditionaltext.trim()check during render. Whiletextis typed asstring, callers passingundefinedat runtime (for example, partialCheckRuntest fixtures ingithub/github-uiReposChecksStatusBadge.browser.test.tsx) now throwTypeError: Cannot read properties of undefined (reading 'trim')instead of rendering inertly.Checking
typeof text === 'string' && text.trim().length > 0keeps the TypeScript contract unchanged while treating missing runtime values as empty text.Changelog
New
Changed
Tooltipnow guardstextwithtypeof text === 'string'before trimming so missing runtime values stay inert instead of throwing during render.Removed
Rollout strategy
Testing & Reviewing
Local validation:
npm run build: passedVITEST_FAIL_ON_CONSOLE=true npx vitest run --project '@primer/react' packages/react/src/TooltipV2/__tests__/Tooltip.test.tsx: passed (44/44 tests)npm run type-check: passednpx eslint --max-warnings=0 --fix packages/react/src/TooltipV2/Tooltip.tsx: passednpm run format:diff: passednpm testandscript/test-e2esuites were not run locally (covered by CI).