将会话、权限、位置、收款与排队存储拆分为独立模块,同步更新各 ViewModel 与单元测试,并补充素材管理 UI 与个人空间设置交互。 Co-authored-by: Cursor <cursoragent@cursor.com>
78 lines
3.6 KiB
Swift
78 lines
3.6 KiB
Swift
//
|
|
// LocationReportHistoryViewModelTests.swift
|
|
// suixinkanTests
|
|
//
|
|
|
|
import XCTest
|
|
@testable import suixinkan
|
|
|
|
@MainActor
|
|
/// 位置上报历史 ViewModel 测试。
|
|
final class LocationReportHistoryViewModelTests: XCTestCase {
|
|
|
|
private var appStore: AppStore!
|
|
private var defaults: UserDefaults!
|
|
|
|
override func setUp() {
|
|
defaults = UserDefaults(suiteName: "LocationReportHistoryViewModelTests")!
|
|
defaults.removePersistentDomain(forName: "LocationReportHistoryViewModelTests")
|
|
appStore = AppStore(defaults: defaults)
|
|
appStore.session.userId = "staff-99"
|
|
}
|
|
|
|
override func tearDown() {
|
|
appStore.logout()
|
|
super.tearDown()
|
|
}
|
|
|
|
func testLoadReportListPopulatesItems() async throws {
|
|
let listJSON = """
|
|
{"code":100000,"msg":"success","data":{"list":[{"id":1,"staff_id":99,"type":1,"latitude":"30.1","longitude":"120.2","address":"测试地址","remark":"","created_at":"2026-01-01 10:00:00"}],"total":1}}
|
|
""".data(using: .utf8)!
|
|
let api = HomeAPI(client: APIClient(environment: .testing, session: MockURLSession(responses: [listJSON])))
|
|
let viewModel = LocationReportHistoryViewModel(appStore: appStore)
|
|
|
|
await viewModel.loadReportList(api: api, refresh: true)
|
|
|
|
XCTAssertEqual(viewModel.reportList.count, 1)
|
|
XCTAssertEqual(viewModel.reportList.first?.address, "测试地址")
|
|
XCTAssertFalse(viewModel.canLoadMore)
|
|
}
|
|
|
|
func testSelectFilterReloadsWithTypeCode() async throws {
|
|
let allJSON = """
|
|
{"code":100000,"msg":"success","data":{"list":[{"id":1,"staff_id":99,"type":1,"latitude":"30","longitude":"120","address":"A","remark":"","created_at":"2026-01-01 10:00:00"}],"total":1}}
|
|
""".data(using: .utf8)!
|
|
let markedJSON = """
|
|
{"code":100000,"msg":"success","data":{"list":[{"id":2,"staff_id":99,"type":2,"latitude":"31","longitude":"121","address":"B","remark":"","created_at":"2026-01-02 10:00:00"}],"total":1}}
|
|
""".data(using: .utf8)!
|
|
let api = HomeAPI(client: APIClient(environment: .testing, session: MockURLSession(responses: [allJSON, markedJSON])))
|
|
let viewModel = LocationReportHistoryViewModel(appStore: appStore)
|
|
|
|
await viewModel.loadReportList(api: api, refresh: true)
|
|
await viewModel.selectFilter(.marked, api: api)
|
|
|
|
XCTAssertEqual(viewModel.selectedFilter, .marked)
|
|
XCTAssertEqual(viewModel.reportList.first?.type, 2)
|
|
XCTAssertEqual(viewModel.reportList.first?.address, "B")
|
|
}
|
|
|
|
func testPaginationAppendsItems() async throws {
|
|
let page1JSON = """
|
|
{"code":100000,"msg":"success","data":{"list":[{"id":1,"staff_id":99,"type":1,"latitude":"30","longitude":"120","address":"第一页","remark":"","created_at":"2026-01-01 10:00:00"}],"total":2}}
|
|
""".data(using: .utf8)!
|
|
let page2JSON = """
|
|
{"code":100000,"msg":"success","data":{"list":[{"id":2,"staff_id":99,"type":1,"latitude":"31","longitude":"121","address":"第二页","remark":"","created_at":"2026-01-02 10:00:00"}],"total":2}}
|
|
""".data(using: .utf8)!
|
|
let api = HomeAPI(client: APIClient(environment: .testing, session: MockURLSession(responses: [page1JSON, page2JSON])))
|
|
let viewModel = LocationReportHistoryViewModel(appStore: appStore)
|
|
|
|
await viewModel.loadReportList(api: api, refresh: true)
|
|
await viewModel.loadMoreIfNeeded(currentIndex: 0, api: api)
|
|
|
|
XCTAssertEqual(viewModel.reportList.count, 2)
|
|
XCTAssertEqual(viewModel.reportList.last?.address, "第二页")
|
|
XCTAssertFalse(viewModel.canLoadMore)
|
|
}
|
|
}
|