This PR contains the work done to improve upon the editing experience on the **Task list item and the **New task** views. Reviewed-on: rock-n-code/todos-app#9 Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
50 lines
1.5 KiB
Swift
50 lines
1.5 KiB
Swift
@testable import class Todos.Todo
|
|
@testable import protocol Todos.TodoRepository
|
|
|
|
/// A mock implementation of the TodoRepository protocol for unit testing.
|
|
///
|
|
/// This mock repository provides a simple in-memory storage mechanism for testing
|
|
/// without requiring a full SwiftData ModelContext setup. It tracks inserted and
|
|
/// deleted todos, allowing tests to verify repository operations.
|
|
final class MockTodoRepository {
|
|
|
|
// MARK: Properties
|
|
|
|
/// Array of todo tasks managed by this mock repository.
|
|
///
|
|
/// This property tracks all todos in the repository, maintaining state across
|
|
/// insert and delete operations. It serves as the in-memory storage for the mock,
|
|
/// allowing tests to verify repository behavior without requiring SwiftData.
|
|
private(set) var todos: [Todo]
|
|
|
|
// MARK: Initializers
|
|
|
|
/// Creates a new mock repository with an optional initial set of todo tasks.
|
|
///
|
|
/// This initializer allows tests to set up a repository with pre-existing todos
|
|
/// or start with an empty repository. The default parameter value provides
|
|
/// convenient initialization for most test cases.
|
|
///
|
|
/// - Parameter todos: An array of todo tasks to initialize the repository with. Defaults to an empty array.
|
|
init(todos: [Todo] = []) {
|
|
self.todos = todos
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - TodoRepository
|
|
|
|
extension MockTodoRepository: TodoRepository {
|
|
|
|
// MARK: Functions
|
|
|
|
func delete(_ todo: Todo) {
|
|
todos.removeAll { $0.id == todo.id }
|
|
}
|
|
|
|
func insert(_ todo: Todo) {
|
|
todos.append(todo)
|
|
}
|
|
|
|
}
|