Updated the cases of the TaskListViewModelTests tests in the unit tests target to use the MockTodoRepository class instead.
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
import Foundation
|
||||
import SwiftData
|
||||
import SwiftUI
|
||||
import Testing
|
||||
|
||||
@@ -91,43 +89,59 @@ struct TaskListViewModelTests {
|
||||
|
||||
// MARK: Method tests
|
||||
|
||||
@Test("deleteTasks removes a single task from model context")
|
||||
func deleteTasksSingle() throws {
|
||||
@Test("delete removes a single todo task from repository")
|
||||
func deleteSingleTodo() {
|
||||
// GIVEN
|
||||
let container = try ModelContainer(
|
||||
for: Todo.self,
|
||||
configurations: .init(isStoredInMemoryOnly: true)
|
||||
)
|
||||
let context = ModelContext(container)
|
||||
|
||||
let viewModel = TaskListView.ViewModel(group: .today)
|
||||
let todosToInsert: [Todo] = [
|
||||
let todos: [Todo] = [
|
||||
.init(title: "Task 1", due: .now, order: 0),
|
||||
.init(title: "Task 2", due: .now, order: 1),
|
||||
.init(title: "Task 3", due: .now, order: 2),
|
||||
]
|
||||
let countInitial = todosToInsert.count
|
||||
|
||||
for todo in todosToInsert {
|
||||
context.insert(todo)
|
||||
}
|
||||
let repository = MockTodoRepository(todos: todos)
|
||||
let viewModel = TaskListView.ViewModel(group: .today)
|
||||
|
||||
// WHEN
|
||||
viewModel.deleteTasks(
|
||||
todosToInsert,
|
||||
viewModel.delete(
|
||||
todos,
|
||||
at: IndexSet(integer: 1),
|
||||
from: context
|
||||
from: repository
|
||||
)
|
||||
|
||||
// THEN
|
||||
let descriptor = FetchDescriptor<Todo>()
|
||||
let todos = try context.fetch(descriptor)
|
||||
#expect(repository.todos.count == 2)
|
||||
#expect(repository.todos.contains { $0.title == "Task 1" })
|
||||
#expect(repository.todos.contains { $0.title == "Task 3" })
|
||||
#expect(!repository.todos.contains { $0.title == "Task 2" })
|
||||
}
|
||||
|
||||
#expect(countInitial == 3)
|
||||
#expect(todos.count == 2)
|
||||
#expect(todos.contains(where: { $0.title == "Task 1" }))
|
||||
#expect(todos.contains(where: { $0.title == "Task 3" }))
|
||||
#expect(!todos.contains(where: { $0.title == "Task 2" }))
|
||||
@Test("delete removes multiple todo tasks from repository")
|
||||
func deleteMultipleTodos() {
|
||||
// GIVEN
|
||||
let todos: [Todo] = [
|
||||
.init(title: "Task 1", due: .now, order: 0),
|
||||
.init(title: "Task 2", due: .now, order: 1),
|
||||
.init(title: "Task 3", due: .now, order: 2),
|
||||
.init(title: "Task 4", due: .now, order: 3),
|
||||
.init(title: "Task 5", due: .now, order: 4),
|
||||
]
|
||||
|
||||
let repository = MockTodoRepository(todos: todos)
|
||||
let viewModel = TaskListView.ViewModel(group: .today)
|
||||
|
||||
// WHEN
|
||||
viewModel.delete(
|
||||
todos,
|
||||
at: IndexSet([1, 3]),
|
||||
from: repository
|
||||
)
|
||||
|
||||
// THEN
|
||||
#expect(repository.todos.count == 3)
|
||||
#expect(repository.todos.contains { $0.title == "Task 1" })
|
||||
#expect(repository.todos.contains { $0.title == "Task 3" })
|
||||
#expect(repository.todos.contains { $0.title == "Task 5" })
|
||||
#expect(!repository.todos.contains { $0.title == "Task 2" })
|
||||
#expect(!repository.todos.contains { $0.title == "Task 4" })
|
||||
}
|
||||
|
||||
@Test("reorderTasks updates order property when moving task forward")
|
||||
@@ -180,7 +194,7 @@ struct TaskListViewModelTests {
|
||||
#expect(tasks[3].order == 1)
|
||||
}
|
||||
|
||||
@Test("toggleCompletedVisbility toggles the showCompleted property")
|
||||
@Test("toggleCompletedVisibility toggles the showCompleted property")
|
||||
func toggleCompletedVisibility() {
|
||||
// GIVEN
|
||||
let viewModel = TaskListView.ViewModel(group: .today)
|
||||
|
||||
Reference in New Issue
Block a user