From 329ea29966cc09ca4f41f1bc3493e9e5f415a4ef Mon Sep 17 00:00:00 2001 From: m3mo Date: Tue, 3 Feb 2026 19:45:17 +0100 Subject: [PATCH] Add unit tests for auth feature Tests for UserEntity, UserModel, and TokenModel covering serialization, parsing, and equality. --- .../auth/data/models/token_model_test.dart | 124 ++++++++++++++++ .../auth/data/models/user_model_test.dart | 138 ++++++++++++++++++ .../domain/entities/user_entity_test.dart | 124 ++++++++++++++++ 3 files changed, 386 insertions(+) create mode 100644 test/features/auth/data/models/token_model_test.dart create mode 100644 test/features/auth/data/models/user_model_test.dart create mode 100644 test/features/auth/domain/entities/user_entity_test.dart diff --git a/test/features/auth/data/models/token_model_test.dart b/test/features/auth/data/models/token_model_test.dart new file mode 100644 index 0000000..d6b6fa4 --- /dev/null +++ b/test/features/auth/data/models/token_model_test.dart @@ -0,0 +1,124 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:agenda_tasks/features/auth/data/models/token_model.dart'; + +void main() { + group('TokenModel', () { + group('constructor', () { + test('should create TokenModel with required fields', () { + const token = TokenModel( + accessToken: 'access-token-123', + refreshToken: 'refresh-token-456', + ); + + expect(token.accessToken, 'access-token-123'); + expect(token.refreshToken, 'refresh-token-456'); + expect(token.tokenType, 'bearer'); + }); + + test('should create TokenModel with custom tokenType', () { + const token = TokenModel( + accessToken: 'access-token-123', + refreshToken: 'refresh-token-456', + tokenType: 'custom', + ); + + expect(token.tokenType, 'custom'); + }); + }); + + group('fromJson', () { + test('should parse complete JSON correctly', () { + final json = { + 'access_token': 'access-token-123', + 'refresh_token': 'refresh-token-456', + 'token_type': 'bearer', + }; + + final token = TokenModel.fromJson(json); + + expect(token.accessToken, 'access-token-123'); + expect(token.refreshToken, 'refresh-token-456'); + expect(token.tokenType, 'bearer'); + }); + + test('should default tokenType to bearer when null', () { + final json = { + 'access_token': 'access-token-123', + 'refresh_token': 'refresh-token-456', + 'token_type': null, + }; + + final token = TokenModel.fromJson(json); + + expect(token.tokenType, 'bearer'); + }); + + test('should default tokenType to bearer when missing', () { + final json = { + 'access_token': 'access-token-123', + 'refresh_token': 'refresh-token-456', + }; + + final token = TokenModel.fromJson(json); + + expect(token.tokenType, 'bearer'); + }); + + test('should parse different token types', () { + final json = { + 'access_token': 'access-token-123', + 'refresh_token': 'refresh-token-456', + 'token_type': 'Bearer', + }; + + final token = TokenModel.fromJson(json); + + expect(token.tokenType, 'Bearer'); + }); + }); + + group('toJson', () { + test('should serialize to JSON correctly', () { + const token = TokenModel( + accessToken: 'access-token-123', + refreshToken: 'refresh-token-456', + tokenType: 'bearer', + ); + + final json = token.toJson(); + + expect(json['access_token'], 'access-token-123'); + expect(json['refresh_token'], 'refresh-token-456'); + expect(json['token_type'], 'bearer'); + }); + + test('should include default tokenType in JSON', () { + const token = TokenModel( + accessToken: 'access-token-123', + refreshToken: 'refresh-token-456', + ); + + final json = token.toJson(); + + expect(json['token_type'], 'bearer'); + }); + }); + + group('round-trip serialization', () { + test('should maintain data after fromJson -> toJson', () { + final originalJson = { + 'access_token': 'access-token-123', + 'refresh_token': 'refresh-token-456', + 'token_type': 'bearer', + }; + + final token = TokenModel.fromJson(originalJson); + final resultJson = token.toJson(); + + expect(resultJson['access_token'], originalJson['access_token']); + expect(resultJson['refresh_token'], originalJson['refresh_token']); + expect(resultJson['token_type'], originalJson['token_type']); + }); + }); + }); +} diff --git a/test/features/auth/data/models/user_model_test.dart b/test/features/auth/data/models/user_model_test.dart new file mode 100644 index 0000000..a18273d --- /dev/null +++ b/test/features/auth/data/models/user_model_test.dart @@ -0,0 +1,138 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:agenda_tasks/features/auth/data/models/user_model.dart'; +import 'package:agenda_tasks/features/auth/domain/entities/user_entity.dart'; + +void main() { + group('UserModel', () { + final testCreatedAt = DateTime(2026, 1, 1, 10, 30, 0); + + group('fromJson', () { + test('should parse complete JSON correctly', () { + final json = { + 'id': 'user-123', + 'email': 'test@example.com', + 'name': 'Test User', + 'created_at': '2026-01-01T10:30:00.000', + }; + + final user = UserModel.fromJson(json); + + expect(user.id, 'user-123'); + expect(user.email, 'test@example.com'); + expect(user.name, 'Test User'); + expect(user.createdAt, testCreatedAt); + }); + + test('should parse JSON without created_at', () { + final json = { + 'id': 'user-123', + 'email': 'test@example.com', + 'name': 'Test User', + }; + + final user = UserModel.fromJson(json); + + expect(user.id, 'user-123'); + expect(user.email, 'test@example.com'); + expect(user.name, 'Test User'); + expect(user.createdAt, isNull); + }); + + test('should parse JSON with null created_at', () { + final json = { + 'id': 'user-123', + 'email': 'test@example.com', + 'name': 'Test User', + 'created_at': null, + }; + + final user = UserModel.fromJson(json); + + expect(user.createdAt, isNull); + }); + }); + + group('toJson', () { + test('should serialize to JSON correctly with all fields', () { + final user = UserModel( + id: 'user-123', + email: 'test@example.com', + name: 'Test User', + createdAt: testCreatedAt, + ); + + final json = user.toJson(); + + expect(json['id'], 'user-123'); + expect(json['email'], 'test@example.com'); + expect(json['name'], 'Test User'); + expect(json['created_at'], isNotNull); + }); + + test('should serialize to JSON with null created_at', () { + const user = UserModel( + id: 'user-123', + email: 'test@example.com', + name: 'Test User', + ); + + final json = user.toJson(); + + expect(json['id'], 'user-123'); + expect(json['email'], 'test@example.com'); + expect(json['name'], 'Test User'); + expect(json['created_at'], isNull); + }); + }); + + group('fromEntity', () { + test('should create UserModel from UserEntity with all fields', () { + final entity = UserEntity( + id: 'entity-123', + email: 'entity@example.com', + name: 'Entity User', + createdAt: testCreatedAt, + ); + + final model = UserModel.fromEntity(entity); + + expect(model.id, entity.id); + expect(model.email, entity.email); + expect(model.name, entity.name); + expect(model.createdAt, entity.createdAt); + }); + + test('should create UserModel from UserEntity without createdAt', () { + const entity = UserEntity( + id: 'entity-123', + email: 'entity@example.com', + name: 'Entity User', + ); + + final model = UserModel.fromEntity(entity); + + expect(model.id, entity.id); + expect(model.email, entity.email); + expect(model.name, entity.name); + expect(model.createdAt, isNull); + }); + }); + + group('round-trip serialization', () { + test('should maintain data after fromJson -> toJson', () { + final originalJson = { + 'id': 'user-123', + 'email': 'test@example.com', + 'name': 'Test User', + }; + + final user = UserModel.fromJson(originalJson); + final resultJson = user.toJson(); + + expect(resultJson['id'], originalJson['id']); + expect(resultJson['email'], originalJson['email']); + expect(resultJson['name'], originalJson['name']); + }); + }); + }); +} diff --git a/test/features/auth/domain/entities/user_entity_test.dart b/test/features/auth/domain/entities/user_entity_test.dart new file mode 100644 index 0000000..c505c70 --- /dev/null +++ b/test/features/auth/domain/entities/user_entity_test.dart @@ -0,0 +1,124 @@ +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)); + }); + }); + }); +}