|
1 | | -import { beforeEach, describe, expect, it, vi } from "vitest"; |
2 | | - |
3 | | -import type { NavigateOptions } from "../../src/common/navigate"; |
4 | | -import { canGoBack, goBack, navigate } from "../../src/common/navigate"; |
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +import type { |
| 4 | + NavigateOptions, |
| 5 | + UnsavedChangesGuard, |
| 6 | +} from "../../src/common/navigate"; |
| 7 | +import { |
| 8 | + canGoBack, |
| 9 | + goBack, |
| 10 | + navigate, |
| 11 | + registerUnsavedChangesGuard, |
| 12 | + unregisterUnsavedChangesGuard, |
| 13 | +} from "../../src/common/navigate"; |
5 | 14 |
|
6 | 15 | // navigate() closes open dialogs before touching history. |
7 | 16 | vi.mock("../../src/dialogs/make-dialog-manager", () => ({ |
@@ -54,6 +63,133 @@ describe("navigate", () => { |
54 | 63 | }); |
55 | 64 | }); |
56 | 65 |
|
| 66 | +describe("unsaved changes guard", () => { |
| 67 | + const registeredGuards: UnsavedChangesGuard[] = []; |
| 68 | + |
| 69 | + const registerGuard = (isDirty: boolean, promptResult = true) => { |
| 70 | + const guard = { |
| 71 | + isDirty: vi.fn(() => isDirty), |
| 72 | + prompt: vi.fn(async () => promptResult), |
| 73 | + }; |
| 74 | + registerUnsavedChangesGuard(guard); |
| 75 | + registeredGuards.push(guard); |
| 76 | + return guard; |
| 77 | + }; |
| 78 | + |
| 79 | + beforeEach(() => { |
| 80 | + setEntry("/config"); |
| 81 | + }); |
| 82 | + |
| 83 | + afterEach(() => { |
| 84 | + registeredGuards.splice(0).forEach(unregisterUnsavedChangesGuard); |
| 85 | + }); |
| 86 | + |
| 87 | + it("navigates without prompting when no guard is dirty", async () => { |
| 88 | + const guard = registerGuard(false); |
| 89 | + |
| 90 | + expect(await navigate("/config/areas")).toBe(true); |
| 91 | + |
| 92 | + expect(window.location.pathname).toEqual("/config/areas"); |
| 93 | + expect(guard.prompt).not.toHaveBeenCalled(); |
| 94 | + }); |
| 95 | + |
| 96 | + it("prompts a dirty guard and navigates when confirmed", async () => { |
| 97 | + const guard = registerGuard(true, true); |
| 98 | + |
| 99 | + expect(await navigate("/config/areas")).toBe(true); |
| 100 | + |
| 101 | + expect(guard.prompt).toHaveBeenCalledOnce(); |
| 102 | + expect(window.location.pathname).toEqual("/config/areas"); |
| 103 | + }); |
| 104 | + |
| 105 | + it("leaves history untouched when the prompt is declined", async () => { |
| 106 | + const guard = registerGuard(true, false); |
| 107 | + const listener = vi.fn(); |
| 108 | + window.addEventListener("location-changed", listener); |
| 109 | + |
| 110 | + expect(await navigate("/config/areas")).toBe(false); |
| 111 | + |
| 112 | + window.removeEventListener("location-changed", listener); |
| 113 | + expect(guard.prompt).toHaveBeenCalledOnce(); |
| 114 | + expect(window.location.pathname).toEqual("/config"); |
| 115 | + expect(listener).not.toHaveBeenCalled(); |
| 116 | + }); |
| 117 | + |
| 118 | + it("does not prompt when navigating to the current path", async () => { |
| 119 | + const guard = registerGuard(true, false); |
| 120 | + |
| 121 | + expect(await navigate("/config")).toBe(true); |
| 122 | + |
| 123 | + expect(guard.prompt).not.toHaveBeenCalled(); |
| 124 | + }); |
| 125 | + |
| 126 | + it("skips a pending prompt once no guard is dirty anymore", async () => { |
| 127 | + let dirty = true; |
| 128 | + let resolvePrompt!: (value: boolean) => void; |
| 129 | + const guard: UnsavedChangesGuard = { |
| 130 | + isDirty: () => dirty, |
| 131 | + prompt: vi.fn( |
| 132 | + () => |
| 133 | + new Promise<boolean>((resolve) => { |
| 134 | + resolvePrompt = resolve; |
| 135 | + }) |
| 136 | + ), |
| 137 | + }; |
| 138 | + registerUnsavedChangesGuard(guard); |
| 139 | + registeredGuards.push(guard); |
| 140 | + |
| 141 | + const first = navigate("/config/areas"); |
| 142 | + dirty = false; |
| 143 | + |
| 144 | + expect(await navigate("/config/devices/dashboard")).toBe(true); |
| 145 | + expect(window.location.pathname).toEqual("/config/devices/dashboard"); |
| 146 | + |
| 147 | + resolvePrompt(true); |
| 148 | + expect(await first).toBe(true); |
| 149 | + expect(guard.prompt).toHaveBeenCalledOnce(); |
| 150 | + }); |
| 151 | + |
| 152 | + it("shares one pending prompt between concurrent navigations", async () => { |
| 153 | + let resolvePrompt!: (value: boolean) => void; |
| 154 | + const guard: UnsavedChangesGuard = { |
| 155 | + isDirty: () => true, |
| 156 | + prompt: vi.fn( |
| 157 | + () => |
| 158 | + new Promise<boolean>((resolve) => { |
| 159 | + resolvePrompt = resolve; |
| 160 | + }) |
| 161 | + ), |
| 162 | + }; |
| 163 | + registerUnsavedChangesGuard(guard); |
| 164 | + registeredGuards.push(guard); |
| 165 | + |
| 166 | + const first = navigate("/config/areas"); |
| 167 | + const second = navigate("/config/devices/dashboard"); |
| 168 | + resolvePrompt(true); |
| 169 | + |
| 170 | + expect(await Promise.all([first, second])).toEqual([true, true]); |
| 171 | + expect(guard.prompt).toHaveBeenCalledOnce(); |
| 172 | + }); |
| 173 | + |
| 174 | + it("stops prompting once the guard is unregistered", async () => { |
| 175 | + const guard = registerGuard(true, false); |
| 176 | + unregisterUnsavedChangesGuard(guard); |
| 177 | + |
| 178 | + expect(await navigate("/config/areas")).toBe(true); |
| 179 | + |
| 180 | + expect(guard.prompt).not.toHaveBeenCalled(); |
| 181 | + }); |
| 182 | + |
| 183 | + it("does not prompt for goBack's fallback navigation", async () => { |
| 184 | + const guard = registerGuard(true, false); |
| 185 | + |
| 186 | + await goBack("/config/cloud/account"); |
| 187 | + |
| 188 | + expect(window.location.pathname).toEqual("/config/cloud/account"); |
| 189 | + expect(guard.prompt).not.toHaveBeenCalled(); |
| 190 | + }); |
| 191 | +}); |
| 192 | + |
57 | 193 | describe("goBack", () => { |
58 | 194 | beforeEach(() => { |
59 | 195 | setEntry("/config/cloud/remote"); |
|
0 commit comments