mirror of
https://github.com/tenrok/vue-native-websocket.git
synced 2026-06-23 14:10:34 +03:00
Merge pull request #70 from sharkykh/bugfix/custom-mutations
Fix custom mutation names feature + test
This commit is contained in:
@@ -175,9 +175,34 @@ export default new Vuex.Store({
|
|||||||
##### With custom mutation names
|
##### With custom mutation names
|
||||||
|
|
||||||
``` js
|
``` js
|
||||||
|
// mutation-types.js
|
||||||
|
const SOCKET_ONOPEN = '✅ Socket connected!'
|
||||||
|
const SOCKET_ONCLOSE = '❌ Socket disconnected!'
|
||||||
|
const SOCKET_ONERROR = '❌ Socket Error!!!'
|
||||||
|
const SOCKET_ONMESSAGE = 'Websocket message received'
|
||||||
|
const SOCKET_RECONNECT = 'Websocket reconnected'
|
||||||
|
const SOCKET_RECONNECT_ERROR = 'Websocket is having issues reconnecting..'
|
||||||
|
|
||||||
|
export {
|
||||||
|
SOCKET_ONOPEN,
|
||||||
|
SOCKET_ONCLOSE,
|
||||||
|
SOCKET_ONERROR,
|
||||||
|
SOCKET_ONMESSAGE,
|
||||||
|
SOCKET_RECONNECT,
|
||||||
|
SOCKET_RECONNECT_ERROR
|
||||||
|
}
|
||||||
|
|
||||||
// store.js
|
// store.js
|
||||||
import Vue from 'vue'
|
import Vue from 'vue'
|
||||||
import Vuex from 'vuex'
|
import Vuex from 'vuex'
|
||||||
|
import {
|
||||||
|
SOCKET_ONOPEN,
|
||||||
|
SOCKET_ONCLOSE,
|
||||||
|
SOCKET_ONERROR,
|
||||||
|
SOCKET_ONMESSAGE,
|
||||||
|
SOCKET_RECONNECT,
|
||||||
|
SOCKET_RECONNECT_ERROR
|
||||||
|
} from './mutation-types'
|
||||||
|
|
||||||
Vue.use(Vuex);
|
Vue.use(Vuex);
|
||||||
|
|
||||||
@@ -189,46 +214,53 @@ export default new Vuex.Store({
|
|||||||
reconnectError: false,
|
reconnectError: false,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
mutations:{
|
mutations: {
|
||||||
SOCKET_ONOPEN (state, event) {
|
[SOCKET_ONOPEN](state, event) {
|
||||||
state.socket.isConnected = true
|
state.socket.isConnected = true
|
||||||
},
|
},
|
||||||
SOCKET_ONCLOSE (state, event) {
|
[SOCKET_ONCLOSE](state, event) {
|
||||||
state.socket.isConnected = false
|
state.socket.isConnected = false
|
||||||
},
|
},
|
||||||
SOCKET_ONERROR (state, event) {
|
[SOCKET_ONERROR](state, event) {
|
||||||
console.error(state, event)
|
console.error(state, event)
|
||||||
},
|
},
|
||||||
// default handler called for all methods
|
// default handler called for all methods
|
||||||
SOCKET_ONMESSAGE (state, message) {
|
[SOCKET_ONMESSAGE](state, message) {
|
||||||
state.socket.message = message
|
state.socket.message = message
|
||||||
},
|
},
|
||||||
// mutations for reconnect methods
|
// mutations for reconnect methods
|
||||||
SOCKET_RECONNECT(state, count) {
|
[SOCKET_RECONNECT](state, count) {
|
||||||
console.info(state, count)
|
console.info(state, count)
|
||||||
},
|
},
|
||||||
SOCKET_RECONNECT_ERROR(state) {
|
[SOCKET_RECONNECT_ERROR](state) {
|
||||||
state.socket.reconnectError = true;
|
state.socket.reconnectError = true;
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// index.js
|
// index.js
|
||||||
import store from './store'
|
import store from './store'
|
||||||
|
import {
|
||||||
|
SOCKET_ONOPEN,
|
||||||
|
SOCKET_ONCLOSE,
|
||||||
|
SOCKET_ONERROR,
|
||||||
|
SOCKET_ONMESSAGE,
|
||||||
|
SOCKET_RECONNECT,
|
||||||
|
SOCKET_RECONNECT_ERROR
|
||||||
|
} from './mutation-types'
|
||||||
|
|
||||||
const mutations = {
|
const mutations = {
|
||||||
SOCKET_ONOPEN: '✅ Socket connected!',
|
SOCKET_ONOPEN,
|
||||||
SOCKET_ONCLOSE: '❌ Socket disconnected!',
|
SOCKET_ONCLOSE,
|
||||||
SOCKET_ONERROR: '❌ Socket Error!!!',
|
SOCKET_ONERROR,
|
||||||
SOCKET_ONMESSAGE: 'Websocket message received',
|
SOCKET_ONMESSAGE,
|
||||||
SOCKET_RECONNECT: 'Websocket reconnected',
|
SOCKET_RECONNECT,
|
||||||
SOCKET_RECONNECT_ERROR: 'Websocket is having issues reconnecting..'
|
SOCKET_RECONNECT_ERROR
|
||||||
};
|
}
|
||||||
|
|
||||||
Vue.use(VueNativeSock, 'ws://localhost:9090', {
|
Vue.use(VueNativeSock, 'ws://localhost:9090', {
|
||||||
store: store,
|
store: store,
|
||||||
format: 'json',
|
mutations: mutations
|
||||||
mutations: mutations
|
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
Vendored
+1
-1
File diff suppressed because one or more lines are too long
+2
-3
@@ -95,9 +95,8 @@ export default class {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (this.mutations) {
|
if (this.mutations) {
|
||||||
this.store[this.mutations[method] || method](target, msg)
|
target = this.mutations[target] || target
|
||||||
} else {
|
|
||||||
this.store[method](target, msg)
|
|
||||||
}
|
}
|
||||||
|
this.store[method](target, msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ describe('Observer.js', () => {
|
|||||||
}, 100)
|
}, 100)
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO: DRY
|
// TODO: DRY
|
||||||
it('passes a json action to the provided vuex store', (done) => {
|
it('passes a json action to the provided vuex store', (done) => {
|
||||||
let expectedMsg = { action: 'setName', value: 'steve' }
|
let expectedMsg = { action: 'setName', value: 'steve' }
|
||||||
let mockStore = sinon.mock({
|
let mockStore = sinon.mock({
|
||||||
@@ -117,7 +117,7 @@ describe('Observer.js', () => {
|
|||||||
}, 100)
|
}, 100)
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO: DRY
|
// TODO: DRY
|
||||||
it('passes a namespaced json action to the provided vuex store', (done) => {
|
it('passes a namespaced json action to the provided vuex store', (done) => {
|
||||||
let expectedMsg = { namespace: 'users', action: 'setName', value: 'steve' }
|
let expectedMsg = { namespace: 'users', action: 'setName', value: 'steve' }
|
||||||
let mockStore = sinon.mock({
|
let mockStore = sinon.mock({
|
||||||
@@ -145,6 +145,36 @@ describe('Observer.js', () => {
|
|||||||
}, 100)
|
}, 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', () => {
|
describe('reconnection feature', () => {
|
||||||
let observer, mockServer, vm, mockStore
|
let observer, mockServer, vm, mockStore
|
||||||
let wsUrl = 'ws://localhost:8080'
|
let wsUrl = 'ws://localhost:8080'
|
||||||
|
|||||||
Reference in New Issue
Block a user