From 1f047fb36e7403ec0636b24ce1706b34f446ea51 Mon Sep 17 00:00:00 2001 From: Erik Drobne Date: Sun, 24 Mar 2024 12:10:22 +0100 Subject: [PATCH] fix(unit tests): Change main actor isolation in the unit tests --- .../SwiftUICoordinatorTests/CoordinatorTests.swift | 13 ++++++++----- .../DeepLinkHandlerTests.swift | 10 ++++++++-- .../NavigationControllerTests.swift | 5 +++-- ...NavigationControllerTransitionHandlerTests.swift | 5 +++-- Tests/SwiftUICoordinatorTests/NavigatorTests.swift | 8 ++++++-- .../RootCoordinatorTests.swift | 4 ++-- .../TransitionProviderTests.swift | 5 +++-- Tests/SwiftUICoordinatorTests/TransitionTests.swift | 4 ++-- 8 files changed, 35 insertions(+), 19 deletions(-) diff --git a/Tests/SwiftUICoordinatorTests/CoordinatorTests.swift b/Tests/SwiftUICoordinatorTests/CoordinatorTests.swift index 2a1c490..a29daad 100644 --- a/Tests/SwiftUICoordinatorTests/CoordinatorTests.swift +++ b/Tests/SwiftUICoordinatorTests/CoordinatorTests.swift @@ -1,10 +1,9 @@ import XCTest import Foundation @testable import SwiftUICoordinator - -@MainActor -final class CoordinatorTests: XCTestCase { - + +final class CoordinatorTests: XCTestCase { + @MainActor func test_coordinatorInitialState() { let navigationController = NavigationController() let sut = MockCoordinator(parent: nil, startRoute: .circle, navigationController: navigationController) @@ -12,6 +11,7 @@ final class CoordinatorTests: XCTestCase { XCTAssertTrue(sut.childCoordinators.isEmpty) } + @MainActor func test_addChildCoordinator() { let navigationController = NavigationController() let sut = MockAppCoordinator(window: UIWindow(), navigationController: navigationController) @@ -26,6 +26,7 @@ final class CoordinatorTests: XCTestCase { XCTAssert(sut.childCoordinators.first?.coordinator === coordinator) } + @MainActor func test_addMultipleChildrenToCoordinator() { let navigationController = NavigationController() let sut = MockAppCoordinator(window: UIWindow(), navigationController: navigationController) @@ -48,6 +49,7 @@ final class CoordinatorTests: XCTestCase { XCTAssert(sut.childCoordinators.first?.coordinator === coordinator) } + @MainActor func test_removeChildCoordinator() { let navigationController = NavigationController() let sut = MockAppCoordinator(window: UIWindow(), navigationController: navigationController) @@ -62,7 +64,8 @@ final class CoordinatorTests: XCTestCase { XCTAssertTrue(sut.childCoordinators.isEmpty) } - + + @MainActor func test_coordinatorDoesNotRetainChildCoordinators() { let navigationController = NavigationController() let sut = MockAppCoordinator(window: UIWindow(), navigationController: navigationController) diff --git a/Tests/SwiftUICoordinatorTests/DeepLinkHandlerTests.swift b/Tests/SwiftUICoordinatorTests/DeepLinkHandlerTests.swift index f318c50..a2c98c4 100644 --- a/Tests/SwiftUICoordinatorTests/DeepLinkHandlerTests.swift +++ b/Tests/SwiftUICoordinatorTests/DeepLinkHandlerTests.swift @@ -8,9 +8,9 @@ import XCTest @testable import SwiftUICoordinator -@MainActor final class DeepLinkHandlerTests: XCTestCase { - + + @MainActor func test_linkForURLThrowsInvalidSchemeError() { let host = "circle" let url = URL(string: "://\(host)") @@ -24,6 +24,7 @@ final class DeepLinkHandlerTests: XCTestCase { } } + @MainActor func test_linkForURLThrowsUnknownURLError() { let scheme = "myapp" let url = URL(string: "\(scheme)://") @@ -37,6 +38,7 @@ final class DeepLinkHandlerTests: XCTestCase { } } + @MainActor func test_linkForURLReturnsNil() { let scheme = "myapp" let action = "square" @@ -45,6 +47,7 @@ final class DeepLinkHandlerTests: XCTestCase { XCTAssertNil(try MockDeepLinkHandler.shared.link(for: XCTUnwrap(url))) } + @MainActor func test_linkForURLSuccess() { let scheme = "myapp" let action = "circle" @@ -58,6 +61,7 @@ final class DeepLinkHandlerTests: XCTestCase { }(), "Link for URL threw an error.") } + @MainActor func test_paramsThrowsMissingQueryStringError() { let scheme = "myapp" let action = "circle" @@ -73,6 +77,7 @@ final class DeepLinkHandlerTests: XCTestCase { } } + @MainActor func test_paramsReturnsEmptyCollection() { let scheme = "myapp" let action = "circle" @@ -85,6 +90,7 @@ final class DeepLinkHandlerTests: XCTestCase { }(), "Params for url threw an error.") } + @MainActor func test_paramsForURLSuccess() { let scheme = "myapp" let action = "rectangle" diff --git a/Tests/SwiftUICoordinatorTests/NavigationControllerTests.swift b/Tests/SwiftUICoordinatorTests/NavigationControllerTests.swift index 45fc9b1..c6d43e6 100644 --- a/Tests/SwiftUICoordinatorTests/NavigationControllerTests.swift +++ b/Tests/SwiftUICoordinatorTests/NavigationControllerTests.swift @@ -9,14 +9,15 @@ import Foundation import XCTest @testable import SwiftUICoordinator -@MainActor final class NavigationControllerTests: XCTestCase { - + + @MainActor func test_navigationBarIsHiddenByDefault() { let sut = NavigationController() XCTAssertTrue(sut.isNavigationBarHidden) } + @MainActor func test_navigationBarIsNotHidden() { let sut = NavigationController(isNavigationBarHidden: false) XCTAssertFalse(sut.isNavigationBarHidden) diff --git a/Tests/SwiftUICoordinatorTests/NavigationControllerTransitionHandlerTests.swift b/Tests/SwiftUICoordinatorTests/NavigationControllerTransitionHandlerTests.swift index b74157a..2bdfb1a 100644 --- a/Tests/SwiftUICoordinatorTests/NavigationControllerTransitionHandlerTests.swift +++ b/Tests/SwiftUICoordinatorTests/NavigationControllerTransitionHandlerTests.swift @@ -8,9 +8,9 @@ import XCTest @testable import SwiftUICoordinator -@MainActor final class NavigationControllerTransitionHandlerTests: XCTestCase { - + + @MainActor func test_animationForEligibleRoutes() { let provider = TransitionProvider(transitions: [MockTransition(), MockTransition()]) let handler = NavigationControllerTransitionHandler(provider: provider) @@ -27,6 +27,7 @@ final class NavigationControllerTransitionHandlerTests: XCTestCase { XCTAssertTrue(sut is MockTransition) } + @MainActor func test_animationForNoMatchingTransitions() { let provider = TransitionProvider(transitions: []) let handler = NavigationControllerTransitionHandler(provider: provider) diff --git a/Tests/SwiftUICoordinatorTests/NavigatorTests.swift b/Tests/SwiftUICoordinatorTests/NavigatorTests.swift index f1b6f3f..5da3039 100644 --- a/Tests/SwiftUICoordinatorTests/NavigatorTests.swift +++ b/Tests/SwiftUICoordinatorTests/NavigatorTests.swift @@ -9,9 +9,9 @@ import XCTest import Foundation @testable import SwiftUICoordinator -@MainActor final class NavigatorTests: XCTestCase { - + + @MainActor func test_showRouteThrowsError() { let sut = MockCoordinator(parent: nil, startRoute: .circle, navigationController: NavigationController()) XCTAssertNoThrow(try sut.start()) @@ -31,23 +31,27 @@ final class NavigatorTests: XCTestCase { } } + @MainActor func test_showRouteNoThrow() { let sut = MockCoordinator(parent: nil, startRoute: .circle, navigationController: NavigationController()) XCTAssertNoThrow(try sut.start()) } + @MainActor func test_setRoutes() { let sut = MockCoordinator(parent: nil, startRoute: .circle, navigationController: NavigationController()) sut.set(routes: [.rectangle, .rectangle]) XCTAssertEqual(sut.viewControllers.count, 2) } + @MainActor func test_appendRoutes() { let sut = MockCoordinator(parent: nil, startRoute: .circle, navigationController: NavigationController()) sut.append(routes: [.rectangle, .circle]) XCTAssertEqual(sut.viewControllers.count, 2) } + @MainActor func test_popToRoot() { let sut = MockCoordinator(parent: nil, startRoute: .circle, navigationController: NavigationController()) sut.append(routes: [.rectangle, .circle]) diff --git a/Tests/SwiftUICoordinatorTests/RootCoordinatorTests.swift b/Tests/SwiftUICoordinatorTests/RootCoordinatorTests.swift index a8a3654..77b719e 100644 --- a/Tests/SwiftUICoordinatorTests/RootCoordinatorTests.swift +++ b/Tests/SwiftUICoordinatorTests/RootCoordinatorTests.swift @@ -8,9 +8,9 @@ import XCTest @testable import SwiftUICoordinator -@MainActor final class RootCoordinatorTests: XCTestCase { - + + @MainActor func test_rootViewControllerInitialization() { let navigationController = NavigationController() let window = UIWindow() diff --git a/Tests/SwiftUICoordinatorTests/TransitionProviderTests.swift b/Tests/SwiftUICoordinatorTests/TransitionProviderTests.swift index 228b80d..5f4b697 100644 --- a/Tests/SwiftUICoordinatorTests/TransitionProviderTests.swift +++ b/Tests/SwiftUICoordinatorTests/TransitionProviderTests.swift @@ -8,9 +8,9 @@ import XCTest @testable import SwiftUICoordinator -@MainActor class TransitionProviderTests: XCTestCase { - + + @MainActor func test_transitionProviderInitialization() { let transitions = [MockTransition(), MockTransition()] let sut = TransitionProvider(transitions: transitions) @@ -19,6 +19,7 @@ class TransitionProviderTests: XCTestCase { XCTAssertNotNil(sut.transitions.first?.transition) } + @MainActor func test_weakTransitionDoesNotRetain() { var transition: Transitionable? = MockTransition() let weakTransition: WeakTransition? = WeakTransition(transition!) diff --git a/Tests/SwiftUICoordinatorTests/TransitionTests.swift b/Tests/SwiftUICoordinatorTests/TransitionTests.swift index c15aeb6..9aed14f 100644 --- a/Tests/SwiftUICoordinatorTests/TransitionTests.swift +++ b/Tests/SwiftUICoordinatorTests/TransitionTests.swift @@ -9,9 +9,9 @@ import XCTest import Foundation @testable import SwiftUICoordinator -@MainActor final class TransitionTests: XCTestCase { - + + @MainActor func test_registerTransitions() { let transition = MockTransition() let provider = TransitionProvider(transitions: [transition])