Skip to content

Commit

Permalink
fix: doc(/absolute_path).path returns absolute_path without the start…
Browse files Browse the repository at this point in the history
…ing /
  • Loading branch information
atn832 committed Jan 9, 2023
1 parent 38ca8cb commit c01eb23
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 30 deletions.
4 changes: 4 additions & 0 deletions lib/src/fake_cloud_firestore_instance.dart
Expand Up @@ -49,6 +49,10 @@ class FakeFirebaseFirestore implements FirebaseFirestore {

@override
DocumentReference<Map<String, dynamic>> doc(String path) {
// Remove the starting '/' if found, like the actual Firestore.
if (path.startsWith('/')) {
path = path.substring(1);
}
final segments = path.split('/');
// The actual behavior of Firestore for an invalid number of segments
// differs by platforms. This library imitates it with assert.
Expand Down
69 changes: 39 additions & 30 deletions test/fake_cloud_firestore_test.dart
Expand Up @@ -370,39 +370,48 @@ void main() {
}));
});

test('Document reference path', () async {
final instance = FakeFirebaseFirestore();
final documentReference = instance
.collection('users')
.doc('aaa')
.collection('friends')
.doc('bbb')
.collection('friends-friends')
.doc('ccc');
group('Document reference path', () {
test('collection(...).doc(...).path', () async {
final instance = FakeFirebaseFirestore();
final documentReference = instance
.collection('users')
.doc('aaa')
.collection('friends')
.doc('bbb')
.collection('friends-friends')
.doc('ccc');

expect(documentReference.path, 'users/aaa/friends/bbb/friends-friends/ccc');
expect(
documentReference.parent.path, 'users/aaa/friends/bbb/friends-friends');
});
expect(
documentReference.path, 'users/aaa/friends/bbb/friends-friends/ccc');
expect(documentReference.parent.path,
'users/aaa/friends/bbb/friends-friends');
});

test('Document and collection parent', () async {
final instance = FakeFirebaseFirestore();
final documentReference = instance
.collection('users')
.doc('aaa')
.collection('friends')
.doc('bbb')
.collection('friends-friends')
.doc('ccc');

final friendsFriends = documentReference.parent;
final bbb = friendsFriends.parent;
expect(bbb, isNotNull);
final friends = bbb!.parent;
final bbbSibling = friends.doc('bbb-sibling');
expect(bbbSibling.path, 'users/aaa/friends/bbb-sibling');
});
test('firestore.(Absolute doc reference).path', () async {
final instance = FakeFirebaseFirestore();
// Both should return 'users/abc'.
expect(instance.doc('/users/abc').path, 'users/abc');
expect(instance.doc('users/abc').path, 'users/abc');
});

test('Document and collection parent', () async {
final instance = FakeFirebaseFirestore();
final documentReference = instance
.collection('users')
.doc('aaa')
.collection('friends')
.doc('bbb')
.collection('friends-friends')
.doc('ccc');

final friendsFriends = documentReference.parent;
final bbb = friendsFriends.parent;
expect(bbb, isNotNull);
final friends = bbb!.parent;
final bbbSibling = friends.doc('bbb-sibling');
expect(bbbSibling.path, 'users/aaa/friends/bbb-sibling');
});
});
test('firestore field', () async {
final instance = FakeFirebaseFirestore();
final documentReference =
Expand Down

0 comments on commit c01eb23

Please sign in to comment.