Skip to content

Commit

Permalink
Adds support for listCollections
Browse files Browse the repository at this point in the history
It just creates a reference from getCollections to listCollections.

Co-authored-by: Nick Gavrilov <[email protected]>
  • Loading branch information
heygambo and ilearnio committed Feb 28, 2020
1 parent bd85b69 commit 4dfa974
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
behavior of `onAuthStateChanged()` (see below)
- Support for Firebase Messaging (Admin API)
- Support for [FieldValue.increment](https://firebase.google.com/docs/reference/js/firebase.firestore.FieldValue#increment)
- Support for `listCollections` in [DocumentReferences](https://googleapis.dev/nodejs/firestore/latest/DocumentReference.html#listCollections)

### Changed
- (Breaking) Consistent with Firebase SDK [version 4.0.0](https://firebase.google.com/support/release-notes/js#version_500_-_may_8_2018) and later,
Expand All @@ -23,7 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed
- `onAuthStateChanged` now correctly calls its callback immediately with
the current auth state.
the current auth state.
- `MockStorage.bucket()` and `MockStorageBucket.file()` now return the
existing artifact if one exists, rather than overwriting it with a new
one.
Expand Down
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/firestore-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ MockFirestoreDocument.prototype.getCollections = function () {
});
});
};
MockFirestoreDocument.prototype.listCollections = MockFirestoreDocument.prototype.getCollections;

MockFirestoreDocument.prototype._hasChild = function (key) {
return _.isObject(this.data) && _.has(this.data, key);
Expand Down
67 changes: 67 additions & 0 deletions test/unit/firestore-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,73 @@ describe('MockFirestoreDocument', function () {
});
});

describe('#listCollections', function () {
beforeEach(function () {
db.doc('doc/subcol/subcol-doc').set({ foo: 'bar' });
db.doc('doc/subcol2/subcol-doc').set({ foo: 'bar' });
db.doc('doc/subcol/subcol-doc/deep-col/deep-doc').set({ foo: 'bar' });
db.doc('doc/subcol/subcol-doc/deep-col2/deep-doc').set({ foo: 'bar' });
db.flush();
});
afterEach(function () {
db.doc('doc/subcol/subcol-doc').delete();
db.doc('doc/subcol2/subcol-doc').delete();
db.doc('doc/subcol/subcol-doc/deep-col/deep-doc').delete();
db.doc('doc/subcol/subcol-doc/deep-col2/deep-doc').delete();
db.flush();
});

context('when present', function () {
it('returns collections of document', function (done) {
db.doc('doc').listCollections().then(function (colRefs) {
expect(colRefs).to.be.an('array');
expect(colRefs).to.have.length(2);
expect(colRefs[0].path).to.equal('doc/subcol');
expect(colRefs[1].path).to.equal('doc/subcol2');
done();
});
db.flush();
});

it('returns deeply nested collections of document', function (done) {
db.doc('doc/subcol/subcol-doc').listCollections().then(function (colRefs) {
expect(colRefs).to.be.an('array');
expect(colRefs).to.have.length(2);
expect(colRefs[0].path).to.equal('doc/subcol/subcol-doc/deep-col');
expect(colRefs[1].path).to.equal('doc/subcol/subcol-doc/deep-col2');
done();
});
db.flush();
});
});

context('when not present', function () {
it('returns empty list of collections', function (done) {
db.doc('not-existing').listCollections().then(function (colRefs) {
expect(colRefs).to.be.an('array');
expect(colRefs).to.have.length(0);
done();
});
db.flush();
});

it('skips collections that has no documents', function (done) {
db.doc('doc/subcol/subcol-doc').delete();
db.doc('doc/subcol2/subcol-doc').delete();
db.doc('doc/subcol/subcol-doc/deep-col/deep-doc').delete();
db.doc('doc/subcol/subcol-doc/deep-col2/deep-doc').delete();
db.flush();

db.doc('doc').listCollections().then(function (colRefs) {
expect(colRefs).to.be.an('array');
expect(colRefs).to.have.length(0);
done();
});
db.flush();
});
});
});

describe('#onSnapshot', function () {
it('calls observer with initial state', function (done) {
doc.onSnapshot(function(snap) {
Expand Down

0 comments on commit 4dfa974

Please sign in to comment.