Skip to content

Commit

Permalink
fixed minor bags
Browse files Browse the repository at this point in the history
  • Loading branch information
Viktor Shcheglov committed Jan 8, 2018
1 parent 3551355 commit a3b33dd
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ export default new Vuex.Store({
state.message = message
},
// mutations for reconnect methods
[ws.WS_RECONNECT](state, count) {
SOCKET_RECONNECT(state, count) {
console.info(state, count)
},
[ws.WS_RECONNECT_ERROR](state) {
SOCKET_RECONNECT_ERROR(state) {
state.socket.reconnectError = true;
},
}
Expand Down
2 changes: 1 addition & 1 deletion dist/build.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export default {
},
beforeDestroy () {
let sockets = this.$options['sockets']
clearTimeout(observer.reconnectTimeoutId)

if (sockets) {
Object.keys(sockets).forEach((key) => {
Expand Down
4 changes: 2 additions & 2 deletions src/Observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ export default class {

if (this.store) { this.passToStore('SOCKET_' + eventType, event) }

if (this.reconnection && this.eventType === 'onopen') { this.reconnectionCount = 0 }
if (this.reconnection && eventType === 'onopen') { this.reconnectionCount = 0 }

if (this.reconnection && eventType === 'onclose') { this.reconnect(event) }
if (this.reconnection && eventType === 'onclose') { this.reconnect() }
}
})
}
Expand Down
48 changes: 48 additions & 0 deletions test/unit/specs/Observer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,52 @@ describe('Observer.js', () => {
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)
})
})
})

0 comments on commit a3b33dd

Please sign in to comment.