Shared Preferences Web Test - Dart
Shared Preferences Web Test - Dart
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences_platform_interface/shared_preferences_platfor
m_interface.dart';
import 'package:shared_preferences_web/shared_preferences_web.dart';
void main() {
group('SharedPreferencesPlugin', () {
setUp(() {
html.window.localStorage.clear();
});
test('registers itself', () {
expect(SharedPreferencesStorePlatform.instance,
isNot(isA<SharedPreferencesPlugin>()));
SharedPreferencesPlugin.registerWith(null);
expect(SharedPreferencesStorePlatform.instance,
isA<SharedPreferencesPlugin>());
});
test('getAll', () async {
final SharedPreferencesPlugin store = SharedPreferencesPlugin();
expect(await store.getAll(), isEmpty);
test('remove', () async {
final SharedPreferencesPlugin store = SharedPreferencesPlugin();
html.window.localStorage['flutter.testKey'] = '"test value"';
expect(html.window.localStorage['flutter.testKey'], isNotNull);
expect(await store.remove('flutter.testKey'), isTrue);
expect(html.window.localStorage['flutter.testKey'], isNull);
expect(
() => store.remove('unprefixed'),
throwsA(isA<FormatException>()),
);
});
test('setValue', () async {
final SharedPreferencesPlugin store = SharedPreferencesPlugin();
for (String key in kTestValues.keys) {
final dynamic value = kTestValues[key];
expect(await store.setValue(key.split('.').last, key, value), true);
}
expect(html.window.localStorage.keys, hasLength(kTestValues.length));
for (String key in html.window.localStorage.keys) {
expect(html.window.localStorage[key], json.encode(kTestValues[key]));
}
test('clear', () async {
final SharedPreferencesPlugin store = SharedPreferencesPlugin();
html.window.localStorage['flutter.testKey1'] = '"test value"';
html.window.localStorage['flutter.testKey2'] = '42';
html.window.localStorage['unprefixed_key'] = 'not a flutter value';
expect(await store.clear(), isTrue);
expect(html.window.localStorage.keys.single, 'unprefixed_key');
});
});
}