Skip to content

Commit

Permalink
finished DI
Browse files Browse the repository at this point in the history
  • Loading branch information
Mo0Khaled committed Feb 27, 2022
1 parent e371c48 commit 3c959c3
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:clean_arc/core/exceptions/failuers.dart';
import 'package:clean_arc/core/usecases/usecase.dart';
import 'package:clean_arc/core/utils/input_converter.dart';
import 'package:clean_arc/features/number_trivia/domain/entities/number_trivia.dart';
import 'package:clean_arc/features/number_trivia/domain/usecases/get_converete_number_trivia_usecase.dart';
Expand Down Expand Up @@ -48,7 +49,13 @@ class NumberTriviaBloc extends Bloc<NumberTriviaEvent, NumberTriviaState> {
);
});

on<GetTriviaForRandomNumber>((event, emit) {});
on<GetTriviaForRandomNumber>(
(event, emit) async {
emit(NumberTriviaLoading());
final failureOrTrivia = await getRandomNumberTrivia(NoParams());
_eitherLoadedOrErrorState(emit, failureOrTrivia);
},
);
}

void _eitherLoadedOrErrorState(Emitter<NumberTriviaState> emit,
Expand Down
48 changes: 48 additions & 0 deletions lib/injection_container.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import 'package:clean_arc/core/platform/networrk_info.dart';
import 'package:clean_arc/core/utils/input_converter.dart';
import 'package:clean_arc/features/number_trivia/data/datasources/number_trivia_locale_data_source.dart';
import 'package:clean_arc/features/number_trivia/data/repositories/number_trivia_repository_impl.dart';
import 'package:clean_arc/features/number_trivia/domain/usecases/get_converete_number_trivia_usecase.dart';
import 'package:clean_arc/features/number_trivia/domain/usecases/get_random_number_trivia_usecase.dart';
import 'package:clean_arc/features/number_trivia/presentation/bloc/number_trivia_bloc.dart';
import 'package:data_connection_checker_tv/data_connection_checker.dart';
import 'package:get_it/get_it.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'features/number_trivia/data/datasources/number_trivia_remote_data_source.dart';
import 'features/number_trivia/domain/repositories/number_trivia_repository.dart';
import 'package:http/http.dart' as http;
final sl = GetIt.instance;

Future<void> init() async {
//! Features - Number Trivia
sl.registerFactory(
() => NumberTriviaBloc(
getConcreteNumberTrivia: sl(),
getRandomNumberTrivia: sl(),
inputConverter: sl(),
),
);
//! Use Cases
sl.registerLazySingleton(() => GetConcreteNumberTriviaUseCase(sl()));
sl.registerLazySingleton(() => GetRandomNumberTriviaUseCase(sl()));
//! Repository
sl.registerLazySingleton<NumberTriviaRepository>(
() => NumberTriviaRepositoryImpl(
remoteDataSource: sl(),
localeDataSource: sl(),
networkInfo: sl(),
),
);
//! DataSources
sl.registerLazySingleton<NumberTriviaLocaleDataSource>(() => NumberTriviaLocaleDataSourceImpl(sharedPreferences: sl()));
sl.registerLazySingleton<NumberTriviaRemoteDataSource>(() => NumberTriviaRemoteDataSourceImpl(httpClient: sl()));
//! Core
sl.registerLazySingleton(() => InputConverter());
sl.registerLazySingleton<NetworkInfo>(() => NetworkInfoImpl(sl()));
//! External
final sharedPreferences = await SharedPreferences.getInstance();
sl.registerLazySingleton<SharedPreferences>(() => sharedPreferences);
sl.registerLazySingleton<http.Client>(() => http.Client());
sl.registerLazySingleton<DataConnectionChecker>(() => DataConnectionChecker());
}
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dependencies:
flutter:
sdk: flutter

cupertino_icons: ^1.0.2
cupertino_icons: ^1.0.4
get_it: ^7.2.0
# Bloc for state management
flutter_bloc: ^8.0.1
Expand All @@ -32,7 +32,7 @@ dev_dependencies:
sdk: flutter
mockito: ^5.1.0
build_runner: ^2.1.7
flutter_lints: ^1.0.0
flutter_lints: ^1.0.4

flutter:

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import 'package:clean_arc/core/exceptions/exception.dart';
import 'package:clean_arc/core/exceptions/failuers.dart';
import 'package:clean_arc/core/usecases/usecase.dart';
import 'package:clean_arc/core/utils/input_converter.dart';
import 'package:clean_arc/features/number_trivia/domain/entities/number_trivia.dart';
import 'package:clean_arc/features/number_trivia/domain/usecases/get_converete_number_trivia_usecase.dart';
Expand Down Expand Up @@ -46,16 +49,17 @@ void main() {
.thenReturn(const Right(tNumberParsed));
test(
'should call the input converter to validate and convert a string to unsigned int',
() async {
// arrange
setUpMockInputConverterSuccess();
when(mockGetConcreteNumberTrivia(any)).thenAnswer((_) async => const Right(tTrivia));
// act
bloc.add(const GetTriviaForConcreteNumber(tNumberString));
await untilCalled(mockInputConverter.stringToUnsignedInt(any));
// assert
verify(mockInputConverter.stringToUnsignedInt(tNumberString));
});
() async {
// arrange
setUpMockInputConverterSuccess();
when(mockGetConcreteNumberTrivia(any))
.thenAnswer((_) async => const Right(tTrivia));
// act
bloc.add(const GetTriviaForConcreteNumber(tNumberString));
await untilCalled(mockInputConverter.stringToUnsignedInt(any));
// assert
verify(mockInputConverter.stringToUnsignedInt(tNumberString));
});

test('should emit error state when the input is invalid', () async {
// arrange
Expand All @@ -79,10 +83,127 @@ void main() {
when(mockGetConcreteNumberTrivia(any))
.thenAnswer((realInvocation) async => const Right(tTrivia));
// act
bloc.add(GetTriviaForConcreteNumber(tNumberString));
bloc.add(const GetTriviaForConcreteNumber(tNumberString));
await untilCalled(mockGetConcreteNumberTrivia(any));
// assert
verify(mockGetConcreteNumberTrivia(const Params(number: tNumberParsed)));
});

test('should emit [Loading,loaded] when data is gotten successfully',
() async {
// arrange
setUpMockInputConverterSuccess();
when(mockGetConcreteNumberTrivia(any))
.thenAnswer((realInvocation) async => const Right(tTrivia));
// assert
final expected = [
NumberTriviaLoading(),
const NumberTriviaLoaded(trivia: tTrivia),
];
expectLater(bloc.stream, emitsInOrder(expected));
// act
bloc.add(const GetTriviaForConcreteNumber(tNumberString));
});

test('should emit [Loading,failure] when gotten data fails',
() async {
// arrange
setUpMockInputConverterSuccess();
when(mockGetConcreteNumberTrivia(any))
.thenAnswer((_) async => Left(ServerFailure()));
// assert
final expected = [
NumberTriviaLoading(),
const NumberTriviaFailure(errorMessage:SERVER_FAILURE_MESSAGE ),
];
expectLater(bloc.stream, emitsInOrder(expected));
// act
bloc.add(const GetTriviaForConcreteNumber(tNumberString));
});

test('should emit [Loading,failure] with a proper message for the error when getting data fails',
() async {
// arrange
setUpMockInputConverterSuccess();
when(mockGetConcreteNumberTrivia(any))
.thenAnswer((_) async => Left(CacheFailure()));
// assert
final expected = [
NumberTriviaLoading(),
const NumberTriviaFailure(errorMessage:CACHE_FAILURE_MESSAGE ),
];
expectLater(bloc.stream, emitsInOrder(expected));
// act
bloc.add(const GetTriviaForConcreteNumber(tNumberString));
});
});

