Skip to content

Commit 4cee9bd

Browse files
committed
call snapshot observers with initial data
1 parent bbe1b38 commit 4cee9bd

File tree

6 files changed

+73
-17
lines changed

6 files changed

+73
-17
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
node_modules
44
coverage
55
browser
6+
.vscode

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ If you've encountered a bug or want to request new functionality, go ahead and f
99
* make your additions in `./src`
1010
* Do not edit the files in `./browser`. They are built automatically for releases. [@bendrucker](https://github.com/bendrucker) will have to spend time rebasing your PR and that makes him :cry:.
1111
* add test units in `./test`
12-
* `npm test` (node version < 8)
12+
* `npm test`
1313
* submit a pull request when all tests pass

src/firestore-document.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ MockFirestoreDocument.prototype.onSnapshot = function (optionsOrObserverOrOnNext
219219
// compare the current state to the one from when this function was created
220220
// and send the data to the callback if different.
221221
if (err === null) {
222-
if (JSON.stringify(self.data) !== JSON.stringify(context.data) || includeMetadataChanges || forceTrigger) {
222+
if (!_.isEqual(self.data, context.data) || includeMetadataChanges || forceTrigger) {
223223
onNext(new DocumentSnapshot(self.id, self.ref, self._getData()));
224224
context.data = self._getData();
225225
}

src/firestore-query.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,22 @@ MockFirestoreQuery.prototype.onSnapshot = function (optionsOrObserverOrOnNext, o
163163
// compare the current state to the one from when this function was created
164164
// and send the data to the callback if different.
165165
if (err === null) {
166-
self.get().then(function (querySnapshot) {
167-
var results = self._results();
168-
if (JSON.stringify(results) !== JSON.stringify(context.data) || includeMetadataChanges || forceTrigger) {
166+
if (forceTrigger) {
167+
const results = self._results();
168+
if (_.size(self.data) !== 0) {
169169
onNext(new QuerySnapshot(self.parent === null ? self : self.parent.collection(self.id), results));
170-
// onNext(new QuerySnapshot(self.id, self.ref, results));
171-
context.data = results;
170+
} else {
171+
onNext(new QuerySnapshot(self.parent === null ? self : self.parent.collection(self.id)));
172172
}
173-
});
173+
} else {
174+
self.get().then(function (querySnapshot) {
175+
var results = self._results();
176+
if (!_.isEqual(results, context.data) || includeMetadataChanges) {
177+
onNext(new QuerySnapshot(self.parent === null ? self : self.parent.collection(self.id), results));
178+
context.data = results;
179+
}
180+
});
181+
}
174182
} else {
175183
onError(err);
176184
}

test/unit/firestore-collection.js

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -370,14 +370,19 @@ describe('MockFirestoreCollection', function () {
370370

371371
describe('#onSnapshot', function () {
372372
it('returns value after collection is updated', function (done) {
373+
var callCount = 0;
373374
collection.onSnapshot(function(snap) {
375+
callCount += 1;
374376
var names = [];
375377
snap.docs.forEach(function(doc) {
376378
names.push(doc.data().name);
377379
});
378-
expect(names).to.contain('A');
379-
expect(names).not.to.contain('a');
380-
done();
380+
381+
if (callCount === 2) {
382+
expect(names).to.contain('A');
383+
expect(names).not.to.contain('a');
384+
done();
385+
}
381386
});
382387
collection.doc('a').update({name: 'A'}, {setMerge: true});
383388
collection.flush();
@@ -391,27 +396,49 @@ describe('MockFirestoreCollection', function () {
391396
snap.docs.forEach(function(doc) {
392397
names.push(doc.data().name);
393398
});
394-
if (callCount === 1) {
399+
400+
if (callCount === 2) {
395401
expect(names).to.contain('A');
396-
} else if (callCount === 2) {
397402
expect(names).not.to.contain('a');
403+
}
404+
405+
if (callCount === 3) {
406+
expect(names).to.contain('AA');
407+
expect(names).not.to.contain('A');
398408
done();
399409
}
400410
});
411+
401412
collection.doc('a').update({name: 'A'}, {setMerge: true});
402413
collection.flush();
403414
collection.doc('a').update({name: 'AA'}, {setMerge: true});
404415
collection.flush();
405416
});
406417

407418
it('should unsubscribe', function (done) {
419+
var callCount = 0;
408420
var unsubscribe = collection.onSnapshot(function(snap) {
409-
throw new Error("This should be unsubscribed.");
421+
callCount += 1;
410422
});
411-
unsubscribe();
423+
412424
collection.doc('a').update({name: 'A'}, {setMerge: true});
413425
collection.flush();
414-
done();
426+
427+
process.nextTick(function() {
428+
expect(callCount).to.equal(2);
429+
430+
collection.doc('a').update({name: 'AA'}, {setMerge: true});
431+
unsubscribe();
432+
433+
collection.flush();
434+
435+
process.nextTick(function() {
436+
expect(callCount).to.equal(2);
437+
done();
438+
});
439+
});
440+
441+
415442
});
416443

417444
it('Calls onError if error', function (done) {

test/unit/firestore-document.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,14 @@ describe('MockFirestoreDocument', function () {
465465
});
466466

467467
describe('#onSnapshot', function () {
468-
it('returns value after document is updated', function (done) {
468+
it('calls observer with initial state', function (done) {
469+
doc.onSnapshot(function(snap) {
470+
expect(snap.get('title')).to.equal('title');
471+
done();
472+
});
473+
});
474+
475+
it('calls observer when document is updated', function (done) {
469476
// onSnapshot calls immediately with the current state;
470477
// we only care about the updated..
471478
var first = true;
@@ -481,6 +488,19 @@ describe('MockFirestoreDocument', function () {
481488
db.flush();
482489
});
483490

491+
it('does not call observer when no changes occur', function (done) {
492+
var first = true;
493+
494+
doc.onSnapshot(function(snap) {
495+
if (!first) throw new Error('Observer called unexpectedly!');
496+
first = false;
497+
});
498+
499+
doc.update({title: 'title'}, {setMerge: true});
500+
db.flush();
501+
done();
502+
});
503+
484504
it('returns error if error occured', function (done) {
485505
var error = new Error("An error occured.");
486506
doc.errs.onSnapshot = error;

0 commit comments

Comments
 (0)