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

fix:Ensure alert message triggers only once when onDropdownClose in select widget #34812

Conversation

PrasadMadine
Copy link
Contributor

@PrasadMadine PrasadMadine commented Jul 9, 2024

Description:

When using the select widget, setting the "onDropdownClosed" event to show an alert results in the alert being shown twice instead of once.
I have raised this PR to ensure that show alert is called only once in the select widget on calling onCloseDropdown.

Issue Link

Cypress video:

Select4_Spec.ts.mp4

Screenshots:

Before resolving bug:

Screenshot from 2024-07-05 09-46-03

After resolving bug :

Screenshot from 2024-07-08 08-55-14

Summary by CodeRabbit

  • Tests

    • Introduced new tests for the Select widget to validate onDropdownClose, onDropdownOpen, and onOptionChange events, ensuring alerts display correctly during interactions.
    • Added a test suite for the SelectComponent to verify that dropdown callbacks are triggered correctly.
  • New Features

    • Enhanced the visibility toggle logic for the Select widget's popover, improving user experience by preventing redundant actions when toggling via button clicks.
    • Added a specific method to handle button click events for toggling the popover visibility.
    • Improved responsiveness of the Select widget by ensuring active item updates only occur when the selected item changes.

Copy link
Contributor

coderabbitai bot commented Jul 9, 2024

Walkthrough

The recent changes introduce a new test suite for the Select widget in the application. This suite validates the behavior of the onDropdownClose, onDropdownOpen, and onOptionChange events, ensuring alerts are displayed correctly and only once during user interactions. The tests simulate user actions, verifying that the appropriate alerts appear during dropdown operations, thereby confirming the event handling logic functions as intended. Additionally, modifications were made to the togglePopoverVisibility method to enhance its functionality.

Changes

File Path Change Summary
app/client/cypress/e2e/Regression/ClientSide/Widgets/Select/Select4_spec.ts Introduces a new test suite for the Select widget, validating dropdown event handling and alert displays.
app/client/src/widgets/SelectWidget/component/index.tsx Modifies togglePopoverVisibility method to accept an optional isButtonClick parameter and adjusts popover visibility logic accordingly.
app/client/src/widgets/SelectWidget/component/index.test.tsx Adds a new test suite for SelectComponent, verifying the correct invocation of dropdown callbacks.

Possibly related PRs

Suggested labels

Bug, Enhancement, ok-to-test, Widgets & Accelerators Pod, Integrations Pod General

Suggested reviewers

  • sagar-qa007
  • rahulbarwal
  • ApekshaBhosale

Poem

In the realm of widgets, a new test takes flight,
With dropdowns and alerts, all shining so bright.
Clicks and selections, a dance in the air,
Ensuring each action is handled with care.
A rabbit's delight in this code we embrace,
Testing the Select, bringing joy to the space! 🐇✨


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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b7853a9 and 86c083e.

Files selected for processing (2)
  • app/client/cypress/e2e/Regression/ClientSide/Widgets/Select/Select4_spec.ts (1 hunks)
  • app/client/src/widgets/SelectWidget/component/index.tsx (1 hunks)
Files skipped from review due to trivial changes (1)
  • app/client/src/widgets/SelectWidget/component/index.tsx
Additional context used
Path-based instructions (1)
app/client/cypress/e2e/Regression/ClientSide/Widgets/Select/Select4_spec.ts (1)

Pattern app/client/cypress/**/**.*: Follow best practices for Cypress code and e2e automation.
Avoid using cy.wait in code.
Avoid using cy.pause in code.
Use variables for locators, not strings.
Use data-* attributes for selectors; avoid Xpaths and CSS attributes.
Avoid selectors like .btn.submit or button[type=submit].
Perform logins via API with LoginFromAPI.
Only interact with controlled sites/servers.
Ensure tests can run with it.only and are independent.
Use before, beforeEach, after, afterEach correctly; clean state before tests.
Check new specs for flakiness by running them 10 times on CI.
Use multiple assertions; don't treat Cypress as unit tests.
Use constants for strings.
Include datasource operations in before hooks.

Additional comments not posted (3)
app/client/cypress/e2e/Regression/ClientSide/Widgets/Select/Select4_spec.ts (3)

1-12: LGTM! Imports and describe block are correctly structured.

The imports follow best practices for Cypress code. The describe block includes appropriate tags.


14-16: LGTM! before hook is correctly setting up the test environment.

The before hook appropriately drags and drops a Select widget.


19-34: LGTM! Ensure test flakiness is checked.

