Files
suixinkan_uikit/suixinkanTests/AccountSwitchViewModelTests.swift

79 lines
2.4 KiB
Swift
Raw Permalink 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.

//
// AccountSwitchViewModelTests.swift
// suixinkanTests
//
import XCTest
@testable import suixinkan
@MainActor
/// ViewModel
final class AccountSwitchViewModelTests: XCTestCase {
func testLoadFiltersInvalidAccounts() async throws {
let json = """
{
"code": 100000,
"msg": "success",
"data": {
"token": "token",
"scenic_users": [
{
"account_type": "scenic_user",
"id": 1,
"user_id": 1,
"scenic_user_id": 1,
"ss_user_id": 9001,
"username": "u1",
"real_name": "",
"nickname": "",
"phone": "13800138000",
"scenic_id": 10,
"scenic_name": "A",
"is_current": true
}
],
"store_users": []
}
}
""".data(using: .utf8)!
let session = MockURLSession(responses: [json])
let api = ProfileAPI(client: APIClient(environment: .testing, session: session))
let viewModel = AccountSwitchViewModel()
try await viewModel.load(api: api, force: true)
XCTAssertEqual(viewModel.accounts.count, 1)
XCTAssertEqual(viewModel.selectedAccount?.businessUserId, 9001)
}
func testSwitchAccountThrowsWhenBusinessUserIdInvalid() async {
let viewModel = AccountSwitchViewModel()
let account = AccountSwitchAccount(
accountType: V9ScenicUser.accountTypeValue,
businessUserId: 0,
title: "景区A",
subtitle: "角色A",
phone: "13800138000",
realName: "张三",
avatar: "",
scenicName: "景区A",
storeId: nil,
storeName: "",
scenicId: 10,
isCurrent: false
)
let authAPI = AuthAPI(client: APIClient(environment: .testing, session: MockURLSession(responses: [])))
do {
_ = try await viewModel.switchAccount(account, api: authAPI)
XCTFail("无效账号应失败")
} catch {
guard let switchError = error as? AccountSwitchError else {
return XCTFail("期望 AccountSwitchError实际为 \(error)")
}
XCTAssertEqual(switchError, .invalidAccount)
}
}
}