group('GetTriviaForRandomNumber', () {
const tNumberTrivia = NumberTrivia(text: 'text', number: 1);

test(
'should get data from the random use case',
() async {
// arrange
when(mockGetRandomNumberTrivia(any))
.thenAnswer((_) async => const Right(tNumberTrivia));
// act
bloc.add(GetTriviaForRandomNumber());
await untilCalled(mockGetRandomNumberTrivia(any));
// assert
verify(mockGetRandomNumberTrivia(NoParams()));
},
);

test(
'should emit [Loading, Loaded] when data is gotten successfully',
() async {
// arrange
when(mockGetRandomNumberTrivia(any))
.thenAnswer((_) async => const Right(tNumberTrivia));
// assert late
final expected = [
NumberTriviaLoading(),
const NumberTriviaLoaded(trivia: tNumberTrivia),
];
expectLater(bloc.stream, emitsInOrder(expected));
// act
bloc.add(GetTriviaForRandomNumber());
},
);

test(
'should emit [Loading, Error] when data is gotten unsuccessfully',
() async {
// arrange
when(mockGetRandomNumberTrivia(any))
.thenAnswer((_) async => Left(ServerFailure()));
// assert late
final expected = [
NumberTriviaLoading(),
const NumberTriviaFailure(errorMessage: SERVER_FAILURE_MESSAGE),
];
expectLater(bloc.stream, emitsInOrder(expected));
// act
bloc.add(GetTriviaForRandomNumber());
},
);

test(
'should emit [Loading, Error] with a proper message for the error when getting data fails',
() async {
// arrange
when(mockGetRandomNumberTrivia(any))
.thenAnswer((_) async => Left(CacheFailure()));
// assert late
final expected = [
NumberTriviaLoading(),
const NumberTriviaFailure(errorMessage: CACHE_FAILURE_MESSAGE),
];
expectLater(bloc.stream, emitsInOrder(expected));
// act
bloc.add(GetTriviaForRandomNumber());
},
);
});
}
}

0 comments on commit 3c959c3

Please sign in to comment.