diff --git a/resources/js/lib/chat-dock.ts b/resources/js/lib/chat-dock.ts index c99020a98..ea1d62886 100644 --- a/resources/js/lib/chat-dock.ts +++ b/resources/js/lib/chat-dock.ts @@ -48,7 +48,10 @@ function subscribe(listener: () => void): () => void { listeners.add(listener); const onStorage = (event: StorageEvent) => { - if (event.key === storageKey) { + if ( + event.storageArea === window.localStorage && + (event.key === storageKey || event.key === null) + ) { dock = null; listener(); } diff --git a/tests/Unit/ChatDockStorage.test.tsx b/tests/Unit/ChatDockStorage.test.tsx new file mode 100644 index 000000000..e8621b9d9 --- /dev/null +++ b/tests/Unit/ChatDockStorage.test.tsx @@ -0,0 +1,65 @@ +/// +import assert from 'node:assert/strict'; +import * as React from 'react'; +import { afterEach, test, vi } from 'vite-plus/test'; + +const notify = vi.fn(); +let unsubscribe: () => void; +vi.doMock('react', () => ({ + ...React, + useSyncExternalStore: ( + subscribe: (listener: () => void) => () => void, + snapshot: () => unknown, + ) => { + unsubscribe = subscribe(notify); + return snapshot(); + }, +})); + +afterEach(() => { + unsubscribe?.(); + vi.unstubAllGlobals(); +}); + +test('clears the dock when another tab wipes local storage', async () => { + const values = new Map(); + const listeners = new Map< + string, + (event: { key: string | null; storageArea: object | null }) => void + >(); + const localStorage = { + getItem: (key: string) => values.get(key) ?? null, + setItem: (key: string, value: string) => values.set(key, value), + removeItem: (key: string) => values.delete(key), + }; + values.set('chat-dock', JSON.stringify({ + isDocked: true, + isExpanded: true, + conversation: 'alice', + })); + vi.stubGlobal('window', { + localStorage, + addEventListener: (event: string, callback: (event: { key: string | null; storageArea: object | null }) => void) => + listeners.set(event, callback), + removeEventListener: (event: string) => listeners.delete(event), + }); + const { useChatDock } = await import('../../resources/js/lib/chat-dock'); + + assert.equal(useChatDock().isDocked, true); + const onStorage = listeners.get('storage'); + onStorage?.({ key: null, storageArea: {} }); + assert.equal(notify.mock.calls.length, 0); + assert.equal(useChatDock().isDocked, true); + + values.clear(); + onStorage?.({ key: null, storageArea: localStorage }); + + assert.equal(notify.mock.calls.length, 1); + assert.deepEqual(useChatDock(), { + isDocked: false, + isExpanded: false, + conversation: null, + }); + unsubscribe(); + assert.equal(listeners.has('storage'), false); +});