Skip to content

Commit 8d40b88

Browse files
committed
Merge branch 'issue-81-onsnapshot' of github.com:zeevl/firebase-mock into issue-81-onsnapshot
2 parents d55bbd4 + 55e64c9 commit 8d40b88

File tree

6 files changed

+74
-22
lines changed

6 files changed

+74
-22
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: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
'use strict';
22

33
var _ = require('./lodash');
4-
var assert = require('assert');
54
var Stream = require('stream');
65
var Promise = require('rsvp').Promise;
7-
var autoId = require('firebase-auto-ids');
86
var QuerySnapshot = require('./firestore-query-snapshot');
97
var Queue = require('./queue').Queue;
108
var utils = require('./utils');
11-
var validate = require('./validators');
129

1310
function MockFirestoreQuery(path, data, parent, name) {
1411
this.errs = {};
@@ -68,8 +65,7 @@ MockFirestoreQuery.prototype.get = function () {
6865
return new Promise(function (resolve, reject) {
6966
self._defer('get', _.toArray(arguments), function () {
7067
var results = self._results();
71-
var limit = 0;
72-
68+
7369
if (err === null) {
7470
if (_.size(self.data) !== 0) {
7571
resolve(new QuerySnapshot(self.parent === null ? self : self.parent.collection(self.id), results));
@@ -163,14 +159,22 @@ MockFirestoreQuery.prototype.onSnapshot = function (optionsOrObserverOrOnNext, o
163159
// compare the current state to the one from when this function was created
164160
// and send the data to the callback if different.
165161
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) {
162+
if (forceTrigger) {
163+
const results = self._results();
164+
if (_.size(self.data) !== 0) {
169165
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;
166+
} else {
167+
onNext(new QuerySnapshot(self.parent === null ? self : self.parent.collection(self.id)));
172168
}
173-
});
169+
} else {
170+
self.get().then(function (querySnapshot) {
171+
var results = self._results();
172+
if (!_.isEqual(results, context.data) || includeMetadataChanges) {
173+
onNext(new QuerySnapshot(self.parent === null ? self : self.parent.collection(self.id), results));
174+
context.data = results;
175+
}
176+
});
177+
}
174178
} else {
175179
onError(err);
176180
}

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)