diff --git a/.changeset/small-scissors-try.md b/.changeset/small-scissors-try.md new file mode 100644 index 00000000000..6f712ea8bf8 --- /dev/null +++ b/.changeset/small-scissors-try.md @@ -0,0 +1,5 @@ +--- +"@firebase/firestore": patch +--- + +Fix the implementation of `collection()` with multiple path segments. diff --git a/packages/firestore/src/lite-api/reference.ts b/packages/firestore/src/lite-api/reference.ts index 50e6d9a652c..bfd7fce14c3 100644 --- a/packages/firestore/src/lite-api/reference.ts +++ b/packages/firestore/src/lite-api/reference.ts @@ -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, diff --git a/packages/firestore/test/lite/integration.test.ts b/packages/firestore/test/lite/integration.test.ts index a5035d58d3b..888ec9350c3 100644 --- a/packages/firestore/test/lite/integration.test.ts +++ b/packages/firestore/test/lite/integration.test.ts @@ -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'); @@ -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'); }); });