diff --git a/lib/realtime.js b/lib/realtime.js index a4e77784..17487ad2 100644 --- a/lib/realtime.js +++ b/lib/realtime.js @@ -358,21 +358,20 @@ function extractNoteIdFromSocket (socket) { return false } -function parseNoteIdFromSocket (socket, callback) { - var noteId = extractNoteIdFromSocket(socket) +async function parseNoteIdFromSocketAsync (socket) { + const noteId = extractNoteIdFromSocket(socket) if (!noteId) { - return callback(null, null) + return null } - models.Note.parseNoteId(noteId, function (err, id) { - if (err || !id) return callback(err, id) - return callback(null, id) - }) -} -function parseNoteIdFromSocketAsync (socket) { return new Promise((resolve, reject) => { - parseNoteIdFromSocket(socket, (err, id) => { - if (err) return reject(err) + models.Note.parseNoteId(noteId, function (err, id) { + if (err) { + reject(err) + } + if (!id) { + resolve(null) + } resolve(id) }) }) @@ -806,7 +805,6 @@ function terminate () { exports = module.exports = realtime exports.extractNoteIdFromSocket = extractNoteIdFromSocket -exports.parseNoteIdFromSocket = parseNoteIdFromSocket exports.updateNote = updateNote exports.failConnection = failConnection exports.updateUserData = updateUserData diff --git a/test/realtime/connection.test.js b/test/realtime/connection.test.js index 50b6d618..af5fdb92 100644 --- a/test/realtime/connection.test.js +++ b/test/realtime/connection.test.js @@ -43,7 +43,7 @@ describe('realtime#connection', function () { it('should fast return when server not start', () => { const mockSocket = makeMockSocket() realtime.maintenance = true - const spy = sinon.spy(realtime, 'parseNoteIdFromSocket') + const spy = sinon.spy(realtime, 'parseNoteIdFromSocketAsync') realtime.connection(mockSocket) assert(!spy.called) }) diff --git a/test/realtime/parseNoteIdFromSocket.test.js b/test/realtime/parseNoteIdFromSocket.test.js index 1eb6d438..4bef3626 100644 --- a/test/realtime/parseNoteIdFromSocket.test.js +++ b/test/realtime/parseNoteIdFromSocket.test.js @@ -7,7 +7,7 @@ const sinon = require('sinon') const { makeMockSocket, removeModuleFromRequireCache } = require('./utils') -describe('realtime#parseNoteIdFromSocket', function () { +describe('realtime#parseNoteIdFromSocketAsync', function () { let realtime beforeEach(() => { @@ -28,13 +28,15 @@ describe('realtime#parseNoteIdFromSocket', function () { mock.stopAll() }) - it('should return null when socket not send noteId', function () { + it('should return null when socket not send noteId', async function () { realtime = require('../../lib/realtime') const mockSocket = makeMockSocket() - const fakeCallback = sinon.fake() - realtime.parseNoteIdFromSocket(mockSocket, fakeCallback) - assert(fakeCallback.called) - assert.deepStrictEqual(fakeCallback.getCall(0).args, [null, null]) + try { + const notes = await realtime.parseNoteIdFromSocketAsync(mockSocket) + assert(notes === null) + } catch (err) { + assert.fail('should not occur any error') + } }) describe('noteId exists', function () { @@ -47,17 +49,20 @@ describe('realtime#parseNoteIdFromSocket', function () { } }) }) - it('should return noteId when noteId exists', function () { + it('should return noteId when noteId exists', async function () { realtime = require('../../lib/realtime') const noteId = '123456' const mockSocket = makeMockSocket(undefined, { noteId: noteId }) realtime = require('../../lib/realtime') - const fakeCallback = sinon.fake() - realtime.parseNoteIdFromSocket(mockSocket, fakeCallback) - assert(fakeCallback.called) - assert.deepStrictEqual(fakeCallback.getCall(0).args, [null, noteId]) + let parsedNoteId + try { + parsedNoteId = await realtime.parseNoteIdFromSocketAsync(mockSocket) + } catch (err) { + assert.fail(`should not occur any error ${err} `) + } + assert(parsedNoteId === noteId) }) }) @@ -71,17 +76,15 @@ describe('realtime#parseNoteIdFromSocket', function () { } }) }) - it('should return null when noteId not exists', function () { + it('should return null when noteId not exists', async function () { realtime = require('../../lib/realtime') const noteId = '123456' const mockSocket = makeMockSocket(undefined, { noteId: noteId }) realtime = require('../../lib/realtime') - const fakeCallback = sinon.fake() - realtime.parseNoteIdFromSocket(mockSocket, fakeCallback) - assert(fakeCallback.called) - assert.deepStrictEqual(fakeCallback.getCall(0).args, [null, null]) + const parsedNoteId = await realtime.parseNoteIdFromSocketAsync(mockSocket) + assert(parsedNoteId === null) }) }) @@ -96,17 +99,18 @@ describe('realtime#parseNoteIdFromSocket', function () { } }) }) - it('should return error when noteId parse error', function () { + it('should return error when noteId parse error', async function () { realtime = require('../../lib/realtime') const noteId = '123456' const mockSocket = makeMockSocket(undefined, { noteId: noteId }) realtime = require('../../lib/realtime') - const fakeCallback = sinon.fake() - realtime.parseNoteIdFromSocket(mockSocket, fakeCallback) - assert(fakeCallback.called) - assert.deepStrictEqual(fakeCallback.getCall(0).args, ['error', null]) + try { + await realtime.parseNoteIdFromSocketAsync(mockSocket) + } catch (err) { + assert(err === 'error') + } }) }) })