Skip to content

Conversation

@chromium-52
Copy link
Contributor

@chromium-52 chromium-52 commented Dec 12, 2024

Purpose

Fixes #629

This PR

Changes the default hello label to an empty string for the tooltip component.

Adds tests for the tooltip component.

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features
    • The label prop for the Tooltip component is now optional, with a default value of an empty string.
  • Bug Fixes
    • Improved error handling for the Tooltip component when no label is provided or when the label is of an invalid type.
  • Tests
    • Added comprehensive unit tests for the Tooltip component to validate rendering and interaction behavior, including hover visibility.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 12, 2024

Caution

Review failed

The pull request is closed.

Walkthrough

The Tooltip component in the src/components/ui/Tooltip/Tooltip.tsx file has been updated to make the label prop optional by changing its default value from 'hello' to an empty string ''. The placement prop remains optional with a default value of 'top'. Additionally, a comprehensive set of unit tests has been introduced in src/components/ui/Tooltip/tests/Tooltip.test.tsx to verify the rendering and interaction behavior of the Tooltip component.

Changes

File Change Summary
src/components/ui/Tooltip/Tooltip.tsx Updated Tooltip function signature to make label optional with a default value of ''.
src/components/ui/Tooltip/tests/Tooltip.test.tsx Added unit tests for Tooltip component to verify rendering and visibility behavior on hover.

Assessment against linked issues

Objective Addressed Explanation
Remove the default 'hello' label that's added to Tooltip

Possibly related PRs

  • Change default label in <Tooltip /> #638: This PR directly modifies the Tooltip component to make the label prop optional and changes its default value, which aligns with the changes made in the main PR.

Suggested reviewers

  • kotAPI

Poem

In the garden of code where the tooltips play,
A label once default has now gone away.
Hover and see, the tooltip will show,
A dance of interactions, watch it glow!
With tests now in place, we’re ready to cheer,
For a cleaner, bright future, let’s give a big cheer! 🐇✨

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. This feature will be included in our Pro Plan when released.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3d939cf and d594b6e.

📒 Files selected for processing (1)
  • src/components/ui/Tooltip/tests/Tooltip.test.tsx (1 hunks)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
src/components/ui/Tooltip/tests/Tooltip.test.tsx (1)

13-26: LGTM! Well-structured interaction test.

The hover test is well implemented with:

  • Clear test steps through comments
  • Proper async handling
  • Complete interaction cycle testing

Consider adding these additional test cases:

test('renders with different placement values', () => {
    render(<Tooltip label="label" placement="bottom">Hover me</Tooltip>);
    // ... verify placement
});

test('spreads additional props to Tooltip', () => {
    render(<Tooltip label="label" data-testid="my-tooltip">Hover me</Tooltip>);
    expect(screen.getByTestId('my-tooltip')).toBeInTheDocument();
});
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 918f111 and 469f381.

📒 Files selected for processing (2)
  • src/components/ui/Tooltip/Tooltip.tsx (1 hunks)
  • src/components/ui/Tooltip/tests/Tooltip.test.tsx (1 hunks)
🔇 Additional comments (2)
src/components/ui/Tooltip/Tooltip.tsx (1)

25-25: LGTM! Good improvement in the component API.

Making the label prop required is a positive change as it:

  1. Prevents accidental empty/meaningless tooltips
  2. Makes the component's requirements more explicit
  3. Provides better TypeScript type safety
src/components/ui/Tooltip/tests/Tooltip.test.tsx (1)

7-11: LGTM! Good basic test coverage.

The initial render test effectively verifies that:

  • The trigger element renders correctly
  • The tooltip is hidden by default

@chromium-52
Copy link
Contributor Author

@kotAPI @GoldGroove06
Should we also add clsx for src/components/tools/Popper/Popper.tsx?

@GoldGroove06
Copy link
Contributor

Yeah sure it requires clsx, nice catch

import userEvent from '@testing-library/user-event';
import Tooltip from '../Tooltip';

describe('Tooltip', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

awesome! great job on adding tests

following the above comment, let us treat label as an empty string

test for weird cases when say label is a number, boolean etc (you might have to write tests in js instead of ts for those tests)

Copy link
Contributor Author

@chromium-52 chromium-52 Dec 13, 2024

Choose a reason for hiding this comment

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

test for weird cases when say label is a number, boolean etc

Is this just because this is a public library and could be used in someone's JS project?

edit: and if so, shouldn't we be increasing the scope of label's type in TooltipProps to include other types in addition to string?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this just because this is a public library and could be used in someone's JS project?

Correct, that's the goal here

Copy link
Collaborator

Choose a reason for hiding this comment

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

edit: and if so, shouldn't we be increasing the scope of label's type in TooltipProps to include other types in addition to string?

No, so the label will always be a string, the component shouldn't crash and bring the entire app down when weird props are passed - we do the error handling inside the component so its smart enough to deal with such real life edge cases

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah then should I just be testing that the component renders the toooltip without any errors when non-string props are passed in?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah then should I just be testing that the component renders the toooltip without any errors when non-string props are passed in?

correct, it shouldn't break the component - say when bool or integers are passed (this doesn't happen in ts projects, but its a risk in js projects)

