Skip to content

Commit da580fc

Browse files
authored
Make setMockInitialValues handle non-prefixed keys (flutter#2260)
1 parent f554497 commit da580fc

File tree

5 files changed

+24
-9
lines changed

5 files changed

+24
-9
lines changed

packages/shared_preferences/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.5.4+4
2+
3+
* `setMockInitialValues` needs to handle non-prefixed keys since that's an implementation detail.
4+
15
## 0.5.4+3
26

37
* Android: Suppress casting warnings.

packages/shared_preferences/README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,5 @@ _incrementCounter() async {
4242
You can populate `SharedPreferences` with initial values in your tests by running this code:
4343

4444
```dart
45-
const MethodChannel('plugins.flutter.io/shared_preferences')
46-
.setMockMethodCallHandler((MethodCall methodCall) async {
47-
if (methodCall.method == 'getAll') {
48-
return <String, dynamic>{}; // set initial values here if desired
49-
}
50-
return null;
51-
});
45+
SharedPrefernces.setMockInitialValues (Map<String, dynamic> values);
5246
```

packages/shared_preferences/lib/shared_preferences.dart

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,17 @@ class SharedPreferences {
181181
/// If the singleton instance has been initialized already, it is nullified.
182182
@visibleForTesting
183183
static void setMockInitialValues(Map<String, dynamic> values) {
184+
final Map<String, dynamic> newValues =
185+
values.map<String, dynamic>((String key, dynamic value) {
186+
String newKey = key;
187+
if (!key.startsWith(_prefix)) {
188+
newKey = '$_prefix$key';
189+
}
190+
return MapEntry<String, dynamic>(newKey, value);
191+
});
184192
_kChannel.setMockMethodCallHandler((MethodCall methodCall) async {
185193
if (methodCall.method == 'getAll') {
186-
return values;
194+
return newValues;
187195
}
188196
return null;
189197
});

packages/shared_preferences/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: Flutter plugin for reading and writing simple key-value pairs.
33
Wraps NSUserDefaults on iOS and SharedPreferences on Android.
44
author: Flutter Team <[email protected]>
55
homepage: https://github.com/flutter/plugins/tree/master/packages/shared_preferences
6-
version: 0.5.4+3
6+
version: 0.5.4+4
77

88
flutter:
99
plugin:

packages/shared_preferences/test/shared_preferences_test.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,13 @@ void main() {
197197
expect(preferences.getStringList('myList'), <String>[]);
198198
});
199199
});
200+
201+
test('calling mock initial values with non-prefixed keys succeeds', () async {
202+
SharedPreferences.setMockInitialValues(<String, String>{
203+
'test': 'foo',
204+
});
205+
final SharedPreferences prefs = await SharedPreferences.getInstance();
206+
final String value = prefs.getString('test');
207+
expect(value, 'foo');
208+
});
200209
}

0 commit comments

Comments
 (0)