Skip to content

Commit ade02f0

Browse files
mbrandonwp4checo
authored andcommitted
Test store dependency binding (#1620)
* Bind dependencies when constructing test store and when asserting. * clean up * clean up; * feedback (cherry picked from commit 9a22c5a8a9d0d31fb0f2e61481cb026d5e83282a) # Conflicts: # Tests/ComposableArchitectureTests/TestStoreTests.swift
1 parent d41781d commit ade02f0

File tree

2 files changed

+252
-213
lines changed

2 files changed

+252
-213
lines changed

Sources/ComposableArchitecture/TestStore.swift

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -568,8 +568,9 @@ open class TestStore<State, Action, ScopedState, ScopedAction, Environment> {
568568
/// - initialState: The state the feature starts in.
569569
/// - reducer: The reducer that powers the runtime of the feature.
570570
public init<Reducer: ReducerProtocol>(
571-
initialState: State,
571+
initialState: @autoclosure () -> State,
572572
reducer: Reducer,
573+
prepareDependencies: (inout DependencyValues) -> Void = { _ in },
573574
file: StaticString = #file,
574575
line: UInt = #line
575576
)
@@ -580,6 +581,11 @@ open class TestStore<State, Action, ScopedState, ScopedAction, Environment> {
580581
Action == ScopedAction,
581582
Environment == Void
582583
{
584+
var dependencies = DependencyValues()
585+
dependencies.context = .test
586+
prepareDependencies(&dependencies)
587+
let initialState = DependencyValues.$_current.withValue(dependencies) { initialState() }
588+
583589
let reducer = TestReducer(Reduce(reducer), initialState: initialState)
584590
self._environment = .init(wrappedValue: ())
585591
self.file = file
@@ -589,6 +595,7 @@ open class TestStore<State, Action, ScopedState, ScopedAction, Environment> {
589595
self.store = Store(initialState: initialState, reducer: reducer)
590596
self.timeout = 100 * NSEC_PER_MSEC
591597
self.toScopedState = { $0 }
598+
self.dependencies = dependencies
592599
}
593600

594601
@available(
@@ -1037,7 +1044,9 @@ extension TestStore where ScopedState: Equatable {
10371044
case .on:
10381045
var expectedWhenGivenPreviousState = expected
10391046
if let updateStateToExpectedResult = updateStateToExpectedResult {
1040-
try updateStateToExpectedResult(&expectedWhenGivenPreviousState)
1047+
try DependencyValues.$_current.withValue(self.dependencies) {
1048+
try updateStateToExpectedResult(&expectedWhenGivenPreviousState)
1049+
}
10411050
}
10421051
expected = expectedWhenGivenPreviousState
10431052

@@ -1050,7 +1059,9 @@ extension TestStore where ScopedState: Equatable {
10501059
case .off:
10511060
var expectedWhenGivenActualState = actual
10521061
if let updateStateToExpectedResult = updateStateToExpectedResult {
1053-
try updateStateToExpectedResult(&expectedWhenGivenActualState)
1062+
try DependencyValues.$_current.withValue(self.dependencies) {
1063+
try updateStateToExpectedResult(&expectedWhenGivenActualState)
1064+
}
10541065
}
10551066
expected = expectedWhenGivenActualState
10561067

@@ -1062,10 +1073,12 @@ extension TestStore where ScopedState: Equatable {
10621073
&& expectedWhenGivenActualState == actual
10631074
{
10641075
var expectedWhenGivenPreviousState = current
1065-
if let modify = updateStateToExpectedResult {
1076+
if let updateStateToExpectedResult = updateStateToExpectedResult {
10661077
_XCTExpectFailure(strict: false) {
10671078
do {
1068-
try modify(&expectedWhenGivenPreviousState)
1079+
try DependencyValues.$_current.withValue(self.dependencies) {
1080+
try updateStateToExpectedResult(&expectedWhenGivenPreviousState)
1081+
}
10691082
} catch {
10701083
XCTFail(
10711084
"""
@@ -2063,11 +2076,7 @@ public struct TestStoreTask: Hashable, Sendable {
20632076

20642077
class TestReducer<State, Action>: ReducerProtocol {
20652078
let base: Reduce<State, Action>
2066-
var dependencies = { () -> DependencyValues in
2067-
var dependencies = DependencyValues()
2068-
dependencies.context = .test
2069-
return dependencies
2070-
}()
2079+
var dependencies = DependencyValues()
20712080
let effectDidSubscribe = AsyncStream<Void>.streamWithContinuation()
20722081
var inFlightEffects: Set<LongLivingEffect> = []
20732082
var receivedActions: [(action: Action, state: State)] = []

0 commit comments

Comments
 (0)