Skip to content

Commit

Permalink
Fix the implementation of collection() with multiple path segments. (
Browse files Browse the repository at this point in the history
…#5440)

* Fix the implementation of `collection()` with multiple path segments.

* Create small-scissors-try.md
  • Loading branch information
ehsannas committed Sep 8, 2021
1 parent 2d46e08 commit bf5772f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/small-scissors-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@firebase/firestore": patch
---

Fix the implementation of `collection()` with multiple path segments.
7 changes: 3 additions & 4 deletions packages/firestore/src/lite-api/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,10 +385,9 @@ export function collection(
'a DocumentReference or FirebaseFirestore'
);
}
const absolutePath = ResourcePath.fromString(
parent.path,
...pathSegments
).child(ResourcePath.fromString(path));
const absolutePath = parent._path.child(
ResourcePath.fromString(path, ...pathSegments)
);
validateCollectionPath(absolutePath);
return new CollectionReference(
parent.firestore,
Expand Down
24 changes: 20 additions & 4 deletions packages/firestore/test/lite/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,15 @@ describe('collection', () => {
});
});

it('can be used relative to Firestore root with multiple arguments', () => {
return withTestDb(db => {
const result = collection(db, 'coll1/doc1', '/coll2', 'doc2/', '/coll3/');
expect(result).to.be.an.instanceOf(CollectionReference);
expect(result.id).to.equal('coll3');
expect(result.path).to.equal('coll1/doc1/coll2/doc2/coll3');
});
});

it('can be used relative to collection', () => {
return withTestDb(db => {
const result = collection(collection(db, 'coll'), 'doc/subcoll');
Expand All @@ -268,12 +277,19 @@ describe('collection', () => {
});
});

it('can be used with multiple arguments', () => {
it('can be used relative to collection with multiple arguments', () => {
return withTestDb(db => {
const result = collection(db, 'coll1/doc1', 'coll2');
const col = collection(db, 'coll1');
const result = collection(
col,
'/doc1/coll2/doc2/',
'/coll3',
'doc3/',
'/coll4/'
);
expect(result).to.be.an.instanceOf(CollectionReference);
expect(result.id).to.equal('coll2');
expect(result.path).to.equal('coll1/doc1/coll2');
expect(result.id).to.equal('coll4');
expect(result.path).to.equal('coll1/doc1/coll2/doc2/coll3/doc3/coll4');
});
});

Expand Down

0 comments on commit bf5772f

Please sign in to comment.