Skip to content

Commit

Permalink
fixed firing of updates for query
Browse files Browse the repository at this point in the history
  • Loading branch information
atn832 committed Jan 8, 2023
1 parent 7dc8bcf commit 38ca8cb
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 33 deletions.
2 changes: 1 addition & 1 deletion lib/src/mock_collection_reference.dart
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class MockCollectionReference<T extends Object?> extends MockQuery<T>
final documentReference = doc();
await documentReference.set(data);
_firestore.saveDocument(documentReference.path);
QuerySnapshotStreamManager().fireSnapshotUpdate(firestore, path);
await QuerySnapshotStreamManager().fireSnapshotUpdate(firestore, path);
return documentReference;
}

Expand Down
8 changes: 4 additions & 4 deletions lib/src/mock_document_reference.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class MockDocumentReference<T extends Object?> implements DocumentReference<T> {
}

/// Sets document raw data. Does not check for existence.
Future<void> _setRawData(Map<String, dynamic> data) {
Future<void> _setRawData(Map<String, dynamic> data) async {
validateDocumentValue(data);
// Copy data so that subsequent change to `data` should not affect the data
// stored in mock document.
Expand All @@ -120,7 +120,7 @@ class MockDocumentReference<T extends Object?> implements DocumentReference<T> {
_applyValues(document, key, value);
});
_firestore.saveDocument(path);
QuerySnapshotStreamManager().fireSnapshotUpdate(firestore, path);
await QuerySnapshotStreamManager().fireSnapshotUpdate(firestore, path);
fireSnapshotUpdate();
return Future.value(null);
}
Expand Down Expand Up @@ -248,11 +248,11 @@ class MockDocumentReference<T extends Object?> implements DocumentReference<T> {
}

@override
Future<void> delete() {
Future<void> delete() async {
rootParent.remove(id);
_firestore.removeSavedDocument(path);
// Notify on the parent collection.
QuerySnapshotStreamManager().fireSnapshotUpdate(firestore, path);
await QuerySnapshotStreamManager().fireSnapshotUpdate(firestore, path);
// Notify the document listeners.
fireSnapshotUpdate();
return Future.value();
Expand Down
7 changes: 4 additions & 3 deletions lib/src/query_snapshot_stream_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ class QuerySnapshotStreamManager {
return streamController;
}

void fireSnapshotUpdate(FirebaseFirestore firestore, String path) {
Future<void> fireSnapshotUpdate(
FirebaseFirestore firestore, String path) async {
if (!_streamCache.containsKey(firestore)) {
// Normal. It happens if you try to fire updates before anyone has
// subscribed to snapshots.
Expand All @@ -88,15 +89,15 @@ class QuerySnapshotStreamManager {
final exactPathCache = _streamCache[firestore]![path];
if (exactPathCache != null) {
for (final query in exactPathCache.keys) {
query.get().then(exactPathCache[query]!.add);
await query.get().then(exactPathCache[query]!.add);
}
}

// When a document is modified, fire an update on the parent collection.
if (path.contains('/')) {
final tokens = path.split('/');
final parentPath = tokens.sublist(0, tokens.length - 1).join('/');
fireSnapshotUpdate(firestore, parentPath);
await fireSnapshotUpdate(firestore, parentPath);
}
}
}
41 changes: 16 additions & 25 deletions test/mock_query_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1073,39 +1073,30 @@ void main() {
];

final unarchivedAscContents = [
// beginning. add testData[0].
[],
// testData[1]
['hello!'],
// add testData[2].
['hola!', 'hello!'],
// add testData[3].
['hola!', 'hello!', 'Ciao!'],
// update testData[3] as archived.
['hola!', 'hello!'],
// delete testData[2].
['hello!'],
];

final instance = FakeFirebaseFirestore();
var called = 0;
instance
.collection('messages')
.orderBy('receivedAt')
.where('archived', isEqualTo: false)
.snapshots()
.listen(expectAsync1((snapshot) {
final docs = snapshot.docs;
try {
if (called == 0) {
expect(docs, isEmpty);
return;
} else {
expect(docs.length, unarchivedAscContents[called - 1].length);
}
for (var i = 0; i < docs.length; i++) {
expect(
docs[i].get('content'),
equals(unarchivedAscContents[called - 1][i]),
);
}
} finally {
called++;
}
}, count: unarchivedAscContents.length + 1));
expect(
instance
.collection('messages')
.orderBy('receivedAt')
.where('archived', isEqualTo: false)
.snapshots()
.map((snapshot) =>
snapshot.docs.map((d) => d.get('content')).toList()),
emitsInOrder(unarchivedAscContents));

// add data
await instance.collection('messages').add(testData[0]);
Expand Down

0 comments on commit 38ca8cb

Please sign in to comment.