StoreKitHelper/Tests/StoreKitHelperTests/StoreKitHelperTests.swift
2026-03-25 22:02:40 +08:00

65 lines
2.3 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.

import Testing
import StoreKit
import StoreKitTest
@testable import StoreKitHelper
//
enum TestProduct: String, InAppProduct {
case basic = "test.basic"
case premium = "test.premium"
var id: String { rawValue }
}
@Test("InAppProduct protocol conformance")
func testInAppProductProtocol() async throws {
// InAppProduct
let products = TestProduct.allCases
#expect(products.count == 2)
#expect(products.contains(.basic))
#expect(products.contains(.premium))
#expect(TestProduct.basic.id == "test.basic")
#expect(TestProduct.premium.id == "test.premium")
}
@Test("StoreContext initialization")
func testStoreContextInitialization() async throws {
let store = await StoreContext(products: TestProduct.allCases)
//
await MainActor.run {
#expect(store.products.isEmpty) // App Store
#expect(store.purchasedProductIDs.isEmpty)
#expect(store.purchaseStatus == .loading)
#expect(store.hasResolvedPurchaseStatus == false)
#expect(store.hasNotPurchased == false)
#expect(store.hasPurchased == false)
}
}
@Test("Purchase status check methods")
func testPurchaseStatusMethods() async throws {
let store = await StoreContext(products: TestProduct.allCases)
await MainActor.run {
//
#expect(store.purchaseStatus == .loading)
#expect(store.isPurchased("test.basic") == false)
#expect(store.isPurchased(TestProduct.premium) == false)
store._setPurchaseStatusForTesting(.notPurchased)
#expect(store.purchaseStatus == .notPurchased)
#expect(store.hasResolvedPurchaseStatus == true)
#expect(store.hasNotPurchased == true)
#expect(store.hasPurchased == false)
// 使
store._setPurchasedProductIDsForTesting(["test.basic"])
#expect(store.isPurchased("test.basic") == true)
#expect(store.isPurchased(TestProduct.basic) == true)
#expect(store.isPurchased("test.premium") == false)
#expect(store.purchaseStatus == .purchased)
#expect(store.hasPurchased == true)
#expect(store.hasNotPurchased == false)
}
}