From abe860bec754829758c4f0c27cc5db2f2f91d17b Mon Sep 17 00:00:00 2001 From: Marcel Date: Tue, 2 Jun 2026 19:48:39 +0200 Subject: [PATCH] test(hooks): migrate useUnsavedWarning spec to shared $app/navigation mock Replaces the local beforeNavigate-capture plumbing and simulateNavigate helper with the shared $mocks/$app/navigation module via a sync factory. The per-test reset now comes from the shared module's embedded beforeEach. Part of #560. Co-Authored-By: Claude Opus 4.8 --- .../hooks/useUnsavedWarning.svelte.test.ts | 31 +++---------------- 1 file changed, 4 insertions(+), 27 deletions(-) diff --git a/frontend/src/lib/shared/hooks/useUnsavedWarning.svelte.test.ts b/frontend/src/lib/shared/hooks/useUnsavedWarning.svelte.test.ts index d85dc4c3..48cc1f08 100644 --- a/frontend/src/lib/shared/hooks/useUnsavedWarning.svelte.test.ts +++ b/frontend/src/lib/shared/hooks/useUnsavedWarning.svelte.test.ts @@ -1,35 +1,12 @@ -import { describe, it, expect, vi, beforeEach } from 'vitest'; +import { describe, it, expect, vi } from 'vitest'; +import * as navMock from '$mocks/$app/navigation'; -// Capture the beforeNavigate callback so tests can simulate navigation events -let registeredBeforeNavigate: - | ((nav: { cancel: () => void; to: { url: { href: string } } | null }) => void) - | null = null; +vi.mock('$app/navigation', () => ({ ...navMock })); -const mockGoto = vi.fn(); - -vi.mock('$app/navigation', () => ({ - beforeNavigate: vi.fn((fn: typeof registeredBeforeNavigate) => { - registeredBeforeNavigate = fn; - }), - goto: mockGoto -})); +const { simulateNavigate, goto: mockGoto } = navMock; const { createUnsavedWarning } = await import('./useUnsavedWarning.svelte'); -function simulateNavigate(href: string | null = '/somewhere') { - const cancel = vi.fn(); - registeredBeforeNavigate?.({ - cancel, - to: href ? { url: { href } } : null - }); - return cancel; -} - -beforeEach(() => { - registeredBeforeNavigate = null; - mockGoto.mockClear(); -}); - describe('createUnsavedWarning', () => { it('isDirty starts false', () => { const w = createUnsavedWarning();