Skip to content

Commit bab8353

Browse files
author
Bonny BUN
committed
Add methods for children testing in mock_data_snapshot.dart
1 parent 172d87a commit bab8353

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

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

0 commit comments

Comments
 (0)