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

Open
wants to merge 9 commits into
base: release
Choose a base branch
from

Conversation

PrasadMadine
Copy link

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

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

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
Author

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

@PrasadMadine
Copy link
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
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
Author

Hello @rahulbarwal Please review this PR

@PrasadMadine
Copy link
Author

Hello @rahulbarwal , Could you please review this PR.

1 similar comment
@PrasadMadine
Copy link
Author

Hello @rahulbarwal , Could you please review this PR.

@@ -177,7 +177,7 @@ class SelectComponent extends React.Component<
};
handleCloseList = () => {
if (this.state.isOpen) {
this.togglePopoverVisibility();
// this.togglePopoverVisibility();
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. Instead of putting a comment, lets remove this line completely.
  2. For my curiosity, please explain, how this line was causing the alert to come twice? How is it showing that alert once now(which line)?

Copy link
Author

Choose a reason for hiding this comment

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

Screenshot from 2024-07-05 09-45-34
Screenshot from 2024-07-05 09-44-50

Here in handleCloseList, if isOpen is true togglePopoverVisibility is calling so in togglePopoverVisibility, again it is checking isOpen, so that’s why it is calling two times.

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

PrasadMadine commented Sep 25, 2024

Hello @rahulbarwal , I have fixed the bug with new approach and also addressed the comments, other functionality is also working fine. Could you please review this PR..
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: 3

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

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

📒 Files selected for processing (1)
  • app/client/cypress/e2e/Regression/ClientSide/Widgets/Select/Select4_spec.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
app/client/cypress/e2e/Regression/ClientSide/Widgets/Select/Select4_spec.ts (1)

Pattern app/client/cypress/**/**.*: Review the following e2e test code written using the Cypress test library. Ensure that:

  • Follow best practices for Cypress code and e2e automation.
  • Avoid using cy.wait in code.
  • Avoid using cy.pause in code.
  • Avoid using agHelper.sleep().
  • Use locator variables for locators and do not use plain strings.
  • Use data-* attributes for selectors.
  • Avoid Xpaths, Attributes and CSS path.
  • Avoid selectors like .btn.submit or button[type=submit].
  • Perform logins via API with LoginFromAPI.
  • Perform logout via API with LogOutviaAPI.
  • Perform signup via API with SignupFromAPI.
  • Avoid using it.only.
  • Avoid using after and aftereach in test cases.
  • Use multiple assertions for expect statements.
  • Avoid using strings for assertions.
  • Do not use duplicate filenames even with different paths.
  • Avoid using agHelper.Sleep, this.Sleep in any file in code.
🔇 Additional comments not posted (3)
app/client/cypress/e2e/Regression/ClientSide/Widgets/Select/Select4_spec.ts (3)

1-14: Well done on the imports and file structure, class!

The imports are nicely organized, and the file structure follows Cypress conventions. I'm pleased to see you've included the issue link as requested in a previous review. Keep up the good work!


15-17: Good job setting up the widget, students!

Your use of the before hook to set up the widget is a great practice. It helps keep our test clean and organized.


20-37: Excellent work on setting up the event handlers!

You've done a fantastic job setting up the JavaScript contexts for the various events. This is a crucial part of testing the widget's behavior.

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.

The solution will work :)
Still I would not recommend this approach.
Although you are thinking in the right direction now, the main problem was that togglePopoverVisibility was getting called twice.
Reason

  1. once from this line(in popoverProps):
    onClose: this.handleCloseList,
  2. and, second time due to the button click.

Which means all we have to do is add an exception to the togglePopoverVisibility function to identify if it is being called from the button click (in case of close).
Instead of making these complex changes lets send a flag back from button click to togglePopoverVisibility (which now has an exception to handle buttonclick separately)

@PrasadMadine
Copy link
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
Comment on lines 79 to 87
if (!isButtonClick && this.state.isOpen) {
this.handleOnDropdownClose();
} else {
this.setState({ isOpen: false });
} else if (isButtonClick) {
this.handleOnDropdownOpen();
this.setState((prev) => ({
...prev,
isOpen: !prev.isOpen,
}));
Copy link
Contributor

Choose a reason for hiding this comment

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

We can not remove handleOnDropdownClose and handleOnDropdownClose functionality. Instead I'd recommend:

togglePopoverVisibilityFromButton = () => {
    this.togglePopoverVisibility(true);
  };

  togglePopoverVisibility = (isButtonClick = false) => {
    // This is an edge case, this method gets called twice if user closes it by clicking on the `SelectButton`
    // which in turn triggers handleOnDropdownClose twice, to solve we have this exception to tell if click event is from button
    if (isButtonClick && this.state.isOpen) return;

    if (this.state.isOpen) {
      this.handleOnDropdownClose();
    } else {
      this.handleOnDropdownOpen();
    }

    this.setState({ isOpen: !this.state.isOpen });
  };

@PrasadMadine
Copy link
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
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
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!

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.

3 participants