Skip to content

Test store dependency binding #1620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions Sources/ComposableArchitecture/TestStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -564,8 +564,9 @@ open class TestStore<State, Action, ScopedState, ScopedAction, Environment> {
/// - initialState: The state the feature starts in.
/// - reducer: The reducer that powers the runtime of the feature.
public init<Reducer: ReducerProtocol>(
initialState: State,
initialState: @autoclosure () -> State,
reducer: Reducer,
prepareDependencies: (inout DependencyValues) -> Void = { _ in },
file: StaticString = #file,
line: UInt = #line
)
Expand All @@ -576,6 +577,11 @@ open class TestStore<State, Action, ScopedState, ScopedAction, Environment> {
Action == ScopedAction,
Environment == Void
{
var dependencies = DependencyValues()
dependencies.context = .test
prepareDependencies(&dependencies)
let initialState = DependencyValues.$_current.withValue(dependencies) { initialState() }

let reducer = TestReducer(Reduce(reducer), initialState: initialState)
self._environment = .init(wrappedValue: ())
self.file = file
Expand All @@ -585,6 +591,7 @@ open class TestStore<State, Action, ScopedState, ScopedAction, Environment> {
self.store = Store(initialState: initialState, reducer: reducer)
self.timeout = 100 * NSEC_PER_MSEC
self.toScopedState = { $0 }
self.dependencies = dependencies
}

@available(
Expand Down Expand Up @@ -1033,7 +1040,9 @@ extension TestStore where ScopedState: Equatable {
case .on:
var expectedWhenGivenPreviousState = expected
if let updateStateToExpectedResult = updateStateToExpectedResult {
try updateStateToExpectedResult(&expectedWhenGivenPreviousState)
try DependencyValues.$_current.withValue(self.dependencies) {
try updateStateToExpectedResult(&expectedWhenGivenPreviousState)
}
}
expected = expectedWhenGivenPreviousState

Expand All @@ -1046,7 +1055,9 @@ extension TestStore where ScopedState: Equatable {
case .off:
var expectedWhenGivenActualState = actual
if let updateStateToExpectedResult = updateStateToExpectedResult {
try updateStateToExpectedResult(&expectedWhenGivenActualState)
try DependencyValues.$_current.withValue(self.dependencies) {
try updateStateToExpectedResult(&expectedWhenGivenActualState)
}
}
expected = expectedWhenGivenActualState

Expand All @@ -1058,10 +1069,12 @@ extension TestStore where ScopedState: Equatable {
&& expectedWhenGivenActualState == actual
{
var expectedWhenGivenPreviousState = current
if let modify = updateStateToExpectedResult {
if let updateStateToExpectedResult = updateStateToExpectedResult {
_XCTExpectFailure(strict: false) {
do {
try modify(&expectedWhenGivenPreviousState)
try DependencyValues.$_current.withValue(self.dependencies) {
try updateStateToExpectedResult(&expectedWhenGivenPreviousState)
}
} catch {
XCTFail(
"""
Expand Down Expand Up @@ -2059,11 +2072,7 @@ public struct TestStoreTask: Hashable, Sendable {

class TestReducer<State, Action>: ReducerProtocol {
let base: Reduce<State, Action>
var dependencies = { () -> DependencyValues in
var dependencies = DependencyValues()
dependencies.context = .test
return dependencies
}()
var dependencies = DependencyValues()
let effectDidSubscribe = AsyncStream<Void>.streamWithContinuation()
var inFlightEffects: Set<LongLivingEffect> = []
var receivedActions: [(action: Action, state: State)] = []
Expand Down
30 changes: 30 additions & 0 deletions Tests/ComposableArchitectureTests/TestStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,34 @@ final class TestStoreTests: XCTestCase {

store.send(true) { $0 = 1 }
}

func testDependenciesEarlyBinding() async {
struct Feature: ReducerProtocol {
struct State: Equatable {
var count = 0
var date: Date
init() {
@Dependency(\.date.now) var now: Date
self.date = now
}
}
func reduce(into state: inout State, action: Void) -> EffectTask<Void> {
state.count += 1
return .none
}
}

let store = TestStore(
initialState: Feature.State(),
reducer: Feature()
) {
$0.date = .constant(Date(timeIntervalSince1970: 1234567890))
}

await store.send(()) {
@Dependency(\.date.now) var now: Date
$0.count = 1
$0.date = now
}
}
}