PromucFlow_constructor/app/client/cypress/e2e/Regression/ClientSide
Jacques Ikot c0e6c3c0ba
fix: map widget marker onClick duplication (#39517)
## Description
When clicking on a marker in the Map Widget, the onMarkerClick event was
being triggered twice for a single click, causing duplicate actions and
potentially unexpected behavior in applications.

```js
// In MapWidget/widget/index.tsx
onMarkerClick = (lat?: number, long?: number, title?: string) => {
  this.disableDrag(true);
  const selectedMarker = {
    lat: lat,
    long: long,
    title: title,
  };
  console.log("🚀 ~ MapWidget ~ title:", title); // This was logging twice

  this.props.updateWidgetMetaProperty("selectedMarker", selectedMarker, {
    triggerPropertyName: "onMarkerClick",
    dynamicString: this.props.onMarkerClick,
    event: {
      type: EventType.ON_MARKER_CLICK,
    },
  });
};
```

## Root Cause
This happens because:
1. The Marker component was adding duplicate event listeners to the
Google Maps marker.
2. Two separate useEffect hooks were both adding "click" event
listeners:
   - One during marker initialisation
   - Another in a separate useEffect that tracks the onClick prop

```js
// First listener in initialization useEffect
googleMapMarker.addListener("click", () => {
  if (onClick) onClick();
});

// Second listener in a separate useEffect
marker.addListener("click", () => {
  if (onClick) onClick();
});
```
3. When a marker was clicked, both event listeners fired, causing the
onClick callback to execute twice.
4. This resulted in onMarkerClick being called twice for a single user
interaction.

## Solution
Modify the `Marker.tsx` component to avoid adding duplicate event
listeners:
1. Remove the click event listener from the initialisation useEffect
2. Add proper cleanup for event listeners using clearListeners and
removeListener
3. Store the listener reference to properly remove it in the cleanup
function
4. Apply the same pattern to the dragend event listener

```js
// track on onclick - with proper cleanup
useEffect(() => {
  if (!marker) return;

  // Clear existing click listeners before adding a new one
  google.maps.event.clearListeners(marker, "click");
  
  // Add the new click listener
  const clickListener = marker.addListener("click", () => {
    if (onClick) onClick();
  });

  // Return cleanup function
  return () => {
    google.maps.event.removeListener(clickListener);
  };
}, [marker, onClick]);
```

## Why this works
1. Properly cleans up existing listeners before adding new ones
2. Ensures only one click handler is active at any time
3. Prevents event handler duplication during component re-renders

## Testing
Added a Cypress test that verifies the marker click event triggers
exactly once per click:
1. Creates a map widget with a marker
2. Uses a JS Object to track click count
3. Verifies the click count increments by exactly 1 for each click
4. Ensures no duplicate events are triggered


Fixes #39514 

## Automation

/ok-to-test tags="@tag.Widget, @tag.Sanity, @tag.Maps, @tag.Binding"

### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results  -->
> [!TIP]
> 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
> Workflow run:
<https://github.com/appsmithorg/appsmith/actions/runs/13673511245>
> Commit: 3c025ebd9dd85a3c409bc1582e13e324d8dbb2cd
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=13673511245&attempt=1"
target="_blank">Cypress dashboard</a>.
> Tags: `@tag.Widget, @tag.Sanity, @tag.Maps, @tag.Binding`
> Spec:
> <hr>Wed, 05 Mar 2025 11:49:24 UTC
<!-- end of auto-generated comment: Cypress test results  -->


## Communication
Should the DevRel and Marketing teams inform users about this change?
- [ ] Yes
- [x] No


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **Bug Fixes**
- Enhanced the behavior of map markers so they respond more reliably to
user interactions like clicking and dragging, addressing issues that
could affect overall stability.

- **Tests**
- Added comprehensive tests to ensure marker functionality remains
consistent and dependable during use.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-03-05 15:25:52 +01:00
..
ActionExecution fix: console log update and fix modal issue (#38692) 2025-01-19 09:25:04 +05:30
AdminSettings test: Admin setting new test cases with email (#38522) 2025-01-30 17:25:07 +05:30
Anvil chore: fix form widgets bugs (#38492) 2025-01-07 17:34:15 +05:30
AppNavigation chore: Addding dependent tags (#36965) 2024-10-18 18:19:08 +05:30
AuditLogs
Autocomplete feat: update autocomplete sorting (#38690) 2025-01-17 16:30:52 +05:30
Binding chore: update select component (#38954) 2025-02-06 13:10:25 +05:30
Branding chore: Migrate Tenant to Organization (#38891) 2025-02-18 20:41:07 +05:30
BugTests fix: Enabled js mode by default for Table and Select widget form display ui button (#39032) 2025-02-06 14:50:41 +05:30
CodeComment chore: Addding dependent tags (#36965) 2024-10-18 18:19:08 +05:30
CommunityTemplate chore: Addding dependent tags (#36965) 2024-10-18 18:19:08 +05:30
Debugger fix: Fix flakiness of JSObjects_navigation_spec.ts spec (#39269) 2025-02-14 16:57:13 +05:30
DynamicHeight chore: removed old flags for airgap instances (#36609) 2024-10-07 15:26:25 +05:30
Editor
EmbedSettings fix: Fix copy issue in embedsetting spec (#38590) 2025-01-13 10:43:55 +05:30
ExplorerTests test: validate 'Show Bindings' menu for queries & JS Objects in split-pane (#38577) 2025-01-13 10:51:17 +05:30
Fork test: adding tests for Fork app related to multiple workspace (#39263) 2025-02-25 13:28:33 +05:30
FormLogin chore: change snapshot package (#35867) 2024-09-12 11:09:42 +03:00
FormNativeToRawTests
Git chore: fixes mergeviaremote_spec flakiness (#38635) 2025-01-14 13:56:46 +01:00
Github
Google
Homepage chore: Addding dependent tags (#36965) 2024-10-18 18:19:08 +05:30
IDE chore: entity tabs replacement (#38989) 2025-02-06 11:33:54 +03:00
JSLibrary test: adding tests for custom lib (#37901) 2024-12-11 10:56:38 +05:30
JSObject chore: entity tabs replacement (#38989) 2025-02-06 11:33:54 +03:00
Linting feat: Enable new toolbar for cypress (#37148) 2024-12-03 09:21:43 +05:30
Login test: Sign in and Sign up cases (#39028) 2025-02-05 15:20:33 +05:30
MobileResponsiveTests chore: skipping cases for mockdb usage (#38888) 2025-01-30 11:47:38 +05:30
Onboarding chore: Addding dependent tags (#36965) 2024-10-18 18:19:08 +05:30
OneClickBinding chore: skipping cases for mockdb usage (#38888) 2025-01-30 11:47:38 +05:30
OtherUIFeatures fix: Fix from for promise failure (#39295) 2025-02-17 12:52:38 +05:30
PartialImportExport test: adding test for page functionality (#38538) 2025-01-18 20:26:42 +05:30
PeekOverlay feat: Inspect State CTA for discovery (#39100) 2025-02-14 21:57:08 +05:30
Performance chore: Addding dependent tags (#36965) 2024-10-18 18:19:08 +05:30
ProductRamps chore: rm dr ce (#34765) 2024-07-31 08:24:51 +05:30
PropertyPane fix: Revert "Revert "feat: Added focus ring for focus visible (#37700)" (#… (#38655) 2025-02-03 11:12:29 +05:30
PublishedApps test: flaky check for published Spec (#38427) 2025-01-03 12:08:10 +05:30
Refactoring chore: Addding dependent tags (#36965) 2024-10-18 18:19:08 +05:30
SetProperty chore: Addding dependent tags (#36965) 2024-10-18 18:19:08 +05:30
SettingsPane chore: Added sanity and tags for blank tag specs (#36421) 2024-09-19 18:21:58 +05:30
Templates chore: git mod - test fixes (#38357) 2025-01-07 12:30:42 +01:00
ThemingTests chore: Addding dependent tags (#36965) 2024-10-18 18:19:08 +05:30
UserProfile
VisualTests fix: Checking fix working for js indent failure (#38382) 2024-12-27 08:43:46 +05:30
Widgets fix: map widget marker onClick duplication (#39517) 2025-03-05 15:25:52 +01:00
Workspace chore: Addding dependent tags (#36965) 2024-10-18 18:19:08 +05:30