Add `spy` constructor to ObservableMap (#4)
Sometimes it's useful to observe changes on top of a Map implementation
which has different behavior from one of the built in map types.
- Add a test calling the 'spy' constructor
Most functionality is already tested. Make sure that calling the 'spy'
constructor delegates to the underlying map implementation.
diff --git a/lib/src/observable_map.dart b/lib/src/observable_map.dart
index a99610c..54e40e2 100644
--- a/lib/src/observable_map.dart
+++ b/lib/src/observable_map.dart
@@ -56,6 +56,9 @@
return result;
}
+ /// Creates a new observable map wrapping [other].
+ ObservableMap.spy(Map<K, V> other) : _map = other;
+
@override
Iterable<K> get keys => _map.keys;
diff --git a/test/observable_map_test.dart b/test/observable_map_test.dart
index a424ce1..04aeccc 100644
--- a/test/observable_map_test.dart
+++ b/test/observable_map_test.dart
@@ -362,6 +362,21 @@
});
});
});
+
+ group('Updates delegate as a spy', () {
+ Map delegate;
+ ObservableMap map;
+
+ setUp(() {
+ delegate = {};
+ map = new ObservableMap.spy(delegate);
+ });
+
+ test('[]=', () {
+ map['a'] = 42;
+ expect(delegate, {'a': 42});
+ });
+ });
}
_lengthChange(map, int oldValue, int newValue) =>