129 lines
3.7 KiB
Dart
129 lines
3.7 KiB
Dart
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:tm_app/core/utils/result.dart';
|
|
import 'package:tm_app/view_models/auth_view_model.dart';
|
|
|
|
import '../fixtures/test_fixtures.dart';
|
|
import '../mocks/mock_auth_repository.dart';
|
|
|
|
void main() {
|
|
group('AuthViewModel Utility Methods', () {
|
|
late MockAuthRepository mockRepository;
|
|
late AuthViewModel authViewModel;
|
|
|
|
setUp(() {
|
|
mockRepository = MockAuthRepository();
|
|
authViewModel = AuthViewModel(authRepository: mockRepository);
|
|
});
|
|
|
|
tearDown(() {
|
|
authViewModel.dispose();
|
|
});
|
|
|
|
group('Password Visibility Toggle', () {
|
|
test('should toggle password visibility', () {
|
|
expect(authViewModel.obscurePassword, true);
|
|
|
|
authViewModel.togglePasswordVisibility();
|
|
expect(authViewModel.obscurePassword, false);
|
|
|
|
authViewModel.togglePasswordVisibility();
|
|
expect(authViewModel.obscurePassword, true);
|
|
});
|
|
|
|
test('should toggle forgot password visibility', () {
|
|
expect(authViewModel.obscureForgotPassword, true);
|
|
|
|
authViewModel.toggleForgotPasswordVisibility();
|
|
expect(authViewModel.obscureForgotPassword, false);
|
|
|
|
authViewModel.toggleForgotPasswordVisibility();
|
|
expect(authViewModel.obscureForgotPassword, true);
|
|
});
|
|
});
|
|
|
|
group('Message Clearing', () {
|
|
test('should clear messages', () {
|
|
// Set some messages first through login error
|
|
mockRepository.setLoginResult(Result.error(Exception('Login failed')));
|
|
authViewModel.usernameController.text = 'testuser';
|
|
authViewModel.passwordController.text = 'wrongpass';
|
|
|
|
authViewModel.clearMessages();
|
|
|
|
expect(authViewModel.successMessage, null);
|
|
expect(authViewModel.errorMessage, null);
|
|
});
|
|
});
|
|
|
|
group('Check Login Status', () {
|
|
test('should check if user is logged in - true', () async {
|
|
mockRepository.setCheckIsLoggedInResult(const Result.ok(true));
|
|
|
|
await authViewModel.checkIsLoggedIn();
|
|
|
|
expect(authViewModel.isLoggedIn, true);
|
|
});
|
|
|
|
test('should check if user is logged in - false', () async {
|
|
mockRepository.setCheckIsLoggedInResult(const Result.ok(false));
|
|
|
|
await authViewModel.checkIsLoggedIn();
|
|
|
|
expect(authViewModel.isLoggedIn, false);
|
|
});
|
|
|
|
test('should handle check login error', () async {
|
|
mockRepository.setCheckIsLoggedInResult(
|
|
Result.error(Exception('Check failed')),
|
|
);
|
|
|
|
await authViewModel.checkIsLoggedIn();
|
|
|
|
expect(authViewModel.isLoggedIn, false);
|
|
});
|
|
});
|
|
|
|
group('Local Logout', () {
|
|
test('should perform local logout', () async {
|
|
// Set a logged in user first
|
|
authViewModel.usernameController.text = 'testuser';
|
|
authViewModel.passwordController.text = 'testpass';
|
|
mockRepository.setLoginResult(
|
|
const Result.ok(TestFixtures.loginResponse),
|
|
);
|
|
await authViewModel.login();
|
|
|
|
await authViewModel.localLogout();
|
|
|
|
expect(authViewModel.loggedInUser, null);
|
|
});
|
|
|
|
test('should handle local logout exception', () async {
|
|
mockRepository.setLocalLogoutException(
|
|
Exception('Local logout failed'),
|
|
);
|
|
|
|
// Should not throw exception
|
|
await authViewModel.localLogout();
|
|
|
|
expect(authViewModel.loggedInUser, null);
|
|
});
|
|
});
|
|
|
|
group('Text Change Notification', () {
|
|
test('should notify listeners on text change', () {
|
|
int notificationCount = 0;
|
|
|
|
authViewModel.addListener(() {
|
|
notificationCount++;
|
|
});
|
|
|
|
// Trigger text change
|
|
authViewModel.usernameController.text = 'test';
|
|
|
|
expect(notificationCount, greaterThan(0));
|
|
});
|
|
});
|
|
});
|
|
}
|