a7fcafadda
Renders nothing for the first [delay] (default 200ms) of sustained loading, then renders the [whileDelayed] child until loading completes. Mirrors the web useDelayed hook so a fast network response doesn't flash a skeleton. Used in the next commit's home screen rewrite; otherwise unused this slice. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
77 lines
2.4 KiB
Dart
77 lines
2.4 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
import 'package:minstrel/shared/delayed_loading.dart';
|
|
|
|
void main() {
|
|
testWidgets('renders nothing for the first 100ms of loading', (tester) async {
|
|
await tester.pumpWidget(const MaterialApp(
|
|
home: DelayedLoading(
|
|
isLoading: true,
|
|
delay: Duration(milliseconds: 200),
|
|
whileDelayed: Text('LOADING'),
|
|
whenReady: Text('READY'),
|
|
),
|
|
));
|
|
await tester.pump(const Duration(milliseconds: 100));
|
|
expect(find.text('LOADING'), findsNothing);
|
|
expect(find.text('READY'), findsNothing);
|
|
});
|
|
|
|
testWidgets('renders whileDelayed after delay elapses', (tester) async {
|
|
await tester.pumpWidget(const MaterialApp(
|
|
home: DelayedLoading(
|
|
isLoading: true,
|
|
delay: Duration(milliseconds: 200),
|
|
whileDelayed: Text('LOADING'),
|
|
whenReady: Text('READY'),
|
|
),
|
|
));
|
|
await tester.pump(const Duration(milliseconds: 250));
|
|
expect(find.text('LOADING'), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('renders whenReady when not loading', (tester) async {
|
|
await tester.pumpWidget(const MaterialApp(
|
|
home: DelayedLoading(
|
|
isLoading: false,
|
|
whileDelayed: Text('LOADING'),
|
|
whenReady: Text('READY'),
|
|
),
|
|
));
|
|
expect(find.text('READY'), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('resets timer when isLoading flips false then true', (tester) async {
|
|
await tester.pumpWidget(const MaterialApp(
|
|
home: DelayedLoading(
|
|
isLoading: true,
|
|
delay: Duration(milliseconds: 200),
|
|
whileDelayed: Text('LOADING'),
|
|
whenReady: Text('READY'),
|
|
),
|
|
));
|
|
await tester.pump(const Duration(milliseconds: 150));
|
|
// Switch to not-loading, then back to loading.
|
|
await tester.pumpWidget(const MaterialApp(
|
|
home: DelayedLoading(
|
|
isLoading: false,
|
|
whileDelayed: Text('LOADING'),
|
|
whenReady: Text('READY'),
|
|
),
|
|
));
|
|
await tester.pumpWidget(const MaterialApp(
|
|
home: DelayedLoading(
|
|
isLoading: true,
|
|
delay: Duration(milliseconds: 200),
|
|
whileDelayed: Text('LOADING'),
|
|
whenReady: Text('READY'),
|
|
),
|
|
));
|
|
// Original 150ms shouldn't carry over — at 100ms past restart
|
|
// we should still see nothing.
|
|
await tester.pump(const Duration(milliseconds: 100));
|
|
expect(find.text('LOADING'), findsNothing);
|
|
});
|
|
}
|