Add unit tests for task domain layer

Tests for TaskEntity and Priority enum covering
construction, copyWith, equality, and parsing.
This commit is contained in:
m3mo 2026-02-03 19:45:59 +01:00
parent 329ea29966
commit 1bfa2722eb
2 changed files with 206 additions and 0 deletions

View File

@ -0,0 +1,147 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:agenda_tasks/features/tasks/domain/entities/task_entity.dart';
import 'package:agenda_tasks/features/tasks/domain/enums/priority.dart';
void main() {
group('TaskEntity', () {
final testDate = DateTime(2026, 2, 3);
final testCreatedAt = DateTime(2026, 1, 1);
TaskEntity createTask({
String id = 'test-id',
String title = 'Test Task',
String? description,
DateTime? date,
String? time,
Priority priority = Priority.medium,
bool isDone = false,
}) {
return TaskEntity(
id: id,
title: title,
description: description,
date: date ?? testDate,
time: time,
priority: priority,
isDone: isDone,
createdAt: testCreatedAt,
);
}
test('should create a TaskEntity with required fields', () {
final task = TaskEntity(
id: 'task-1',
title: 'My Task',
date: testDate,
);
expect(task.id, 'task-1');
expect(task.title, 'My Task');
expect(task.date, testDate);
expect(task.priority, Priority.medium);
expect(task.isDone, false);
expect(task.description, isNull);
expect(task.time, isNull);
});
test('should create a TaskEntity with all fields', () {
final task = TaskEntity(
id: 'task-1',
title: 'My Task',
description: 'Task description',
date: testDate,
time: '14:30',
priority: Priority.high,
isDone: true,
createdAt: testCreatedAt,
updatedAt: testCreatedAt,
);
expect(task.id, 'task-1');
expect(task.title, 'My Task');
expect(task.description, 'Task description');
expect(task.date, testDate);
expect(task.time, '14:30');
expect(task.priority, Priority.high);
expect(task.isDone, true);
expect(task.createdAt, testCreatedAt);
expect(task.updatedAt, testCreatedAt);
});
group('copyWith', () {
test('should return a copy with updated title', () {
final original = createTask();
final copy = original.copyWith(title: 'Updated Title');
expect(copy.title, 'Updated Title');
expect(copy.id, original.id);
expect(copy.date, original.date);
expect(copy.priority, original.priority);
});
test('should return a copy with updated priority', () {
final original = createTask(priority: Priority.low);
final copy = original.copyWith(priority: Priority.high);
expect(copy.priority, Priority.high);
expect(copy.title, original.title);
});
test('should return a copy with updated isDone', () {
final original = createTask(isDone: false);
final copy = original.copyWith(isDone: true);
expect(copy.isDone, true);
expect(original.isDone, false);
});
test('should return a copy with updated date', () {
final original = createTask();
final newDate = DateTime(2026, 3, 15);
final copy = original.copyWith(date: newDate);
expect(copy.date, newDate);
expect(original.date, testDate);
});
test('should return identical copy when no arguments provided', () {
final original = createTask(
description: 'Test desc',
time: '10:00',
);
final copy = original.copyWith();
expect(copy.id, original.id);
expect(copy.title, original.title);
expect(copy.description, original.description);
expect(copy.date, original.date);
expect(copy.time, original.time);
expect(copy.priority, original.priority);
expect(copy.isDone, original.isDone);
});
});
group('equality', () {
test('should be equal when ids are the same', () {
final task1 = createTask(id: 'same-id', title: 'Task 1');
final task2 = createTask(id: 'same-id', title: 'Task 2');
expect(task1, equals(task2));
});
test('should not be equal when ids are different', () {
final task1 = createTask(id: 'id-1', title: 'Same Title');
final task2 = createTask(id: 'id-2', title: 'Same Title');
expect(task1, isNot(equals(task2)));
});
test('should have same hashCode when ids are the same', () {
final task1 = createTask(id: 'same-id');
final task2 = createTask(id: 'same-id');
expect(task1.hashCode, equals(task2.hashCode));
});
});
});
}

View File

@ -0,0 +1,59 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:agenda_tasks/features/tasks/domain/enums/priority.dart';
void main() {
group('Priority', () {
group('values', () {
test('should have three priority levels', () {
expect(Priority.values.length, 3);
expect(Priority.values, contains(Priority.low));
expect(Priority.values, contains(Priority.medium));
expect(Priority.values, contains(Priority.high));
});
});
group('displayName', () {
test('should return "Low" for Priority.low', () {
expect(Priority.low.displayName, 'Low');
});
test('should return "Medium" for Priority.medium', () {
expect(Priority.medium.displayName, 'Medium');
});
test('should return "High" for Priority.high', () {
expect(Priority.high.displayName, 'High');
});
});
group('fromString', () {
test('should parse "low" to Priority.low', () {
expect(Priority.fromString('low'), Priority.low);
});
test('should parse "medium" to Priority.medium', () {
expect(Priority.fromString('medium'), Priority.medium);
});
test('should parse "high" to Priority.high', () {
expect(Priority.fromString('high'), Priority.high);
});
test('should parse uppercase "LOW" to Priority.low', () {
expect(Priority.fromString('LOW'), Priority.low);
});
test('should parse mixed case "Medium" to Priority.medium', () {
expect(Priority.fromString('Medium'), Priority.medium);
});
test('should return Priority.medium for invalid string', () {
expect(Priority.fromString('invalid'), Priority.medium);
});
test('should return Priority.medium for empty string', () {
expect(Priority.fromString(''), Priority.medium);
});
});
});
}