-
Notifications
You must be signed in to change notification settings - Fork 87
/
cloud_firestore_mocks_base.dart
62 lines (50 loc) · 1.75 KB
/
cloud_firestore_mocks_base.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import 'dart:convert';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:cloud_firestore_platform_interface/cloud_firestore_platform_interface.dart';
import 'package:mockito/mockito.dart';
import 'mock_collection_reference.dart';
import 'mock_document_reference.dart';
import 'mock_field_value_factory_platform.dart';
import 'mock_write_batch.dart';
import 'util.dart';
class MockFirestoreInstance extends Mock implements Firestore {
Map<String, dynamic> _root = Map();
Map<String, dynamic> _snapshotStreamControllerRoot = Map();
/// Saved documents' full paths from root. For example:
/// 'users/abc/friends/foo'
final Set<String> _savedDocumentPaths = <String>{};
MockFirestoreInstance() {
_setupFieldValueFactory();
}
@override
CollectionReference collection(String path) {
return MockCollectionReference(this, path, getSubpath(_root, path),
getSubpath(_snapshotStreamControllerRoot, path));
}
@override
DocumentReference document(String path) {
final documentId = path.split('/').last;
return MockDocumentReference(this, path, documentId, getSubpath(_root, path), _root,
getSubpath(_snapshotStreamControllerRoot, path));
}
WriteBatch batch() {
return MockWriteBatch();
}
String dump() {
JsonEncoder encoder = JsonEncoder.withIndent(' ', myEncode);
final jsonText = encoder.convert(_root);
return jsonText;
}
void saveDocument(String path) {
_savedDocumentPaths.add(path);
}
bool hasSavedDocument(String path) {
return _savedDocumentPaths.contains(path);
}
bool removeSavedDocument(String path) {
return _savedDocumentPaths.remove(path);
}
_setupFieldValueFactory() {
FieldValueFactoryPlatform.instance = MockFieldValueFactoryPlatform();
}
}