|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_bloc_app_template/features/rockets/bloc/rockets_bloc.dart'; |
| 3 | +import 'package:flutter_bloc_app_template/features/rockets/rockets_screen.dart'; |
| 4 | +import 'package:flutter_bloc_app_template/features/rockets/widget/rocket_item/rocket_item.dart'; |
| 5 | +import 'package:flutter_test/flutter_test.dart'; |
| 6 | +import 'package:integration_test/integration_test.dart'; |
| 7 | +import 'package:mocktail/mocktail.dart'; |
| 8 | + |
| 9 | +import '../test/bloc/utils.dart'; |
| 10 | +import '../test/features/rockets/rockets_screen_test.dart'; |
| 11 | +import '../test/repository/rocket_repository_test.dart'; |
| 12 | + |
| 13 | +void main() { |
| 14 | + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); |
| 15 | + |
| 16 | + late RocketsBloc rocketsBloc; |
| 17 | + |
| 18 | + setUp(() { |
| 19 | + rocketsBloc = MockRocketsBloc(); |
| 20 | + }); |
| 21 | + |
| 22 | + group('Rockets Screen Tests', () { |
| 23 | + testWidgets( |
| 24 | + 'renders CircularProgressIndicator ' |
| 25 | + 'when rocket list state is initial', (tester) async { |
| 26 | + when(() => rocketsBloc.state).thenReturn(const RocketsLoadingState()); |
| 27 | + |
| 28 | + await tester.pumpLocalizedWidgetWithBloc<RocketsBloc>( |
| 29 | + bloc: rocketsBloc, |
| 30 | + child: const RocketsBlocContent(), |
| 31 | + locale: const Locale('en'), |
| 32 | + ); |
| 33 | + |
| 34 | + await tester.pump(); |
| 35 | + |
| 36 | + expect(find.byType(CircularProgressIndicator), findsOneWidget); |
| 37 | + }); |
| 38 | + |
| 39 | + testWidgets( |
| 40 | + 'renders Empty list text ' |
| 41 | + 'when rocket list state is success but with 0 items', (tester) async { |
| 42 | + when(() => rocketsBloc.state).thenReturn(const RocketsEmptyState()); |
| 43 | + await tester.pumpLocalizedWidgetWithBloc<RocketsBloc>( |
| 44 | + bloc: rocketsBloc, |
| 45 | + child: const RocketsBlocContent(), |
| 46 | + locale: const Locale('en'), |
| 47 | + ); |
| 48 | + await tester.pumpAndSettle(); |
| 49 | + |
| 50 | + expect(find.text('Empty list'), findsOneWidget); |
| 51 | + }); |
| 52 | + |
| 53 | + testWidgets('renders 1 item', (tester) async { |
| 54 | + when(() => rocketsBloc.state).thenReturn( |
| 55 | + RocketsSuccessState( |
| 56 | + rockets: [ |
| 57 | + getRocketResource(), |
| 58 | + ], |
| 59 | + ), |
| 60 | + ); |
| 61 | + await tester.pumpLocalizedWidgetWithBloc<RocketsBloc>( |
| 62 | + bloc: rocketsBloc, |
| 63 | + child: const RocketsBlocContent(), |
| 64 | + locale: const Locale('en'), |
| 65 | + ); |
| 66 | + await tester.pumpAndSettle(); |
| 67 | + |
| 68 | + expect(find.byType(RocketItemWidget), findsNWidgets(1)); |
| 69 | + }); |
| 70 | + |
| 71 | + testWidgets('renders error text', (tester) async { |
| 72 | + when(() => rocketsBloc.state).thenReturn(const RocketsErrorState()); |
| 73 | + await tester.pumpLocalizedWidgetWithBloc<RocketsBloc>( |
| 74 | + bloc: rocketsBloc, |
| 75 | + child: const RocketsBlocContent(), |
| 76 | + locale: const Locale('en'), |
| 77 | + ); |
| 78 | + await tester.pumpAndSettle(); |
| 79 | + |
| 80 | + expect(find.text('Try Again'), findsOneWidget); |
| 81 | + }); |
| 82 | + }); |
| 83 | +} |
0 commit comments