2026-02-10 15:31:52 +00:00
|
|
|
import Foundation
|
|
|
|
|
import SwiftData
|
2026-02-11 13:00:46 +00:00
|
|
|
import SwiftUI
|
2026-02-10 15:31:52 +00:00
|
|
|
import Testing
|
|
|
|
|
|
|
|
|
|
@testable import Todos
|
|
|
|
|
|
|
|
|
|
@Suite("TaskList view model tests")
|
|
|
|
|
struct TaskListViewModelTests {
|
|
|
|
|
|
2026-02-11 13:00:46 +00:00
|
|
|
// MARK: Initializers tests
|
2026-02-10 15:31:52 +00:00
|
|
|
|
|
|
|
|
@Test(
|
2026-02-11 13:00:46 +00:00
|
|
|
"properties are set correctly during initialization",
|
|
|
|
|
arguments: TodoGroup.allCases
|
|
|
|
|
)
|
|
|
|
|
func initialization(for group: TodoGroup) {
|
|
|
|
|
// GIVEN
|
|
|
|
|
// WHEN
|
|
|
|
|
let viewModel = TaskListView.ViewModel(group: group)
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
#expect(viewModel.group == group)
|
|
|
|
|
#expect(viewModel.showCompleted == false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Computed property tests
|
|
|
|
|
|
|
|
|
|
@Test(
|
|
|
|
|
"buttonCompleted returns correct values when showCompleted is true",
|
|
|
|
|
arguments: TodoGroup.allCases, [true, false]
|
|
|
|
|
)
|
|
|
|
|
func buttonCompleted(
|
|
|
|
|
for group: TodoGroup,
|
|
|
|
|
showCompleted: Bool
|
|
|
|
|
) {
|
|
|
|
|
// GIVEN
|
|
|
|
|
let viewModel = TaskListView.ViewModel(group: group)
|
|
|
|
|
|
|
|
|
|
viewModel.showCompleted = showCompleted
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
let (title, icon) = viewModel.buttonCompleted
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
if showCompleted {
|
|
|
|
|
#expect(title == "view.task-list.label.hide-completed")
|
|
|
|
|
#expect(icon == "eye.slash")
|
|
|
|
|
} else {
|
|
|
|
|
#expect(title == "view.task-list.label.show-completed")
|
|
|
|
|
#expect(icon == "eye")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test(
|
|
|
|
|
"canShowCompleted returns correct value for each group",
|
|
|
|
|
arguments: zip(
|
|
|
|
|
TodoGroup.allCases,
|
|
|
|
|
[true, true, true, false]
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
func canShowCompleted(
|
|
|
|
|
for group: TodoGroup,
|
|
|
|
|
expects expectation: Bool
|
|
|
|
|
) {
|
|
|
|
|
// GIVEN
|
|
|
|
|
let viewModel = TaskListView.ViewModel(group: group)
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
let canShowCompleted = viewModel.canShowCompleted
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
#expect(canShowCompleted == expectation)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test(
|
|
|
|
|
"navigationTitle returns correct view title for each group",
|
2026-02-10 15:31:52 +00:00
|
|
|
arguments: TodoGroup.allCases
|
|
|
|
|
)
|
|
|
|
|
func navigationTitle(for group: TodoGroup) {
|
|
|
|
|
// GIVEN
|
|
|
|
|
let viewModel = TaskListView.ViewModel(group: group)
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
let title = viewModel.navigationTitle
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
#expect(title == group.title)
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-11 13:00:46 +00:00
|
|
|
// MARK: Method tests
|
|
|
|
|
|
|
|
|
|
@Test("toggleCompletedVisbility toggles the showCompleted property")
|
|
|
|
|
func toggleCompletedVisibility() {
|
|
|
|
|
// GIVEN
|
|
|
|
|
let viewModel = TaskListView.ViewModel(group: .today)
|
|
|
|
|
let initialValue = viewModel.showCompleted
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
viewModel.toggleCompletedVisibility()
|
|
|
|
|
let afterFirstToggle = viewModel.showCompleted
|
|
|
|
|
|
|
|
|
|
viewModel.toggleCompletedVisibility()
|
|
|
|
|
let afterSecondToggle = viewModel.showCompleted
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
#expect(initialValue == false)
|
|
|
|
|
#expect(afterFirstToggle == true)
|
|
|
|
|
#expect(afterSecondToggle == false)
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-10 15:31:52 +00:00
|
|
|
}
|