The it block follows best practices for Cypress tests. Ensure this test is not flaky by running it multiple times on CI.

…to fix/bug-alert-shows-twice-for-select-widget-26696
@PrasadMadine PrasadMadine changed the title fixed the bug alert message is showing twice in select widget and add… fix:Ensure alert message triggers only once when onDropdownClose in select widget Jul 10, 2024
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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 86c083e and ebf1d7d.

Files selected for processing (1)
  • app/client/cypress/e2e/Regression/ClientSide/Widgets/Select/Select4_spec.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • app/client/cypress/e2e/Regression/ClientSide/Widgets/Select/Select4_spec.ts

coderabbitai[bot]
coderabbitai bot previously approved these changes Jul 12, 2024
@PrasadMadine
Copy link
Contributor Author

Hi @sagar-qa007 , Please go through the files changed, the screenshot you have attached is the previous one before update.

@PrasadMadine
Copy link
Contributor Author

Hi @sagar-qa007 , Can you please review this, if there are any comments will work on those.

Copy link

This PR has not seen activitiy for a while. It will be closed in 7 days unless further activity is detected.

@github-actions github-actions bot added the Stale label Jul 29, 2024
@PrasadMadine
Copy link
Contributor Author

Hello @sagar-qa007 , Can you please review this PR?
I will work on the comments if there are any.

@github-actions github-actions bot removed the Stale label Aug 2, 2024
@rahulbarwal rahulbarwal added the Widgets Product This label groups issues related to widgets label Aug 5, 2024
@sagar-qa007
Copy link
Contributor

@rahulbarwal Please check this PR.

@PrasadMadine
Copy link
Contributor Author

Hello @rahulbarwal Please review this PR

@PrasadMadine
Copy link
Contributor Author

Hello @rahulbarwal , Could you please review this PR.

1 similar comment
@PrasadMadine
Copy link
Contributor Author

Hello @rahulbarwal , Could you please review this PR.

@rahulbarwal
Copy link
Contributor

Thanks, @PrasadMadine for explanation.
I debugged this further, and found that this removal will create one issue, i.e. the select dropdown will not close when you clicking outside of the select.

  • The solution you've proposed is incorrect and needs further debugging.
  • We need to find out the root cause of why togglePopoverVisibility is getting called twice?

@PrasadMadine
Copy link
Contributor Author

Hello @rahulbarwal , Thanks for your inputs, I have addressed the comments. Could you please review this.
Thankyou

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: 2

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 98d07b2 and ad953b3.

📒 Files selected for processing (1)
  • app/client/src/widgets/SelectWidget/component/index.tsx (2 hunks)

app/client/src/widgets/SelectWidget/component/index.tsx Outdated Show resolved Hide resolved
app/client/src/widgets/SelectWidget/component/index.tsx Outdated Show resolved Hide resolved
Copy link
Contributor

@rahulbarwal rahulbarwal left a comment

Choose a reason for hiding this comment

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

Code rabbit ai's suggestion makes sense, lets incorporate changes according to that.
@sagar-qa007 , please review the cypress changes.

app/client/src/widgets/SelectWidget/component/index.tsx Outdated Show resolved Hide resolved
app/client/src/widgets/SelectWidget/component/index.tsx Outdated Show resolved Hide resolved
@PrasadMadine
Copy link
Contributor Author

Hello @rahulbarwal , Addressed the suggested changes, can you please review this.

Copy link
Contributor

@rahulbarwal rahulbarwal left a comment

Choose a reason for hiding this comment

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

@PrasadMadine Thanks for being consistent with recommendations 🙏 .
One final suggestion, we don't need a cypress test for this. We can simply create one unit test here which mocks the onDropdownClose prop and ensures that it is called only once on clicking the select button.

PS: We are internally pivoting towards unit tests where ever possible.

@PrasadMadine
Copy link
Contributor Author

Hello @rahulbarwal , Will work on unit test case and should I remove the cypress file.

@rahulbarwal
Copy link
Contributor

Hello @rahulbarwal , Will work on unit test case and should I remove the cypress file.

Yes

@PrasadMadine
Copy link
Contributor Author

Hello @rahulbarwal , I have added the unit test, please review this and let me know if there are any changes to be done.
Thankyou

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)
app/client/src/widgets/SelectWidget/component/index.test.tsx (2)

6-23: Your mock props are like a well-organized pencil case!

You've done a splendid job setting up the mock props for our SelectComponent. It's comprehensive and well-structured. However, let's add a little more color to our drawing, shall we?

Consider adding a few more realistic options to the options array. It'll make our test even more lifelike! Here's a suggestion:

 options: [],
