Files
suixinkan_ios_uikit/suixinkan_iosTests/NavigationRouterTests.swift
汉秋 1a6d4a1791 Fix home navigation bar visibility and hide scan tab title.
Home uses a custom top bar, so TabNavigationController now syncs system nav bar visibility with the stack top; remove the scan tab text label to show icon only.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-29 08:59:31 +08:00

186 lines
7.7 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// NavigationRouterTests.swift
// suixinkanTests
//
// Created by Codex on 2026/6/22.
//
import UIKit
import XCTest
@testable import suixinkan_ios
/// UIKit Tab 宿 push
@MainActor
final class AppNavigatorTests: XCTestCase {
/// push TabBar
func testPushUsesCurrentNavigationStack() {
let services = AppServices(apiClient: APIClient(session: AppNavigatorRecordingURLSession()))
let host = AppNavigatorTestHost(services: services)
let navigator = AppNavigator()
navigator.attach(host: host)
navigator.push(.home(.paymentCollection), from: nil, animated: false)
let homeStack = host.navigationController(for: .home)!
XCTAssertEqual(homeStack.viewControllers.count, 2)
XCTAssertTrue(homeStack.viewControllers.last?.hidesBottomBarWhenPushed == true)
XCTAssertTrue(homeStack.viewControllers.last is PaymentCollectionViewController)
}
/// push
func testHomeNavigationBarVisibilityFollowsTopViewController() {
let services = AppServices(apiClient: APIClient(session: AppNavigatorRecordingURLSession()))
let navigationController = TabNavigationController(tab: .home, services: services)
XCTAssertTrue(navigationController.isNavigationBarHidden)
navigationController.push(route: .home(.paymentCollection), animated: false)
XCTAssertFalse(navigationController.isNavigationBarHidden)
navigationController.popToRootViewController(animated: false)
XCTAssertTrue(navigationController.isNavigationBarHidden)
}
/// tabRoot Tab
func testTabRootSelectsTabResetsStackAndPushesRoute() {
let services = AppServices(apiClient: APIClient(session: AppNavigatorRecordingURLSession()))
let host = AppNavigatorTestHost(services: services)
let navigator = AppNavigator()
navigator.attach(host: host)
host.navigationController(for: .home)?.push(route: .home(.wallet), animated: false)
navigator.openHome(.taskManagement, policy: .tabRoot(.home), animated: false)
let homeStack = host.navigationController(for: .home)!
XCTAssertEqual(host.selectedTab, .home)
XCTAssertEqual(homeStack.viewControllers.count, 2)
XCTAssertTrue(homeStack.viewControllers.last is TaskManagementViewController)
}
/// preserveTab Tab
func testPreserveTabKeepsExistingStack() {
let services = AppServices(apiClient: APIClient(session: AppNavigatorRecordingURLSession()))
let host = AppNavigatorTestHost(services: services)
let navigator = AppNavigator()
navigator.attach(host: host)
host.navigationController(for: .profile)?.push(route: .profile(.settings), animated: false)
navigator.openProfile(.realNameAuth, policy: .preserveTab(.profile), animated: false)
let profileStack = host.navigationController(for: .profile)!
XCTAssertEqual(host.selectedTab, .profile)
XCTAssertEqual(profileStack.viewControllers.count, 3)
XCTAssertTrue(profileStack.viewControllers[1] is SettingsViewController)
XCTAssertTrue(profileStack.viewControllers.last is RealNameAuthViewController)
}
/// 宿 attach
func testPendingActionsRunAfterAttachInFIFOOrder() {
let services = AppServices(apiClient: APIClient(session: AppNavigatorRecordingURLSession()))
let host = AppNavigatorTestHost(services: services)
let navigator = AppNavigator()
navigator.openHome(.paymentCollection, policy: .tabRoot(.home), animated: false)
navigator.openHome(.messageCenter, policy: .preserveTab(.home), animated: false)
navigator.attach(host: host)
let homeStack = host.navigationController(for: .home)!
XCTAssertEqual(homeStack.viewControllers.count, 3)
XCTAssertTrue(homeStack.viewControllers[1] is PaymentCollectionViewController)
XCTAssertTrue(homeStack.viewControllers.last is MessageCenterViewController)
}
/// resetAllStacks 宿
func testResetAllStacksClearsPendingAndMountedStacks() {
let services = AppServices(apiClient: APIClient(session: AppNavigatorRecordingURLSession()))
let host = AppNavigatorTestHost(services: services)
let navigator = AppNavigator()
navigator.openHome(.paymentCollection, policy: .tabRoot(.home), animated: false)
navigator.resetAllStacks()
navigator.attach(host: host)
XCTAssertEqual(host.navigationController(for: .home)?.viewControllers.count, 1)
navigator.openHome(.wallet, policy: .tabRoot(.home), animated: false)
navigator.resetAllStacks()
XCTAssertEqual(host.navigationController(for: .home)?.viewControllers.count, 1)
XCTAssertEqual(host.selectedTab, .home)
}
/// Tab
func testOpenOrdersEntryAppliesRootStateAndScanCode() {
let services = AppServices(apiClient: APIClient(session: AppNavigatorRecordingURLSession()))
let host = AppNavigatorTestHost(services: services)
let navigator = AppNavigator()
navigator.attach(host: host)
navigator.openOrdersEntry(.verificationOrders, scannedCode: "VERIFY001")
XCTAssertEqual(host.selectedTab, .orders)
XCTAssertEqual(host.appliedOrdersEntry, .verificationOrders)
XCTAssertEqual(host.appliedScanCode, "VERIFY001")
XCTAssertEqual(host.navigationController(for: .orders)?.viewControllers.count, 1)
}
}
/// AppNavigator 宿 Tab
@MainActor
private final class AppNavigatorTestHost: AppNavigationHost {
private let services: AppServices
private var controllers: [AppTab: TabNavigationController] = [:]
var selectedTab: AppTab = .home
var appliedOrdersEntry: OrdersEntry?
var appliedScanCode: String?
/// Tab
init(services: AppServices) {
self.services = services
for tab in AppTab.allCases {
controllers[tab] = TabNavigationController(tab: tab, services: services)
}
}
/// Tab
func selectTab(_ tab: AppTab) {
selectedTab = tab
}
/// Tab
func navigationController(for tab: AppTab) -> TabNavigationController? {
controllers[tab]
}
/// Tab
func selectedNavigationController() -> TabNavigationController? {
controllers[selectedTab]
}
/// Tab
func resetAllStacks() {
for controller in controllers.values {
controller.popToRootViewController(animated: false)
}
selectedTab = .home
}
///
func applyOrdersEntry(_ entry: OrdersEntry, scannedCode: String?) {
appliedOrdersEntry = entry
appliedScanCode = scannedCode
}
}
///
private final class AppNavigatorRecordingURLSession: URLSessionProtocol {
///
func data(for request: URLRequest) async throws -> (Data, URLResponse) {
let response = HTTPURLResponse(
url: request.url ?? URL(string: "https://example.com")!,
statusCode: 200,
httpVersion: nil,
headerFields: nil
)!
return (Data(), response)
}
}