|
| 1 | +import 'dart:convert'; |
| 2 | + |
| 3 | +import 'package:bloc_test/bloc_test.dart'; |
| 4 | +import 'package:dipantau_desktop_client/core/error/failure.dart'; |
| 5 | +import 'package:dipantau_desktop_client/core/usecase/usecase.dart'; |
| 6 | +import 'package:dipantau_desktop_client/feature/data/model/general/general_response.dart'; |
| 7 | +import 'package:dipantau_desktop_client/feature/presentation/bloc/setup_credential/setup_credential_bloc.dart'; |
| 8 | +import 'package:flutter_test/flutter_test.dart'; |
| 9 | +import 'package:mockito/mockito.dart'; |
| 10 | + |
| 11 | +import '../../../../fixture/fixture_reader.dart'; |
| 12 | +import '../../../../helper/mock_helper.mocks.dart'; |
| 13 | + |
| 14 | +void main() { |
| 15 | + late SetupCredentialBloc bloc; |
| 16 | + late MockHelper mockHelper; |
| 17 | + late MockPing mockPing; |
| 18 | + |
| 19 | + const errorMessage = 'testErrorMessage'; |
| 20 | + |
| 21 | + setUp(() { |
| 22 | + mockHelper = MockHelper(); |
| 23 | + mockPing = MockPing(); |
| 24 | + bloc = SetupCredentialBloc( |
| 25 | + helper: mockHelper, |
| 26 | + ping: mockPing, |
| 27 | + ); |
| 28 | + }); |
| 29 | + |
| 30 | + test( |
| 31 | + 'pastikan output dari initial state', |
| 32 | + () async { |
| 33 | + // assert |
| 34 | + expect( |
| 35 | + bloc.state, |
| 36 | + isA<InitialSetupCredentialState>(), |
| 37 | + ); |
| 38 | + }, |
| 39 | + ); |
| 40 | + |
| 41 | + group('ping setup credential', () { |
| 42 | + final params = NoParams(); |
| 43 | + final event = PingSetupCredentialEvent(); |
| 44 | + |
| 45 | + blocTest( |
| 46 | + 'pastikan emit [LoadingSetupCredentialState, SuccessPingSetupCredentialState] ketika terima event ' |
| 47 | + 'PingSetupCredentialEvent dengan proses berhasil', |
| 48 | + build: () { |
| 49 | + final response = GeneralResponse.fromJson( |
| 50 | + json.decode( |
| 51 | + fixture('general_response.json'), |
| 52 | + ), |
| 53 | + ); |
| 54 | + final result = (failure: null, response: response); |
| 55 | + when(mockPing(any)).thenAnswer((_) async => result); |
| 56 | + return bloc; |
| 57 | + }, |
| 58 | + act: (SetupCredentialBloc bloc) { |
| 59 | + return bloc.add(event); |
| 60 | + }, |
| 61 | + expect: () => [ |
| 62 | + isA<LoadingSetupCredentialState>(), |
| 63 | + isA<SuccessPingSetupCredentialState>(), |
| 64 | + ], |
| 65 | + verify: (_) { |
| 66 | + verify(mockPing(params)); |
| 67 | + }, |
| 68 | + ); |
| 69 | + |
| 70 | + blocTest( |
| 71 | + 'pastikan emit [LoadingSetupCredentialState, FailureSetupCredentialState] ketika terima event ' |
| 72 | + 'PingSetupCredentialEvent dengan proses gagal dari endpoint', |
| 73 | + build: () { |
| 74 | + final result = (failure: ServerFailure(errorMessage), response: null); |
| 75 | + when(mockPing(any)).thenAnswer((_) async => result); |
| 76 | + return bloc; |
| 77 | + }, |
| 78 | + act: (SetupCredentialBloc bloc) { |
| 79 | + return bloc.add(event); |
| 80 | + }, |
| 81 | + expect: () => [ |
| 82 | + isA<LoadingSetupCredentialState>(), |
| 83 | + isA<FailureSetupCredentialState>(), |
| 84 | + ], |
| 85 | + verify: (_) { |
| 86 | + verify(mockPing(params)); |
| 87 | + }, |
| 88 | + ); |
| 89 | + |
| 90 | + blocTest( |
| 91 | + 'pastikan emit [LoadingSetupCredentialState, FailureSetupCredentialState] ketika terima event ' |
| 92 | + 'PingSetupCredentialEvent dengan kondisi internet tidak terhubung ketika hit endpoint', |
| 93 | + build: () { |
| 94 | + final result = (failure: ConnectionFailure(), response: null); |
| 95 | + when(mockPing(any)).thenAnswer((_) async => result); |
| 96 | + return bloc; |
| 97 | + }, |
| 98 | + act: (SetupCredentialBloc bloc) { |
| 99 | + return bloc.add(event); |
| 100 | + }, |
| 101 | + expect: () => [ |
| 102 | + isA<LoadingSetupCredentialState>(), |
| 103 | + isA<FailureSetupCredentialState>(), |
| 104 | + ], |
| 105 | + verify: (_) { |
| 106 | + verify(mockPing(params)); |
| 107 | + }, |
| 108 | + ); |
| 109 | + |
| 110 | + blocTest( |
| 111 | + 'pastikan emit [LoadingSetupCredentialState, FailureSetupCredentialState] ketika terima event ' |
| 112 | + 'PingSetupCredentialEvent dengan proses gagal parsing respon JSON endpoint', |
| 113 | + build: () { |
| 114 | + final result = (failure: ParsingFailure(errorMessage), response: null); |
| 115 | + when(mockPing(any)).thenAnswer((_) async => result); |
| 116 | + return bloc; |
| 117 | + }, |
| 118 | + act: (SetupCredentialBloc bloc) { |
| 119 | + return bloc.add(event); |
| 120 | + }, |
| 121 | + expect: () => [ |
| 122 | + isA<LoadingSetupCredentialState>(), |
| 123 | + isA<FailureSetupCredentialState>(), |
| 124 | + ], |
| 125 | + verify: (_) { |
| 126 | + verify(mockPing(params)); |
| 127 | + }, |
| 128 | + ); |
| 129 | + }); |
| 130 | +} |
0 commit comments