Files
tm_app/test/model/customer_test.dart
T
amirrezaghabeli 13dbb5fae5 Enhance Customer model to include role attribute and update related components
- Added a new `role` attribute to the `Customer` model in `customer.dart` and updated the corresponding generated files.
- Modified the `AuthViewModel` to set the user role based on the logged-in user's role.
- Updated UI components in `TenderDetailActions` and `DesktopTendersPage` to conditionally render elements based on the user's role.
- Refactored tests to validate the new role attribute in the `Customer` model.
2025-10-14 09:12:24 +03:30

238 lines
7.0 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
import 'package:tm_app/data/services/model/address/address.dart';
import 'package:tm_app/data/services/model/contact_person/contact_person.dart';
import 'package:tm_app/data/services/model/customer/customer.dart';
void main() {
group('Customer Model Tests', () {
test('fromJson should parse JSON correctly', () {
final json = {
'id': '123',
'type': 'business',
'status': 'active',
'first_name': 'John',
'last_name': 'Doe',
'full_name': 'John Doe',
'email': 'john@example.com',
'phone': '021234567',
'mobile': '09123456789',
'registration_number': 'REG123',
'tax_id': 'TAX001',
'industry': 'Software',
'business_type': 'Tech',
'employee_count': 50,
'annual_revenue': 1000000,
'founded_year': 2015,
'role': 'admin',
'is_verified': true,
'is_compliant': false,
'compliance_notes': 'Pending documents',
'language': 'en',
'username': 'john_doe',
'currency': 'USD',
'timezone': 'UTC+3',
'created_at': 1700000000,
'updated_at': 1750000000,
'created_by': 'admin',
'updated_by': 'manager',
'address': {
'street': '123 Main St',
'city': 'Tehran',
'state': 'Tehran',
'postal_code': '12345',
'country': 'Iran',
'address_type': 'business',
'is_default': true,
},
'contactPersons': {
'first_name': 'Ali',
'last_name': 'Ahmadi',
'full_name': 'Ali Ahmadi',
'email': 'ali@example.com',
'phone': '09121234567',
'mobile': '09121234567',
'position': 'Manager',
'is_primary': true,
},
};
final customer = Customer.fromJson(json);
expect(customer.id, '123');
expect(customer.firstName, 'John');
expect(customer.lastName, 'Doe');
expect(customer.role, 'admin');
expect(customer.isVerified, true);
expect(customer.isCompliant, false);
expect(customer.address?.city, 'Tehran');
expect(customer.contactPersons?.firstName, 'Ali');
expect(customer.contactPersons?.isPrimary, true);
});
test('toJson should return valid JSON map', () {
final address = const Address(
street: 'Main St',
city: 'Tehran',
state: 'Tehran',
postalCode: '12345',
country: 'Iran',
addressType: 'business',
isDefault: true,
);
final contact = const ContactPerson(
firstName: 'Ali',
lastName: 'Doe',
fullName: 'Ali Doe',
position: 'Manager',
email: 'ali@example.com',
phone: '09121234567',
mobile: '09121234567',
isPrimary: true,
);
final customer = Customer(
id: '999',
type: 'business',
status: 'pending',
firstName: 'Sara',
lastName: 'Smith',
fullName: 'Sara Smith',
email: 'sara@example.com',
phone: '021111111',
mobile: '0912000000',
registrationNumber: 'R999',
taxId: 'T999',
industry: 'Finance',
businessType: 'Corporate',
employeeCount: 200,
annualRevenue: 5000000,
foundedYear: 2010,
role: 'admin',
isVerified: false,
isCompliant: true,
complianceNotes: 'Verified',
language: 'fa',
username: 'sara_smith',
currency: 'IRR',
timezone: 'Asia/Tehran',
createdAt: 1700000000,
updatedAt: 1750000000,
createdBy: 'system',
updatedBy: 'admin',
address: address,
contactPersons: contact,
);
final json = customer.toJson();
expect(json['id'], '999');
expect(json['first_name'], 'Sara');
expect(json['registration_number'], 'R999');
expect(json['address'], isNotNull);
expect((json['address'] as Address).toJson()['city'], 'Tehran');
// Todo: Check the bottom line
// expect((json['contact_persons'] as ContactPerson).toJson()['first_name'],'Ali',);
});
test('copyWith should modify specific fields only', () {
final customer = const Customer(
id: '123',
type: 'business',
status: 'active',
firstName: 'John',
lastName: 'Doe',
fullName: 'John Doe',
email: 'john@example.com',
phone: '021234567',
mobile: '09123456789',
registrationNumber: 'REG123',
taxId: 'TAX001',
industry: 'Software',
businessType: 'Tech',
employeeCount: 50,
annualRevenue: 1000000,
foundedYear: 2015,
role: 'admin',
isVerified: true,
isCompliant: true,
complianceNotes: 'OK',
language: 'en',
username: 'john_doe',
currency: 'USD',
timezone: 'UTC+3',
createdAt: 1700000000,
updatedAt: 1750000000,
createdBy: 'admin',
updatedBy: 'manager',
address: Address(
street: '123 Main St',
city: 'Tehran',
state: 'Tehran',
postalCode: '12345',
country: 'Iran',
addressType: 'business',
isDefault: true,
),
contactPersons: ContactPerson(
firstName: 'Ali',
lastName: 'Doe',
fullName: 'Ali n',
position: 'Manager',
email: 'ali@example.com',
phone: '09121234567',
mobile: '09121234567',
isPrimary: true,
),
);
final updated = customer.copyWith(
firstName: 'NewName',
role: 'analyst',
isVerified: false,
);
expect(updated.firstName, 'NewName');
expect(updated.role, 'analyst');
expect(updated.isVerified, false);
expect(updated.id, '123');
});
test('fromJson handles null fields safely', () {
final json = {'id': null, 'first_name': null, 'is_verified': null};
final customer = Customer.fromJson(json);
expect(customer.id, isNull);
expect(customer.firstName, isNull);
expect(customer.role, isNull);
expect(customer.isVerified, isNull);
});
test('fromJson -> toJson round-trip should preserve data', () {
final json = {
'id': 'A1',
'first_name': 'Ali',
'last_name': 'Rezaei',
'role': 'admin',
'is_verified': true,
'address': {'city': 'Tehran'},
'contactPersons': {'first_name': 'Sara', 'is_primary': true},
};
final customer = Customer.fromJson(json);
final output = customer.toJson();
expect(output['id'], 'A1');
expect(output['first_name'], 'Ali');
expect(output['role'], 'admin');
expect(output['is_verified'], true);
final addressJson = (output['address'] as Address).toJson();
final contactJson = (output['contactPersons'] as ContactPerson).toJson();
expect(addressJson['city'], 'Tehran');
expect(contactJson['first_name'], 'Sara');
});
});
}