mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 11:47:51 +00:00
Swift Testing for Unit Tests PART 1 (#5119)
* migrated a lot of unit tests to Swift Testing and added a new implementation for deferred fulfillment more tests migration Cleaned the code manually to establish some good patterns more code improvements some more code improvements removed empty tests update project * more pr suggestions and cleanups * removed the TestSetup pattern * fixing claude not reusing tests * pr suggestion + added indent rule to swiftformat so that we can prevent AIs to change that
This commit is contained in:
@@ -8,19 +8,20 @@
|
||||
|
||||
import Combine
|
||||
@testable import ElementX
|
||||
import XCTest
|
||||
import Testing
|
||||
|
||||
@MainActor
|
||||
class SecureBackupLogoutConfirmationScreenViewModelTests: XCTestCase {
|
||||
var viewModel: SecureBackupLogoutConfirmationScreenViewModel!
|
||||
var context: SecureBackupLogoutConfirmationScreenViewModel.Context {
|
||||
@Suite
|
||||
struct SecureBackupLogoutConfirmationScreenViewModelTests {
|
||||
private var viewModel: SecureBackupLogoutConfirmationScreenViewModel
|
||||
private var context: SecureBackupLogoutConfirmationScreenViewModel.Context {
|
||||
viewModel.context
|
||||
}
|
||||
|
||||
var secureBackupController: SecureBackupControllerMock!
|
||||
var reachabilitySubject: CurrentValueSubject<NetworkMonitorReachability, Never>!
|
||||
private var secureBackupController: SecureBackupControllerMock
|
||||
private var reachabilitySubject: CurrentValueSubject<NetworkMonitorReachability, Never>
|
||||
|
||||
override func setUp() {
|
||||
init() {
|
||||
secureBackupController = SecureBackupControllerMock()
|
||||
secureBackupController.underlyingKeyBackupState = CurrentValueSubject<SecureBackupKeyBackupState, Never>(.enabled).asCurrentValuePublisher()
|
||||
|
||||
@@ -30,36 +31,57 @@ class SecureBackupLogoutConfirmationScreenViewModelTests: XCTestCase {
|
||||
homeserverReachabilityPublisher: reachabilitySubject.asCurrentValuePublisher())
|
||||
}
|
||||
|
||||
func testInitialState() {
|
||||
XCTAssertEqual(context.viewState.mode, .saveRecoveryKey)
|
||||
@Test
|
||||
func initialState() {
|
||||
#expect(context.viewState.mode == .saveRecoveryKey)
|
||||
}
|
||||
|
||||
func testOngoingState() async throws {
|
||||
testInitialState()
|
||||
@Test
|
||||
func ongoingState() async throws {
|
||||
#expect(context.viewState.mode == .saveRecoveryKey)
|
||||
|
||||
let progressExpectation = expectation(description: "The upload progress callback should be called.")
|
||||
secureBackupController.waitForKeyBackupUploadUploadStateSubjectClosure = { stateSubject in
|
||||
try? await Task.sleep(for: .seconds(4))
|
||||
stateSubject.send(.uploading(uploadedKeyCount: 50, totalKeyCount: 100))
|
||||
progressExpectation.fulfill()
|
||||
return .success(())
|
||||
try await confirmation { confirmation in
|
||||
secureBackupController.waitForKeyBackupUploadUploadStateSubjectClosure = { stateSubject in
|
||||
try? await Task.sleep(for: .seconds(4))
|
||||
stateSubject.send(.uploading(uploadedKeyCount: 50, totalKeyCount: 100))
|
||||
confirmation()
|
||||
return .success(())
|
||||
}
|
||||
|
||||
let deferredWaiting = deferFulfillment(context.observe(\.viewState.mode)) { $0 == .waitingToStart(hasStalled: false) }
|
||||
context.send(viewAction: .logout)
|
||||
_ = try await deferredWaiting.fulfill()
|
||||
|
||||
// Wait for the 2-second timeout.
|
||||
let deferredHasStalled = deferFulfillment(context.observe(\.viewState.mode)) { $0 == .waitingToStart(hasStalled: true) }
|
||||
_ = try await deferredHasStalled.fulfill()
|
||||
|
||||
try await deferFulfillment(context.observe(\.viewState.mode)) { $0 == .backupOngoing(progress: 0.5) }.fulfill()
|
||||
}
|
||||
|
||||
let deferredWaiting = deferFulfillment(context.observe(\.viewState.mode)) { $0 == .waitingToStart(hasStalled: false) }
|
||||
context.send(viewAction: .logout)
|
||||
try await deferredWaiting.fulfill()
|
||||
|
||||
// Wait for the 2-second timeout.
|
||||
let deferredHasStalled = deferFulfillment(context.observe(\.viewState.mode)) { $0 == .waitingToStart(hasStalled: true) }
|
||||
try await deferredHasStalled.fulfill()
|
||||
|
||||
// Wait for the progress to be reported.
|
||||
await fulfillment(of: [progressExpectation])
|
||||
XCTAssertEqual(context.viewState.mode, .backupOngoing(progress: 0.5))
|
||||
}
|
||||
|
||||
func testOfflineState() async throws {
|
||||
try await testOngoingState()
|
||||
@Test
|
||||
func offlineState() async throws {
|
||||
#expect(context.viewState.mode == .saveRecoveryKey)
|
||||
|
||||
try await confirmation { confirmation in
|
||||
secureBackupController.waitForKeyBackupUploadUploadStateSubjectClosure = { stateSubject in
|
||||
try? await Task.sleep(for: .seconds(4))
|
||||
stateSubject.send(.uploading(uploadedKeyCount: 50, totalKeyCount: 100))
|
||||
confirmation()
|
||||
return .success(())
|
||||
}
|
||||
|
||||
let deferredWaiting = deferFulfillment(context.observe(\.viewState.mode)) { $0 == .waitingToStart(hasStalled: false) }
|
||||
context.send(viewAction: .logout)
|
||||
try await deferredWaiting.fulfill()
|
||||
|
||||
// Wait for the 2-second timeout.
|
||||
let deferredHasStalled = deferFulfillment(context.observe(\.viewState.mode)) { $0 == .waitingToStart(hasStalled: true) }
|
||||
try await deferredHasStalled.fulfill()
|
||||
|
||||
try await deferFulfillment(context.observe(\.viewState.mode)) { $0 == .backupOngoing(progress: 0.5) }.fulfill()
|
||||
}
|
||||
|
||||
let deferred = deferFulfillment(context.observe(\.viewState.mode)) { $0 == .offline }
|
||||
reachabilitySubject.send(.unreachable)
|
||||
|
||||
Reference in New Issue
Block a user