Skip to content

Commit 8aa7ecf

Browse files
committed
Improve API (append expected state change)
1 parent 1586d5f commit 8aa7ecf

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ assert(
3333
scheduler.advance(by: .seconds(5))
3434
}
3535

36-
Send(action: .anotherAction(.stopSomething)) { expectedStateChange in
36+
Send(action: .anotherAction(.stopSomething)).expectStateToHaveChanged { state in
3737
// if during Send or Receive action, your state is expected to mutate, you must indicate which change is expected to happen here:
38-
expectedStateChange.somePropertyShouldHaveChangedTo = true
38+
state.somePropertyShouldHaveChangedTo = true
3939
// any unexpected state mutation will fail the test, as well as any expected state mutation that doesn't occur, will also fail the test
4040
}
4141

@@ -52,9 +52,9 @@ assert(
5252
} else {
5353
return false
5454
}
55-
} stateChange: { expectedStateChange in
55+
}.expectStateToHaveChanged { state in
5656
// if during Send or Receive action, your state is expected to mutate, you must indicate which change is expected to happen here:
57-
expectedStateChange.somePropertyShouldHaveChangedTo = true
57+
state.somePropertyShouldHaveChangedTo = true
5858
// any unexpected state mutation will fail the test, as well as any expected state mutation that doesn't occur, will also fail the test
5959
}
6060
}

Sources/TestingExtensions/UseCaseTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ public struct Send<ActionType, StateType>: StepProtocol {
7171
public var asStep: Step<ActionType, StateType> {
7272
.send(action: action(), file: file, line: line, stateChange: stateChange)
7373
}
74+
75+
public func expectStateToHaveChanged(_ expectedMutation: @escaping (inout StateType) -> Void = { _ in }) -> Send {
76+
.init(action: action(), file: file, line: line, stateChange: { state in
77+
self.stateChange(&state)
78+
expectedMutation(&state)
79+
})
80+
}
7481
}
7582

7683
public struct Receive<ActionType, StateType>: StepProtocol {
@@ -105,6 +112,13 @@ public struct Receive<ActionType, StateType>: StepProtocol {
105112
public var asStep: Step<ActionType, StateType> {
106113
.receive(isExpectedAction: isExpectedAction, file: file, line: line, stateChange: stateChange)
107114
}
115+
116+
public func expectStateToHaveChanged(_ expectedMutation: @escaping (inout StateType) -> Void = { _ in }) -> Receive {
117+
.init(isExpectedAction: isExpectedAction, file: file, line: line, stateChange: { state in
118+
self.stateChange(&state)
119+
expectedMutation(&state)
120+
})
121+
}
108122
}
109123

110124
public struct SideEffectResult<ActionType, StateType>: StepProtocol {

0 commit comments

Comments
 (0)