Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ for testing a firebase service that isn't fully mocked.
- ```ref```
- ```value```
- ```exists```
- ```hasChild```
- ```child```
- ```children```


### Contributing
Expand Down
43 changes: 43 additions & 0 deletions lib/src/mock_data_snapshot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,47 @@ class MockDataSnapshot extends Mock implements DataSnapshot {

@override
bool get exists => _value != null;

@override
bool hasChild(String path) {
final value = _value;
if (value is Map) {
return value.containsKey(path);
} else if (value is List) {
int? index = int.tryParse(path);
if (index != null) {
return index >= 0 && index < value.length;
}
}
return false;
}

@override
DataSnapshot child(String path) {
final value = _value;
if (value is Map) {
return MockDataSnapshot(_ref.child(path), value[path]);
} else if (value is List) {
int? index = int.tryParse(path);
if (index != null && index >= 0 && index < value.length) {
return MockDataSnapshot(_ref.child("$index"), value[index]);
}
}
return MockDataSnapshot(_ref.child(path), null);
}

@override
Iterable<DataSnapshot> get children {
final value = _value;
if (value is Map) {
return value
.map((key, value) =>
MapEntry(key, MockDataSnapshot(_ref.child(key), value)))
.values;
} else if (value is List) {
var index = 0;
return value.map((e) => MockDataSnapshot(_ref.child("${index++}"), e));
}
return [];
}
}
9 changes: 4 additions & 5 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: firebase_database_mocks

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

version: 0.5.0
version: 0.6.0

repository: https://github.com/sitatec/firebase_database_mocks.git
homepage: https://github.com/sitatec/firebase_database_mocks.git
Expand All @@ -16,10 +16,9 @@ dependencies:
sdk: flutter
flutter_test:
sdk: flutter
firebase_database: ^10.0.2
mockito: ^5.3.0
firebase_core_platform_interface: ^4.5.0
firebase_database: ^10.0.6
mockito: ^5.3.2
firebase_core_platform_interface: ^4.5.2

dev_dependencies:

flutter_lints: ^2.0.1
52 changes: 52 additions & 0 deletions test/mock_data_snapshot_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,56 @@ void main() {
test('Should return key that is the same as in reference', () {
expect(MockDataSnapshot(reference, null).key, equals(reference.key));
});

group("child tests", () {
late List<String> sampleList;
late Map<String, dynamic> sampleMap;
setUp(() {
sampleList = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
sampleMap = {"a": 2, "b": 5, "c": 0};
});

test('Should return the same updated list', () async {
await reference.update({"list": sampleList});
final dataSnapshot = await reference.child("list").get();

expect(dataSnapshot.value, sampleList);
expect(dataSnapshot.hasChild("1"), true);
expect(dataSnapshot.hasChild("-1"), false);
expect(dataSnapshot.child("1").value, "b");
expect(dataSnapshot.child("-1").value, null);
var children = dataSnapshot.children.toList();
for (int i = 0; i < children.length; i++) {
expect(children[i].value, sampleList[i]);
}
});

test('Should return the same updated map', () async {
await reference.update({"map": sampleMap});

final MockDataSnapshot dataSnapshot =
await reference.child("map").get() as MockDataSnapshot;
expect(dataSnapshot.value, sampleMap);
expect(dataSnapshot.hasChild("b"), true);
expect(dataSnapshot.hasChild("foo"), false);
expect(dataSnapshot.child("b").value, 5);
expect(dataSnapshot.child("d").value, null);
var children = dataSnapshot.children.toList();
for (int i = 0; i < children.length; i++) {
expect(children[i].value, sampleMap[children[i].key]);
}
});

test('Should return the same single value', () async {
await reference.update({"value": 42});
var dataSnapshot = (await reference.child("value").get());
expect(dataSnapshot.value, 42);
expect(dataSnapshot.child("foo").value, null);

await reference.set(100);
dataSnapshot = (await reference.get());
expect(dataSnapshot.value, 100);
expect(dataSnapshot.children, []);
});
});
}