Skip to content

Commit 5224148

Browse files
authored
Call prepareDependencies once when initializing TestStore. (#2111)
* Call prepareDependencies once when initializing TestStore. * clean up
1 parent 4270230 commit 5224148

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

Sources/ComposableArchitecture/TestStore.swift

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -663,22 +663,18 @@ public final class TestStore<State, Action, ScopedState, ScopedAction, Environme
663663
Environment == Void
664664
{
665665
var dependencies = DependencyValues._current
666-
let initialState = withDependencies {
666+
let reducer = withDependencies {
667667
prepareDependencies(&dependencies)
668668
$0 = dependencies
669669
} operation: {
670-
initialState()
670+
TestReducer(Reduce(reducer()), initialState: initialState())
671671
}
672-
673-
let reducer = TestReducer(
674-
Reduce(withDependencies(prepareDependencies) { reducer() }), initialState: initialState
675-
)
676672
self._environment = .init(wrappedValue: ())
677673
self.file = file
678674
self.fromScopedAction = fromScopedAction
679675
self.line = line
680676
self.reducer = reducer
681-
self.store = Store(initialState: initialState, reducer: reducer)
677+
self.store = Store(initialState: reducer.state, reducer: reducer)
682678
self.timeout = 100 * NSEC_PER_MSEC
683679
self.toScopedState = toScopedState
684680
self.dependencies = dependencies
@@ -708,22 +704,18 @@ public final class TestStore<State, Action, ScopedState, ScopedAction, Environme
708704
Environment == Void
709705
{
710706
var dependencies = DependencyValues._current
711-
prepareDependencies(&dependencies)
712-
let initialState = withDependencies {
707+
let reducer = withDependencies {
708+
prepareDependencies(&dependencies)
713709
$0 = dependencies
714710
} operation: {
715-
initialState()
711+
TestReducer(Reduce(reducer()), initialState: initialState())
716712
}
717-
718-
let reducer = TestReducer(
719-
Reduce(withDependencies(prepareDependencies) { reducer() }), initialState: initialState
720-
)
721713
self._environment = .init(wrappedValue: ())
722714
self.file = file
723715
self.fromScopedAction = { $0 }
724716
self.line = line
725717
self.reducer = reducer
726-
self.store = Store(initialState: initialState, reducer: reducer)
718+
self.store = Store(initialState: reducer.state, reducer: reducer)
727719
self.timeout = 100 * NSEC_PER_MSEC
728720
self.toScopedState = { $0 }
729721
self.dependencies = dependencies

Tests/ComposableArchitectureTests/TestStoreTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,18 @@ final class TestStoreTests: BaseTCATestCase {
394394
}
395395
}
396396

397+
func testPrepareDependenciesCalledOnce() {
398+
var count = 0
399+
let store = TestStore(initialState: 0) {
400+
EmptyReducer<Int, Void>()
401+
} withDependencies: { _ in
402+
count += 1
403+
}
404+
405+
XCTAssertEqual(count, 1)
406+
_ = store
407+
}
408+
397409
func testEffectEmitAfterSkipInFlightEffects() async {
398410
let mainQueue = DispatchQueue.test
399411
enum Action: Equatable { case tap, response }

0 commit comments

Comments
 (0)