|
4 | 4 | import { mount } from '@vue/test-utils' |
5 | 5 | import { beforeEach, describe, expect, it, vi } from 'vitest' |
6 | 6 | import { defineComponent, h, nextTick, onUnmounted, provide, ref, shallowRef } from 'vue' |
7 | | -import { MAP_INJECTION_KEY, useGoogleMapsResource } from '../../packages/script/src/runtime/components/GoogleMaps/useGoogleMapsResource' |
| 7 | +import { MAP_INJECTION_KEY, useGoogleMapsResource, waitForMapsReady } from '../../packages/script/src/runtime/components/GoogleMaps/useGoogleMapsResource' |
8 | 8 | import { createMockGoogleMapsAPI } from './__mocks__/google-maps-api' |
9 | 9 |
|
10 | 10 | // Helper to create a wrapper component that provides mock map context |
@@ -367,3 +367,112 @@ describe('google Maps component lifecycle - memory leak prevention', () => { |
367 | 367 | ) |
368 | 368 | }) |
369 | 369 | }) |
| 370 | + |
| 371 | +describe('waitForMapsReady', () => { |
| 372 | + // Regression: the previous resolveQueryToLatLng wait pattern used a |
| 373 | + // non-immediate watch after `await load()`. If load() populated mapsApi |
| 374 | + // synchronously, the watcher missed the change and the promise hung |
| 375 | + // forever. The fix re-checks state after load() and uses an immediate |
| 376 | + // watcher (matching importLibrary's pattern). It also waits for `map` |
| 377 | + // to be set, since PlacesService construction requires it. |
| 378 | + |
| 379 | + it('returns immediately when both refs are already populated', async () => { |
| 380 | + const mapsApi = shallowRef<any>({ places: {} }) |
| 381 | + const map = shallowRef<any>({}) |
| 382 | + const status = ref<string>('loaded') |
| 383 | + const load = vi.fn(() => Promise.resolve()) |
| 384 | + |
| 385 | + await waitForMapsReady({ mapsApi, map, status, load }) |
| 386 | + |
| 387 | + // Should not have called load() at all |
| 388 | + expect(load).not.toHaveBeenCalled() |
| 389 | + }) |
| 390 | + |
| 391 | + it('does not hang when load() resolves synchronously', async () => { |
| 392 | + // Reproduces the original race: load() populates mapsApi synchronously, |
| 393 | + // so a non-immediate watcher would never fire. |
| 394 | + const mapsApi = shallowRef<any>(undefined) |
| 395 | + const map = shallowRef<any>(undefined) |
| 396 | + const status = ref<string>('loading') |
| 397 | + const load = vi.fn(() => { |
| 398 | + // Synchronously populate both refs before returning the resolved promise |
| 399 | + mapsApi.value = { places: {} } |
| 400 | + map.value = {} |
| 401 | + return Promise.resolve() |
| 402 | + }) |
| 403 | + |
| 404 | + // The original race would hang forever — vitest's per-test timeout |
| 405 | + // is the deterministic backstop, no wall-clock setTimeout needed. |
| 406 | + await expect(waitForMapsReady({ mapsApi, map, status, load })).resolves.toBeUndefined() |
| 407 | + |
| 408 | + expect(load).toHaveBeenCalledOnce() |
| 409 | + }) |
| 410 | + |
| 411 | + it('resolves when refs are populated asynchronously after load()', async () => { |
| 412 | + const mapsApi = shallowRef<any>(undefined) |
| 413 | + const map = shallowRef<any>(undefined) |
| 414 | + const status = ref<string>('loading') |
| 415 | + const load = vi.fn(() => Promise.resolve()) |
| 416 | + |
| 417 | + const promise = waitForMapsReady({ mapsApi, map, status, load }) |
| 418 | + |
| 419 | + // Populate refs after a tick — simulates the normal onLoaded flow |
| 420 | + await nextTick() |
| 421 | + mapsApi.value = { places: {} } |
| 422 | + await nextTick() |
| 423 | + map.value = {} |
| 424 | + |
| 425 | + await expect(promise).resolves.toBeUndefined() |
| 426 | + }) |
| 427 | + |
| 428 | + it('rejects synchronously when status is already error', async () => { |
| 429 | + const mapsApi = shallowRef<any>(undefined) |
| 430 | + const map = shallowRef<any>(undefined) |
| 431 | + const status = ref<string>('error') |
| 432 | + const load = vi.fn(() => Promise.resolve()) |
| 433 | + |
| 434 | + await expect( |
| 435 | + waitForMapsReady({ mapsApi, map, status, load }), |
| 436 | + ).rejects.toThrow('Google Maps script failed to load') |
| 437 | + |
| 438 | + // Should bail before calling load() |
| 439 | + expect(load).not.toHaveBeenCalled() |
| 440 | + }) |
| 441 | + |
| 442 | + it('rejects when status transitions to error during the wait', async () => { |
| 443 | + const mapsApi = shallowRef<any>(undefined) |
| 444 | + const map = shallowRef<any>(undefined) |
| 445 | + const status = ref<string>('loading') |
| 446 | + const load = vi.fn(() => Promise.resolve()) |
| 447 | + |
| 448 | + const promise = waitForMapsReady({ mapsApi, map, status, load }) |
| 449 | + |
| 450 | + await nextTick() |
| 451 | + status.value = 'error' |
| 452 | + |
| 453 | + await expect(promise).rejects.toThrow('Google Maps script failed to load') |
| 454 | + }) |
| 455 | + |
| 456 | + it('waits for map even when mapsApi is set first', async () => { |
| 457 | + // Guards against the second bug: PlacesService construction needs |
| 458 | + // map.value, not just mapsApi.value. |
| 459 | + const mapsApi = shallowRef<any>({ places: {} }) |
| 460 | + const map = shallowRef<any>(undefined) |
| 461 | + const status = ref<string>('loading') |
| 462 | + const load = vi.fn(() => Promise.resolve()) |
| 463 | + |
| 464 | + let settled = false |
| 465 | + const promise = waitForMapsReady({ mapsApi, map, status, load }).then(() => { |
| 466 | + settled = true |
| 467 | + }) |
| 468 | + |
| 469 | + // Flush microtasks: settlement should not happen while map is undefined |
| 470 | + await nextTick() |
| 471 | + await nextTick() |
| 472 | + expect(settled).toBe(false) |
| 473 | + |
| 474 | + map.value = {} |
| 475 | + await expect(promise).resolves.toBeUndefined() |
| 476 | + expect(settled).toBe(true) |
| 477 | + }) |
| 478 | +}) |
0 commit comments