Skip to content

Instantly share code, notes, and snippets.

@mdb1
Last active April 24, 2025 13:41
Show Gist options
  • Save mdb1/64004c408e52e931b13cb846fa964f93 to your computer and use it in GitHub Desktop.
Save mdb1/64004c408e52e931b13cb846fa964f93 to your computer and use it in GitHub Desktop.
NavigationRouter - Unit Tests
import XCTest
final class NavigationRouterTests: XCTestCase {
private enum TestRoute: Hashable {
case home, detail, settings, profile
}
private var router: NavigationRouter<TestRoute>!
override func setUp() {
super.setUp()
router = NavigationRouter<TestRoute>()
}
override func tearDown() {
router = nil
super.tearDown()
}
func testPush_addsRouteToStack() {
router.push(.home)
XCTAssertEqual(router.navigationPath, [.home])
router.push(.detail)
XCTAssertEqual(router.navigationPath, [.home, .detail])
}
func testPop_removesLastRouteFromStack() {
router.push(.home)
router.push(.detail)
router.pop()
XCTAssertEqual(router.navigationPath, [.home])
}
func testPop_onEmptyStack_doesNothing() {
// Should not crash
router.pop()
XCTAssertTrue(router.navigationPath.isEmpty)
}
func testPopToRoot_clearsNavigationStack() {
router.push(.home)
router.push(.detail)
router.push(.settings)
router.popToRoot()
XCTAssertTrue(router.navigationPath.isEmpty)
}
func testPopTo_validRoute_popsCorrectly() {
router.push(.home)
router.push(.detail)
router.push(.settings)
router.push(.profile)
router.pop(to: .detail)
XCTAssertEqual(router.navigationPath, [.home, .detail])
}
func testPopTo_nonExistingRoute_doesNothing() {
router.push(.home)
router.push(.detail)
router.pop(to: .profile) // .profile was never pushed
XCTAssertEqual(router.navigationPath, [.home, .detail])
}
func testDebugDescription_emptyPath() {
XCTAssertEqual(router.pathDebugDescription, "Navigation Path: []")
}
func testDebugDescription_singleRoute() {
router.push(.home)
XCTAssertEqual(router.pathDebugDescription, "Navigation Path: [home]")
}
func testDebugDescription_multipleRoutes() {
router.push(.home)
router.push(.detail)
router.push(.settings)
XCTAssertEqual(router.pathDebugDescription, "Navigation Path: [home > detail > settings]")
}
func testDebugDescription_afterPop() {
router.push(.home)
router.push(.detail)
router.push(.settings)
router.pop()
XCTAssertEqual(router.pathDebugDescription, "Navigation Path: [home > detail]")
}
func testDebugDescription_afterPopToRoot() {
router.push(.home)
router.push(.detail)
router.popToRoot()
XCTAssertEqual(router.pathDebugDescription, "Navigation Path: []")
}
func testDebugDescription_afterPopToSpecificRoute() {
router.push(.home)
router.push(.detail)
router.push(.settings)
router.pop(to: .home)
XCTAssertEqual(router.pathDebugDescription, "Navigation Path: [home]")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment