Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tabs): add click handling for tab items in tests and implementation #3917

Open
wants to merge 1 commit into
base: canary
Choose a base branch
from

Conversation

liaoyinglong
Copy link

@liaoyinglong liaoyinglong commented Oct 18, 2024

📝 Description

Add click handling for tab items in tests and implementation

⛳️ Current behavior (updates)

Currently, the tab component lacks specific click handling and testing for individual tab items.

🚀 New behavior

  • Added a new test case to verify that tab clicks are handled correctly
  • Implemented click handling for individual tab items in the Tab component

💣 Is this a breaking change (Yes/No):

No

📝 Additional Information

This change improves the interactivity and testability of the Tabs component by ensuring that clicks on individual tab items are properly handled and can be tested.

References:

Summary by CodeRabbit

  • New Features

    • Enhanced test coverage for the Tabs component, ensuring click events trigger the correct handlers.
    • Improved interactive behavior for tab items, allowing users to click and see immediate responses.
  • Bug Fixes

    • Improved handling of click events in the Tab component by allowing variable arguments to be passed to click handlers.

Copy link

changeset-bot bot commented Oct 18, 2024

🦋 Changeset detected

Latest commit: fa9c239

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@nextui-org/tabs Patch
@nextui-org/react Patch

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

Copy link
Contributor

coderabbitai bot commented Oct 18, 2024

Walkthrough

The pull request introduces a new test case for the Tabs component, specifically verifying that click events on individual tabs invoke the correct click handlers. Additionally, it modifies the handleClick function in the Tab component to ensure that chained click handlers are executed properly. These changes enhance the functionality and test coverage of the Tabs component.

Changes

File Path Change Summary
packages/components/tabs/__tests__/tabs.test.tsx Added a new test case to verify that tab clicks trigger the appropriate click handlers.
packages/components/tabs/src/tab.tsx Modified the handleClick function to ensure that chained click handlers are executed correctly.

Possibly related PRs

Suggested labels

📋 Scope : Docs

Suggested reviewers

  • jrgarciadev

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 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.

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

vercel bot commented Oct 18, 2024

@liaoyinglong is attempting to deploy a commit to the NextUI Inc Team on Vercel.

A member of the Team first needs to authorize it.

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 (2)
packages/components/tabs/src/tab.tsx (1)

81-82: Approve change with type safety improvement suggestion

The modification to accept variable arguments in the handleClick function enhances flexibility and aligns with the PR objectives. It allows for proper forwarding of additional arguments to the click handlers.

However, to improve type safety, consider using a more specific type instead of any[]. For example:

const handleClick = (...args: React.MouseEvent<HTMLElement>[]) => {
  chain(onClick, tabProps.onClick)(...args);
  // ... rest of the function
};

This change maintains the flexibility while providing better type checking.

packages/components/tabs/__tests__/tabs.test.tsx (1)

392-413: Good addition of click handling test, with room for improvement

The new test case effectively verifies that click handlers for individual tabs are called correctly. This is a valuable addition to the test suite, aligning well with the PR objective of adding click handling for tab items.

However, consider the following enhancements to make the test more robust:

  1. Verify tab selection: After clicking a tab, check if it becomes selected (e.g., aria-selected="true").
  2. Content visibility: Ensure the content of the selected tab is displayed.
  3. Naming: Use more descriptive names for click handler mocks, e.g., handleItem1Click instead of item1Click.

Example improvement:

it("Tab click should be handled and select the tab", async () => {
  const handleItem1Click = jest.fn();
  const handleItem2Click = jest.fn();
  const wrapper = render(
    <Tabs>
      <Tab key="item1" data-testid="item1" title="Item 1" onClick={handleItem1Click}>
        <div data-testid="content1">Content 1</div>
      </Tab>
      <Tab key="item2" data-testid="item2" title="Item 2" onClick={handleItem2Click}>
        <div data-testid="content2">Content 2</div>
      </Tab>
    </Tabs>,
  );
  const tab1 = wrapper.getByTestId("item1");
  const tab2 = wrapper.getByTestId("item2");

  await user.click(tab1);
  expect(handleItem1Click).toHaveBeenCalled();
  expect(tab1).toHaveAttribute("aria-selected", "true");
  expect(wrapper.getByTestId("content1")).toBeVisible();

  await user.click(tab2);
  expect(handleItem2Click).toHaveBeenCalled();
  expect(tab2).toHaveAttribute("aria-selected", "true");
  expect(wrapper.getByTestId("content2")).toBeVisible();
});

