Skip to content

Commit

Permalink
fix(unit tests): Change main actor isolation in the unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
erikdrobne committed Mar 24, 2024
1 parent 5f38de3 commit 1f047fb
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 19 deletions.
13 changes: 8 additions & 5 deletions Tests/SwiftUICoordinatorTests/CoordinatorTests.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
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)
XCTAssertNil(sut.parent)
XCTAssertTrue(sut.childCoordinators.isEmpty)
}

@MainActor
func test_addChildCoordinator() {
let navigationController = NavigationController()
let sut = MockAppCoordinator(window: UIWindow(), navigationController: navigationController)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions Tests/SwiftUICoordinatorTests/DeepLinkHandlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand All @@ -24,6 +24,7 @@ final class DeepLinkHandlerTests: XCTestCase {
}
}

@MainActor
func test_linkForURLThrowsUnknownURLError() {
let scheme = "myapp"
let url = URL(string: "\(scheme)://")
Expand All @@ -37,6 +38,7 @@ final class DeepLinkHandlerTests: XCTestCase {
}
}

@MainActor
func test_linkForURLReturnsNil() {
let scheme = "myapp"
let action = "square"
Expand All @@ -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"
Expand All @@ -58,6 +61,7 @@ final class DeepLinkHandlerTests: XCTestCase {
}(), "Link for URL threw an error.")
}

@MainActor
func test_paramsThrowsMissingQueryStringError() {
let scheme = "myapp"
let action = "circle"
Expand All @@ -73,6 +77,7 @@ final class DeepLinkHandlerTests: XCTestCase {
}
}

@MainActor
func test_paramsReturnsEmptyCollection() {
let scheme = "myapp"
let action = "circle"
Expand All @@ -85,6 +90,7 @@ final class DeepLinkHandlerTests: XCTestCase {
}(), "Params for url threw an error.")
}

@MainActor
func test_paramsForURLSuccess() {
let scheme = "myapp"
let action = "rectangle"
Expand Down
5 changes: 3 additions & 2 deletions Tests/SwiftUICoordinatorTests/NavigationControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions Tests/SwiftUICoordinatorTests/NavigatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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])
Expand Down
4 changes: 2 additions & 2 deletions Tests/SwiftUICoordinatorTests/RootCoordinatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import XCTest
@testable import SwiftUICoordinator

@MainActor
final class RootCoordinatorTests: XCTestCase {


@MainActor
func test_rootViewControllerInitialization() {
let navigationController = NavigationController()
let window = UIWindow()
Expand Down
5 changes: 3 additions & 2 deletions Tests/SwiftUICoordinatorTests/TransitionProviderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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!)
Expand Down
4 changes: 2 additions & 2 deletions Tests/SwiftUICoordinatorTests/TransitionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down

0 comments on commit 1f047fb

Please sign in to comment.