Skip to content
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

Add overloads of TestStore.send that accept CaseKeyPaths #2681

Merged
merged 2 commits into from
Feb 28, 2024
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
78 changes: 78 additions & 0 deletions Sources/ComposableArchitecture/TestStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1934,6 +1934,84 @@ extension TestStore where State: Equatable {
}
}

extension TestStore where State: Equatable {
/// Sends an action to the store and asserts when state changes.
///
/// This method is similar to ``send(_:assert:file:line:)-2co21``, except it allows
/// you to specify a `CaseKeyPath` of an action with no associated value to be sent to the store.
///
/// It can be useful when sending nested action. For example::
///
/// ```swift
/// await store.send(.view(.tap))
/// ```
///
/// Can be simplified to:
///
/// ```swift
/// await store.send(\.view.tap)
/// ```
///
/// - Parameters:
/// - action: A `CaseKeyPath` to an action.
/// - updateStateToExpectedResult: A closure that asserts state changed by sending the action to
/// the store. The mutable state sent to this closure must be modified to match the state of
/// the store after processing the given action. Do not provide a closure if no change is
/// expected.
/// - Returns: A ``TestStoreTask`` that represents the lifecycle of the effect executed when
/// sending the action.
@MainActor
@discardableResult
@_disfavoredOverload
public func send(
_ action: CaseKeyPath<Action, Void>,
assert updateStateToExpectedResult: ((_ state: inout State) throws -> Void)? = nil,
file: StaticString = #file,
line: UInt = #line
) async -> TestStoreTask {
await self.send(action(), assert: updateStateToExpectedResult, file: file, line: line)
}

/// Sends an action to the store and asserts when state changes.
///
/// This method is similar to ``send(_:assert:file:line:)-1oopl``, except it allows
/// you to specify a value for the associated value of the action.
///
/// It can be useful when sending nested action. For example::
///
/// ```swift
/// await store.send(.view(.delete([19, 23]))
/// ```
///
/// Can be simplified to:
///
/// ```swift
/// await store.send(\.view.delete, [19, 23])
/// ```
///
/// - Parameters:
/// - action: A `CaseKeyPath` to an action with an associated value.
/// - value: A value for the associated value specified in `action`.
/// - updateStateToExpectedResult: A closure that asserts state changed by sending the action to
/// the store. The mutable state sent to this closure must be modified to match the state of
/// the store after processing the given action. Do not provide a closure if no change is
/// expected.
/// - Returns: A ``TestStoreTask`` that represents the lifecycle of the effect executed when
/// sending the action.
@MainActor
@discardableResult
@_disfavoredOverload
public func send<Value>(
_ action: CaseKeyPath<Action, Value>,
_ value: Value,
assert updateStateToExpectedResult: ((_ state: inout State) throws -> Void)? = nil,
file: StaticString = #file,
line: UInt = #line
) async -> TestStoreTask {
await self.send(action(value), assert: updateStateToExpectedResult, file: file, line: line)
}
}

extension TestStore {
/// Clears the queue of received actions from effects.
///
Expand Down
47 changes: 46 additions & 1 deletion Tests/ComposableArchitectureTests/TestStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,8 @@
return .send(.delegate(.success(42)))
case .delegate:
return .none
case .view:
return .none
}
}
}
Expand All @@ -558,6 +560,43 @@
await store.send(.tap)
await store.receive(\.delegate.success, 43)
}

func testSendCaseKeyPath() async {
let store = TestStore<Int, Action>(initialState: 0) {
Reduce { state, action in
switch action {
case .tap:
return .send(.delegate(.success(42)))
case .delegate:
return .none
case .view(.tap):
state = state + 1
return .send(.delegate(.success(42 * 42)))
case let .view(.delete(indexSet)):
let sum = indexSet.reduce(0, +)
if sum == 42 {
state = state + 1
}
return .send(.delegate(.success(sum)))
}
}
}
await store.send(\.tap)
await store.receive(\.delegate.success, 42)

await store.send(\.view.tap) {
$0 = 1
}
await store.receive(\.delegate.success, 42 * 42)

await store.send(\.view.delete, [0])
await store.receive(\.delegate.success, 0)

await store.send(\.view.delete, [19, 23]) {
$0 = 2
}
await store.receive(\.delegate.success, 42)
}
}

private struct Client: DependencyKey {
Expand All @@ -575,9 +614,15 @@
private enum Action {
case tap
case delegate(Delegate)
case view(View)
@CasePathable
enum Delegate {
case success(Int)
}
@CasePathable
enum View {
case tap
case delete(IndexSet)
}
}
#endif
#endif