forked from eatvio/eatvio_app
154 lines
5.8 KiB
Dart
154 lines
5.8 KiB
Dart
import 'package:eatvio_app/features/auth/models/token_model.dart';
|
||
import 'package:eatvio_app/features/auth/presentation/bloc/auth_cubit/auth_cubit.dart';
|
||
import 'package:eatvio_app/features/auth/presentation/bloc/auth_cubit/auth_state.dart';
|
||
import 'package:flutter/services.dart';
|
||
import 'package:flutter_test/flutter_test.dart';
|
||
import 'package:shared_preferences/shared_preferences.dart';
|
||
|
||
import '../../../../fixtures/doubles/auth_doubles.dart';
|
||
import '../../../../fixtures/fixture_reader.dart';
|
||
|
||
void main() {
|
||
TestWidgetsFlutterBinding.ensureInitialized();
|
||
|
||
group('AuthCubit', () {
|
||
AuthCubit? authCubit;
|
||
late FixtureAuthRepository authRepository;
|
||
late Map<String, dynamic> credentialsFixture;
|
||
|
||
late TokenModel tokenFixture;
|
||
|
||
setUp(() async {
|
||
tokenFixture = TokenModel.fromJson(FixtureReader.readAsMap('auth/token_response.json'));
|
||
credentialsFixture = FixtureReader.readAsMap('auth/login_credentials.json');
|
||
authRepository = FixtureAuthRepository(tokenFixture: tokenFixture);
|
||
SharedPreferences.setMockInitialValues({});
|
||
|
||
// Mock Fluttertoast plugin
|
||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||
.setMockMethodCallHandler(
|
||
const MethodChannel('PonnamKarthik/fluttertoast'),
|
||
(MethodCall methodCall) async {
|
||
return null;
|
||
},
|
||
);
|
||
});
|
||
|
||
tearDown(() async {
|
||
if (authCubit != null && !authCubit!.isClosed) {
|
||
await Future.delayed(const Duration(milliseconds: 100));
|
||
authCubit!.close();
|
||
authCubit = null;
|
||
}
|
||
|
||
// Clear mock handlers
|
||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||
.setMockMethodCallHandler(
|
||
const MethodChannel('PonnamKarthik/fluttertoast'),
|
||
null,
|
||
);
|
||
});
|
||
|
||
test('начальное состояние - AuthState', () {
|
||
authCubit = AuthCubit(authRepository: authRepository);
|
||
expect(authCubit!.state, const AuthState());
|
||
});
|
||
|
||
test('getTokenByCredentials - успешная авторизация', () async {
|
||
authCubit = AuthCubit(authRepository: authRepository);
|
||
|
||
final result = await authCubit!.getTokenByCredentials(
|
||
credentialsFixture['email'] as String,
|
||
credentialsFixture['password'] as String,
|
||
);
|
||
|
||
expect(result, true);
|
||
await Future.delayed(const Duration(milliseconds: 100));
|
||
expect(authCubit!.state, isA<AuthStateLoaded>());
|
||
final loadedState = authCubit!.state as AuthStateLoaded;
|
||
expect(loadedState.token, tokenFixture.token);
|
||
expect(
|
||
authRepository.credentialPayloads.single,
|
||
credentialsFixture.map((key, value) => MapEntry(key, value.toString())),
|
||
);
|
||
});
|
||
|
||
test('getTokenByCredentials - переход в состояние loading', () async {
|
||
authCubit = AuthCubit(authRepository: authRepository);
|
||
authRepository.responseDelay = const Duration(milliseconds: 50);
|
||
|
||
final future = authCubit!.getTokenByCredentials(
|
||
credentialsFixture['email'] as String,
|
||
credentialsFixture['password'] as String,
|
||
);
|
||
await Future.delayed(const Duration(milliseconds: 10));
|
||
|
||
expect(authCubit!.state, isA<AuthStateLoading>());
|
||
await future;
|
||
});
|
||
|
||
test('getTokenByCredentials - обработка ошибки авторизации', () async {
|
||
authCubit = AuthCubit(authRepository: authRepository);
|
||
authRepository.error = Exception('Неверный адрес электронной почты или пароль');
|
||
|
||
final result = await authCubit!.getTokenByCredentials('wrong@example.com', 'wrongpassword');
|
||
|
||
expect(result, false);
|
||
await Future.delayed(const Duration(milliseconds: 100));
|
||
expect(authCubit!.state, isA<AuthStateError>());
|
||
final errorState = authCubit!.state as AuthStateError;
|
||
expect(errorState.errorMessage, contains('Exception'));
|
||
expect(
|
||
authRepository.credentialPayloads.single,
|
||
{'email': 'wrong@example.com', 'password': 'wrongpassword'},
|
||
);
|
||
});
|
||
|
||
test('getTokenByCredentials - обработка различных типов ошибок', () async {
|
||
authCubit = AuthCubit(authRepository: authRepository);
|
||
authRepository.error = const FormatException('Invalid credentials format');
|
||
|
||
final result = await authCubit!.getTokenByCredentials('invalid', 'pass');
|
||
|
||
expect(result, false);
|
||
await Future.delayed(const Duration(milliseconds: 100));
|
||
expect(authCubit!.state, isA<AuthStateError>());
|
||
final errorState = authCubit!.state as AuthStateError;
|
||
expect(errorState.errorMessage, contains('FormatException'));
|
||
});
|
||
|
||
test('getTokenByCredentials - проверка вызова с правильными параметрами', () async {
|
||
authCubit = AuthCubit(authRepository: authRepository);
|
||
|
||
await authCubit!.getTokenByCredentials('user@test.com', 'securePassword123');
|
||
|
||
expect(
|
||
authRepository.credentialPayloads.single,
|
||
{'email': 'user@test.com', 'password': 'securePassword123'},
|
||
);
|
||
expect(
|
||
authRepository.credentialPayloads.where((call) => call['email'] == 'other@test.com'),
|
||
isEmpty,
|
||
);
|
||
});
|
||
|
||
test('getTokenByCredentials - множественные вызовы', () async {
|
||
authCubit = AuthCubit(authRepository: authRepository);
|
||
|
||
await authCubit!.getTokenByCredentials('first@test.com', 'password1');
|
||
await Future.delayed(const Duration(milliseconds: 100));
|
||
await authCubit!.getTokenByCredentials('second@test.com', 'password2');
|
||
|
||
expect(authRepository.credentialPayloads.length, 2);
|
||
expect(
|
||
authRepository.credentialPayloads,
|
||
[
|
||
{'email': 'first@test.com', 'password': 'password1'},
|
||
{'email': 'second@test.com', 'password': 'password2'},
|
||
],
|
||
);
|
||
});
|
||
});
|
||
}
|
||
|