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 <noreply@anthropic.com>
This commit is contained in:
@@ -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
|
vi.mock('$app/navigation', () => ({ ...navMock }));
|
||||||
let registeredBeforeNavigate:
|
|
||||||
| ((nav: { cancel: () => void; to: { url: { href: string } } | null }) => void)
|
|
||||||
| null = null;
|
|
||||||
|
|
||||||
const mockGoto = vi.fn();
|
const { simulateNavigate, goto: mockGoto } = navMock;
|
||||||
|
|
||||||
vi.mock('$app/navigation', () => ({
|
|
||||||
beforeNavigate: vi.fn((fn: typeof registeredBeforeNavigate) => {
|
|
||||||
registeredBeforeNavigate = fn;
|
|
||||||
}),
|
|
||||||
goto: mockGoto
|
|
||||||
}));
|
|
||||||
|
|
||||||
const { createUnsavedWarning } = await import('./useUnsavedWarning.svelte');
|
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', () => {
|
describe('createUnsavedWarning', () => {
|
||||||
it('isDirty starts false', () => {
|
it('isDirty starts false', () => {
|
||||||
const w = createUnsavedWarning();
|
const w = createUnsavedWarning();
|
||||||
|
|||||||
Reference in New Issue
Block a user