|
| 1 | +import { mount } from '@vue/test-utils'; |
| 2 | +import { useSocket, onSocketEvent } from '../composables'; |
| 3 | +import Main from '../plugin'; |
| 4 | +import io from '../__mocks__/socket.io-client'; |
| 5 | + |
| 6 | +it('useSocket() injects $socket', () => { |
| 7 | + let injectedSocketExtension; |
| 8 | + const wrapper = mount({ |
| 9 | + render: () => null, |
| 10 | + setup() { |
| 11 | + injectedSocketExtension = useSocket(); |
| 12 | + }, |
| 13 | + }, { global: { plugins: [[Main, io('ws://localhost')]] } }); |
| 14 | + expect(injectedSocketExtension).toBe(wrapper.vm.$socket); |
| 15 | +}); |
| 16 | + |
| 17 | +describe('onSocketEvent()', () => { |
| 18 | + it('subscribes to the event', () => { |
| 19 | + const spy = jest.fn(); |
| 20 | + const socket = io('ws://localhost'); |
| 21 | + mount({ |
| 22 | + render: () => null, |
| 23 | + setup() { |
| 24 | + onSocketEvent('event', spy); |
| 25 | + }, |
| 26 | + }, { global: { plugins: [[Main, socket]] } }); |
| 27 | + socket.fireServerEvent('event', 'data'); |
| 28 | + expect(spy).toHaveBeenCalledWith('data'); |
| 29 | + }); |
| 30 | + |
| 31 | + it('unsubscribes before unmounted', () => { |
| 32 | + const spy = jest.fn(); |
| 33 | + const socket = io('ws://localhost'); |
| 34 | + const wrapper = mount({ |
| 35 | + render: () => null, |
| 36 | + setup() { |
| 37 | + onSocketEvent('event', spy); |
| 38 | + }, |
| 39 | + }, { global: { plugins: [[Main, socket]] } }); |
| 40 | + wrapper.unmount(); |
| 41 | + socket.fireServerEvent('event', 'data'); |
| 42 | + expect(spy).not.toHaveBeenCalled(); |
| 43 | + }); |
| 44 | +}); |
0 commit comments