diff --git a/lib/src/store/in_memory_backing_store.dart b/lib/src/store/in_memory_backing_store.dart index 89627fe..03f72a6 100644 --- a/lib/src/store/in_memory_backing_store.dart +++ b/lib/src/store/in_memory_backing_store.dart @@ -125,7 +125,6 @@ class InMemoryBackingStore implements BackingStore { (bool, Object?)? oldValue; if (_store.containsKey(key)) { oldValue = _store[key]; - _store[key] = valueToAdd; } else if (value is BackedModel) { value.backingStore?.subscribe( (dataKey, previousValue, newValue) { @@ -136,10 +135,10 @@ class InMemoryBackingStore implements BackingStore { }, key, ); - } else { - _store[key] = valueToAdd; } + _store[key] = valueToAdd; + if (value is Iterable) { value.whereType().forEach((model) { model.backingStore?.initializationCompleted = false; diff --git a/test/store/in_memory_backing_store_test.dart b/test/store/in_memory_backing_store_test.dart index 1a5ff6e..39ea69c 100644 --- a/test/store/in_memory_backing_store_test.dart +++ b/test/store/in_memory_backing_store_test.dart @@ -1,7 +1,10 @@ import 'package:kiota_abstractions/kiota_abstractions.dart'; import 'package:mockito/annotations.dart'; +import 'package:mockito/mockito.dart'; import 'package:test/test.dart'; +import 'in_memory_backing_store_test.mocks.dart'; + @GenerateMocks([BackedModel]) void main() { group('InMemoryBackingStore', () { @@ -69,5 +72,51 @@ void main() { expect(store.iterate(), hasLength(1)); expect(store.get('key'), 'value2'); }); + + test('propagates initialization completed to backed models', () { + final aModel = MockBackedModel(); + final aStore = InMemoryBackingStore(); + + when(aModel.backingStore).thenReturn(aStore); + + final bStore = InMemoryBackingStore(); + + expect(aStore.initializationCompleted, isTrue); + expect(bStore.initializationCompleted, isTrue); + + bStore + ..set('aModel', aModel) + ..initializationCompleted = false; + + expect(aStore.initializationCompleted, isFalse); + expect(bStore.initializationCompleted, isFalse); + + bStore.initializationCompleted = true; + + expect(aStore.initializationCompleted, isTrue); + expect(bStore.initializationCompleted, isTrue); + + final cModel = MockBackedModel(); + final cStore = InMemoryBackingStore(); + when(cModel.backingStore).thenReturn(cStore); + + bStore.set('cModel', cModel); + }); + + test('returns only changed values', () + { + final store = InMemoryBackingStore() + ..set('name', 'Peter') + ..set('email', 'peter@neverland.com') + ..returnOnlyChangedValues = true; + + final changedEntries = store.iterate().toList(); + expect(changedEntries, hasLength(0)); + + store.set('name', 'Wendy'); + + final changedEntries2 = store.iterate().toList(); + expect(changedEntries2, hasLength(1)); + }); }); }