Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,21 @@ function isLocationQuery(s: string | any) {
return typeof s === 'string' && (s.split(',').length > 2 || s.includes('+'))
}

type ScriptGoogleMapsCenter = ScriptGoogleMapsProps['center'] | google.maps.MapOptions['center']

function getCenterWatchKey(center: ScriptGoogleMapsCenter): string | undefined {
const raw = toRaw(center)
if (!raw)
return undefined
if (typeof raw === 'string')
return `query:${raw}`
const lat = typeof (raw as any).lat === 'function' ? (raw as any).lat() : (raw as any).lat
const lng = typeof (raw as any).lng === 'function' ? (raw as any).lng() : (raw as any).lng
if (lat != null && lng != null)
return `latlng:${lat},${lng}`
return undefined
}

const queryToLatLngCache = new Map<string, google.maps.LatLng | google.maps.LatLngLiteral>()

async function resolveQueryToLatLng(query: string) {
Expand Down Expand Up @@ -449,14 +464,14 @@ onMounted(() => {
// Clear centerOverride when the controlled center prop changes so external
// updates take effect (otherwise centerOverride, written from the user's
// pan during re-init, would permanently win over future prop updates).
watch([() => props.center, () => props.mapOptions?.center], () => {
watch([() => getCenterWatchKey(props.center), () => getCenterWatchKey(props.mapOptions?.center)], () => {
centerOverride.value = undefined
})
watch([() => options.value.center, isMapReady, map], async (next) => {
watch([() => getCenterWatchKey(options.value.center), isMapReady, map], async () => {
if (!map.value) {
return
}
let center = toRaw(next[0])
let center = toRaw(options.value.center)
if (center) {
if (isLocationQuery(center) && isMapReady.value) {
center = await resolveQueryToLatLng(center as string)
Expand Down
32 changes: 32 additions & 0 deletions test/unit/google-maps-regressions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,38 @@ describe('google Maps Regressions', () => {

expect(map.setCenter).not.toHaveBeenCalled()
})

it('uses a stable center watch key for equivalent inline mapOptions centers', () => {
function getCenterWatchKey(center: any) {
if (!center)
return undefined
if (typeof center === 'string')
return `query:${center}`
const lat = typeof center.lat === 'function' ? center.lat() : center.lat
const lng = typeof center.lng === 'function' ? center.lng() : center.lng
if (lat != null && lng != null)
return `latlng:${lat},${lng}`
return center
}

const firstRender = { center: { lat: -34.397, lng: 150.644 }, zoom: 8 }
const secondRender = { center: { lat: -34.397, lng: 150.644 }, zoom: 8 }

expect(firstRender.center).not.toBe(secondRender.center)
expect(getCenterWatchKey(firstRender.center)).toBe(getCenterWatchKey(secondRender.center))
})

it('changes the center watch key when coordinates actually change', () => {
function getCenterWatchKey(center: any) {
const lat = typeof center.lat === 'function' ? center.lat() : center.lat
const lng = typeof center.lng === 'function' ? center.lng() : center.lng
return `latlng:${lat},${lng}`
}

expect(getCenterWatchKey({ lat: -34.397, lng: 150.644 }))
.not
.toBe(getCenterWatchKey({ lat: -34.387, lng: 150.654 }))
})
Comment on lines +459 to +489
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win

Avoid testing a duplicated helper implementation.

These tests re-implement getCenterWatchKey locally, so they can still pass if the production logic changes/regresses. Please extract the helper to a shared module (or test via component behavior) and import that single implementation here.

Proposed direction
-    it('uses a stable center watch key for equivalent inline mapOptions centers', () => {
-      function getCenterWatchKey(center: any) {
-        ...
-      }
+    it('uses a stable center watch key for equivalent inline mapOptions centers', () => {
+      // import { getCenterWatchKey } from '../../packages/script/src/runtime/components/GoogleMaps/centerWatchKey'
       ...
     })
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@test/unit/google-maps-regressions.test.ts` around lines 459 - 489, Tests
duplicate the getCenterWatchKey helper inline which hides regressions; remove
the local implementations and import the canonical helper instead. Update
google-maps-regressions.test.ts to import the shared getCenterWatchKey function
(used by production code) and replace the inline functions in both specs with
calls to that imported helper so the tests validate the real implementation's
behavior rather than a duplicated copy.

})

describe('infoWindow group close', () => {
Expand Down
Loading