forked from eatvio/eatvio_app
feat(test): updated tests to increase tests coverage
This commit is contained in:
169
test/features/registration/registration_error_message_test.dart
Normal file
169
test/features/registration/registration_error_message_test.dart
Normal file
@@ -0,0 +1,169 @@
|
||||
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 the error itself when it is a String', () {
|
||||
expect(registrationErrorMessage('Some failure'), 'Some failure');
|
||||
});
|
||||
|
||||
test('returns toString for arbitrary objects', () {
|
||||
final error = Exception('Boom');
|
||||
expect(registrationErrorMessage(error), error.toString());
|
||||
});
|
||||
|
||||
test('returns DioException.message when type is not 422 and message is set', () {
|
||||
final dio = DioException(
|
||||
requestOptions: RequestOptions(path: '/x'),
|
||||
message: 'Network down',
|
||||
type: DioExceptionType.connectionError,
|
||||
);
|
||||
|
||||
expect(registrationErrorMessage(dio), 'Network down');
|
||||
});
|
||||
|
||||
test('falls back to DioException.toString when message is null', () {
|
||||
final dio = DioException(
|
||||
requestOptions: RequestOptions(path: '/x'),
|
||||
type: DioExceptionType.unknown,
|
||||
);
|
||||
|
||||
expect(registrationErrorMessage(dio), dio.toString());
|
||||
});
|
||||
|
||||
test('parses 422 message and field errors', () {
|
||||
final dio = DioException(
|
||||
requestOptions: RequestOptions(path: '/register'),
|
||||
response: Response(
|
||||
requestOptions: RequestOptions(path: '/register'),
|
||||
statusCode: 422,
|
||||
data: <String, dynamic>{
|
||||
'message': 'Validation failed',
|
||||
'errors': <String, dynamic>{
|
||||
'email': <String>['Email is required', 'Email must be valid'],
|
||||
'password': <String>['Password too short'],
|
||||
},
|
||||
},
|
||||
),
|
||||
type: DioExceptionType.badResponse,
|
||||
);
|
||||
|
||||
final result = registrationErrorMessage(dio);
|
||||
expect(result, contains('Validation failed'));
|
||||
expect(result, contains('Email is required'));
|
||||
expect(result, contains('Email must be valid'));
|
||||
expect(result, contains('Password too short'));
|
||||
});
|
||||
|
||||
test('handles 422 with only message', () {
|
||||
final dio = DioException(
|
||||
requestOptions: RequestOptions(path: '/x'),
|
||||
response: Response(
|
||||
requestOptions: RequestOptions(path: '/x'),
|
||||
statusCode: 422,
|
||||
data: <String, dynamic>{'message': 'Email already taken'},
|
||||
),
|
||||
type: DioExceptionType.badResponse,
|
||||
);
|
||||
|
||||
expect(registrationErrorMessage(dio), 'Email already taken');
|
||||
});
|
||||
|
||||
test('handles 422 with only errors map', () {
|
||||
final dio = DioException(
|
||||
requestOptions: RequestOptions(path: '/x'),
|
||||
response: Response(
|
||||
requestOptions: RequestOptions(path: '/x'),
|
||||
statusCode: 422,
|
||||
data: <String, dynamic>{
|
||||
'errors': <String, dynamic>{
|
||||
'phone': <String>['Phone is required'],
|
||||
},
|
||||
},
|
||||
),
|
||||
type: DioExceptionType.badResponse,
|
||||
);
|
||||
|
||||
expect(registrationErrorMessage(dio), contains('Phone is required'));
|
||||
});
|
||||
|
||||
test('handles 422 with empty errors collection — returns generic message', () {
|
||||
final data = <String, dynamic>{
|
||||
'errors': <String, dynamic>{
|
||||
'phone': <String>[],
|
||||
'email': <String>[],
|
||||
},
|
||||
};
|
||||
final dio = DioException(
|
||||
requestOptions: RequestOptions(path: '/x'),
|
||||
response: Response(
|
||||
requestOptions: RequestOptions(path: '/x'),
|
||||
statusCode: 422,
|
||||
data: data,
|
||||
),
|
||||
type: DioExceptionType.badResponse,
|
||||
);
|
||||
|
||||
expect(
|
||||
registrationErrorMessage(dio),
|
||||
'Ошибка регистрации: $data',
|
||||
);
|
||||
});
|
||||
|
||||
test('handles 422 when errors is not a Map', () {
|
||||
final data = <String, dynamic>{
|
||||
'errors': <String>['unexpected payload'],
|
||||
};
|
||||
final dio = DioException(
|
||||
requestOptions: RequestOptions(path: '/x'),
|
||||
response: Response(
|
||||
requestOptions: RequestOptions(path: '/x'),
|
||||
statusCode: 422,
|
||||
data: data,
|
||||
),
|
||||
type: DioExceptionType.badResponse,
|
||||
);
|
||||
|
||||
expect(
|
||||
registrationErrorMessage(dio),
|
||||
'Ошибка регистрации: $data',
|
||||
);
|
||||
});
|
||||
|
||||
test('falls back to DioException default for 422 with non-map data', () {
|
||||
final dio = DioException(
|
||||
requestOptions: RequestOptions(path: '/x'),
|
||||
response: Response(
|
||||
requestOptions: RequestOptions(path: '/x'),
|
||||
statusCode: 422,
|
||||
data: 'plain string',
|
||||
),
|
||||
message: 'Server returned 422',
|
||||
type: DioExceptionType.badResponse,
|
||||
);
|
||||
|
||||
expect(registrationErrorMessage(dio), 'Server returned 422');
|
||||
});
|
||||
|
||||
test('handles 422 when value in errors map is not a List', () {
|
||||
final data = <String, dynamic>{
|
||||
'message': 'Bad data',
|
||||
'errors': <String, dynamic>{
|
||||
'phone': 'not a list, will be skipped',
|
||||
},
|
||||
};
|
||||
final dio = DioException(
|
||||
requestOptions: RequestOptions(path: '/x'),
|
||||
response: Response(
|
||||
requestOptions: RequestOptions(path: '/x'),
|
||||
statusCode: 422,
|
||||
data: data,
|
||||
),
|
||||
type: DioExceptionType.badResponse,
|
||||
);
|
||||
|
||||
expect(registrationErrorMessage(dio), 'Bad data');
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user