-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
137 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>())); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>>()); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |