Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Make setMockInitialValues handle non-prefixed keys #2260

Merged
merged 4 commits into from
Nov 10, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/shared_preferences/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.5.4+4

* setMockInitialValues need to handle non-prefixed keys since that's an implementation detail.

## 0.5.4+3

* Android: Suppress casting warnings.
Expand Down
8 changes: 1 addition & 7 deletions packages/shared_preferences/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,5 @@ _incrementCounter() async {
You can populate `SharedPreferences` with initial values in your tests by running this code:

```dart
const MethodChannel('plugins.flutter.io/shared_preferences')
.setMockMethodCallHandler((MethodCall methodCall) async {
if (methodCall.method == 'getAll') {
return <String, dynamic>{}; // set initial values here if desired
}
return null;
});
SharedPrefernces.setMockInitialValues (Map<String, dynamic> values);
```
10 changes: 9 additions & 1 deletion packages/shared_preferences/lib/shared_preferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,17 @@ class SharedPreferences {
/// If the singleton instance has been initialized already, it is nullified.
@visibleForTesting
static void setMockInitialValues(Map<String, dynamic> values) {
final Map<String, dynamic> newValues =
values.map<String, dynamic>((String key, dynamic value) {
String newKey = key;
if (!key.startsWith(_prefix)) {
newKey = '$_prefix$key';
}
return MapEntry<String, dynamic>(newKey, value);
});
_kChannel.setMockMethodCallHandler((MethodCall methodCall) async {
if (methodCall.method == 'getAll') {
return values;
return newValues;
}
return null;
});
Expand Down
2 changes: 1 addition & 1 deletion packages/shared_preferences/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Flutter plugin for reading and writing simple key-value pairs.
Wraps NSUserDefaults on iOS and SharedPreferences on Android.
author: Flutter Team <[email protected]>
homepage: https://github.com/flutter/plugins/tree/master/packages/shared_preferences
version: 0.5.4+3
version: 0.5.4+4

flutter:
plugin:
Expand Down
9 changes: 9 additions & 0 deletions packages/shared_preferences/test/shared_preferences_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,13 @@ void main() {
expect(preferences.getStringList('myList'), <String>[]);
});
});

test('calling mock initial values with non-prefixed keys succeeds', () async {
SharedPreferences.setMockInitialValues(<String, String>{
'test': 'foo',
});
final SharedPreferences prefs = await SharedPreferences.getInstance();
final String value = prefs.getString('test');
expect(value, 'foo');
});
}