Skip to content

Commit b0ede7e

Browse files
committed
test(mock): fix multiple onSnapshot for documents and collections in mock
1 parent 44f6f39 commit b0ede7e

File tree

3 files changed

+38
-35
lines changed

3 files changed

+38
-35
lines changed

src/index.js

-3
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,7 @@ function updateDataFromDocumentSnapshot ({ snapshot, target, path, subs, depth =
176176

177177
function subscribeToDocument ({ ref, target, path, depth, resolve }) {
178178
const subs = Object.create(null)
179-
// console.log('subDoc(1)', key)
180-
// const lol = key
181179
const unbind = ref.onSnapshot(doc => {
182-
// console.log('subDoc(2)', lol, key)
183180
if (doc.exists) {
184181
updateDataFromDocumentSnapshot({
185182
snapshot: createSnapshot(doc),

test/helpers/mock.js

+37-22
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,45 @@ export class Key {
3030
}
3131
}
3232

33-
export class DocumentReference {
33+
class callbacksAndErrors {
34+
constructor () {
35+
this._cbId = 0
36+
this.cbs = {}
37+
this.onErrors = {}
38+
}
39+
40+
_addCallbacks (cb, onError) {
41+
const id = this._cbId++
42+
this.cbs[id] = cb
43+
this.onErrors[id] = onError
44+
return () => {
45+
delete this.cbs[id]
46+
delete this.onErrors[id]
47+
}
48+
}
49+
50+
_callCallbacks (data) {
51+
Object.values(this.cbs).forEach(
52+
cb => cb(data)
53+
)
54+
}
55+
}
56+
57+
export class DocumentReference extends callbacksAndErrors {
3458
constructor ({ collection, id, data, index }) {
59+
super()
3560
this.collection = collection
3661
this.id = id
3762
this.data = data
3863
this.index = index
3964
this.exists = false
40-
this.cb = this.onError = noop
4165
}
4266

4367
onSnapshot (cb, onError) {
44-
this.cb = cb
45-
this.onError = onError
46-
// TODO timeout a cb
4768
setTimeout(() => {
48-
this.cb(new DocumentSnapshot(null, this.id, this.data, this.exists))
69+
cb(new DocumentSnapshot(null, this.id, this.data, this.exists))
4970
}, 0)
50-
return () => {
51-
this.cb = this.onError = noop
52-
}
71+
return this._addCallbacks(cb, onError)
5372
}
5473

5574
get path () {
@@ -68,31 +87,29 @@ export class DocumentReference {
6887
async update (data) {
6988
Object.assign(this.data, data)
7089
this.exists = true
71-
this.cb(new DocumentSnapshot(null, this.id, this.data, true))
90+
this._callCallbacks(new DocumentSnapshot(null, this.id, this.data, true))
7291
return this.collection._modify(this.id, this.data, this)
7392
}
7493

7594
async set (data) {
7695
this.data = { ...data }
7796
this.exists = true
78-
this.cb(new DocumentSnapshot(null, this.id, this.data, true))
97+
this._callCallbacks(new DocumentSnapshot(null, this.id, this.data, true))
7998
return this.collection._modify(this.id, this.data, this)
8099
}
81100
}
82101

83-
class CollectionReference {
102+
class CollectionReference extends callbacksAndErrors {
84103
constructor (name) {
104+
super()
85105
this.data = {}
86106
this.name = name
87-
this.cb = this.onError = noop
88107
}
89108

90109
onSnapshot (cb, onError) {
91-
this.cb = cb
92-
this.onError = onError
93110
setTimeout(() => {
94111
// Object.keys(this.data).map((k, i) => console.log(k, 'at', i, this.data[k].data))
95-
this.cb({
112+
cb({
96113
docChanges: Object.keys(this.data).map((id, newIndex) => ({
97114
type: 'added',
98115
doc: new DocumentSnapshot(null, new Key(id), this.data[id].data),
@@ -101,9 +118,7 @@ class CollectionReference {
101118
}))
102119
})
103120
}, 0)
104-
return () => {
105-
this.cb = this.onError = noop
106-
}
121+
return this._addCallbacks(cb, onError)
107122
}
108123

109124
async add (data) {
@@ -114,7 +129,7 @@ class CollectionReference {
114129
data,
115130
index: Object.keys(this.data).length
116131
})
117-
this.cb({
132+
this._callCallbacks({
118133
docChanges: [{
119134
type: 'added',
120135
doc: new DocumentSnapshot(null, id, data),
@@ -141,7 +156,7 @@ class CollectionReference {
141156
async _remove (id) {
142157
const ref = this.data[id.v]
143158
delete this.data[id.v]
144-
this.cb({
159+
this._callCallbacks({
145160
docChanges: [{
146161
doc: new DocumentSnapshot(null, id, ref.data),
147162
type: 'removed'
@@ -158,7 +173,7 @@ class CollectionReference {
158173
this.data[id.v] = ref
159174
type = 'added'
160175
}
161-
this.cb({
176+
this._callCallbacks({
162177
docChanges: [{
163178
type,
164179
doc: new DocumentSnapshot(null, id, data),

test/refs-documents.spec.js

+1-10
Original file line numberDiff line numberDiff line change
@@ -317,18 +317,9 @@ test('unbinds removed properties', async () => {
317317
onSnapshotSpy.mockRestore()
318318
})
319319

320-
// XXX seems to bug on jest but works on real example...
321-
// could be the mock but don't see how
322-
// the key variable changes for no reason inside of
323-
// subscribeToDocument callback passed to onSnapshot
324-
test.skip('binds refs on arrays', async () => {
325-
const a = db.collection().doc()
326-
const b = db.collection().doc()
327-
const c = db.collection().doc()
320+
test('binds refs on arrays', async () => {
328321
const item = db.collection().doc()
329-
await a.update({ isA: true })
330322
await b.update({ isB: true })
331-
await c.update({ isC: true })
332323

333324
await item.update({
334325
arr: [a, b, a]

0 commit comments

Comments
 (0)