69 lines
2.4 KiB
Dart
69 lines
2.4 KiB
Dart
|
|
import 'package:bible_tracker/services/last_location.dart';
|
||
|
|
import 'package:flutter_test/flutter_test.dart';
|
||
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
||
|
|
|
||
|
|
void main() {
|
||
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||
|
|
|
||
|
|
group('LastLocation.tryParseRoute', () {
|
||
|
|
test('parses tracker-only route', () {
|
||
|
|
final parsed = LastLocation.tryParseRoute('/tracker/abc');
|
||
|
|
expect(parsed, const LastLocation(trackerId: 'abc'));
|
||
|
|
});
|
||
|
|
|
||
|
|
test('parses tracker + book route', () {
|
||
|
|
final parsed = LastLocation.tryParseRoute('/tracker/abc/book/12');
|
||
|
|
expect(parsed, const LastLocation(trackerId: 'abc', bookIndex: 12));
|
||
|
|
});
|
||
|
|
|
||
|
|
test('returns null for unrelated routes', () {
|
||
|
|
expect(LastLocation.tryParseRoute(null), isNull);
|
||
|
|
expect(LastLocation.tryParseRoute('/'), isNull);
|
||
|
|
expect(LastLocation.tryParseRoute('/settings'), isNull);
|
||
|
|
expect(LastLocation.tryParseRoute('/tracker'), isNull);
|
||
|
|
expect(
|
||
|
|
LastLocation.tryParseRoute('/tracker/abc/book/not-a-number'),
|
||
|
|
isNull,
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('toRouteName round-trips', () {
|
||
|
|
const a = LastLocation(trackerId: 'abc');
|
||
|
|
const b = LastLocation(trackerId: 'abc', bookIndex: 0);
|
||
|
|
expect(LastLocation.tryParseRoute(a.toRouteName()), a);
|
||
|
|
expect(LastLocation.tryParseRoute(b.toRouteName()), b);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
group('LastLocationService', () {
|
||
|
|
test('returns null when nothing is persisted', () async {
|
||
|
|
SharedPreferences.setMockInitialValues(<String, Object>{});
|
||
|
|
final prefs = await SharedPreferences.getInstance();
|
||
|
|
final service = LastLocationService(prefs);
|
||
|
|
expect(service.load(), isNull);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('round-trips a location through SharedPreferences', () async {
|
||
|
|
SharedPreferences.setMockInitialValues(<String, Object>{});
|
||
|
|
final prefs = await SharedPreferences.getInstance();
|
||
|
|
final service = LastLocationService(prefs);
|
||
|
|
|
||
|
|
const location = LastLocation(trackerId: 't1', bookIndex: 7);
|
||
|
|
await service.save(location);
|
||
|
|
expect(service.load(), location);
|
||
|
|
|
||
|
|
await service.save(null);
|
||
|
|
expect(service.load(), isNull);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('load returns null when stored JSON is corrupt', () async {
|
||
|
|
SharedPreferences.setMockInitialValues(<String, Object>{
|
||
|
|
'bible_tracker.last_location.v1': 'not-json',
|
||
|
|
});
|
||
|
|
final prefs = await SharedPreferences.getInstance();
|
||
|
|
final service = LastLocationService(prefs);
|
||
|
|
expect(service.load(), isNull);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
}
|