Skip to content

Commit

Permalink
[#9] Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nkhanh44 committed Aug 11, 2023
1 parent a386213 commit eedeef0
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 17 deletions.
3 changes: 2 additions & 1 deletion lib/api/authentication_api_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ part 'authentication_api_service.g.dart';

@RestApi()
abstract class AuthenticationApiService {
factory AuthenticationApiService(Dio dio, {String baseUrl}) = _AuthenticationApiService;
factory AuthenticationApiService(Dio dio, {String baseUrl}) =
_AuthenticationApiService;

@POST('/oauth/token')
Future<LoginResponse> login(
Expand Down
2 changes: 2 additions & 0 deletions lib/env.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ class Env {
static String get restApiEndpoint {
return FlutterConfig.get('REST_API_ENDPOINT');
}

static String get clientId {
return FlutterConfig.get('CLIENT_ID');
}

static String get clientSecret {
return FlutterConfig.get('CLIENT_SECRET');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,16 @@ class AuthenticationRepositoryImpl extends AuthenticationRepository {
required String password,
}) async {
try {
final response = await _authenticationApiService.login(
LoginRequest(
email: email,
password: password,
clientId: Env.clientId,
clientSecret: Env.clientSecret,
grantType: grantType,
),
);
final response = await _authenticationApiService.login(LoginRequest(
email: email,
password: password,
clientId: Env.clientId,
clientSecret: Env.clientSecret,
grantType: grantType,
));
return response.toLoginModel();
} catch (exception) {
throw NetworkExceptions.fromDioException(exception);
}
}
}
}
4 changes: 2 additions & 2 deletions lib/usecases/login_use_case.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dart:async';
import 'package:survey_flutter/model/login_model.dart';
import 'package:survey_flutter/repository/authentication_repository.dart';
import 'package:survey_flutter/repositories/authentication_repository.dart';
import 'package:survey_flutter/usecases/base/base_use_case.dart';
import 'package:injectable/injectable.dart';

Expand Down Expand Up @@ -32,4 +32,4 @@ class LoginUseCase extends UseCase<LoginModel, LoginParams> {
return Failed(UseCaseException(exception));
}
}
}
}
57 changes: 57 additions & 0 deletions test/api/repositories/authentication_repository_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import 'package:flutter_config/flutter_config.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:survey_flutter/repositories/authentication_repository.dart';
import 'package:survey_flutter/api/exception/network_exceptions.dart';
import 'package:survey_flutter/model/response/login_response.dart';

import '../../mocks/generate_mocks.mocks.dart';

void main() {
group('AuthenticationRepositoryTest', () {
late MockAuthenticationApiService mockAuthApiService;
late AuthenticationRepositoryImpl authRepository;

const email = "email";
const password = "password";

setUpAll(() {
FlutterConfig.loadValueForTesting({
'CLIENT_ID': 'CLIENT_ID',
'CLIENT_SECRET': 'CLIENT_SECRET',
});
});

setUp(() {
mockAuthApiService = MockAuthenticationApiService();
authRepository = AuthenticationRepositoryImpl(mockAuthApiService);
});

test('When login successfully, it returns correct model', () async {
final loginResponse = LoginResponse(
id: "",
accessToken: "",
tokenType: "",
expiresIn: 0,
refreshToken: "",
createdAt: 0,
);

when(mockAuthApiService.login(any))
.thenAnswer((_) async => loginResponse);

final result =
await authRepository.login(email: email, password: password);

expect(result, loginResponse.toLoginModel());
});

test('When login fail, it returns failed exception', () async {
when(mockAuthApiService.login(any)).thenThrow(Exception());

final result = authRepository.login(email: email, password: password);

expect(result, throwsA(isA<NetworkExceptions>()));
});
});
}
64 changes: 64 additions & 0 deletions test/api/usecases/login_use_case_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:survey_flutter/model/login_model.dart';
import 'package:survey_flutter/usecases/base/base_use_case.dart';
import 'package:survey_flutter/usecases/login_use_case.dart';
import '../../mocks/generate_mocks.mocks.dart';

void main() {
group('LoginUseCaseTest', () {
late MockAuthenticationRepository mockAuthRepository;
late LoginUseCase useCase;

const email = "email";
const password = "password";

setUp(() {
mockAuthRepository = MockAuthenticationRepository();
useCase = LoginUseCase(mockAuthRepository);
});

test('When login success, it returns success model', () async {
const loginModel = LoginModel(
id: "",
accessToken: "",
expiresIn: 0,
refreshToken: "",
);
when(mockAuthRepository.login(
email: email,
password: password,
)).thenAnswer((_) async => loginModel);

final result = await useCase.call(LoginParams(
email: email,
password: password,
));

verify(mockAuthRepository.login(
email: email,
password: password,
));
expect(result, isA<Success<LoginModel>>());
});

test('When login fail, it returns failure with exception', () async {
when(mockAuthRepository.login(
email: email,
password: password,
)).thenAnswer((_) => Future.error(Exception));

final result = await useCase.call(LoginParams(
email: email,
password: password,
));

verify(mockAuthRepository.login(
email: email,
password: password,
));

expect(result, isA<Failed<LoginModel>>());
});
});
}
6 changes: 2 additions & 4 deletions test/mocks/generate_mocks.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import 'package:dio/dio.dart';
import 'package:survey_flutter/api/authentication_api_service.dart';
import 'package:mockito/annotations.dart';
import 'package:survey_flutter/repositories/authentication_repository.dart';

@GenerateMocks([
AuthenticationApiService,
DioError,
])
@GenerateMocks([AuthenticationApiService, AuthenticationRepository, DioError])
main() {
// empty class to generate mock repository classes
}

0 comments on commit eedeef0

Please sign in to comment.