These changes would provide a more comprehensive test of the tab click functionality.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 8a33eab and 9a3550a.

📒 Files selected for processing (2)
  • packages/components/tabs/tests/tabs.test.tsx (1 hunks)
  • packages/components/tabs/src/tab.tsx (1 hunks)
🧰 Additional context used

Comment on lines 81 to 82
const handleClick = (...args: any[]) => {
chain(onClick, tabProps.onClick)(...args);
Copy link
Member

Choose a reason for hiding this comment

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

can you share some examples of args?

Copy link
Author

Choose a reason for hiding this comment

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

No, just ensure that the DOM event can be smoothly passed through.

Copy link
Author

Choose a reason for hiding this comment

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

Like this

image

Copy link
Member

Choose a reason for hiding this comment

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

This's for tabProps.onClick. There should be no args for handleClick.

Copy link
Author

Choose a reason for hiding this comment

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

Here, handleClick did not receive event, so tabProps.onClick will not have event either.

Copy link
Member

Choose a reason for hiding this comment

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

please add a test case to cover this change as well.

This comment was marked as outdated.

Copy link
Author

Choose a reason for hiding this comment

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

I removed the event parameter and updated the tests; please take a look when you have time.

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 (2)
.changeset/seven-tips-help.md (1)

1-5: Changeset looks good, but consider enhancing the description.

The changeset correctly identifies this as a patch update for the "@nextui-org/tabs" package, which aligns with the nature of the changes described in the PR objectives.

Consider expanding the description slightly to provide more context. For example:

- add click handling for tab items in tests and implementation
+ add click handling for tab items in tests and implementation to improve interactivity and test coverage

This additional detail will be helpful for maintainers and users reviewing the changelog in the future.

packages/components/tabs/__tests__/tabs.test.tsx (1)

392-413: LGTM! Consider enhancing assertions for more robust testing.

The new test case "Tab click should be handled" is well-structured and effectively verifies the click handling for tab items. It follows good testing practices by using mock functions and simulating user interactions.

To further improve the test case, consider the following suggestions:

  1. Check the number of times each click handler is called to ensure they are not triggered multiple times unexpectedly.
  2. Verify that clicking one tab doesn't trigger the other tab's click handler.

Example of enhanced assertions:

expect(item1Click).toHaveBeenCalledTimes(1);
expect(item2Click).not.toHaveBeenCalled();

await user.click(tab2);
expect(item2Click).toHaveBeenCalledTimes(1);
expect(item1Click).toHaveBeenCalledTimes(1); // Ensure it wasn't called again

These additional checks will make the test more robust and help catch potential issues with event propagation or multiple trigger events.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 9a3550a and a3ca3f9.

📒 Files selected for processing (3)
  • .changeset/seven-tips-help.md (1 hunks)
  • packages/components/tabs/tests/tabs.test.tsx (1 hunks)
  • packages/components/tabs/src/tab.tsx (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/components/tabs/src/tab.tsx
🧰 Additional context used

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)
packages/components/tabs/__tests__/tabs.test.tsx (1)

392-415: LGTM! Consider enhancing test coverage.

The new test case "Tab click should be handled" is well-structured and effectively verifies the basic functionality of tab click handling. It correctly sets up mock functions, renders the component, simulates user interactions, and checks if the click handlers are called with the correct event targets.

To further improve the test coverage, consider the following suggestions:

  1. Add assertions to verify the selected state of tabs after clicks. This ensures that clicking a tab not only triggers the click handler but also updates the component's state correctly.

  2. Test the scenario where a tab is clicked multiple times. This can help verify that repeated clicks don't cause unexpected behavior.

  3. Include a test case for clicking a disabled tab to ensure that the click handler is not called and the tab's state doesn't change.

Here's an example of how you might implement these suggestions:

it("Tab click should be handled", async () => {
  const item1Click = jest.fn();
  const item2Click = jest.fn();
  const wrapper = render(
    <Tabs>
      <Tab key="item1" data-testid="item1" title="Item 1" onClick={item1Click}>
        <div>Content 1</div>
      </Tab>
      <Tab key="item2" data-testid="item2" title="Item 2" onClick={item2Click}>
        <div>Content 2</div>
      </Tab>
      <Tab key="item3" data-testid="item3" title="Item 3" isDisabled>
        <div>Content 3</div>
      </Tab>
    </Tabs>,
  );
  const tab1 = wrapper.getByTestId("item1");
  const tab2 = wrapper.getByTestId("item2");
  const tab3 = wrapper.getByTestId("item3");

  // Test initial state
  expect(tab1).toHaveAttribute("aria-selected", "true");
  expect(tab2).toHaveAttribute("aria-selected", "false");

  // Test clicking tab2
  await user.click(tab2);
  expect(item2Click).toHaveBeenCalledTimes(1);
  expect(item2Click.mock.lastCall[0].target).toBe(tab2);
  expect(tab1).toHaveAttribute("aria-selected", "false");
  expect(tab2).toHaveAttribute("aria-selected", "true");

  // Test clicking tab2 again
  await user.click(tab2);
  expect(item2Click).toHaveBeenCalledTimes(2);
  expect(tab2).toHaveAttribute("aria-selected", "true");

  // Test clicking disabled tab
  await user.click(tab3);
  expect(tab3).toHaveAttribute("aria-selected", "false");
  // Ensure no handler is called for disabled tab
});

These enhancements will provide a more comprehensive test suite for the tab click handling functionality.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between a3ca3f9 and e894b2f.

📒 Files selected for processing (1)
  • packages/components/tabs/tests/tabs.test.tsx (1 hunks)
🧰 Additional context used

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 (2)
.changeset/seven-tips-help.md (1)

1-5: Consider enhancing the changeset description.

While the current description is accurate, it could be more detailed to better document the changes for future reference. Consider adding specifics about:

  • The improvements to the handleClick function for chained handlers
  • The new test case that verifies tab click handling

Here's a suggested enhancement:

---
"@nextui-org/tabs": patch
---

-add click handling for tab items in tests and implementation
+add click handling for tab items in tests and implementation
+
+- Improved `handleClick` function to properly handle chained click handlers
+- Added test case to verify tab click handling functionality
packages/components/tabs/__tests__/tabs.test.tsx (1)

392-422: Well-structured test case for tab click handling.

The test case effectively verifies the click handling functionality and state management. The structure is clear with distinct sections for initial state verification, click behavior testing, and multiple click handling.

Consider enhancing the test coverage by:

  1. Adding assertions for the first tab's click handler (item1Click)
  2. Verifying the arguments passed to click handlers
  3. Testing click handler behavior with disabled tabs

Example addition:

  // Test clicking tab2 again
  await user.click(tab2);
  expect(item2Click).toHaveBeenCalledTimes(2);
  expect(tab2).toHaveAttribute("aria-selected", "true");
+ 
+ // Test clicking tab1
+ await user.click(tab1);
+ expect(item1Click).toHaveBeenCalledTimes(1);
+ expect(item2Click).toHaveBeenCalledTimes(2);
+ expect(tab1).toHaveAttribute("aria-selected", "true");
+ expect(tab2).toHaveAttribute("aria-selected", "false");
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between e894b2f and fa9c239.

📒 Files selected for processing (3)
  • .changeset/seven-tips-help.md (1 hunks)
  • packages/components/tabs/tests/tabs.test.tsx (1 hunks)
  • packages/components/tabs/src/tab.tsx (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/components/tabs/src/tab.tsx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants