forked from pouchdb-community/pouchdb-wrappers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
209 lines (185 loc) · 5.14 KB
/
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* global describe, it, beforeEach, afterEach, emit */
const assert = require('assert').strict
const PouchDB = require('pouchdb')
const wrapper = require('.')
describe('PouchDB-wrappers', () => {
let db
beforeEach(() => {
db = new PouchDB('galactic-alpaca')
})
afterEach(async () => {
try { await db.destroy() } catch {}
})
it('should wrap put handler', async () => {
let ok = false
const handler = {
put: function (original, ...args) {
ok = true
return original(...args)
}
}
wrapper.install(db, handler)
await db.put({ _id: 'mydoc' })
assert(ok)
})
it('should wrap destroy handler', async () => {
let ok = false
const handler = {
destroy: function (original, ...args) {
ok = true
return original(...args)
}
}
wrapper.install(db, handler)
await db.destroy({ _id: 'mydoc' })
assert(ok)
})
it('should manage multiple handlers', async function () {
let ok1, ok2
const handlers1 = {
get: function (original, ...args) {
if (!ok2) ok1 = true // assert first-added runs first
return original(...args)
}
}
const handlers2 = {
get: function (original, ...args) {
ok2 = true
return original(...args)
}
}
wrapper.install(db, handlers1)
wrapper.install(db, handlers2)
await db.put({ _id: 'mydoc' })
await db.get('mydoc')
assert(ok1)
assert(ok2)
})
it('should allow use of callbacks for wrappers', (done) => {
wrapper.install(db, {
get: async function (original, docId) {
const doc = await original(docId)
doc.modified = true
return doc
}
})
db.put({ _id: 'mydoc' }).then(() => {
db.get('mydoc', (_, doc) => {
assert(doc.modified)
done()
})
})
})
it('should allow use of callbacks for wrappers with ...args', (done) => {
wrapper.install(db, {
get: async function (original, ...args) {
const doc = await original(...args)
doc.modified = true
doc.args_count = args.length
return doc
}
})
db.put({ _id: 'mydoc' }).then(() => {
db.get('mydoc', { revs: true }, (_, doc) => {
assert.equal(doc._revisions.start, 1)
assert(doc.modified)
assert.equal(doc.args_count, 2)
done()
})
})
})
it('should throw an error when uninstalling an non-installed method', async () => {
let ok = false
const handler = {
get: function (original, ...args) {
return original(...args)
}
}
try {
wrapper.uninstall(db, handler)
} catch (err) {
ok = true
assert.equal(err.message, 'No wrapper methods installed, so no methods can be uninstalled.')
} finally {
assert(ok)
ok = false
}
try {
wrapper.install(db, {})
wrapper.uninstall(db, handler)
} catch (err) {
ok = true
assert.equal(err.message, `Wrapper method for 'get' not installed: ${handler.get.toString()}`)
} finally {
assert(ok)
}
})
it('should successfully uninstall a method', async function () {
let ok = true
const handlers = {
get: function (original, ...args) {
ok = false
return original(...args)
}
}
wrapper.install(db, handlers)
wrapper.uninstall(db, handlers)
await db.put({ _id: 'mydoc' })
await db.get('mydoc')
assert(ok)
})
it('should not wrap methods that do not exist', function () {
let ok = false
try {
wrapper.install(db, { fake: () => {} })
} catch (err) {
ok = true
assert.equal(err.message, 'Method \'fake\' does not exist on given base, so it cannot be wrapped.')
} finally {
assert(ok)
}
})
it('should not uninstall methods that are not installed', function () {
let ok = false
try {
wrapper.install(db, { get: (original, ...args) => { return original(...args) } })
wrapper.uninstall(db, { get: (a) => { return a } })
} catch (err) {
const errorInNode = 'Wrapper method for \'get\' not installed: (a) => { return a }'
const errorInBrowser = 'Wrapper method for \'get\' not installed: a => {\n return a;\n }'
if (err.message === errorInNode || err.message === errorInBrowser) {
ok = true
}
} finally {
assert(ok)
}
})
it('should wrap synchronous methods ok', async function () {
let ok = false
wrapper.install(db, {
changes: function (original, ...args) {
ok = true
return original(...args)
}
})
const changes = db.changes()
assert(changes.db)
assert(ok)
})
it('should not callback-ify methods with function args that are not a callback', async function () {
wrapper.install(db, {
query: function (original, ...args) {
return original(...args)
}
})
await db.put({ _id: 'hello', hello: 'world' })
const result = await db.query((doc) => {
if (doc.hello === 'world') {
emit(doc.hello)
}
})
assert.equal(result.rows.length, 1)
assert.equal(result.rows[0].id, 'hello')
assert.equal(result.rows[0].key, 'world')
})
})