From ed1bba9371c603317439320d33ab7e1b6b1f3888 Mon Sep 17 00:00:00 2001 From: Maurice Date: Thu, 26 Nov 2026 14:03:46 +0100 Subject: [PATCH] test(client): finish moving the tests to msw 3 Two more window.location stand-ins sat on http://localhost while the page runs on http://localhost:3000; they now use locationAt like the rest, which brings back the plugin settings answer FE-COMP- PLUGINSETTINGS-020 waits for. FE-PAGE-JOURNEYDETAIL-153 ended while the journal reload it set off was still out, which timed out after the test; it now waits for that read. The reload mocks in versionHandover.test.ts carry the signatures of location.replace and location.reload, as locationAt's overrides require. --- .../components/Plugins/usePluginUserSettings.test.ts | 3 ++- .../src/components/Settings/PluginSettingsTab.test.tsx | 3 ++- client/src/pages/JourneyDetailPage.test.tsx | 8 ++++++++ client/src/utils/versionHandover.test.ts | 10 +++++----- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/client/src/components/Plugins/usePluginUserSettings.test.ts b/client/src/components/Plugins/usePluginUserSettings.test.ts index e3909ee7c..fdb7fb672 100644 --- a/client/src/components/Plugins/usePluginUserSettings.test.ts +++ b/client/src/components/Plugins/usePluginUserSettings.test.ts @@ -1,5 +1,6 @@ // FE-COMP-PLUGIN-USERSET-001 to -012: the per plugin user settings logic both settings shells share. import { act, renderHook, waitFor } from '@testing-library/react'; +import { locationAt } from '../../../tests/helpers/location'; import { pluginsApi, type PluginAction, type PluginUserSettingField } from '../../api/client'; import { SECRET_MASK } from './settingsForm'; import { usePluginOAuth, usePluginUserSettings } from './usePluginUserSettings'; @@ -150,7 +151,7 @@ describe('usePluginOAuth', () => { it('FE-COMP-PLUGIN-USERSET-012: a connect hands off to the provider and keeps the button busy', async () => { const realLocation = window.location; - const loc = { href: 'http://localhost/settings' }; + const loc = locationAt('/settings'); Object.defineProperty(window, 'location', { writable: true, configurable: true, value: loc }); try { vi.spyOn(pluginsApi, 'oauthConnect').mockResolvedValue({ diff --git a/client/src/components/Settings/PluginSettingsTab.test.tsx b/client/src/components/Settings/PluginSettingsTab.test.tsx index 9ff8a496c..a5721a016 100644 --- a/client/src/components/Settings/PluginSettingsTab.test.tsx +++ b/client/src/components/Settings/PluginSettingsTab.test.tsx @@ -4,6 +4,7 @@ import userEvent from '@testing-library/user-event'; import { http, HttpResponse } from 'msw'; import { render, screen, waitFor, within } from '../../../tests/helpers/render'; import { server } from '../../../tests/helpers/msw/server'; +import { locationAt } from '../../../tests/helpers/location'; import { ToastContainer } from '../shared/Toast'; import { usePluginStore, type ActivePlugin } from '../../store/pluginStore'; import type { PluginAction, PluginUserSettingField } from '../../api/client'; @@ -367,7 +368,7 @@ describe('PluginSettingsTab', () => { it('FE-COMP-PLUGINSETTINGS-020: an OAuth-only plugin gets a card and Connect hands off to the provider', async () => { const user = userEvent.setup(); - const loc = { href: 'http://localhost/settings', origin: 'http://localhost', pathname: '/settings' }; + const loc = locationAt('/settings'); vi.stubGlobal('location', loc); serve('weather', {}, { configured: true, connected: false }); server.use(http.post('/api/plugin-oauth/weather/connect', () => diff --git a/client/src/pages/JourneyDetailPage.test.tsx b/client/src/pages/JourneyDetailPage.test.tsx index 7821f2a2c..77a4bb9a0 100644 --- a/client/src/pages/JourneyDetailPage.test.tsx +++ b/client/src/pages/JourneyDetailPage.test.tsx @@ -3805,8 +3805,13 @@ describe('JourneyDetailPage', () => { let createCalls = 0; let updateCalls = 0; let providerCalls = 0; + let journeyReads = 0; server.use( + http.get('/api/journeys/1', () => { + journeyReads++; + return HttpResponse.json(mockJourneyDetail); + }), http.get('/api/integrations/memories/:provider/status', () => HttpResponse.json({ connected: true })), http.post('/api/integrations/memories/:provider/search', () => HttpResponse.json({ assets: [{ id: 'context-1', takenAt: '2026-03-15T12:00:00Z', city: 'Rome' }], @@ -3851,10 +3856,13 @@ describe('JourneyDetailPage', () => { expect(createCalls).toBe(1); expect(screen.getByText('New Entry')).toBeInTheDocument(); + const readsBeforeRetry = journeyReads; await user.click(screen.getByRole('button', { name: 'Save' })); await waitFor(() => expect(providerCalls).toBe(2)); expect(createCalls).toBe(1); expect(updateCalls).toBe(1); + // The photos landed and the journal reloads; let that read answer here, not after the test. + await waitFor(() => expect(journeyReads).toBeGreaterThan(readsBeforeRetry)); }); }); diff --git a/client/src/utils/versionHandover.test.ts b/client/src/utils/versionHandover.test.ts index 029136d8f..acd0ba891 100644 --- a/client/src/utils/versionHandover.test.ts +++ b/client/src/utils/versionHandover.test.ts @@ -1,4 +1,4 @@ -import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { afterEach, beforeEach, describe, expect, it, vi, type Mock } from 'vitest' import { locationAt } from '../../tests/helpers/location' /* @@ -76,14 +76,14 @@ function breakStorage(method: 'getItem' | 'setItem', broken: () => Storage) { const marker = () => localStorage.getItem('trek_app_version') // The version the bundle under test is built as (vite define, from package.json). const BUILT = __TREK_UI_VERSION__ -let reload: ReturnType -let plainReload: ReturnType +let reload: Mock +let plainReload: Mock beforeEach(() => { // Every reload here asks for the page under a fresh URL (reloadFresh), which is // a location.replace. `plainReload` is the location.reload() it must not use. - reload = vi.fn() - plainReload = vi.fn() + reload = vi.fn() + plainReload = vi.fn() Object.defineProperty(window, 'location', { writable: true, value: locationAt('/dashboard', { replace: reload, reload: plainReload }),