diff --git a/README.md b/README.md index 33b522c..b4c9ce3 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,9 @@ for testing a firebase service that isn't fully mocked. - ```ref``` - ```value``` - ```exists``` + - ```hasChild``` + - ```child``` + - ```children``` ### Contributing diff --git a/lib/src/mock_data_snapshot.dart b/lib/src/mock_data_snapshot.dart index e1e382d..bf81b82 100644 --- a/lib/src/mock_data_snapshot.dart +++ b/lib/src/mock_data_snapshot.dart @@ -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 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 []; + } } diff --git a/pubspec.yaml b/pubspec.yaml index 5ab0188..181363b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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 @@ -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 diff --git a/test/mock_data_snapshot_test.dart b/test/mock_data_snapshot_test.dart index ccb22f6..d17ca44 100644 --- a/test/mock_data_snapshot_test.dart +++ b/test/mock_data_snapshot_test.dart @@ -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 sampleList; + late Map 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, []); + }); + }); }