refactor(realtime): extract user event "version" to SocketClient

1. extract user event "version" to SocketClient
2. add test case for that

Signed-off-by: BoHong Li <a60814billy@gmail.com>
This commit is contained in:
BoHong Li 2019-05-03 20:02:52 +08:00
parent 3259bd0f11
commit b33c91f41d
No known key found for this signature in database
GPG Key ID: 9696D5590D58290F
2 changed files with 27 additions and 9 deletions

View File

@ -771,6 +771,8 @@ class SocketClient {
this.socket.on('cursor activity', this.cursorActivityEventHandler.bind(this))
// received cursor blur
this.socket.on('cursor blur', this.cursorBlurEventHandlder.bind(this))
// check version
this.socket.on('version', this.checkVersionEventHandler.bind(this))
}
isNoteAndUserExists () {
@ -812,6 +814,13 @@ class SocketClient {
})
}
checkVersionEventHandler () {
this.socket.emit('version', {
version: config.fullversion,
minimumCompatibleVersion: config.minimumCompatibleVersion
})
}
refreshEventHandler () {
exports.emitRefresh(this.socket)
}
@ -998,14 +1007,6 @@ function connection (socket) {
}
socket.emit('online users', out)
})
// check version
socket.on('version', function () {
socket.emit('version', {
version: config.fullversion,
minimumCompatibleVersion: config.minimumCompatibleVersion
})
})
}
exports = module.exports = realtime

View File

@ -15,6 +15,7 @@ function makeMockSocket (headers, query) {
query: Object.assign({}, query)
},
on: sinon.fake(),
emit: sinon.fake(),
broadCastChannelCache: {},
broadcast: {
to: (channel) => {
@ -459,7 +460,10 @@ describe('realtime', function () {
parseNoteTitle: (data) => (data)
}
})
mock('../lib/config', {})
mock('../lib/config', {
fullversion: '1.5.0',
minimumCompatibleVersion: '1.0.0'
})
realtime = require('../lib/realtime')
// get all socket event handler
@ -609,5 +613,18 @@ describe('realtime', function () {
})
})
describe('version', function () {
it('should emit server version ', () => {
const versionFunc = eventFuncMap.get('version')
versionFunc()
assert(clientSocket.emit.called)
assert(clientSocket.emit.lastCall.args[0], 'version')
assert.deepStrictEqual(clientSocket.emit.lastCall.args[1], {
version: '1.5.0',
minimumCompatibleVersion: '1.0.0'
})
})
})
})
})