@@ -38,14 +38,18 @@ afterEach(() => { |
38 | 38 | const otherTabSettings = { ...DEFAULT_CLIENT_SETTINGS, timestampFormat: "12-hour" as const }; |
39 | 39 | const readFailure = new Error("storage unavailable"); |
40 | 40 |
|
41 | | -async function saveInOtherTab(failedReads = 0, saved: ClientSettings | null = otherTabSettings) { |
| 41 | +async function saveInOtherTab( |
| 42 | + failedReads = 0, |
| 43 | + saved: ClientSettings | null = otherTabSettings, |
| 44 | + key: string | null = "t3code:client-settings:v1", |
| 45 | +) { |
42 | 46 | vi.stubGlobal("window", new EventTarget()); |
43 | 47 | await ensureClientSettingsHydrated(); |
44 | 48 | for (let read = 0; read < failedReads; read += 1) { |
45 | 49 | persistenceMocks.getClientSettings.mockRejectedValueOnce(readFailure); |
46 | 50 | } |
47 | 51 | persistenceMocks.getClientSettings.mockResolvedValue(saved); |
48 | | - window.dispatchEvent(Object.assign(new Event("storage"), { key: "t3code:client-settings:v1" })); |
| 52 | + window.dispatchEvent(Object.assign(new Event("storage"), { key })); |
49 | 53 | } |
50 | 54 |
|
51 | 55 | describe("client settings hydration", () => { |
@@ -214,16 +218,25 @@ describe("persistClientSettingsPatch", () => { |
214 | 218 | it.each([ |
215 | 219 | { change: "saved", loaded: null, saved: otherTabSettings }, |
216 | 220 | { change: "cleared", loaded: otherTabSettings, saved: null }, |
217 | | - ])("follows settings another tab $change after this tab loaded", async ({ loaded, saved }) => { |
218 | | - persistenceMocks.getClientSettings.mockResolvedValueOnce(loaded); |
219 | | - await saveInOtherTab(0, saved); |
220 | | - |
221 | | - await persistClientSettingsPatch({ wordWrap: false }); |
222 | | - |
223 | | - const expected = { ...(saved ?? DEFAULT_CLIENT_SETTINGS), wordWrap: false }; |
224 | | - expect(persistenceMocks.setClientSettings).toHaveBeenLastCalledWith(expected); |
225 | | - expect(getClientSettings()).toEqual(expected); |
226 | | - }); |
| 221 | + { |
| 222 | + change: "cleared with localStorage.clear()", |
| 223 | + loaded: otherTabSettings, |
| 224 | + saved: null, |
| 225 | + key: null, |
| 226 | + }, |
| 227 | + ])( |
| 228 | + "follows settings another tab $change after this tab loaded", |
| 229 | + async ({ loaded, saved, key }) => { |
| 230 | + persistenceMocks.getClientSettings.mockResolvedValueOnce(loaded); |
| 231 | + await saveInOtherTab(0, saved, key); |
| 232 | + |
| 233 | + await persistClientSettingsPatch({ wordWrap: false }); |
| 234 | + |
| 235 | + const expected = { ...(saved ?? DEFAULT_CLIENT_SETTINGS), wordWrap: false }; |
| 236 | + expect(persistenceMocks.setClientSettings).toHaveBeenLastCalledWith(expected); |
| 237 | + expect(getClientSettings()).toEqual(expected); |
| 238 | + }, |
| 239 | + ); |
227 | 240 |
|
228 | 241 | it("writes only after re-reading settings another tab saved when the reload failed", async () => { |
229 | 242 | vi.spyOn(console, "error").mockImplementation(() => undefined); |
|