Skip to content

Commit c8a2e94

Browse files
authored
Test Store documentation updates (#1129)
1 parent 6f68096 commit c8a2e94

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
@@ -130,10 +130,10 @@ extension Reducer {
130130
func assert(step: Step) {
131131
switch step.type {
132132
case let .send(action, update):
133-
self.send(action, file: step.file, line: step.line, update)
133+
self.send(action, update, file: step.file, line: step.line)
134134

135135
case let .receive(expectedAction, update):
136-
self.receive(expectedAction, file: step.file, line: step.line, update)
136+
self.receive(expectedAction, update, file: step.file, line: step.line)
137137

138138
case let .environment(work):
139139
if !self.receivedActions.isEmpty {

Sources/ComposableArchitecture/TestSupport/TestStore.swift

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

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

@@ -335,11 +344,19 @@
335344
}
336345

337346
extension TestStore where LocalState: Equatable {
347+
/// Sends an action to the store and asserts when state changes.
348+
///
349+
/// - Parameters:
350+
/// - action: An action.
351+
/// - updateExpectingResult: A closure that asserts state changed by sending the action to the
352+
/// store. The mutable state sent to this closure must be modified to match the state of the
353+
/// store after processing the given action. Do not provide a closure if no change is
354+
/// expected.
338355
public func send(
339356
_ action: LocalAction,
357+
_ updateExpectingResult: ((inout LocalState) throws -> Void)? = nil,
340358
file: StaticString = #file,
341-
line: UInt = #line,
342-
_ update: ((inout LocalState) throws -> Void)? = nil
359+
line: UInt = #line
343360
) {
344361
if !self.receivedActions.isEmpty {
345362
var actions = ""
@@ -364,7 +381,7 @@
364381

365382
try self.expectedStateShouldChange(
366383
expected: &expectedState,
367-
update: update,
384+
modify: updateExpectingResult,
368385
file: file,
369386
line: line
370387
)
@@ -384,19 +401,20 @@
384401

385402
private func expectedStateShouldChange(
386403
expected: inout LocalState,
387-
update: ((inout LocalState) throws -> Void)? = nil,
404+
modify: ((inout LocalState) throws -> Void)? = nil,
388405
file: StaticString,
389406
line: UInt
390407
) throws {
391-
guard let update = update else { return }
408+
guard let modify = modify else { return }
392409
let current = expected
393-
try update(&expected)
410+
try modify(&expected)
394411
if expected == current {
395412
XCTFail(
396413
"""
397-
Expected to modify the expected state, but no change occurred.
414+
Expected state to change, but no change occurred.
398415
399-
Ensure that the state was modified or remove the closure to assert no change.
416+
The trailing closure made no observable modifications to state. If no change to state is \
417+
expected, omit the trailing closure.
400418
""",
401419
file: file, line: line
402420
)
@@ -435,11 +453,19 @@
435453
}
436454

437455
extension TestStore where LocalState: Equatable, Action: Equatable {
456+
/// Asserts an action was received from an effect and asserts when state changes.
457+
///
458+
/// - Parameters:
459+
/// - expectedAction: An action expected from an effect.
460+
/// - updateExpectingResult: A closure that asserts state changed by sending the action to the
461+
/// store. The mutable state sent to this closure must be modified to match the state of the
462+
/// store after processing the given action. Do not provide a closure if no change is
463+
/// expected.
438464
public func receive(
439465
_ expectedAction: Action,
466+
_ updateExpectingResult: ((inout LocalState) throws -> Void)? = nil,
440467
file: StaticString = #file,
441-
line: UInt = #line,
442-
_ update: ((inout LocalState) throws -> Void)? = nil
468+
line: UInt = #line
443469
) {
444470
guard !self.receivedActions.isEmpty else {
445471
XCTFail(
@@ -476,7 +502,7 @@
476502
do {
477503
try self.expectedStateShouldChange(
478504
expected: &expectedState,
479-
update: update,
505+
modify: updateExpectingResult,
480506
file: file,
481507
line: line
482508
)

0 commit comments

Comments
 (0)