Files
suixinkan_ios_new/suixinkanTests/ScenicSpotContextTests.swift
2026-06-22 11:28:01 +08:00

77 lines
2.4 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.

//
// ScenicSpotContextTests.swift
// suixinkanTests
//
// Created by Codex on 2026/6/22.
//
import XCTest
@testable import suixinkan
@MainActor
///
final class ScenicSpotContextTests: XCTestCase {
/// ID
func testReloadRequestsSpotsForScenicId() async {
let context = ScenicSpotContext()
let api = MockScenicSpotAPI()
api.spotResponse = ListPayload(total: 1, list: [ScenicSpotItem(id: 9, name: "观景台")])
await context.reload(scenicId: 88, api: api)
XCTAssertEqual(api.requestedScenicIds, [88])
XCTAssertEqual(context.scenicId, 88)
XCTAssertEqual(context.spots.map(\.id), [9])
XCTAssertEqual(context.loadState, .loaded)
}
///
func testReloadFailureOnlyAffectsScenicSpotContext() async {
let context = ScenicSpotContext()
let api = MockScenicSpotAPI()
api.error = APIError.networkFailed("景点接口失败")
await context.reload(scenicId: 88, api: api)
XCTAssertEqual(api.requestedScenicIds, [88])
XCTAssertTrue(context.spots.isEmpty)
if case .failed = context.loadState {
XCTAssertTrue(true)
} else {
XCTFail("景点接口失败时应进入 failed 状态")
}
}
}
@MainActor
/// ID
private final class MockScenicSpotAPI: AccountContextServing {
var requestedScenicIds: [Int] = []
var spotResponse = ListPayload(total: 0, list: [ScenicSpotItem]())
var error: Error?
/// 使
func rolePermissions() async throws -> [RolePermissionResponse] {
[]
}
/// 使
func scenicListAll() async throws -> ScenicListAllResponse {
ScenicListAllResponse(total: 0, list: [])
}
/// 使
func storeAll() async throws -> ListPayload<StoreItem> {
ListPayload(total: 0, list: [])
}
/// ID
func scenicSpotListAll(scenicId: Int) async throws -> ListPayload<ScenicSpotItem> {
requestedScenicIds.append(scenicId)
if let error {
throw error
}
return spotResponse
}
}