123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- import Vue from 'vue'
- import Emitter from '@/Emitter'
- import Observer from '@/Observer'
- import { Server, WebSocket } from 'mock-socket'
- describe('Observer.js', () => {
- let observer, mockServer
- let wsUrl = 'ws://localhost:8080'
- it('fires onopen event', (done) => {
- mockServer = new Server(wsUrl)
- mockServer.on('connection', ws => {
- ws.send('hi')
- })
- Vue.use(VueNativeSock, wsUrl)
- let vm = new Vue()
- observer = new Observer(wsUrl)
- Emitter.addListener('onopen', (data) => {
- expect(data.type).to.equal('open')
- mockServer.stop(done)
- }, vm)
- })
- it('fires onopen event skip scheme', (done) => {
- mockServer = new Server(wsUrl)
- mockServer.on('connection', ws => {
- ws.send('hi')
- })
- Vue.use(VueNativeSock, '//localhost:8080')
- let vm = new Vue()
- observer = new Observer(wsUrl)
- Emitter.addListener('onopen', (data) => {
- expect(data.type).to.equal('open')
- mockServer.stop(done)
- }, vm)
- })
- // TODO: DRY
- it('passes a json commit to the provided vuex store', (done) => {
- let expectedMsg = { mutation: 'setName', value: 'steve' }
- let mockStore = sinon.mock({ commit: () => {} })
- mockStore.expects('commit').withArgs('SOCKET_ONOPEN')
- mockStore.expects('commit').withArgs(expectedMsg.mutation)
- mockServer = new Server(wsUrl)
- mockServer.on('connection', ws => {
- ws.send(JSON.stringify(expectedMsg))
- })
- Vue.use(VueNativeSock, wsUrl)
- let vm = new Vue()
- observer = new Observer(wsUrl, {
- store: mockStore.object,
- format: 'json',
- websocket: new WebSocket(wsUrl)
- })
- setTimeout(() => {
- mockStore.verify()
- mockServer.stop(done)
- }, 100)
- })
- // TODO: DRY
- it('passes a json action to the provided vuex store', (done) => {
- let expectedMsg = { action: 'setName', value: 'steve' }
- let mockStore = sinon.mock({
- commit: () => {},
- dispatch: () => {}
- })
- mockStore.expects('dispatch').withArgs(expectedMsg.action, expectedMsg)
- mockServer = new Server(wsUrl)
- mockServer.on('connection', ws => {
- ws.send(JSON.stringify(expectedMsg))
- })
- Vue.use(VueNativeSock, wsUrl)
- let vm = new Vue()
- observer = new Observer(wsUrl, {
- store: mockStore.object,
- format: 'json',
- websocket: new WebSocket(wsUrl)
- })
- setTimeout(() => {
- mockStore.verify()
- mockServer.stop(done)
- }, 100)
- })
- // TODO: DRY
- it('passes a namespaced json commit to the provided vuex store', (done) => {
- let expectedMsg = { namespace: 'users', mutation: 'setName', value: 'steve' }
- let mockStore = sinon.mock({ commit: () => {} })
- mockStore.expects('commit').withArgs('SOCKET_ONOPEN')
- mockStore.expects('commit').withArgs(expectedMsg.namespace + '/' + expectedMsg.mutation)
- mockServer = new Server(wsUrl)
- mockServer.on('connection', ws => {
- ws.send(JSON.stringify(expectedMsg))
- })
- Vue.use(VueNativeSock, wsUrl)
- let vm = new Vue()
- observer = new Observer(wsUrl, {
- store: mockStore.object,
- format: 'json',
- websocket: new WebSocket(wsUrl)
- })
- setTimeout(() => {
- mockStore.verify()
- mockServer.stop(done)
- }, 100)
- })
- // TODO: DRY
- it('passes a namespaced json action to the provided vuex store', (done) => {
- let expectedMsg = { namespace: 'users', action: 'setName', value: 'steve' }
- let mockStore = sinon.mock({
- commit: () => {},
- dispatch: () => {}
- })
- mockStore.expects('dispatch').withArgs(expectedMsg.namespace + '/' + expectedMsg.action, expectedMsg)
- mockServer = new Server(wsUrl)
- mockServer.on('connection', ws => {
- ws.send(JSON.stringify(expectedMsg))
- })
- Vue.use(VueNativeSock, wsUrl)
- let vm = new Vue()
- observer = new Observer(wsUrl, {
- store: mockStore.object,
- format: 'json',
- websocket: new WebSocket(wsUrl)
- })
- setTimeout(() => {
- mockStore.verify()
- mockServer.stop(done)
- }, 100)
- })
- // TODO: DRY
- it('passes a custom commit name to the provided vuex store', (done) => {
- let expectedMsg = 'hello world'
- let mutations = {
- SOCKET_ONOPEN: '✅ Socket connected',
- SOCKET_ONMESSAGE: 'Websocket message received'
- }
- let mockStore = sinon.mock({ commit: () => {} })
- mockStore.expects('commit').withArgs(mutations.SOCKET_ONOPEN)
- mockStore.expects('commit').withArgs(mutations.SOCKET_ONMESSAGE)
- mockServer = new Server(wsUrl)
- mockServer.on('connection', ws => {
- ws.send(expectedMsg)
- })
- Vue.use(VueNativeSock, wsUrl)
- let vm = new Vue()
- observer = new Observer(wsUrl, {
- store: mockStore.object,
- mutations,
- websocket: new WebSocket(wsUrl)
- })
- setTimeout(() => {
- mockStore.verify()
- mockServer.stop(done)
- }, 100)
- })
- describe('reconnection feature', () => {
- let observer, mockServer, vm, mockStore
- let wsUrl = 'ws://localhost:8080'
- beforeEach(() => {
- mockServer = new Server(wsUrl)
- mockServer.on('connection', ws => ws.send('hi'))
- Vue.use(VueNativeSock, wsUrl)
- vm = new Vue()
- mockStore = sinon.mock({ commit: () => {} })
- observer = new Observer(wsUrl, {
- store: mockStore.object,
- reconnection: true,
- reconnectionAttempts: 2,
- WebSocket: new WebSocket(wsUrl),
- })
- })
- it('calls #reconnect() method', (done) => {
- sinon.spy(observer, 'reconnect');
- mockServer.close()
- expect(observer.reconnect).to.called
- mockServer.stop(done)
- })
- it('fires SOCKET_RECONNECT event', (done) => {
- sinon.spy(observer, 'passToStore');
- const clock = sinon.useFakeTimers()
- mockServer.close()
- clock.tick(1500);
- expect(observer.passToStore).to.have.been.calledWith('SOCKET_RECONNECT')
- mockServer.stop(done)
- })
- it('fires SOCKET_RECONNECT_ERROR event, after all attemps', (done) => {
- sinon.spy(observer, 'passToStore');
- observer.reconnectionCount = 2
- observer.reconnectionAttempts = 1
- mockServer.close()
- expect(observer.passToStore).to.have.been.calledWith('SOCKET_RECONNECT_ERROR')
- mockServer.stop(done)
- })
- })
- })
|