work on test models part 1

This commit is contained in:
llsajjad
2025-10-06 16:26:53 +03:30
parent 2367899b54
commit 69623d82ba
15 changed files with 1206 additions and 63 deletions
+84
View File
@@ -0,0 +1,84 @@
////////////////////////////
import 'package:flutter_test/flutter_test.dart';
import 'package:tm_app/data/services/model/error/error_model.dart';
void main() {
group('ErrorModel Tests', () {
test('fromJson should parse JSON correctly', () {
final json = {
'message': 'Invalid request',
'code': '400',
'details': 'Missing required field: email',
};
final error = ErrorModel.fromJson(json);
expect(error.message, 'Invalid request');
expect(error.code, '400');
expect(error.details, 'Missing required field: email');
});
test('toJson should return valid JSON map', () {
const error = ErrorModel(
message: 'Unauthorized',
code: '401',
details: 'Token expired',
);
final json = error.toJson();
expect(json, isA<Map<String, dynamic>>());
expect(json['message'], 'Unauthorized');
expect(json['code'], '401');
expect(json['details'], 'Token expired');
});
test(' copyWith should modify specific fields only', () {
const error = ErrorModel(
message: 'Something went wrong',
code: '500',
details: 'Server error',
);
final updated = error.copyWith(
message: 'Timeout error',
);
expect(updated.message, 'Timeout error');
expect(updated.code, '500');
expect(updated.details, 'Server error');
});
test('fromJson handles null fields safely', () {
final json = {
'message': null,
'code': null,
'details': null,
};
final error = ErrorModel.fromJson(json);
expect(error.message, isNull);
expect(error.code, isNull);
expect(error.details, isNull);
});
test('fromJson -> toJson round-trip should preserve data', () {
final json = {
'message': 'Not Found',
'code': '404',
'details': 'User not found',
};
final error = ErrorModel.fromJson(json);
final output = error.toJson();
expect(output['message'], 'Not Found');
expect(output['code'], '404');
expect(output['details'], 'User not found');
});
});
}
////////////////////////////