Skip to content

Commit

Permalink
Patch the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bwaresiak committed Apr 12, 2024
1 parent be3eed9 commit e0386be
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions DuckDuckGoTests/SyncManagementViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ class SyncManagementViewModelTests: XCTestCase, SyncManagementViewModelDelegate
var createAccountAndStartSyncingCalled = false
var capturedOptionModel: SyncSettingsViewModel?

func waitForInvocation() {
let expectation = expectation(description: "Inv")
let cancellable = monitor.$functionCalls.dropFirst().sink { val in
print(val)
expectation.fulfill()
}
wait(for: [expectation], timeout: 5)
}

func testWhenSingleDeviceSetUpPressed_ThenManagerBecomesBusy_AndAccounCreationRequested() {
model.startSyncPressed()
XCTAssertTrue(model.isBusy)
Expand All @@ -54,11 +63,13 @@ class SyncManagementViewModelTests: XCTestCase, SyncManagementViewModelDelegate

func testWhenScanQRCodePressed_ThenSyncWithAnotherDeviceViewIsShown() {
model.scanQRCode()
waitForInvocation()

// You can either test one individual call was made x number of times or check for a whole number of calls
monitor.assert(#selector(showSyncWithAnotherDevice).description, calls: 1)
// async functions selector description apparently contain 'WithCompletionHandler'
monitor.assert(#selector(authenticateUser).description.dropping(suffix: "WithCompletionHandler:"), calls: 1)
monitor.assertCalls([
#selector(showSyncWithAnotherDevice).description: 1
#selector(authenticateUser).description.dropping(suffix: "WithCompletionHandler:"): 1
])
}

Expand Down Expand Up @@ -136,6 +147,7 @@ class SyncManagementViewModelTests: XCTestCase, SyncManagementViewModelDelegate
// MARK: Delegate functions

func authenticateUser() async throws {
monitor.incrementCalls(function: #function.cleaningFunctionName())
}

func showSyncWithAnotherDeviceEnterText() {
Expand Down Expand Up @@ -216,7 +228,7 @@ class SyncManagementViewModelTests: XCTestCase, SyncManagementViewModelDelegate

private class Monitor<T> {

var functionCalls = [String: Int]()
@Published var functionCalls = [String: Int]()

/// Whatever is passed as function is used as the key, the same key should be used for assertions.
/// Use `String.cleaningFunctionName()` with `#function` but be aware that overloaded function names will not be tracked accurately.
Expand Down

0 comments on commit e0386be

Please sign in to comment.