2019-05-27 06:54:39 +00:00
|
|
|
/* eslint-env node, mocha */
|
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const assert = require('assert')
|
|
|
|
const mock = require('mock-require')
|
|
|
|
|
|
|
|
const { makeMockSocket, removeModuleFromRequireCache } = require('./utils')
|
|
|
|
|
2019-05-23 10:40:07 +00:00
|
|
|
describe('realtime#parseNoteIdFromSocketAsync', function () {
|
2019-05-27 06:54:39 +00:00
|
|
|
let realtime
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mock('../../lib/logger', {})
|
|
|
|
mock('../../lib/history', {})
|
|
|
|
mock('../../lib/models', {
|
|
|
|
Note: {
|
|
|
|
parseNoteId: function (noteId, callback) {
|
|
|
|
callback(null, noteId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
mock('../../lib/config', {})
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
2020-01-04 22:14:03 +00:00
|
|
|
removeModuleFromRequireCache('../../lib/realtime/realtime')
|
2019-05-27 06:54:39 +00:00
|
|
|
mock.stopAll()
|
|
|
|
})
|
|
|
|
|
2019-05-23 10:40:07 +00:00
|
|
|
it('should return null when socket not send noteId', async function () {
|
2020-01-04 22:14:03 +00:00
|
|
|
realtime = require('../../lib/realtime/realtime')
|
2019-05-27 06:54:39 +00:00
|
|
|
const mockSocket = makeMockSocket()
|
2019-05-23 10:40:07 +00:00
|
|
|
try {
|
|
|
|
const notes = await realtime.parseNoteIdFromSocketAsync(mockSocket)
|
|
|
|
assert(notes === null)
|
|
|
|
} catch (err) {
|
|
|
|
assert.fail('should not occur any error')
|
|
|
|
}
|
2019-05-27 06:54:39 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('noteId exists', function () {
|
|
|
|
beforeEach(() => {
|
|
|
|
mock('../../lib/models', {
|
|
|
|
Note: {
|
|
|
|
parseNoteId: function (noteId, callback) {
|
|
|
|
callback(null, noteId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2019-05-23 10:40:07 +00:00
|
|
|
it('should return noteId when noteId exists', async function () {
|
2020-01-04 22:14:03 +00:00
|
|
|
realtime = require('../../lib/realtime/realtime')
|
2019-05-27 06:54:39 +00:00
|
|
|
const noteId = '123456'
|
|
|
|
const mockSocket = makeMockSocket(undefined, {
|
|
|
|
noteId: noteId
|
|
|
|
})
|
2020-01-04 22:14:03 +00:00
|
|
|
realtime = require('../../lib/realtime/realtime')
|
2019-05-23 10:40:07 +00:00
|
|
|
let parsedNoteId
|
|
|
|
try {
|
|
|
|
parsedNoteId = await realtime.parseNoteIdFromSocketAsync(mockSocket)
|
|
|
|
} catch (err) {
|
|
|
|
assert.fail(`should not occur any error ${err} `)
|
|
|
|
}
|
|
|
|
assert(parsedNoteId === noteId)
|
2019-05-27 06:54:39 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('noteId not exists', function () {
|
|
|
|
beforeEach(() => {
|
|
|
|
mock('../../lib/models', {
|
|
|
|
Note: {
|
|
|
|
parseNoteId: function (noteId, callback) {
|
|
|
|
callback(null, null)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2019-05-23 10:40:07 +00:00
|
|
|
it('should return null when noteId not exists', async function () {
|
2020-01-04 22:14:03 +00:00
|
|
|
realtime = require('../../lib/realtime/realtime')
|
2019-05-27 06:54:39 +00:00
|
|
|
const noteId = '123456'
|
|
|
|
const mockSocket = makeMockSocket(undefined, {
|
|
|
|
noteId: noteId
|
|
|
|
})
|
2020-01-04 22:14:03 +00:00
|
|
|
realtime = require('../../lib/realtime/realtime')
|
2019-05-23 10:40:07 +00:00
|
|
|
const parsedNoteId = await realtime.parseNoteIdFromSocketAsync(mockSocket)
|
|
|
|
assert(parsedNoteId === null)
|
2019-05-27 06:54:39 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('parse note error', function () {
|
|
|
|
beforeEach(() => {
|
|
|
|
mock('../../lib/models', {
|
|
|
|
Note: {
|
|
|
|
parseNoteId: function (noteId, callback) {
|
|
|
|
/* eslint-disable-next-line */
|
|
|
|
callback('error', null)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2019-05-23 10:40:07 +00:00
|
|
|
it('should return error when noteId parse error', async function () {
|
2020-01-04 22:14:03 +00:00
|
|
|
realtime = require('../../lib/realtime/realtime')
|
2019-05-27 06:54:39 +00:00
|
|
|
const noteId = '123456'
|
|
|
|
const mockSocket = makeMockSocket(undefined, {
|
|
|
|
noteId: noteId
|
|
|
|
})
|
2020-01-04 22:14:03 +00:00
|
|
|
realtime = require('../../lib/realtime/realtime')
|
2019-05-23 10:40:07 +00:00
|
|
|
try {
|
|
|
|
await realtime.parseNoteIdFromSocketAsync(mockSocket)
|
|
|
|
} catch (err) {
|
|
|
|
assert(err === 'error')
|
|
|
|
}
|
2019-05-27 06:54:39 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|