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 Logout', () { late MockAuthRepository mockRepository; late AuthViewModel authViewModel; setUp(() { mockRepository = MockAuthRepository(); authViewModel = AuthViewModel(authRepository: mockRepository); }); tearDown(() { authViewModel.dispose(); }); test('should logout successfully', () async { mockRepository.setLogoutResult( const Result.ok(TestFixtures.logoutResponse), ); // 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.logout(); expect(authViewModel.loggedInUser, null); expect(authViewModel.errorMessage, null); // Cleared after notify }); test('should handle logout error', () async { const errorMessage = 'Logout failed'; mockRepository.setLogoutResult(Result.error(Exception(errorMessage))); await authViewModel.logout(); expect(authViewModel.errorMessage, null); // Cleared after notify }); test('should perform local logout', () async { mockRepository.setLogoutResult( const Result.ok(TestFixtures.logoutResponse), ); // 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.logout(); expect(authViewModel.loggedInUser, null); }); test('should clear error message before logout', () async { mockRepository.setLogoutResult( const Result.ok(TestFixtures.logoutResponse), ); // Set an error first authViewModel.usernameController.text = 'testuser'; authViewModel.passwordController.text = 'wrongpass'; mockRepository.setLoginResult(Result.error(Exception('Login failed'))); await authViewModel.login(); await authViewModel.logout(); expect(authViewModel.errorMessage, null); }); }); }