Skip to content

Commit 3c4f8b6

Browse files
author
Bonny BUN
committed
Add unit tests and edit README.md
1 parent bab8353 commit 3c4f8b6

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ for testing a firebase service that isn't fully mocked.
107107
- ```ref```
108108
- ```value```
109109
- ```exists```
110+
- ```hasChild```
111+
- ```child```
112+
- ```children```
110113

111114

112115
### Contributing

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)