Skip to content

Commit

Permalink
Add Waiter to handle threading for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLeif committed Feb 23, 2024
1 parent af1fd72 commit ebbbbd5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
8 changes: 6 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ let package = Package(
)
],
dependencies: [
.package(url: "https://github.com/0xLeif/Cache", from: "2.0.0")
.package(url: "https://github.com/0xLeif/Cache", from: "2.0.0"),
.package(url: "https://github.com/0xLeif/Waiter", from: "1.0.0")
],
targets: [
.target(
Expand All @@ -29,7 +30,10 @@ let package = Package(
),
.testTarget(
name: "AppStateTests",
dependencies: ["AppState"]
dependencies: [
"AppState",
"Waiter"
]
)
]
)
20 changes: 15 additions & 5 deletions Sources/AppState/Application/Application+public.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,20 @@ public extension Application {
column: column
)

shared.cache.set(
value: dependency,
forKey: dependency.scope.key
)
let setValue = {
shared.cache.set(
value: dependency,
forKey: dependency.scope.key
)
}

#if (!os(Linux) && !os(Windows))
Task {
setValue()
}
#else
setValue()
#endif
}
}

Expand Down Expand Up @@ -199,7 +209,7 @@ public extension Application {
)

var promotions = Application.state(\.dependencyPromotions)
promotions.value.append(promotionOverride)
promotions.value[String(describing: keyPath)] = promotionOverride

return Application.self
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extension Application {
var dependencyPromotions: State<[DependencyOverride]> {
state(initial: [])
var dependencyPromotions: State<[String: DependencyOverride]> {
state(initial: [:])
}

/// `Dependency` struct encapsulates dependencies used throughout the app.
Expand Down
9 changes: 8 additions & 1 deletion Tests/AppStateTests/AppDependencyTests.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Waiter
import XCTest
@testable import AppState

Expand Down Expand Up @@ -58,7 +59,7 @@ final class AppDependencyTests: XCTestCase {
composableService.networking.fetch()
}

func testDependency() {
func testDependency() async throws {
Application.promote(\.networking, with: NetworkService())

let networkingOverride = Application.override(\.networking, with: MockNetworking())
Expand All @@ -75,6 +76,12 @@ final class AppDependencyTests: XCTestCase {

networkingOverride.cancel()

try await Waiter.wait(
on: Application.shared,
for: \.networking,
expecting: { $0.value is NetworkService }
)

let networkingService = Application.dependency(\.networking)

XCTAssertNotNil(networkingService as? NetworkService)
Expand Down

0 comments on commit ebbbbd5

Please sign in to comment.