From 14b0d31884132ab94e6a04a28417dfe6639564bd Mon Sep 17 00:00:00 2001 From: Anh Do Date: Fri, 12 Apr 2024 00:48:52 -0400 Subject: [PATCH] Add tests --- .../xcshareddata/swiftpm/Package.resolved | 2 +- .../TunnelControllerViewModel.swift | 2 +- .../TunnelControllerViewModelTests.swift | 26 ++++++++++++++++++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/DuckDuckGo.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/DuckDuckGo.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index e59793995f..dcc3605227 100644 --- a/DuckDuckGo.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/DuckDuckGo.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -33,7 +33,7 @@ "location" : "https://github.com/duckduckgo/BrowserServicesKit", "state" : { "branch" : "anh/netp/screen-improvements", - "revision" : "ae13b6ab3880db92776938ae1be4aceb4a3b4ea1" + "revision" : "3b354a70cdfff5d46d2c8f525748dee63a36ad88" } }, { diff --git a/LocalPackages/NetworkProtectionMac/Sources/NetworkProtectionUI/Views/TunnelControllerView/TunnelControllerViewModel.swift b/LocalPackages/NetworkProtectionMac/Sources/NetworkProtectionUI/Views/TunnelControllerView/TunnelControllerViewModel.swift index bbc985ba0f..e28b2aed75 100644 --- a/LocalPackages/NetworkProtectionMac/Sources/NetworkProtectionUI/Views/TunnelControllerView/TunnelControllerViewModel.swift +++ b/LocalPackages/NetworkProtectionMac/Sources/NetworkProtectionUI/Views/TunnelControllerView/TunnelControllerViewModel.swift @@ -23,7 +23,7 @@ import SwiftUI @MainActor public final class TunnelControllerViewModel: ObservableObject { - public struct FormattedDataVolume { + public struct FormattedDataVolume: Equatable { public let dataSent: String public let dataReceived: String } diff --git a/LocalPackages/NetworkProtectionMac/Tests/NetworkProtectionUITests/TunnelControllerViewModelTests.swift b/LocalPackages/NetworkProtectionMac/Tests/NetworkProtectionUITests/TunnelControllerViewModelTests.swift index 884dc3d002..e7785da3f4 100644 --- a/LocalPackages/NetworkProtectionMac/Tests/NetworkProtectionUITests/TunnelControllerViewModelTests.swift +++ b/LocalPackages/NetworkProtectionMac/Tests/NetworkProtectionUITests/TunnelControllerViewModelTests.swift @@ -36,12 +36,14 @@ final class TunnelControllerViewModelTests: XCTestCase { let connectionErrorObserver: ConnectionErrorObserver let connectivityIssuesObserver: ConnectivityIssueObserver let controllerErrorMessageObserver: ControllerErrorMesssageObserver + let dataVolumeObserver: DataVolumeObserver init(status: ConnectionStatus, isHavingConnectivityIssues: Bool = false, serverInfo: NetworkProtectionStatusServerInfo = MockStatusReporter.defaultServerInfo, tunnelErrorMessage: String? = nil, - controllerErrorMessage: String? = nil) { + controllerErrorMessage: String? = nil, + dataVolume: DataVolume = .init()) { let mockStatusObserver = MockConnectionStatusObserver() mockStatusObserver.subject.send(status) @@ -62,6 +64,10 @@ final class TunnelControllerViewModelTests: XCTestCase { let mockControllerErrorMessageObserver = MockControllerErrorMesssageObserver() mockControllerErrorMessageObserver.subject.send(controllerErrorMessage) controllerErrorMessageObserver = mockControllerErrorMessageObserver + + let mockDataVolumeObserver = MockDataVolumeObserver() + mockDataVolumeObserver.subject.send(dataVolume) + dataVolumeObserver = mockDataVolumeObserver } func forceRefresh() { @@ -189,6 +195,24 @@ final class TunnelControllerViewModelTests: XCTestCase { XCTAssertFalse(model.showServerDetails) } + /// We expect the model to properly reflect the data volume. + /// + @MainActor + func testProperlyReflectsDataVolume() async throws { + let controller = MockTunnelController() + let statusReporter = MockStatusReporter(status: .connected(connectedDate: Date()), + dataVolume: .init(bytesSent: 512000, bytesReceived: 1024000)) + let model = TunnelControllerViewModel( + controller: controller, + onboardingStatusPublisher: Just(OnboardingStatus.completed).eraseToAnyPublisher(), + statusReporter: statusReporter, + vpnSettings: .init(defaults: .standard), + locationFormatter: MockVPNLocationFormatter(), + appLauncher: MockAppLauncher()) + + XCTAssertEqual(model.formattedDataVolume, .init(dataSent: "512 KB", dataReceived: "1 MB")) + } + /// We expect that setting the model's `isRunning` to `true`, will start the VPN. /// @MainActor