-
Notifications
You must be signed in to change notification settings - Fork 4
fix: close WebSocket on pagehide to support bfcache #162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; | ||
| import Socket from '../src/modules/socket'; | ||
| import type { CatcherMessage } from '@hawk.so/types'; | ||
|
|
||
| const MOCK_WEBSOCKET_URL = 'ws://localhost:1234'; | ||
|
|
||
| type MockWebSocket = { | ||
| url: string; | ||
| readyState: number; | ||
| send: ReturnType<typeof vi.fn>; | ||
| close: ReturnType<typeof vi.fn>; | ||
| onopen?: (event: Event) => void; | ||
| onclose?: (event: CloseEvent) => void; | ||
| onerror?: (event: Event) => void; | ||
| onmessage?: (event: MessageEvent) => void; | ||
| }; | ||
|
|
||
| function createMockWebSocket() { | ||
| const instances: MockWebSocket[] = []; | ||
|
|
||
| const closeSpy = vi.fn(function (this: MockWebSocket) { | ||
| this.readyState = WebSocket.CLOSED; | ||
| this.onclose?.({ code: 1000 } as CloseEvent); | ||
| }); | ||
|
|
||
| const ctor = vi.fn<(url: string) => MockWebSocket>().mockImplementation(function ( | ||
| this: MockWebSocket, | ||
| url: string | ||
| ) { | ||
| this.url = url; | ||
| this.readyState = WebSocket.CONNECTING; | ||
| this.send = vi.fn(); | ||
| this.close = closeSpy; | ||
| this.onopen = undefined; | ||
| this.onclose = undefined; | ||
| this.onerror = undefined; | ||
| this.onmessage = undefined; | ||
|
|
||
| instances.push(this); | ||
| }); | ||
|
|
||
| return { ctor, closeSpy, instances }; | ||
| } | ||
|
|
||
| describe('Socket', () => { | ||
| let ctor: ReturnType<typeof vi.fn>; | ||
| let closeSpy: ReturnType<typeof vi.fn>; | ||
| let instances: MockWebSocket[]; | ||
|
|
||
| let addSpy: ReturnType<typeof vi.spyOn>; | ||
| let removeSpy: ReturnType<typeof vi.spyOn>; | ||
|
|
||
| beforeEach(() => { | ||
| ({ ctor, closeSpy, instances } = createMockWebSocket()); | ||
| (globalThis as any).WebSocket = ctor; | ||
|
|
||
| addSpy = vi.spyOn(window, 'addEventListener'); | ||
| removeSpy = vi.spyOn(window, 'removeEventListener'); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it('should close websocket on pagehide and recreate connection on next send()', async () => { | ||
| const socket = new Socket({ collectorEndpoint: MOCK_WEBSOCKET_URL }); | ||
|
|
||
| const first = instances[0]; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
name is not clear |
||
| first.readyState = WebSocket.OPEN; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do you create I'd suggest to create Socket mock right here, it will be more straightforward |
||
| first.onopen?.(new Event('open')); | ||
| const pagehideHandler = addSpy.mock.calls.find(c => c[0] === 'pagehide')![1] as EventListener; | ||
|
|
||
| window.dispatchEvent(new Event('pagehide')); | ||
|
|
||
| expect(closeSpy).toHaveBeenCalledOnce(); | ||
| expect(removeSpy).toHaveBeenCalledWith('pagehide', pagehideHandler, { capture: true }); | ||
|
|
||
| const sendPromise = socket.send({ foo: 'bar' } as CatcherMessage); | ||
|
|
||
| const second = instances[1]; | ||
| second.readyState = WebSocket.OPEN; | ||
| second.onopen?.(new Event('open')); | ||
| await sendPromise; | ||
|
|
||
| expect(ctor).toHaveBeenCalledTimes(2); | ||
| expect(second.url).toBe(MOCK_WEBSOCKET_URL); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
name is not clear