+options: [
+  { label: "Option 1", value: "1" },
+  { label: "Option 2", value: "2" },
+  { label: "Option 3", value: "3" },
+],

25-36: A gold star for your test, but let's add a few more questions to our pop quiz!

Your test case is spot on, addressing the issue of multiple alerts. It's like solving a tricky math problem correctly! However, to ensure our SelectComponent is as smart as you are, let's add a few more test cases.

Consider adding these additional test scenarios:

  1. Test if onDropdownOpen is called when the dropdown is opened.
  2. Verify that selecting an option calls onOptionSelected.
  3. Check if filtering works correctly when isFilterable is true.

Here's a starter for one of these:

it("should call onOptionSelected when an option is selected", () => {
  const options = [
    { label: "Option 1", value: "1" },
    { label: "Option 2", value: "2" },
  ];
  render(<SelectComponent {...mockProps} options={options} />);
  
  const dropdownButton = screen.getByRole("button");
  userEvent.click(dropdownButton);
  
  const option = screen.getByText("Option 1");
  userEvent.click(option);

  expect(mockProps.onOptionSelected).toHaveBeenCalledWith(options[0]);
});
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between f8da8fc and b7d1357.

📒 Files selected for processing (1)
  • app/client/src/widgets/SelectWidget/component/index.test.tsx (1 hunks)
🔇 Additional comments (1)
app/client/src/widgets/SelectWidget/component/index.test.tsx (1)

1-4: Well done on your imports, class!

You've correctly imported all the necessary libraries and components for our test. It's like you've packed all the right books for our coding adventure!

@PrasadMadine
Copy link
Contributor Author

Hello @rahulbarwal , Can you please review this PR and let me know if there are any changes to be done.

Copy link
Contributor

@rahulbarwal rahulbarwal left a comment

Choose a reason for hiding this comment

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

Minor change requested.

@PrasadMadine
Copy link
Contributor Author

Hello @rahulbarwal , I have made the suggested changes, Could you please review this.
Thankyou

@rahulbarwal
Copy link
Contributor

Client lint is failing: https://github.com/appsmithorg/appsmith/actions/runs/11119240699/job/30894114764?pr=36575#step:9:5209

Please review and fix. Run yarn run lint:ci to ensure it is fixed.

@PrasadMadine
Copy link
Contributor Author

Hello @rahulbarwal , I have fixed the linting errors, Could you please review this again.

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)
app/client/src/widgets/SelectWidget/component/index.tsx (1)

Line range hint 1-524: Excellent work on fixing the double-alert issue!

Class, let's recap what we've learned from this code review:

  1. We added a new method togglePopoverVisibilityFromButton to handle button-specific logic.
  2. We updated togglePopoverVisibility to prevent double-triggering of alerts.
  3. We connected our new method to the SelectButton component.

These changes effectively solve the issue of the alert being triggered twice when closing the dropdown. The code is well-structured and the solution is elegantly implemented.

For extra credit, consider the following:

  1. Add comments explaining the edge case handling in togglePopoverVisibility. This will help future developers understand the reasoning behind the condition.
  2. Consider writing unit tests for these new scenarios to ensure the fix remains robust as the codebase evolves.

Overall, excellent problem-solving skills demonstrated here!

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between a328709 and 0519280.

