import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:fruitshorts/core/widgets/centered_toast.dart'; void main() { testWidgets('should_replace_previous_toast_when_shown_again', (tester) async { late BuildContext hostContext; await tester.pumpWidget( MaterialApp( home: Builder( builder: (context) { hostContext = context; return const Scaffold(body: SizedBox.expand()); }, ), ), ); showCenteredToast(hostContext, 'first message'); await tester.pump(); showCenteredToast(hostContext, 'second message'); await tester.pump(); expect(find.byKey(const Key('centered-toast')), findsOneWidget); expect(find.text('second message'), findsOneWidget); expect(find.text('first message'), findsNothing); await tester.pump(const Duration(seconds: 2)); await tester.pump(const Duration(milliseconds: 200)); expect(find.byKey(const Key('centered-toast')), findsNothing); }); testWidgets('should_allow_underlying_gesture_when_toast_is_visible', ( tester, ) async { late BuildContext hostContext; var taps = 0; await tester.pumpWidget( MaterialApp( home: Builder( builder: (context) { hostContext = context; return Scaffold( body: GestureDetector( key: const Key('toast-host'), behavior: HitTestBehavior.opaque, onTap: () => taps++, child: const SizedBox.expand(), ), ); }, ), ), ); showCenteredToast(hostContext, 'tap-through'); await tester.pump(); await tester.tapAt( tester.getCenter(find.byKey(const Key('centered-toast'))), ); expect(taps, 1); await tester.pump(const Duration(seconds: 2)); await tester.pump(const Duration(milliseconds: 200)); }); testWidgets('should_replace_toast_when_previous_toast_is_fading_out', ( tester, ) async { late BuildContext hostContext; await tester.pumpWidget( MaterialApp( home: Builder( builder: (context) { hostContext = context; return const Scaffold(body: SizedBox.expand()); }, ), ), ); showCenteredToast(hostContext, 'first message'); await tester.pump(); await tester.pump(const Duration(seconds: 2)); showCenteredToast(hostContext, 'replacement message'); await tester.pump(); expect(tester.takeException(), isNull); expect(find.text('replacement message'), findsOneWidget); await tester.pump(const Duration(seconds: 3)); await tester.pumpAndSettle(); }); }