Skip to content

Commit 525c683

Browse files
authored
Merge pull request #14 from bonnybun/additions_in_mock_data_snapshot
Add methods for children testing in mock_data_snapshot.dart
2 parents 271d749 + 3c4f8b6 commit 525c683

File tree

4 files changed

+102
-5
lines changed

4 files changed

+102
-5
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ for testing a firebase service that isn't fully mocked.
109109
- ```ref```
110110
- ```value```
111111
- ```exists```
112+
- ```hasChild```
113+
- ```child```
114+
- ```children```
112115

113116

114117
### Contributing

lib/src/mock_data_snapshot.dart

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,47 @@ class MockDataSnapshot extends Mock implements DataSnapshot {
1818

1919
@override
2020
bool get exists => _value != null;
21+
22+
@override
23+
bool hasChild(String path) {
24+
final value = _value;
25+
if (value is Map) {
26+
return value.containsKey(path);
27+
} else if (value is List) {
28+
int? index = int.tryParse(path);
29+
if (index != null) {
30+
return index >= 0 && index < value.length;
31+
}
32+
}
33+
return false;
34+
}
35+
36+
@override
37+
DataSnapshot child(String path) {
38+
final value = _value;
39+
if (value is Map) {
40+
return MockDataSnapshot(_ref.child(path), value[path]);
41+
} else if (value is List) {
42+
int? index = int.tryParse(path);
43+
if (index != null && index >= 0 && index < value.length) {
44+
return MockDataSnapshot(_ref.child("$index"), value[index]);
45+
}
46+
}
47+
return MockDataSnapshot(_ref.child(path), null);
48+
}
49+
50+
@override
51+
Iterable<DataSnapshot> get children {
52+
final value = _value;
53+
if (value is Map) {
54+
return value
55+
.map((key, value) =>
56+
MapEntry(key, MockDataSnapshot(_ref.child(key), value)))
57+
.values;
58+
} else if (value is List) {
59+
var index = 0;
60+
return value.map((e) => MockDataSnapshot(_ref.child("${index++}"), e));
61+
}
62+
return [];
63+
}
2164
}

pubspec.yaml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: firebase_database_mocks
22

33
description: Fakes to write unit tests for FirebaseDatabase (real-time database).
44

5-
version: 0.5.0
5+
version: 0.6.0
66

77
repository: https://github.com/sitatec/firebase_database_mocks.git
88
homepage: https://github.com/sitatec/firebase_database_mocks.git
@@ -16,10 +16,9 @@ dependencies:
1616
sdk: flutter
1717
flutter_test:
1818
sdk: flutter
19-
firebase_database: ^10.0.2
20-
mockito: ^5.3.0
21-
firebase_core_platform_interface: ^4.5.0
19+
firebase_database: ^10.0.6
20+
mockito: ^5.3.2
21+
firebase_core_platform_interface: ^4.5.2
2222

2323
dev_dependencies:
24-
2524
flutter_lints: ^2.0.1

test/mock_data_snapshot_test.dart

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,56 @@ void main() {
2020
test('Should return key that is the same as in reference', () {
2121
expect(MockDataSnapshot(reference, null).key, equals(reference.key));
2222
});
23+
24+
group("child tests", () {
25+
late List<String> sampleList;
26+
late Map<String, dynamic> sampleMap;
27+
setUp(() {
28+
sampleList = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
29+
sampleMap = {"a": 2, "b": 5, "c": 0};
30+
});
31+
32+
test('Should return the same updated list', () async {
33+
await reference.update({"list": sampleList});
34+
final dataSnapshot = await reference.child("list").get();
35+
36+
expect(dataSnapshot.value, sampleList);
37+
expect(dataSnapshot.hasChild("1"), true);
38+
expect(dataSnapshot.hasChild("-1"), false);
39+
expect(dataSnapshot.child("1").value, "b");
40+
expect(dataSnapshot.child("-1").value, null);
41+
var children = dataSnapshot.children.toList();
42+
for (int i = 0; i < children.length; i++) {
43+
expect(children[i].value, sampleList[i]);
44+
}
45+
});
46+
47+
test('Should return the same updated map', () async {
48+
await reference.update({"map": sampleMap});
49+
50+
final MockDataSnapshot dataSnapshot =
51+
await reference.child("map").get() as MockDataSnapshot;
52+
expect(dataSnapshot.value, sampleMap);
53+
expect(dataSnapshot.hasChild("b"), true);
54+
expect(dataSnapshot.hasChild("foo"), false);
55+
expect(dataSnapshot.child("b").value, 5);
56+
expect(dataSnapshot.child("d").value, null);
57+
var children = dataSnapshot.children.toList();
58+
for (int i = 0; i < children.length; i++) {
59+
expect(children[i].value, sampleMap[children[i].key]);
60+
}
61+
});
62+
63+
test('Should return the same single value', () async {
64+
await reference.update({"value": 42});
65+
var dataSnapshot = (await reference.child("value").get());
66+
expect(dataSnapshot.value, 42);
67+
expect(dataSnapshot.child("foo").value, null);
68+
69+
await reference.set(100);
70+
dataSnapshot = (await reference.get());
71+
expect(dataSnapshot.value, 100);
72+
expect(dataSnapshot.children, []);
73+
});
74+
});
2375
}

0 commit comments

Comments
 (0)