📒 Files selected for processing (2)
  • app/client/src/widgets/SelectWidget/component/index.test.tsx (1 hunks)
  • app/client/src/widgets/SelectWidget/component/index.tsx (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • app/client/src/widgets/SelectWidget/component/index.test.tsx
🔇 Additional comments (3)
app/client/src/widgets/SelectWidget/component/index.tsx (3)

78-80: Well done on adding a specific method for button interactions!

Class, let's take a moment to appreciate the new togglePopoverVisibilityFromButton method. This is an excellent example of separating concerns in our code. By creating a dedicated method for button click events, we're making our code more readable and maintainable.

Can anyone tell me why this might be beneficial? That's right! It allows us to handle button clicks differently if needed in the future, without affecting other ways of toggling the popover.


82-86: Excellent solution to prevent double-triggering!

Now, class, let's examine the improvements made to our togglePopoverVisibility method. Can anyone spot the clever addition? That's right, we've added an isButtonClick parameter!

This change directly addresses our problem of the alert appearing twice. By adding a condition to check if it's a button click and if the popover is already open, we prevent unnecessary toggling. This is a perfect example of how small, thoughtful changes can solve tricky issues.

Remember, students: always consider edge cases in your code!


467-467: Great job connecting the dots!

Class, let's turn our attention to line 467. Can anyone tell me what changed here? Correct! We've updated the togglePopoverVisibility prop to use our new togglePopoverVisibilityFromButton method.

This is a crucial step in implementing our solution. By making this change, we ensure that when the select button is clicked, it uses our new logic that prevents double-triggering of the alert.

Remember, students: when you make changes to your code, always make sure to update all the relevant parts to maintain consistency!

@rahulbarwal
Copy link
Contributor

@PrasadMadine it is still failing with your latest change.
Here is my local run: https://app.warp.dev/block/ZHULhPP8Mc4yM6P54274KR

  • Please run yarn run lint:ci to ensure it is fixed.
  • Also, while you are at it, lets rename index.test.tsx to something meaningful.
appsmith]: /Users/rahulbarwal/Documents/office/code/appsmith/app/client/src/widgets/SelectWidget/component/index.test.tsx
[appsmith]:   2:10  error  'act' is defined but never used  @typescript-eslint/no-unused-vars
[appsmith]:
[appsmith]: /Users/rahulbarwal/Documents/office/code/appsmith/app/client/src/widgets/SelectWidget/component/index.tsx
[appsmith]:   200:1   error    Delete `······`                                                            prettier/prettier

@PrasadMadine
Copy link
Contributor Author

  • index.test.tsx

Hello @rahulbarwal , Can I rename it as SelectWidgetComponent.test.tsx ?

@PrasadMadine
Copy link
Contributor Author

Hello @rahulbarwal , I have made the changes, run this yarn run lint:ci command and checked the linting errors.
Thankyou

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)
app/client/src/widgets/SelectWidget/component/SelectWidgetComponent.test.tsx (2)

5-22: Good job setting up your mock props, students!

Your mock props cover most of the component's properties, which is excellent. However, let's make it even better:

  1. Consider using more realistic values for properties like height and width. For example:

    height: 40,
    width: 200,
  2. It might be helpful to add a few options to test selection functionality:

    options: [
      { label: "Option 1", value: "1" },
      { label: "Option 2", value: "2" },
    ],

These changes will make your tests more robust and realistic. Keep up the good work!


24-36: Excellent start on your test suite, class!

Your test case effectively addresses the issue of onDropdownClose being called multiple times. However, to ensure a comprehensive test suite, consider adding the following test cases:

  1. Test the onDropdownOpen event:

    it("should call onDropdownOpen when select button is clicked", () => {
      render(<SelectComponent {...mockProps} />);
      const dropdownButton = screen.getByTestId("selectbutton.btn.main");
      fireEvent.click(dropdownButton);
      expect(mockProps.onDropdownOpen).toHaveBeenCalledTimes(1);
    });
  2. Test option selection:

    it("should call onOptionSelected when an option is selected", () => {
      render(<SelectComponent {...mockProps} options={[{ label: "Option 1", value: "1" }]} />);
      const dropdownButton = screen.getByTestId("selectbutton.btn.main");
      fireEvent.click(dropdownButton);
      const option = screen.getByText("Option 1");
      fireEvent.click(option);
      expect(mockProps.onOptionSelected).toHaveBeenCalledTimes(1);
    });
  3. Test filtering if isFilterable is true:

    it("should call onFilterChange when filter input changes", () => {
      render(<SelectComponent {...mockProps} isFilterable={true} />);
      const dropdownButton = screen.getByTestId("selectbutton.btn.main");
      fireEvent.click(dropdownButton);
      const filterInput = screen.getByPlaceholderText("Search...");
      fireEvent.change(filterInput, { target: { value: 'test' } });
      expect(mockProps.onFilterChange).toHaveBeenCalledTimes(1);
    });

Adding these test cases will provide better coverage and ensure the component behaves correctly in various scenarios. Keep up the great work!

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 0519280 and bbc672b.

📒 Files selected for processing (2)
  • app/client/src/widgets/SelectWidget/component/SelectWidgetComponent.test.tsx (1 hunks)
  • app/client/src/widgets/SelectWidget/component/index.tsx (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • app/client/src/widgets/SelectWidget/component/index.tsx
🔇 Additional comments (1)
app/client/src/widgets/SelectWidget/component/SelectWidgetComponent.test.tsx (1)

1-3: Well done, class! Your imports are spot on!

You've correctly imported React, testing utilities, and the component under test. This sets a good foundation for your test suite.

@yatinappsmith yatinappsmith merged commit 62a208c into appsmithorg:release Oct 4, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Widgets Product This label groups issues related to widgets
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants