diff --git a/tests/component/wsMixin.test.js b/tests/component/wsMixin.test.js index 71b88c7..69d4eef 100644 --- a/tests/component/wsMixin.test.js +++ b/tests/component/wsMixin.test.js @@ -142,6 +142,49 @@ describe('createWsMixin (wsMixin.js)', () => { ws.onclose({ code: 1006 }) expect(spy).toHaveBeenCalled() }) + + it('allo scadere del timer riapre davvero la connessione', () => { + vi.useFakeTimers() + try { + const wrapper = mountWith() + // il mock non fa mai scattare onopen: sblocca la guardia isConnecting + wrapper.vm.isConnecting = false + const prima = MockWebSocket.instances.length + wrapper.vm.scheduleReconnect() + vi.advanceTimersByTime(1000) + expect(MockWebSocket.instances.length).toBe(prima + 1) + expect(wrapper.vm.reconnectTimeout).toBe(null) + } finally { + vi.useRealTimers() + } + }) + + it('se il costruttore WebSocket lancia, programma la riconnessione', () => { + const wrapper = mountWith() + wrapper.vm.isConnecting = false + const spy = vi.spyOn(wrapper.vm, 'scheduleReconnect').mockImplementation(() => {}) + const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) + vi.stubGlobal('WebSocket', class { constructor() { throw new Error('boom') } }) + try { + wrapper.vm.connectWebSocket() + expect(spy).toHaveBeenCalled() + expect(wrapper.vm.isConnecting).toBe(false) + expect(wrapper.vm.ws).toBe(null) + } finally { + vi.stubGlobal('WebSocket', MockWebSocket) + consoleSpy.mockRestore() + } + }) + + it('onerror azzera lo stato di connessione', () => { + const wrapper = mountWith() + const ws = ultimaWs() + wrapper.vm.wsConnected = true + wrapper.vm.isConnecting = true + ws.onerror() + expect(wrapper.vm.wsConnected).toBe(false) + expect(wrapper.vm.isConnecting).toBe(false) + }) }) describe('sendWs', () => { @@ -161,6 +204,17 @@ describe('createWsMixin (wsMixin.js)', () => { const inviato = JSON.parse(ws.send.mock.calls.at(-1)[0]) expect(inviato.type).toBe('action') }) + + it('ritorna false se send lancia', () => { + const wrapper = mountWith() + const ws = ultimaWs() + ws.readyState = MockWebSocket.OPEN + wrapper.vm.wsConnected = true + ws.send.mockImplementation(() => { throw new Error('boom') }) + const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) + expect(wrapper.vm.sendWs({ type: 'action' })).toBe(false) + consoleSpy.mockRestore() + }) }) describe('cleanup', () => {