Copy link
Collaborator

@kotAPI kotAPI left a comment

Choose a reason for hiding this comment

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

Looks good to me!

LMK when you are ready for merging

@chromium-52
Copy link
Contributor Author

@kotAPI just added the tests. Could you take a look at them?

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (1)
src/components/ui/Tooltip/tests/Tooltip.test.tsx (1)

6-11: Consider adding accessibility assertions

While the basic visibility tests are good, consider adding assertions for ARIA attributes and roles to ensure accessibility:

 test('renders component', () => {
     render(<Tooltip label='label'>Hover me</Tooltip>);
     expect(screen.getByText('Hover me')).toBeInTheDocument();
     expect(screen.queryByText('label')).not.toBeInTheDocument();
+    // Verify accessibility attributes
+    const trigger = screen.getByText('Hover me');
+    expect(trigger).toHaveAttribute('aria-describedby');
 });
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 469f381 and 3d939cf.

📒 Files selected for processing (2)
  • src/components/ui/Tooltip/Tooltip.tsx (2 hunks)
  • src/components/ui/Tooltip/tests/Tooltip.test.tsx (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/components/ui/Tooltip/Tooltip.tsx
🔇 Additional comments (3)
src/components/ui/Tooltip/tests/Tooltip.test.tsx (3)

1-5: LGTM! Proper testing setup

The imports follow testing best practices using @testing-library/react and user-event for interaction testing.


14-27: LGTM! Comprehensive hover interaction testing

The test thoroughly covers the tooltip's visibility lifecycle with clear, descriptive comments.


29-42: LGTM! Good accessibility testing

Excellent use of role selectors for testing empty label case, which aligns with accessibility best practices.

Comment on lines 44 to 53
test('renders tooltip when label is of an invalid type', async() => {
// @ts-expect-error: label should be a string
expect(() => render(<Tooltip label={42}>Hover me</Tooltip>)).not.toThrow();

// @ts-expect-error:label should be a string
expect(() => render(<Tooltip label={true}>Hover me</Tooltip>)).not.toThrow();

// @ts-expect-error: label should be a string
expect(() => render(<Tooltip label={null}>Hover me</Tooltip>)).not.toThrow();
});
Copy link
Contributor

@coderabbitai coderabbitai bot Dec 14, 2024

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Enhance invalid type testing

While the current tests verify that the component doesn't throw errors with invalid types, they should also verify the rendered content:

 test('renders tooltip when label is of an invalid type', async() => {
     // @ts-expect-error: label should be a string
-    expect(() => render(<Tooltip label={42}>Hover me</Tooltip>)).not.toThrow();
+    const { getByText } = render(<Tooltip label={42}>Hover me</Tooltip>);
+    const trigger = getByText('Hover me');
+    await userEvent.hover(trigger);
+    // Verify that number is converted to string
+    expect(screen.getByText('42')).toBeInTheDocument();

     // @ts-expect-error:label should be a string
-    expect(() => render(<Tooltip label={true}>Hover me</Tooltip>)).not.toThrow();
+    const { getByText } = render(<Tooltip label={true}>Hover me</Tooltip>);
+    const trigger = getByText('Hover me');
+    await userEvent.hover(trigger);
+    // Verify that boolean is converted to string
+    expect(screen.getByText('true')).toBeInTheDocument();

     // @ts-expect-error: label should be a string
-    expect(() => render(<Tooltip label={null}>Hover me</Tooltip>)).not.toThrow();
+    const { getByText } = render(<Tooltip label={null}>Hover me</Tooltip>);
+    const trigger = getByText('Hover me');
+    await userEvent.hover(trigger);
+    // Verify empty tooltip is rendered for null
+    expect(screen.getByRole('dialog')).toBeEmptyDOMElement();
 });

Committable suggestion skipped: line range outside the PR's diff.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The second expect won't pass but that's not a bad suggestion tbh

Copy link
Contributor

Choose a reason for hiding this comment

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

@chromium-52, understood. Thank you for your feedback.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh I didn't think you'd respond lol

Copy link
Contributor

Choose a reason for hiding this comment

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

@chromium-52 I'm always here to help! Let me know if there's anything else I can assist you with.

Copy link
Collaborator

Choose a reason for hiding this comment

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

hahaahhah :D

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

@chromium-52 chromium-52 changed the title Remove default label in <Tooltip /> Change default label in <Tooltip /> Dec 14, 2024
@kotAPI kotAPI added the automerge A tag that tells kodiak bot to automerge PRs for us when tests and approval conditions are met label Dec 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automerge A tag that tells kodiak bot to automerge PRs for us when tests and approval conditions are met

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: Remove the default 'hello' label that's added to Tooltip

3 participants