Files
finlog/finlog_app/fluttery/test/preferences/preferences_test.dart

178 lines
5.8 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
import 'package:fluttery/src/preferences/preferences_impl.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() {
late PreferencesImpl preferences;
setUp(() async {
// Clear any existing data and set up a fresh in-memory instance
SharedPreferences.setMockInitialValues({});
// Create preferences instance that will use the real SharedPreferences
// but with in-memory storage for testing
final sharedInstance = await SharedPreferences.getInstance();
preferences = PreferencesImpl(instance: sharedInstance);
// Give time for initialization
await Future.delayed(Duration.zero);
});
group('PreferencesImpl Tests', () {
test('setString and getString work with real implementation', () async {
const key = 'testKey';
const value = 'testValue';
await preferences.setString(key, value);
final result = await preferences.getString(key);
expect(result, value);
});
test('setInt and getInt work with real implementation', () async {
const key = 'testKey';
const value = 42;
await preferences.setInt(key, value);
final result = await preferences.getInt(key);
expect(result, value);
});
test('setBool and getBool work with real implementation', () async {
const key = 'testKey';
const value = true;
await preferences.setBool(key, value);
final result = await preferences.getBool(key);
expect(result, value);
});
test('setDouble and getDouble work with real implementation', () async {
const key = 'testKey';
const value = 3.14;
await preferences.setDouble(key, value);
final result = await preferences.getDouble(key);
expect(result, value);
});
test(
'setStringList and getStringList work with real implementation',
() async {
const key = 'testKey';
const value = ['one', 'two', 'three'];
await preferences.setStringList(key, value);
final result = await preferences.getStringList(key);
expect(result, value);
},
);
test('remove deletes key-value pair', () async {
const key = 'testKey';
const value = 'testValue';
// Set a value first
await preferences.setString(key, value);
expect(await preferences.getString(key), value);
// Remove it
await preferences.remove(key);
final result = await preferences.getString(key);
expect(result, isNull);
});
test('clear removes all data', () async {
// Set multiple values
await preferences.setString('key1', 'value1');
await preferences.setInt('key2', 42);
await preferences.setBool('key3', true);
// Verify they exist
expect(await preferences.getString('key1'), 'value1');
expect(await preferences.getInt('key2'), 42);
expect(await preferences.getBool('key3'), true);
// Clear all
await preferences.clear();
// Verify they're gone
expect(await preferences.getString('key1'), isNull);
expect(await preferences.getInt('key2'), isNull);
expect(await preferences.getBool('key3'), isNull);
});
test('getting non-existent keys returns null', () async {
expect(await preferences.getString('nonExistent'), isNull);
expect(await preferences.getInt('nonExistent'), isNull);
expect(await preferences.getBool('nonExistent'), isNull);
expect(await preferences.getDouble('nonExistent'), isNull);
expect(await preferences.getStringList('nonExistent'), isNull);
});
test('can overwrite existing values of the same type', () async {
const key = 'testKey';
const initialValue = 'initialValue';
const newValue = 'newValue';
await preferences.setString(key, initialValue);
expect(await preferences.getString(key), initialValue);
await preferences.setString(key, newValue);
expect(await preferences.getString(key), newValue);
});
test(
'different keys can store different data types simultaneously',
() async {
await preferences.setString('stringKey', 'value');
await preferences.setInt('intKey', 42);
await preferences.setBool('boolKey', true);
await preferences.setDouble('doubleKey', 3.14);
await preferences.setStringList('listKey', ['a', 'b', 'c']);
expect(await preferences.getString('stringKey'), 'value');
expect(await preferences.getInt('intKey'), 42);
expect(await preferences.getBool('boolKey'), true);
expect(await preferences.getDouble('doubleKey'), 3.14);
expect(await preferences.getStringList('listKey'), ['a', 'b', 'c']);
},
);
test('values can be overwritten with different data types', () async {
const key = 'testKey';
// Store a string value
await preferences.setString(key, 'stringValue');
expect(await preferences.getString(key), 'stringValue');
// Overwrite with an int - this replaces the string value
await preferences.setInt(key, 42);
expect(await preferences.getInt(key), 42);
// Overwrite with a bool - this replaces the int value
await preferences.setBool(key, true);
expect(await preferences.getBool(key), true);
});
test('persistence works across multiple operations', () async {
// Test that values persist through multiple set/get operations
await preferences.setString('key1', 'value1');
await preferences.setInt('key2', 100);
expect(await preferences.getString('key1'), 'value1');
expect(await preferences.getInt('key2'), 100);
// Modify one value and ensure the other remains
await preferences.setString('key1', 'newValue1');
expect(await preferences.getString('key1'), 'newValue1');
expect(await preferences.getInt('key2'), 100);
});
});
}