diff --git a/UnitTests/Tests/View Models/NewTaskViewModelTests.swift b/UnitTests/Tests/View Models/NewTaskViewModelTests.swift index 12961b1..0f32407 100644 --- a/UnitTests/Tests/View Models/NewTaskViewModelTests.swift +++ b/UnitTests/Tests/View Models/NewTaskViewModelTests.swift @@ -124,10 +124,7 @@ struct NewTaskViewModelTests { @MainActor func saveWithAllProperties() throws { // GIVEN - let container = try ModelContainer( - for: Todo.self, - configurations: .init(isStoredInMemoryOnly: true) - ) + let container = try modelContainer let context = container.mainContext let viewModel = NewTaskView.ViewModel() @@ -153,10 +150,7 @@ struct NewTaskViewModelTests { @MainActor func saveWithEmptyNote() throws { // GIVEN - let container = try ModelContainer( - for: Todo.self, - configurations: .init(isStoredInMemoryOnly: true) - ) + let container = try modelContainer let context = container.mainContext let viewModel = NewTaskView.ViewModel() @@ -182,10 +176,7 @@ struct NewTaskViewModelTests { @MainActor func saveWithWhitespaceNote() throws { // GIVEN - let container = try ModelContainer( - for: Todo.self, - configurations: .init(isStoredInMemoryOnly: true) - ) + let container = try modelContainer let context = container.mainContext let viewModel = NewTaskView.ViewModel() @@ -211,10 +202,7 @@ struct NewTaskViewModelTests { @MainActor func saveMultipleTodos() throws { // GIVEN - let container = try ModelContainer( - for: Todo.self, - configurations: .init(isStoredInMemoryOnly: true) - ) + let container = try modelContainer let context = container.mainContext let viewModelFirst = NewTaskView.ViewModel() let viewModelSecond = NewTaskView.ViewModel() @@ -237,3 +225,20 @@ struct NewTaskViewModelTests { } } + +// MARK: - Helpers + +private extension NewTaskViewModelTests { + + // MARK: Computed + + var modelContainer: ModelContainer { + get throws { + try ModelContainer( + for: Todo.self, + configurations: .init(isStoredInMemoryOnly: true) + ) + } + } + +} diff --git a/UnitTests/Tests/View Models/TaskGroupListViewModelTests.swift b/UnitTests/Tests/View Models/TaskGroupListViewModelTests.swift index 7ee98fd..4e5068c 100644 --- a/UnitTests/Tests/View Models/TaskGroupListViewModelTests.swift +++ b/UnitTests/Tests/View Models/TaskGroupListViewModelTests.swift @@ -37,11 +37,10 @@ struct TaskGroupListViewModelTests { ) func countWhenEmptyData(for group: TodoGroup) throws { // GIVEN - let tasks: [Todo] = [] let viewModel = TaskGroupListView.ViewModel() // WHEN - let count = viewModel.count(tasks, for: group) + let count = viewModel.count(emptyTasks, for: group) // THEN #expect(count == 0) @@ -53,13 +52,33 @@ struct TaskGroupListViewModelTests { ) func countWhenFilledData(for group: TodoGroup) throws { // GIVEN + let viewModel = TaskGroupListView.ViewModel() + + // WHEN + let count = viewModel.count(sampleTasks, for: group) + + // THEN + #expect(count == 2) + } + +} + +// MARK: - Helpers + +private extension TaskGroupListViewModelTests { + + // MARK: Computed + + var emptyTasks: [Todo] { [] } + + var sampleTasks: [Todo] { let today: Date = .now let tomorrow = calendar.date(byAdding: .day, value: 1, to: today) ?? today let yesterday = calendar.date(byAdding: .day, value: -1, to: today) ?? today let nextWeek = calendar.date(byAdding: .day, value: 7, to: today) ?? today let lastWeek = calendar.date(byAdding: .day, value: -7, to: today) ?? today - let tasks = [ + return [ Todo(title: "Today task #1", due: today), Todo(title: "Today task #2", due: today), Todo(title: "Tomorrow task #1", due: tomorrow), @@ -67,16 +86,8 @@ struct TaskGroupListViewModelTests { Todo(title: "Yesterday task #1", due: yesterday), Todo(title: "Last week task #1", due: lastWeek), Todo(title: "Completed task #1", due: tomorrow, completed: today), - Todo(title: "Completed task #2", due: yesterday, completed: today) + Todo(title: "Completed task #2", due: yesterday, completed: today), ] - - let viewModel = TaskGroupListView.ViewModel() - - // WHEN - let count = viewModel.count(tasks, for: group) - - // THEN - #expect(count == 2) } }