|
| 1 | +import 'package:bloc_test/bloc_test.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:flutter_bloc_app_template/features/cores/bloc/cores_bloc.dart'; |
| 4 | +import 'package:flutter_bloc_app_template/features/cores/cores_screen.dart'; |
| 5 | +import 'package:flutter_bloc_app_template/features/cores/widget/core_loading_content.dart'; |
| 6 | +import 'package:flutter_bloc_app_template/features/cores/widget/cores_empty_widget.dart'; |
| 7 | +import 'package:flutter_bloc_app_template/features/cores/widget/cores_error_widget.dart'; |
| 8 | +import 'package:flutter_bloc_app_template/features/cores/widget/cores_not_found_widget.dart'; |
| 9 | +import 'package:flutter_bloc_app_template/models/core/core_resource.dart'; |
| 10 | +import 'package:flutter_test/flutter_test.dart'; |
| 11 | +import 'package:integration_test/integration_test.dart'; |
| 12 | +import 'package:mocktail/mocktail.dart'; |
| 13 | + |
| 14 | +import '../test/bloc/utils.dart'; |
| 15 | + |
| 16 | +class MockCoresBloc extends MockBloc<CoresEvent, CoresState> |
| 17 | + implements CoresBloc {} |
| 18 | + |
| 19 | +void main() { |
| 20 | + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); |
| 21 | + |
| 22 | + late MockCoresBloc mockBloc; |
| 23 | + |
| 24 | + setUp(() { |
| 25 | + mockBloc = MockCoresBloc(); |
| 26 | + }); |
| 27 | + |
| 28 | + testWidgets('renders LoadingContent when state is CoresLoadingState', |
| 29 | + (tester) async { |
| 30 | + when(() => mockBloc.state).thenReturn(const CoresLoadingState()); |
| 31 | + |
| 32 | + await tester.pumpLocalizedWidgetWithBloc<CoresBloc>( |
| 33 | + bloc: mockBloc, |
| 34 | + child: const CustomScrollView( |
| 35 | + slivers: [CoresBlocContent()], |
| 36 | + ), |
| 37 | + locale: const Locale('en'), |
| 38 | + ); |
| 39 | + await tester.pump(); |
| 40 | + |
| 41 | + expect(find.byType(CoreLoadingContent), findsOneWidget); |
| 42 | + }); |
| 43 | + |
| 44 | + testWidgets('renders CoresListWidget when state is CoresSuccessState', |
| 45 | + (tester) async { |
| 46 | + final cores = <CoreResource>[ |
| 47 | + const CoreResource( |
| 48 | + coreSerial: 'B1051', |
| 49 | + missions: [MissionResource(name: null, flight: 1)], |
| 50 | + status: 'active', |
| 51 | + ), |
| 52 | + ]; // provide mock cores |
| 53 | + when(() => mockBloc.state) |
| 54 | + .thenReturn(CoresSuccessState(filteredCores: cores)); |
| 55 | + |
| 56 | + await tester.pumpLocalizedWidgetWithBloc<CoresBloc>( |
| 57 | + bloc: mockBloc, |
| 58 | + child: const CustomScrollView( |
| 59 | + slivers: [CoresBlocContent()], |
| 60 | + ), |
| 61 | + locale: const Locale('en'), |
| 62 | + ); |
| 63 | + await tester.pumpAndSettle(); |
| 64 | + |
| 65 | + expect(find.byType(CoresListWidget), findsOneWidget); |
| 66 | + }); |
| 67 | + |
| 68 | + testWidgets('renders CoresErrorWidget when state is CoresErrorState', |
| 69 | + (tester) async { |
| 70 | + const errorMessage = 'Error occurred'; |
| 71 | + when(() => mockBloc.state).thenReturn(const CoresErrorState(errorMessage)); |
| 72 | + |
| 73 | + await tester.pumpLocalizedWidgetWithBloc<CoresBloc>( |
| 74 | + bloc: mockBloc, |
| 75 | + child: const CustomScrollView( |
| 76 | + slivers: [CoresBlocContent()], |
| 77 | + ), |
| 78 | + locale: const Locale('en'), |
| 79 | + ); |
| 80 | + await tester.pumpAndSettle(); |
| 81 | + |
| 82 | + expect(find.byType(CoresErrorWidget), findsOneWidget); |
| 83 | + //expect(find.text(errorMessage), findsOneWidget); |
| 84 | + }); |
| 85 | + |
| 86 | + testWidgets('renders CoresEmptyWidget when state is CoresEmptyState', |
| 87 | + (tester) async { |
| 88 | + when(() => mockBloc.state).thenReturn(const CoresEmptyState()); |
| 89 | + |
| 90 | + await tester.pumpLocalizedWidgetWithBloc<CoresBloc>( |
| 91 | + bloc: mockBloc, |
| 92 | + child: const CustomScrollView( |
| 93 | + slivers: [CoresBlocContent()], |
| 94 | + ), |
| 95 | + locale: const Locale('en'), |
| 96 | + ); |
| 97 | + await tester.pumpAndSettle(); |
| 98 | + |
| 99 | + expect(find.byType(CoresEmptyWidget), findsOneWidget); |
| 100 | + }); |
| 101 | + |
| 102 | + testWidgets('renders CoresNotFoundWidget when state is CoresNotFoundState', |
| 103 | + (tester) async { |
| 104 | + const query = 'Falcon'; |
| 105 | + when(() => mockBloc.state) |
| 106 | + .thenReturn(const CoresNotFoundState(searchQuery: query)); |
| 107 | + |
| 108 | + await tester.pumpLocalizedWidgetWithBloc<CoresBloc>( |
| 109 | + bloc: mockBloc, |
| 110 | + child: const CustomScrollView( |
| 111 | + slivers: [CoresBlocContent()], |
| 112 | + ), |
| 113 | + locale: const Locale('en'), |
| 114 | + ); |
| 115 | + await tester.pumpAndSettle(); |
| 116 | + |
| 117 | + expect(find.byType(CoresNotFoundWidget), findsOneWidget); |
| 118 | + expect(find.textContaining(query), findsOneWidget); |
| 119 | + }); |
| 120 | +} |
0 commit comments