fix: make ChangeLog screen testable with DefaultAssetBundle (#198)
Switch ChangeLogScreen from rootBundle to DefaultAssetBundle.of(context) so the asset source can be overridden in widget tests. Add test/widget/changelog_screen_test.dart with a happy-path test (asset loads and content is rendered) and an error-path test (missing asset shows the error message). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
f962168c1a
commit
4f9a4e666f
@@ -1,7 +1,6 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart' show rootBundle;
|
||||
import 'package:flutter_markdown_plus/flutter_markdown_plus.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
@@ -13,7 +12,8 @@ class ChangeLogScreen extends StatelessWidget {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('ChangeLog')),
|
||||
body: FutureBuilder<String>(
|
||||
future: rootBundle.loadString('assets/changelog.txt'),
|
||||
future:
|
||||
DefaultAssetBundle.of(context).loadString('assets/changelog.txt'),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:sharedinbox/ui/screens/changelog_screen.dart';
|
||||
|
||||
class _FakeAssetBundle extends Fake implements AssetBundle {
|
||||
final String content;
|
||||
_FakeAssetBundle(this.content);
|
||||
|
||||
@override
|
||||
Future<String> loadString(String key, {bool cache = true}) async {
|
||||
if (key == 'assets/changelog.txt') return content;
|
||||
throw FlutterError('Asset not found: $key');
|
||||
}
|
||||
|
||||
@override
|
||||
Future<ByteData> load(String key) async {
|
||||
throw FlutterError('Asset not found: $key');
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
testWidgets('ChangeLogScreen renders changelog content', (tester) async {
|
||||
const fakeChangelog =
|
||||
'* 2026-01-01: Initial release\n* 2026-01-02: Bug fix';
|
||||
|
||||
await tester.pumpWidget(
|
||||
DefaultAssetBundle(
|
||||
bundle: _FakeAssetBundle(fakeChangelog),
|
||||
child: const MaterialApp(home: ChangeLogScreen()),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('ChangeLog'), findsOneWidget);
|
||||
expect(find.textContaining('2026-01-01'), findsOneWidget);
|
||||
expect(find.textContaining('Initial release'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('ChangeLogScreen shows error when asset is missing', (
|
||||
tester,
|
||||
) async {
|
||||
await tester.pumpWidget(
|
||||
DefaultAssetBundle(
|
||||
bundle: _BadAssetBundle(),
|
||||
child: const MaterialApp(home: ChangeLogScreen()),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.textContaining('Error loading changelog:'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
|
||||
class _BadAssetBundle extends Fake implements AssetBundle {
|
||||
@override
|
||||
Future<String> loadString(String key, {bool cache = true}) async {
|
||||
throw FlutterError('Unable to load asset: "$key"');
|
||||
}
|
||||
|
||||
@override
|
||||
Future<ByteData> load(String key) async {
|
||||
throw FlutterError('Unable to load asset: "$key"');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user