Skip to content

Commit 8706d02

Browse files
stephencelismluisbrown
authored andcommitted
Test Store documentation updates (#1129)
1 parent dcfafc1 commit 8706d02

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

Sources/ComposableArchitecture/Internal/Deprecations.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,10 @@ extension Reducer {
134134
func assert(step: Step) {
135135
switch step.type {
136136
case let .send(action, update):
137-
self.send(action, file: step.file, line: step.line, update)
137+
self.send(action, update, file: step.file, line: step.line)
138138

139139
case let .receive(expectedAction, update):
140-
self.receive(expectedAction, file: step.file, line: step.line, update)
140+
self.receive(expectedAction, update, file: step.file, line: step.line)
141141

142142
case let .environment(work):
143143
if !self.receivedActions.isEmpty {

Sources/ComposableArchitecture/TestSupport/TestStore.swift

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,24 @@
167167
/// not expect it would cause a test failure.
168168
///
169169
public final class TestStore<State, LocalState, Action, LocalAction, Environment> {
170+
/// The current environment.
171+
///
172+
/// The environment can be modified throughout a test store's lifecycle in order to influence
173+
/// how it produces effects.
170174
public var environment: Environment
171175

176+
/// The current state.
177+
///
178+
/// When read from a trailing closure assertion in ``send`` or ``receive``, it will equal the
179+
/// `inout` state passed to the closure.
180+
public private(set) var state: State
181+
172182
private let file: StaticString
173183
private let fromLocalAction: (LocalAction) -> Action
174184
private var line: UInt
175185
private var inFlightEffects: Set<LongLivingEffect> = []
176186
var receivedActions: [(action: Action, state: State)] = []
177187
private let reducer: Reducer<State, Action, Environment>
178-
public private(set) var state: State
179188
private var store: Store<State, TestAction>!
180189
private let toLocalState: (State) -> LocalState
181190

@@ -332,11 +341,19 @@
332341
}
333342

334343
extension TestStore where LocalState: Equatable {
344+
/// Sends an action to the store and asserts when state changes.
345+
///
346+
/// - Parameters:
347+
/// - action: An action.
348+
/// - updateExpectingResult: A closure that asserts state changed by sending the action to the
349+
/// store. The mutable state sent to this closure must be modified to match the state of the
350+
/// store after processing the given action. Do not provide a closure if no change is
351+
/// expected.
335352
public func send(
336353
_ action: LocalAction,
354+
_ updateExpectingResult: ((inout LocalState) throws -> Void)? = nil,
337355
file: StaticString = #file,
338-
line: UInt = #line,
339-
_ update: ((inout LocalState) throws -> Void)? = nil
356+
line: UInt = #line
340357
) {
341358
if !self.receivedActions.isEmpty {
342359
var actions = ""
@@ -361,7 +378,7 @@
361378

362379
try self.expectedStateShouldChange(
363380
expected: &expectedState,
364-
update: update,
381+
modify: updateExpectingResult,
365382
file: file,
366383
line: line
367384
)
@@ -381,19 +398,20 @@
381398

382399
private func expectedStateShouldChange(
383400
expected: inout LocalState,
384-
update: ((inout LocalState) throws -> Void)? = nil,
401+
modify: ((inout LocalState) throws -> Void)? = nil,
385402
file: StaticString,
386403
line: UInt
387404
) throws {
388-
guard let update = update else { return }
405+
guard let modify = modify else { return }
389406
let current = expected
390-
try update(&expected)
407+
try modify(&expected)
391408
if expected == current {
392409
XCTFail(
393410
"""
394-
Expected to modify the expected state, but no change occurred.
411+
Expected state to change, but no change occurred.
395412
396-
Ensure that the state was modified or remove the closure to assert no change.
413+
The trailing closure made no observable modifications to state. If no change to state is \
414+
expected, omit the trailing closure.
397415
""",
398416
file: file, line: line
399417
)
@@ -432,11 +450,19 @@
432450
}
433451

434452
extension TestStore where LocalState: Equatable, Action: Equatable {
453+
/// Asserts an action was received from an effect and asserts when state changes.
454+
///
455+
/// - Parameters:
456+
/// - expectedAction: An action expected from an effect.
457+
/// - updateExpectingResult: A closure that asserts state changed by sending the action to the
458+
/// store. The mutable state sent to this closure must be modified to match the state of the
459+
/// store after processing the given action. Do not provide a closure if no change is
460+
/// expected.
435461
public func receive(
436462
_ expectedAction: Action,
463+
_ updateExpectingResult: ((inout LocalState) throws -> Void)? = nil,
437464
file: StaticString = #file,
438-
line: UInt = #line,
439-
_ update: ((inout LocalState) throws -> Void)? = nil
465+
line: UInt = #line
440466
) {
441467
guard !self.receivedActions.isEmpty else {
442468
XCTFail(
@@ -473,7 +499,7 @@
473499
do {
474500
try self.expectedStateShouldChange(
475501
expected: &expectedState,
476-
update: update,
502+
modify: updateExpectingResult,
477503
file: file,
478504
line: line
479505
)

0 commit comments

Comments
 (0)