From e0386be125707ccadd0644c0465837703924051d Mon Sep 17 00:00:00 2001 From: Bartek Waresiak Date: Fri, 12 Apr 2024 10:45:22 +0200 Subject: [PATCH] Patch the tests --- .../SyncManagementViewModelTests.swift | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/DuckDuckGoTests/SyncManagementViewModelTests.swift b/DuckDuckGoTests/SyncManagementViewModelTests.swift index 49c2a89f9e..92c25d61a2 100644 --- a/DuckDuckGoTests/SyncManagementViewModelTests.swift +++ b/DuckDuckGoTests/SyncManagementViewModelTests.swift @@ -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) @@ -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 ]) } @@ -136,6 +147,7 @@ class SyncManagementViewModelTests: XCTestCase, SyncManagementViewModelDelegate // MARK: Delegate functions func authenticateUser() async throws { + monitor.incrementCalls(function: #function.cleaningFunctionName()) } func showSyncWithAnotherDeviceEnterText() { @@ -216,7 +228,7 @@ class SyncManagementViewModelTests: XCTestCase, SyncManagementViewModelDelegate private class Monitor { - 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.