|
| 1 | +import 'package:flutter_bloc_app_template/data/network/api_result.dart'; |
| 2 | +import 'package:flutter_bloc_app_template/data/network/data_source/launches_network_data_source.dart'; |
| 3 | +import 'package:flutter_bloc_app_template/data/network/model/launch/full/network_launch_full_model.dart'; |
| 4 | +import 'package:flutter_bloc_app_template/data/network/model/launch/network_launch_model.dart'; |
| 5 | +import 'package:flutter_bloc_app_template/models/launch/launch_full_resource.dart'; |
| 6 | +import 'package:flutter_bloc_app_template/models/launch/launch_resource.dart'; |
| 7 | +import 'package:flutter_bloc_app_template/repository/launches_repository.dart'; |
| 8 | +import 'package:flutter_test/flutter_test.dart'; |
| 9 | +import 'package:mocktail/mocktail.dart'; |
| 10 | + |
| 11 | +class MockLaunchesDataSource extends Mock implements LaunchesDataSource {} |
| 12 | + |
| 13 | +void main() { |
| 14 | + late LaunchesRepositoryImpl repository; |
| 15 | + late MockLaunchesDataSource mockDataSource; |
| 16 | + |
| 17 | + setUp(() { |
| 18 | + mockDataSource = MockLaunchesDataSource(); |
| 19 | + repository = LaunchesRepositoryImpl(mockDataSource); |
| 20 | + }); |
| 21 | + |
| 22 | + group('LaunchesRepositoryImpl', () { |
| 23 | + group('getLaunches', () { |
| 24 | + test('returns list of LaunchResource when success', () async { |
| 25 | + final networkLaunch = |
| 26 | + const NetworkLaunchModel(flightNumber: 1, missionName: 'Test'); |
| 27 | + when(() => mockDataSource.getLaunches( |
| 28 | + hasId: any(named: 'hasId'), |
| 29 | + limit: any(named: 'limit'), |
| 30 | + offset: any(named: 'offset'), |
| 31 | + launchYear: any(named: 'launchYear'), |
| 32 | + launchSuccess: any(named: 'launchSuccess'), |
| 33 | + order: any(named: 'order'), |
| 34 | + )).thenAnswer( |
| 35 | + (_) async => ApiResult.success([networkLaunch]), |
| 36 | + ); |
| 37 | + |
| 38 | + final result = await repository.getLaunches(); |
| 39 | + |
| 40 | + expect(result, isA<List<LaunchResource>>()); |
| 41 | + expect(result.first.missionName, equals('Test')); |
| 42 | + }); |
| 43 | + |
| 44 | + test('throws Exception when error', () async { |
| 45 | + when(() => mockDataSource.getLaunches( |
| 46 | + hasId: any(named: 'hasId'), |
| 47 | + limit: any(named: 'limit'), |
| 48 | + offset: any(named: 'offset'), |
| 49 | + launchYear: any(named: 'launchYear'), |
| 50 | + launchSuccess: any(named: 'launchSuccess'), |
| 51 | + order: any(named: 'order'), |
| 52 | + )).thenAnswer( |
| 53 | + (_) async => const ApiResult.error('Something went wrong'), |
| 54 | + ); |
| 55 | + |
| 56 | + expect( |
| 57 | + () => repository.getLaunches(), |
| 58 | + throwsA(isA<Exception>()), |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + test('throws Exception when loading', () async { |
| 63 | + when(() => mockDataSource.getLaunches( |
| 64 | + hasId: any(named: 'hasId'), |
| 65 | + limit: any(named: 'limit'), |
| 66 | + offset: any(named: 'offset'), |
| 67 | + launchYear: any(named: 'launchYear'), |
| 68 | + launchSuccess: any(named: 'launchSuccess'), |
| 69 | + order: any(named: 'order'), |
| 70 | + )).thenAnswer( |
| 71 | + (_) async => const ApiResult.loading(), |
| 72 | + ); |
| 73 | + |
| 74 | + expect( |
| 75 | + () => repository.getLaunches(), |
| 76 | + throwsA(isA<Exception>()), |
| 77 | + ); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + group('getLaunch', () { |
| 82 | + test('returns LaunchFullResource when success', () async { |
| 83 | + final networkFull = const NetworkLaunchFullModel( |
| 84 | + flightNumber: 42, missionName: 'Falcon'); |
| 85 | + when(() => mockDataSource.getLaunch(42)).thenAnswer( |
| 86 | + (_) async => ApiResult.success(networkFull), |
| 87 | + ); |
| 88 | + |
| 89 | + final result = await repository.getLaunch(42); |
| 90 | + |
| 91 | + expect(result, isA<LaunchFullResource>()); |
| 92 | + expect(result.missionName, equals('Falcon')); |
| 93 | + }); |
| 94 | + |
| 95 | + test('throws Exception when error', () async { |
| 96 | + when(() => mockDataSource.getLaunch(42)).thenAnswer( |
| 97 | + (_) async => const ApiResult.error('Not found'), |
| 98 | + ); |
| 99 | + |
| 100 | + expect( |
| 101 | + () => repository.getLaunch(42), |
| 102 | + throwsA(isA<Exception>()), |
| 103 | + ); |
| 104 | + }); |
| 105 | + |
| 106 | + test('throws Exception when loading', () async { |
| 107 | + when(() => mockDataSource.getLaunch(42)).thenAnswer( |
| 108 | + (_) async => const ApiResult.loading(), |
| 109 | + ); |
| 110 | + |
| 111 | + expect( |
| 112 | + () => repository.getLaunch(42), |
| 113 | + throwsA(isA<Exception>()), |
| 114 | + ); |
| 115 | + }); |
| 116 | + }); |
| 117 | + }); |
| 118 | +} |
0 commit comments