Files
Auto-GPT/test/task_viewmodel_test.dart
hunteraraujo 5b520eb5ae Implement TaskViewModel with tests and mock data
This commit introduces the TaskViewModel, which manages the business logic for tasks. The ViewModel interacts with a mock data source, providing functionalities like fetching tasks, selecting a task, creating, and deleting tasks.

Additionally, comprehensive tests for TaskViewModel have been added to ensure its behavior aligns with expectations. The mock data source has also been updated to support the new functionalities.

Key Features:
- Task management in TaskViewModel.
- Tests for each major functionality in TaskViewModel.
- Mock data source to simulate data interactions.
2023-08-21 00:03:39 +02:00

80 lines
2.2 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
import 'package:auto_gpt_flutter_client/viewmodels/task_viewmodel.dart';
import 'package:auto_gpt_flutter_client/viewmodels/mock_data.dart';
void main() {
group('TaskViewModel', () {
late TaskViewModel viewModel;
setUp(() {
viewModel = TaskViewModel();
});
test('Fetches tasks successfully', () {
viewModel.fetchTasks();
expect(viewModel.tasks, isNotEmpty);
});
test('Selects a task successfully', () {
viewModel.fetchTasks();
viewModel.selectTask(1);
expect(viewModel.selectedTask, isNotNull);
});
test(
'Notifiers are properly telling UI to update after fetching a task or selecting a task',
() {
bool hasNotified = false;
viewModel.addListener(() {
hasNotified = true;
});
viewModel.fetchTasks();
expect(hasNotified, true);
hasNotified = false; // Reset for next test
viewModel.selectTask(1);
expect(hasNotified, true);
});
test('Simulate error happening while fetching a task', () {
// TODO: Implement once you have error handling in place in fetchTasks.
});
test('No tasks are fetched', () {
// Clear mock data for this test
mockTasks.clear();
viewModel.fetchTasks();
expect(viewModel.tasks, isEmpty);
});
test('No task is selected', () {
expect(viewModel.selectedTask, isNull);
});
test('Creates a task successfully', () {
final initialCount = viewModel.tasks.length;
viewModel.createTask('New Task');
expect(viewModel.tasks.length, initialCount + 1);
});
test('Deletes a task successfully', () {
viewModel.fetchTasks();
final initialCount = viewModel.tasks.length;
viewModel.deleteTask(1);
expect(viewModel.tasks.length, initialCount - 1);
});
test('Deletes a task with invalid id', () {
final initialCount = viewModel.tasks.length;
viewModel.deleteTask(9999); // Assuming no task with this id exists
expect(viewModel.tasks.length, initialCount); // Count remains same
});
test('Select a task that doesn\'t exist', () {
expect(() => viewModel.selectTask(9999), throwsA(isA<ArgumentError>()));
});
});
}