Skip to content

Commit b5e3f8c

Browse files
authored
Merge pull request soumak77#100 from Storr-co/firestore-stream-query
Implement Firestore Query#stream mock
2 parents 040a492 + 4be6fc8 commit b5e3f8c

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/firestore-query.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
var _ = require('./lodash');
44
var assert = require('assert');
5+
var Stream = require('stream');
56
var Promise = require('rsvp').Promise;
67
var autoId = require('firebase-auto-ids');
78
var QuerySnapshot = require('./firestore-query-snapshot');
@@ -108,6 +109,19 @@ MockFirestoreQuery.prototype.get = function () {
108109
});
109110
};
110111

112+
MockFirestoreQuery.prototype.stream = function () {
113+
var stream = new Stream();
114+
115+
this.get().then(function (snapshots) {
116+
snapshots.forEach(function (snapshot) {
117+
stream.emit('data', snapshot);
118+
});
119+
stream.emit('end');
120+
});
121+
122+
return stream;
123+
};
124+
111125
MockFirestoreQuery.prototype.where = function (property, operator, value) {
112126
var query;
113127

test/unit/firestore-collection.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,37 @@ describe('MockFirestoreCollection', function () {
268268
});
269269
});
270270

271+
describe('#stream', function () {
272+
function makeSnapComparable(snap) {
273+
return {
274+
id: snap.id,
275+
data: snap.data(),
276+
};
277+
}
278+
279+
it('returns a stream that emits all results', function (done) {
280+
collection.get().then(function (snaps) {
281+
var streamDocs = [];
282+
283+
collection.stream()
284+
.on('data', function(snap) {
285+
streamDocs.push(makeSnapComparable(snap));
286+
})
287+
.on('end', function () {
288+
try {
289+
expect(streamDocs).to.eql(snaps.docs.map(makeSnapComparable));
290+
done();
291+
} catch (err) {
292+
done(err);
293+
}
294+
});
295+
296+
db.flush();
297+
}).catch(done);
298+
db.flush();
299+
});
300+
});
301+
271302
describe('#orderBy', function () {
272303
it('allow calling orderBy() on collection', function() {
273304
expect(function() {

0 commit comments

Comments
 (0)