Skip to content

Commit

Permalink
unit tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
melnelen committed Nov 27, 2023
1 parent 83ea8d7 commit a69b36b
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
4 changes: 4 additions & 0 deletions FocusOn.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
1908539D2AE6A558005C20EE /* TaskCompletionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1908539C2AE6A558005C20EE /* TaskCompletionView.swift */; };
1908539F2AE6BE87005C20EE /* ConfettiView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1908539E2AE6BE87005C20EE /* ConfettiView.swift */; };
190853A12AE86D18005C20EE /* GoalCompletionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 190853A02AE86D18005C20EE /* GoalCompletionView.swift */; };
19AB32D72B14EB1200E8782B /* HistoryViewModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19AB32D62B14EB1200E8782B /* HistoryViewModelTests.swift */; };
19ACCBAA2A5AF82E00C08E93 /* SwiftUICharts in Frameworks */ = {isa = PBXBuildFile; productRef = 19ACCBA92A5AF82E00C08E93 /* SwiftUICharts */; };
19ACCBAC2A5EABC700C08E93 /* ProgressViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19ACCBAB2A5EABC700C08E93 /* ProgressViewModel.swift */; };
19B42F6F2A7EA14800FE7FD5 /* ProgressViewModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19B42F6E2A7EA14800FE7FD5 /* ProgressViewModelTests.swift */; };
Expand Down Expand Up @@ -57,6 +58,7 @@
1908539C2AE6A558005C20EE /* TaskCompletionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TaskCompletionView.swift; sourceTree = "<group>"; };
1908539E2AE6BE87005C20EE /* ConfettiView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfettiView.swift; sourceTree = "<group>"; };
190853A02AE86D18005C20EE /* GoalCompletionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GoalCompletionView.swift; sourceTree = "<group>"; };
19AB32D62B14EB1200E8782B /* HistoryViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HistoryViewModelTests.swift; sourceTree = "<group>"; };
19ACCBAB2A5EABC700C08E93 /* ProgressViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProgressViewModel.swift; sourceTree = "<group>"; };
19B42F6E2A7EA14800FE7FD5 /* ProgressViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProgressViewModelTests.swift; sourceTree = "<group>"; };
19EB49D52A577B1300AFDB90 /* FocusOn.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = FocusOn.xcdatamodel; sourceTree = "<group>"; };
Expand Down Expand Up @@ -165,6 +167,7 @@
children = (
D040192227EDFFEB0005CF35 /* TodayViewModelTests.swift */,
19B42F6E2A7EA14800FE7FD5 /* ProgressViewModelTests.swift */,
19AB32D62B14EB1200E8782B /* HistoryViewModelTests.swift */,
);
path = FocusOnTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -402,6 +405,7 @@
buildActionMask = 2147483647;
files = (
D040192327EDFFEB0005CF35 /* TodayViewModelTests.swift in Sources */,
19AB32D72B14EB1200E8782B /* HistoryViewModelTests.swift in Sources */,
19B42F6F2A7EA14800FE7FD5 /* ProgressViewModelTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
83 changes: 83 additions & 0 deletions FocusOnTests/HistoryViewModelTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//
// HistoryViewModelTests.swift
// FocusOnTests
//
// Created by Alexandra Ivanova on 27/11/2023.
//

import XCTest
@testable import FocusOn

final class HistoryViewModelTests: XCTestCase {
private var sut: HistoryViewModel!
private var mockDataService: DataServiceProtocol!

override func setUpWithError() throws {
try super.setUpWithError()
mockDataService = MockDataService()
sut = HistoryViewModel(dataService: mockDataService)
}

override func tearDownWithError() throws {
mockDataService = nil
sut = nil
try super.tearDownWithError()
}

func test_HistoryViewModel_FormattedGoalDate_withAValidGoalDate() {
// Given
let date = mockDataService.allGoals!.last!.createdAt
let expectedFormattedDate = "13 Oct 2023"

// When
let formattedDate = sut.formattedGoalDate(from: date)

// Then
XCTAssertEqual(formattedDate, expectedFormattedDate, "Formatted date does not match the expected result.")
}

func test_HistoryViewModel_GoalsForMonth_NumberOfGoals_MonthWithData() {
// Given
let allGoals = mockDataService.allGoals!

// When
let result = sut.goalsForMonth(goals: allGoals, month: "October 2023")

// Then
XCTAssertEqual(result.count, 11, "Incorrect number of goals for the month.")
}

func test_HistoryViewModel_GoalsForMonth_NumberOfGoals_MonthWithNoData() {
// Given
let allGoals = mockDataService.allGoals!

// When
let result = sut.goalsForMonth(goals: allGoals, month: "January 2022")

// Then
XCTAssertEqual(result.count, 0, "Incorrect number of goals for the month.")
}

func test_HistoryViewModel_GoalsForMonth_GoalsSorting() {
// Given
let allGoals = mockDataService.allGoals!

// When
let result = sut.goalsForMonth(goals: allGoals, month: "October 2023")
let firstGoal = result[0]
let secondGoal = result[1]

// Then
XCTAssertEqual(firstGoal.name, "Test goal 0", "Incorrect sorting order or filtering.")
XCTAssertEqual(secondGoal.name, "Test goal 2", "Incorrect sorting order or filtering.")
}

// Helper function to create a Date object with a specific month
private func createDate(month: Int) -> Date {
var components = DateComponents()
components.year = 2022
components.month = month
components.day = 1
return Calendar.current.date(from: components)!
}
}

0 comments on commit a69b36b

Please sign in to comment.