-
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
414 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import 'package:mockito/mockito.dart'; | ||
|
||
import '../entities/user.dart'; | ||
import '../usecases/user_usercase.dart'; | ||
|
||
class MockUserUseCase extends Mock implements UserUseCase { | ||
@override | ||
Future<String> login(String email, String password) => | ||
super.noSuchMethod(Invocation.method(#login, [email, password]), | ||
returnValue: Future.value(''), | ||
returnValueForMissingStub: Future.value('')); | ||
|
||
|
||
} |
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,163 @@ | ||
import 'package:domain/entities/user.dart'; | ||
import 'package:domain/mocks/auth_usecase_mock.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mockito/mockito.dart'; | ||
import 'package:swiftcomp/presentation/settings/viewModels/forget_password_view_model.dart'; | ||
|
||
void main() { | ||
group('ForgetPasswordViewModel Tests', () | ||
{ | ||
group('toggleNewPasswordVisibility', () { | ||
test('should toggle obscureTextNewPassword and notify listeners', () { | ||
final MockAuthUseCase mockAuthUseCase = MockAuthUseCase(); | ||
final ForgetPasswordViewModel forgetPasswordViewModel = ForgetPasswordViewModel(authUseCase: mockAuthUseCase); | ||
|
||
// Initially true | ||
expect(forgetPasswordViewModel.obscureTextNewPassword, true); | ||
|
||
// Toggle | ||
forgetPasswordViewModel.toggleNewPasswordVisibility(); | ||
expect(forgetPasswordViewModel.obscureTextNewPassword, false); | ||
|
||
// Toggle back | ||
forgetPasswordViewModel.toggleNewPasswordVisibility(); | ||
expect(forgetPasswordViewModel.obscureTextNewPassword, true); | ||
}); | ||
}); | ||
|
||
group('toggleConfirmPasswordVisibility', () { | ||
test('should toggle obscureTextConfirmPassword and notify listeners', () { | ||
final MockAuthUseCase mockAuthUseCase = MockAuthUseCase(); | ||
final ForgetPasswordViewModel forgetPasswordViewModel = ForgetPasswordViewModel(authUseCase: mockAuthUseCase); | ||
|
||
expect(forgetPasswordViewModel.obscureTextConfirmPassword, true); | ||
|
||
// Toggle | ||
forgetPasswordViewModel.toggleConfirmPasswordVisibility(); | ||
expect(forgetPasswordViewModel.obscureTextConfirmPassword, false); | ||
|
||
// Toggle back | ||
forgetPasswordViewModel.toggleConfirmPasswordVisibility(); | ||
expect(forgetPasswordViewModel.obscureTextConfirmPassword, true); | ||
}); | ||
}); | ||
|
||
group('forgetPassword', () { | ||
test('should set isLoading to true and back to false during the process', () async { | ||
final MockAuthUseCase mockAuthUseCase = MockAuthUseCase(); | ||
final ForgetPasswordViewModel forgetPasswordViewModel = ForgetPasswordViewModel(authUseCase: mockAuthUseCase); | ||
|
||
const email = '[email protected]'; | ||
|
||
when(mockAuthUseCase.forgetPassword(email)).thenAnswer((_) async {}); | ||
|
||
final future = forgetPasswordViewModel.forgetPassword(email); | ||
|
||
// Verify isLoading is true during the process | ||
expect(forgetPasswordViewModel.isLoading, true); | ||
|
||
await future; | ||
|
||
// Verify isLoading is false after the process | ||
expect(forgetPasswordViewModel.isLoading, false); | ||
}); | ||
|
||
test('should call forgetPassword with the correct email', () async { | ||
final MockAuthUseCase mockAuthUseCase = MockAuthUseCase(); | ||
final ForgetPasswordViewModel forgetPasswordViewModel = ForgetPasswordViewModel(authUseCase: mockAuthUseCase); | ||
|
||
const email = '[email protected]'; | ||
|
||
when(mockAuthUseCase.forgetPassword(email)).thenAnswer((_) async {}); | ||
|
||
await forgetPasswordViewModel.forgetPassword(email); | ||
|
||
verify(mockAuthUseCase.forgetPassword(email)).called(1); | ||
}); | ||
|
||
test('should set isPasswordResetting to true on successful process', () async { | ||
final MockAuthUseCase mockAuthUseCase = MockAuthUseCase(); | ||
final ForgetPasswordViewModel forgetPasswordViewModel = ForgetPasswordViewModel(authUseCase: mockAuthUseCase); | ||
|
||
const email = '[email protected]'; | ||
|
||
when(mockAuthUseCase.forgetPassword(email)).thenAnswer((_) async {}); | ||
|
||
await forgetPasswordViewModel.forgetPassword(email); | ||
|
||
expect(forgetPasswordViewModel.isPasswordResetting, true); | ||
expect(forgetPasswordViewModel.errorMessage, ''); | ||
}); | ||
|
||
test('should set errorMessage on failure and not set isPasswordResetting', () async { | ||
final MockAuthUseCase mockAuthUseCase = MockAuthUseCase(); | ||
final ForgetPasswordViewModel forgetPasswordViewModel = ForgetPasswordViewModel(authUseCase: mockAuthUseCase); | ||
|
||
const email = '[email protected]'; | ||
|
||
when(mockAuthUseCase.forgetPassword(email)).thenThrow(Exception('Error')); | ||
|
||
await forgetPasswordViewModel.forgetPassword(email); | ||
|
||
expect(forgetPasswordViewModel.isPasswordResetting, false); | ||
expect(forgetPasswordViewModel.errorMessage, 'Failed to send confirmation code.'); | ||
}); | ||
|
||
test('should reset isLoading after failure', () async { | ||
final MockAuthUseCase mockAuthUseCase = MockAuthUseCase(); | ||
final ForgetPasswordViewModel forgetPasswordViewModel = ForgetPasswordViewModel(authUseCase: mockAuthUseCase); | ||
|
||
const email = '[email protected]'; | ||
|
||
when(mockAuthUseCase.forgetPassword(email)).thenThrow(Exception('Error')); | ||
|
||
await forgetPasswordViewModel.forgetPassword(email); | ||
|
||
expect(forgetPasswordViewModel.isLoading, false); | ||
}); | ||
}); | ||
group('resetPassword', () { | ||
test('should call repository with correct parameters', () async { | ||
final MockAuthUseCase mockAuthUseCase = MockAuthUseCase(); | ||
|
||
const email = '[email protected]'; | ||
const newPassword = 'newPassword123'; | ||
const confirmationCode = '123456'; | ||
const expectedResult = 'Password reset successfully'; | ||
|
||
// Mock repository behavior | ||
when(mockAuthUseCase.resetPassword(email, newPassword, confirmationCode)) | ||
.thenAnswer((_) async => expectedResult); | ||
|
||
final result = await mockAuthUseCase.resetPassword(email, newPassword, confirmationCode); | ||
|
||
// Verify the method was called with correct parameters | ||
verify(mockAuthUseCase.resetPassword(email, newPassword, confirmationCode)).called(1); | ||
|
||
// Verify the result matches the expected value | ||
expect(result, expectedResult); | ||
}); | ||
|
||
test('should throw an exception if repository throws an error', () async { | ||
final MockAuthUseCase mockAuthUseCase = MockAuthUseCase(); | ||
|
||
const email = '[email protected]'; | ||
const newPassword = 'newPassword123'; | ||
const confirmationCode = '123456'; | ||
|
||
// Mock repository to throw an exception | ||
when(mockAuthUseCase.resetPassword(email, newPassword, confirmationCode)) | ||
.thenThrow(Exception('Error resetting password')); | ||
|
||
// Verify that the method throws the exception | ||
expect( | ||
() async => await mockAuthUseCase.resetPassword(email, newPassword, confirmationCode), | ||
throwsA(isA<Exception>()), | ||
); | ||
|
||
// Verify the method was called with correct parameters | ||
verify(mockAuthUseCase.resetPassword(email, newPassword, confirmationCode)).called(1); | ||
}); | ||
}); | ||
}); | ||
} |
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,124 @@ | ||
|
||
import 'package:domain/entities/user.dart'; | ||
import 'package:domain/mocks/auth_usecase_mock.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mockito/mockito.dart'; | ||
import 'package:swiftcomp/presentation/settings/viewModels/update_password_view_model.dart'; | ||
|
||
void main() { | ||
group('UpdatePasswordViewModel Tests', () | ||
{ | ||
group('toggleNewPasswordVisibility', () { | ||
test('should toggle obscureTextNewPassword and notify listeners', () { | ||
final MockAuthUseCase mockAuthUseCase = MockAuthUseCase(); | ||
final UpdatePasswordViewModel updatePasswordViewModel = UpdatePasswordViewModel(authUseCase: mockAuthUseCase); | ||
|
||
// Initially true | ||
expect(updatePasswordViewModel.obscureTextNewPassword, true); | ||
|
||
// Toggle | ||
updatePasswordViewModel.toggleNewPasswordVisibility(); | ||
expect(updatePasswordViewModel.obscureTextNewPassword, false); | ||
|
||
// Toggle back | ||
updatePasswordViewModel.toggleNewPasswordVisibility(); | ||
expect(updatePasswordViewModel.obscureTextNewPassword, true); | ||
}); | ||
}); | ||
|
||
group('toggleConfirmPasswordVisibility', () { | ||
test('should toggle obscureTextConfirmPassword and notify listeners', () { | ||
final MockAuthUseCase mockAuthUseCase = MockAuthUseCase(); | ||
final UpdatePasswordViewModel forgetPasswordViewModel = UpdatePasswordViewModel(authUseCase: mockAuthUseCase); | ||
|
||
expect(forgetPasswordViewModel.obscureTextConfirmPassword, true); | ||
|
||
// Toggle | ||
forgetPasswordViewModel.toggleConfirmPasswordVisibility(); | ||
expect(forgetPasswordViewModel.obscureTextConfirmPassword, false); | ||
|
||
// Toggle back | ||
forgetPasswordViewModel.toggleConfirmPasswordVisibility(); | ||
expect(forgetPasswordViewModel.obscureTextConfirmPassword, true); | ||
}); | ||
}); | ||
|
||
group('updatePassword', () { | ||
test('should set isLoading to true and back to false during the process', () async { | ||
final MockAuthUseCase mockAuthUseCase = MockAuthUseCase(); | ||
final UpdatePasswordViewModel forgetPasswordViewModel = UpdatePasswordViewModel(authUseCase: mockAuthUseCase); | ||
|
||
const newPassword = 'newSecurePassword123'; | ||
const successMessage = 'Password updated successfully'; | ||
|
||
when(mockAuthUseCase.updatePassword(newPassword)).thenAnswer((_) async => successMessage); | ||
|
||
final future = forgetPasswordViewModel.updatePassword(newPassword); | ||
|
||
// Verify isLoading is true during the process | ||
expect(forgetPasswordViewModel.isLoading, true); | ||
|
||
await future; | ||
|
||
// Verify isLoading is false after the process | ||
expect(forgetPasswordViewModel.isLoading, false); | ||
}); | ||
|
||
test('should call updatePassword with the correct new password', () async { | ||
final MockAuthUseCase mockAuthUseCase = MockAuthUseCase(); | ||
final UpdatePasswordViewModel forgetPasswordViewModel = UpdatePasswordViewModel(authUseCase: mockAuthUseCase); | ||
|
||
const newPassword = 'newSecurePassword123'; | ||
const successMessage = 'Password updated successfully'; | ||
|
||
when(mockAuthUseCase.updatePassword(newPassword)).thenAnswer((_) async => successMessage); | ||
|
||
await forgetPasswordViewModel.updatePassword(newPassword); | ||
|
||
verify(mockAuthUseCase.updatePassword(newPassword)).called(1); | ||
}); | ||
|
||
test('should return success message on successful process', () async { | ||
final MockAuthUseCase mockAuthUseCase = MockAuthUseCase(); | ||
final UpdatePasswordViewModel forgetPasswordViewModel = UpdatePasswordViewModel(authUseCase: mockAuthUseCase); | ||
|
||
const newPassword = 'newSecurePassword123'; | ||
const successMessage = 'Password updated successfully'; | ||
|
||
when(mockAuthUseCase.updatePassword(newPassword)).thenAnswer((_) async => successMessage); | ||
|
||
final result = await forgetPasswordViewModel.updatePassword(newPassword); | ||
|
||
expect(result, successMessage); | ||
expect(forgetPasswordViewModel.errorMessage, ''); | ||
}); | ||
|
||
test('should set and return errorMessage on failure', () async { | ||
final MockAuthUseCase mockAuthUseCase = MockAuthUseCase(); | ||
final UpdatePasswordViewModel forgetPasswordViewModel = UpdatePasswordViewModel(authUseCase: mockAuthUseCase); | ||
|
||
const newPassword = 'newSecurePassword123'; | ||
|
||
when(mockAuthUseCase.updatePassword(newPassword)).thenThrow(Exception('Error')); | ||
|
||
final result = await forgetPasswordViewModel.updatePassword(newPassword); | ||
|
||
expect(result, 'Failed to update password.'); | ||
expect(forgetPasswordViewModel.errorMessage, 'Failed to update password.'); | ||
}); | ||
|
||
test('should reset isLoading after failure', () async { | ||
final MockAuthUseCase mockAuthUseCase = MockAuthUseCase(); | ||
final UpdatePasswordViewModel forgetPasswordViewModel = UpdatePasswordViewModel(authUseCase: mockAuthUseCase); | ||
|
||
const newPassword = 'newSecurePassword123'; | ||
|
||
when(mockAuthUseCase.updatePassword(newPassword)).thenThrow(Exception('Error')); | ||
|
||
await forgetPasswordViewModel.updatePassword(newPassword); | ||
|
||
expect(forgetPasswordViewModel.isLoading, false); | ||
}); | ||
}); | ||
}); | ||
} |
Oops, something went wrong.