87 lines
2.6 KiB
Dart
87 lines
2.6 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 Login', () {
|
|
late MockAuthRepository mockRepository;
|
|
late AuthViewModel authViewModel;
|
|
|
|
setUp(() {
|
|
mockRepository = MockAuthRepository();
|
|
authViewModel = AuthViewModel(authRepository: mockRepository);
|
|
});
|
|
|
|
tearDown(() {
|
|
authViewModel.dispose();
|
|
});
|
|
|
|
test('should login successfully', () async {
|
|
mockRepository.setLoginResult(
|
|
const Result.ok(TestFixtures.loginResponse),
|
|
);
|
|
|
|
authViewModel.usernameController.text = 'testuser';
|
|
authViewModel.passwordController.text = 'testpass';
|
|
|
|
await authViewModel.login();
|
|
|
|
expect(authViewModel.isLoading, false);
|
|
expect(authViewModel.loggedInUser, TestFixtures.customer);
|
|
expect(authViewModel.usernameController.text, isEmpty);
|
|
expect(authViewModel.passwordController.text, isEmpty);
|
|
expect(authViewModel.errorMessage, null);
|
|
});
|
|
|
|
test('should handle login error', () async {
|
|
const errorMessage = 'Invalid credentials';
|
|
mockRepository.setLoginResult(Result.error(Exception(errorMessage)));
|
|
|
|
authViewModel.usernameController.text = 'testuser';
|
|
authViewModel.passwordController.text = 'wrongpass';
|
|
|
|
await authViewModel.login();
|
|
|
|
expect(authViewModel.isLoading, false);
|
|
expect(authViewModel.loggedInUser, null);
|
|
expect(authViewModel.errorMessage, null); // Cleared after notify
|
|
});
|
|
|
|
test('should set loading state during login', () async {
|
|
mockRepository.setLoginResult(
|
|
const Result.ok(TestFixtures.loginResponse),
|
|
);
|
|
|
|
authViewModel.usernameController.text = 'testuser';
|
|
authViewModel.passwordController.text = 'testpass';
|
|
|
|
final loginFuture = authViewModel.login();
|
|
|
|
// Check loading state is set
|
|
expect(authViewModel.isLoading, true);
|
|
expect(authViewModel.errorMessage, null);
|
|
|
|
await loginFuture;
|
|
|
|
expect(authViewModel.isLoading, false);
|
|
});
|
|
|
|
test('should call localLogout before login', () async {
|
|
mockRepository.setLoginResult(
|
|
const Result.ok(TestFixtures.loginResponse),
|
|
);
|
|
|
|
authViewModel.usernameController.text = 'testuser';
|
|
authViewModel.passwordController.text = 'testpass';
|
|
|
|
await authViewModel.login();
|
|
|
|
// Verify that localLogout was called (user is null initially)
|
|
expect(authViewModel.loggedInUser, TestFixtures.customer);
|
|
});
|
|
});
|
|
}
|