-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathparseNoteIdFromSocket.test.js
115 lines (106 loc) · 3.14 KB
/
parseNoteIdFromSocket.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* eslint-env node, mocha */
'use strict'
const assert = require('assert')
const mock = require('mock-require')
const { makeMockSocket, removeModuleFromRequireCache } = require('./utils')
describe('realtime#parseNoteIdFromSocketAsync', function () {
let realtime
beforeEach(() => {
mock('../../lib/logger', {})
mock('../../lib/history', {})
mock('../../lib/models', {
Note: {
parseNoteId: function (noteId, callback) {
callback(null, noteId)
}
}
})
mock('../../lib/config', {})
})
afterEach(() => {
removeModuleFromRequireCache('../../lib/realtime/realtime')
mock.stopAll()
})
it('should return null when socket not send noteId', async function () {
realtime = require('../../lib/realtime/realtime')
const mockSocket = makeMockSocket()
try {
const notes = await realtime.parseNoteIdFromSocketAsync(mockSocket)
assert(notes === null)
} catch (err) {
assert.fail('should not occur any error')
}
})
describe('noteId exists', function () {
beforeEach(() => {
mock('../../lib/models', {
Note: {
parseNoteId: function (noteId, callback) {
callback(null, noteId)
}
}
})
})
it('should return noteId when noteId exists', async function () {
realtime = require('../../lib/realtime/realtime')
const noteId = '123456'
const mockSocket = makeMockSocket(undefined, {
noteId: noteId
})
realtime = require('../../lib/realtime/realtime')
let parsedNoteId
try {
parsedNoteId = await realtime.parseNoteIdFromSocketAsync(mockSocket)
} catch (err) {
assert.fail(`should not occur any error ${err} `)
}
assert(parsedNoteId === noteId)
})
})
describe('noteId not exists', function () {
beforeEach(() => {
mock('../../lib/models', {
Note: {
parseNoteId: function (noteId, callback) {
callback(null, null)
}
}
})
})
it('should return null when noteId not exists', async function () {
realtime = require('../../lib/realtime/realtime')
const noteId = '123456'
const mockSocket = makeMockSocket(undefined, {
noteId: noteId
})
realtime = require('../../lib/realtime/realtime')
const parsedNoteId = await realtime.parseNoteIdFromSocketAsync(mockSocket)
assert(parsedNoteId === null)
})
})
describe('parse note error', function () {
beforeEach(() => {
mock('../../lib/models', {
Note: {
parseNoteId: function (noteId, callback) {
/* eslint-disable-next-line */
callback('error', null)
}
}
})
})
it('should return error when noteId parse error', async function () {
realtime = require('../../lib/realtime/realtime')
const noteId = '123456'
const mockSocket = makeMockSocket(undefined, {
noteId: noteId
})
realtime = require('../../lib/realtime/realtime')
try {
await realtime.parseNoteIdFromSocketAsync(mockSocket)
} catch (err) {
assert(err === 'error')
}
})
})
})