Skip to content

Commit

Permalink
Fixed issue with backed models and added tests for initializationComp…
Browse files Browse the repository at this point in the history
…leted propagation
  • Loading branch information
ricardoboss committed Feb 19, 2024
1 parent 32ed674 commit 9e3015b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
5 changes: 2 additions & 3 deletions lib/src/store/in_memory_backing_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -136,10 +135,10 @@ class InMemoryBackingStore implements BackingStore {
},
key,
);
} else {
_store[key] = valueToAdd;
}

_store[key] = valueToAdd;

if (value is Iterable<Object?>) {
value.whereType<BackedModel>().forEach((model) {
model.backingStore?.initializationCompleted = false;
Expand Down
49 changes: 49 additions & 0 deletions test/store/in_memory_backing_store_test.dart
Original file line number Diff line number Diff line change
@@ -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', () {
Expand Down Expand Up @@ -69,5 +72,51 @@ void main() {
expect(store.iterate(), hasLength(1));
expect(store.get<String>('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', '[email protected]')
..returnOnlyChangedValues = true;

final changedEntries = store.iterate().toList();
expect(changedEntries, hasLength(0));

store.set('name', 'Wendy');

final changedEntries2 = store.iterate().toList();
expect(changedEntries2, hasLength(1));
});
});
}

0 comments on commit 9e3015b

Please sign in to comment.