Files
eatvio_app/test/features/registration/registration_error_message_test.dart
2026-05-04 17:36:48 +03:00

111 lines
3.6 KiB
Dart

import 'package:dio/dio.dart';
import 'package:eatvio_app/features/registration/registration_error_message.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group('registrationErrorMessage', () {
test('returns string when error is String', () {
expect(registrationErrorMessage('Some error'), 'Some error');
});
test('returns message from 422 DioException with message field', () {
final error = DioException(
requestOptions: RequestOptions(path: ''),
response: Response(
requestOptions: RequestOptions(path: ''),
statusCode: 422,
data: {'message': 'Validation failed'},
),
);
expect(registrationErrorMessage(error), 'Validation failed');
});
test('returns errors from 422 DioException with errors field', () {
final error = DioException(
requestOptions: RequestOptions(path: ''),
response: Response(
requestOptions: RequestOptions(path: ''),
statusCode: 422,
data: {
'message': 'Validation error',
'errors': {
'email': ['Email уже используется'],
'name': ['Имя обязательно'],
},
},
),
);
final result = registrationErrorMessage(error);
expect(result, contains('Validation error'));
expect(result, contains('Email уже используется'));
expect(result, contains('Имя обязательно'));
});
test('returns data string for 422 with unknown format', () {
final error = DioException(
requestOptions: RequestOptions(path: ''),
response: Response(
requestOptions: RequestOptions(path: ''),
statusCode: 422,
data: {'unknown_key': 'value'},
),
);
expect(registrationErrorMessage(error), contains('Ошибка регистрации'));
});
test('returns DioException message for non-422 status', () {
final error = DioException(
requestOptions: RequestOptions(path: ''),
message: 'Connection timeout',
response: Response(
requestOptions: RequestOptions(path: ''),
statusCode: 500,
),
);
expect(registrationErrorMessage(error), 'Connection timeout');
});
test('returns DioException toString when message is null', () {
final error = DioException(
requestOptions: RequestOptions(path: ''),
);
expect(registrationErrorMessage(error), contains('DioException'));
});
test('returns toString for unknown error type', () {
expect(registrationErrorMessage(42), '42');
expect(registrationErrorMessage(Exception('Test')), contains('Test'));
});
test('handles 422 with empty errors list', () {
final error = DioException(
requestOptions: RequestOptions(path: ''),
response: Response(
requestOptions: RequestOptions(path: ''),
statusCode: 422,
data: {
'message': 'Error',
'errors': {
'field': <String>[],
},
},
),
);
expect(registrationErrorMessage(error), 'Error');
});
test('handles 422 with non-Map response data', () {
final error = DioException(
requestOptions: RequestOptions(path: ''),
response: Response(
requestOptions: RequestOptions(path: ''),
statusCode: 422,
data: 'plain string error',
),
);
// Not a map, falls through to DioException handling
expect(registrationErrorMessage(error), isNotEmpty);
});
});
}