2019-05-27 07:19:53 +00:00
|
|
|
/* eslint-env node, mocha */
|
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const assert = require('assert')
|
|
|
|
const mock = require('mock-require')
|
|
|
|
const sinon = require('sinon')
|
|
|
|
|
|
|
|
const { makeMockSocket, removeModuleFromRequireCache } = require('./utils')
|
|
|
|
|
|
|
|
describe('realtime#disconnect', function () {
|
|
|
|
const noteId = 'note1_id'
|
|
|
|
let realtime
|
|
|
|
let updateNoteStub
|
|
|
|
let emitOnlineUsersStub
|
|
|
|
let client
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mock('../../lib/logger', {
|
|
|
|
error: () => {
|
|
|
|
}
|
|
|
|
})
|
|
|
|
mock('../../lib/history', {})
|
|
|
|
mock('../../lib/models', {
|
|
|
|
Revision: {
|
|
|
|
saveAllNotesRevision: () => {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
mock('../../lib/config', {})
|
|
|
|
|
2020-01-04 22:14:03 +00:00
|
|
|
realtime = require('../../lib/realtime/realtime')
|
2019-05-27 07:19:53 +00:00
|
|
|
updateNoteStub = sinon.stub(realtime, 'updateNote').callsFake((note, callback) => {
|
|
|
|
callback(null, note)
|
|
|
|
})
|
|
|
|
emitOnlineUsersStub = sinon.stub(realtime, 'emitOnlineUsers')
|
|
|
|
client = makeMockSocket()
|
|
|
|
client.noteId = noteId
|
|
|
|
|
|
|
|
realtime.users[client.id] = {
|
|
|
|
id: client.id,
|
|
|
|
color: '#ff0000',
|
|
|
|
cursor: null,
|
|
|
|
login: false,
|
|
|
|
userid: null,
|
|
|
|
name: null,
|
|
|
|
idle: false,
|
|
|
|
type: null
|
|
|
|
}
|
|
|
|
|
|
|
|
realtime.getNotePool()[noteId] = {
|
|
|
|
id: noteId,
|
|
|
|
server: {
|
|
|
|
isDirty: true
|
|
|
|
},
|
|
|
|
users: {
|
|
|
|
[client.id]: realtime.users[client.id]
|
|
|
|
},
|
|
|
|
socks: [client]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
2020-01-04 22:14:03 +00:00
|
|
|
removeModuleFromRequireCache('../../lib/realtime/realtime')
|
2019-05-10 11:10:00 +00:00
|
|
|
mock.stopAll()
|
2019-05-27 07:19:53 +00:00
|
|
|
sinon.restore()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should disconnect success', function (done) {
|
|
|
|
realtime.queueForDisconnect(client)
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
assert(typeof realtime.users[client.id] === 'undefined')
|
|
|
|
assert(emitOnlineUsersStub.called)
|
|
|
|
assert(updateNoteStub.called)
|
|
|
|
assert(Object.keys(realtime.users).length === 0)
|
|
|
|
assert(Object.keys(realtime.notes).length === 0)
|
|
|
|
done()
|
|
|
|
}, 5)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should disconnect success when note is not dirty', function (done) {
|
|
|
|
realtime.notes[noteId].server.isDirty = false
|
|
|
|
realtime.queueForDisconnect(client)
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
assert(typeof realtime.users[client.id] === 'undefined')
|
|
|
|
assert(emitOnlineUsersStub.called)
|
|
|
|
assert(updateNoteStub.called === false)
|
|
|
|
assert(Object.keys(realtime.users).length === 0)
|
|
|
|
assert(Object.keys(realtime.notes).length === 0)
|
|
|
|
done()
|
|
|
|
}, 5)
|
|
|
|
})
|
|
|
|
})
|