AgendaTasks/test/features/auth/domain/entities/user_entity_test.dart
m3mo 329ea29966 Add unit tests for auth feature
Tests for UserEntity, UserModel, and TokenModel covering
serialization, parsing, and equality.
2026-02-03 19:45:17 +01:00

125 lines
3.4 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
import 'package:agenda_tasks/features/auth/domain/entities/user_entity.dart';
void main() {
group('UserEntity', () {
final testCreatedAt = DateTime(2026, 1, 1, 12, 0, 0);
test('should create a UserEntity with required fields', () {
const user = UserEntity(
id: 'user-1',
email: 'test@example.com',
name: 'Test User',
);
expect(user.id, 'user-1');
expect(user.email, 'test@example.com');
expect(user.name, 'Test User');
expect(user.createdAt, isNull);
});
test('should create a UserEntity with all fields', () {
final user = UserEntity(
id: 'user-1',
email: 'test@example.com',
name: 'Test User',
createdAt: testCreatedAt,
);
expect(user.id, 'user-1');
expect(user.email, 'test@example.com');
expect(user.name, 'Test User');
expect(user.createdAt, testCreatedAt);
});
group('copyWith', () {
test('should return a copy with updated name', () {
const original = UserEntity(
id: 'user-1',
email: 'test@example.com',
name: 'Original Name',
);
final copy = original.copyWith(name: 'Updated Name');
expect(copy.name, 'Updated Name');
expect(copy.id, original.id);
expect(copy.email, original.email);
});
test('should return a copy with updated email', () {
const original = UserEntity(
id: 'user-1',
email: 'old@example.com',
name: 'Test User',
);
final copy = original.copyWith(email: 'new@example.com');
expect(copy.email, 'new@example.com');
expect(copy.name, original.name);
});
test('should return identical copy when no arguments provided', () {
final original = UserEntity(
id: 'user-1',
email: 'test@example.com',
name: 'Test User',
createdAt: testCreatedAt,
);
final copy = original.copyWith();
expect(copy.id, original.id);
expect(copy.email, original.email);
expect(copy.name, original.name);
expect(copy.createdAt, original.createdAt);
});
});
group('equality', () {
test('should be equal when ids are the same', () {
const user1 = UserEntity(
id: 'same-id',
email: 'user1@example.com',
name: 'User 1',
);
const user2 = UserEntity(
id: 'same-id',
email: 'user2@example.com',
name: 'User 2',
);
expect(user1, equals(user2));
});
test('should not be equal when ids are different', () {
const user1 = UserEntity(
id: 'id-1',
email: 'same@example.com',
name: 'Same Name',
);
const user2 = UserEntity(
id: 'id-2',
email: 'same@example.com',
name: 'Same Name',
);
expect(user1, isNot(equals(user2)));
});
test('should have same hashCode when ids are the same', () {
const user1 = UserEntity(
id: 'same-id',
email: 'user1@example.com',
name: 'User 1',
);
const user2 = UserEntity(
id: 'same-id',
email: 'user2@example.com',
name: 'User 2',
);
expect(user1.hashCode, equals(user2.hashCode));
});
});
});
}