Skip to content

Commit 6ce426f

Browse files
mbrandonwmluisbrown
authored andcommitted
Make TestStore.state public (#1127)
* Make TestStore.state public. * wip
1 parent 14edd16 commit 6ce426f

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

Sources/ComposableArchitecture/TestSupport/TestStore.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@
175175
private var inFlightEffects: Set<LongLivingEffect> = []
176176
var receivedActions: [(action: Action, state: State)] = []
177177
private let reducer: Reducer<State, Action, Environment>
178-
private var state: State
178+
public private(set) var state: State
179179
private var store: Store<State, TestAction>!
180180
private let toLocalState: (State) -> LocalState
181181

@@ -352,8 +352,13 @@
352352
)
353353
}
354354
var expectedState = self.toLocalState(self.state)
355+
let previousState = self.state
355356
self.store.send(.init(origin: .send(action), file: file, line: line))
356357
do {
358+
let currentState = self.state
359+
self.state = previousState
360+
defer { self.state = currentState }
361+
357362
try self.expectedStateShouldChange(
358363
expected: &expectedState,
359364
update: update,

Tests/ComposableArchitectureTests/TestStoreTests.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,43 @@ class TestStoreTests: XCTestCase {
145145
}
146146
}
147147
}
148+
149+
func testStateAccess() {
150+
enum Action { case a, b, c, d }
151+
let store = TestStore(
152+
initialState: 0,
153+
reducer: Reducer<Int, Action, Void> { count, action, _ in
154+
switch action {
155+
case .a:
156+
count += 1
157+
return .merge(.init(value: .b), .init(value: .c), .init(value: .d))
158+
case .b, .c, .d:
159+
count += 1
160+
return .none
161+
}
162+
},
163+
environment: ()
164+
)
165+
166+
store.send(.a) {
167+
$0 = 1
168+
XCTAssertEqual(store.state, 0)
169+
}
170+
XCTAssertEqual(store.state, 1)
171+
store.receive(.b) {
172+
$0 = 2
173+
XCTAssertEqual(store.state, 1)
174+
}
175+
XCTAssertEqual(store.state, 2)
176+
store.receive(.c) {
177+
$0 = 3
178+
XCTAssertEqual(store.state, 2)
179+
}
180+
XCTAssertEqual(store.state, 3)
181+
store.receive(.d) {
182+
$0 = 4
183+
XCTAssertEqual(store.state, 3)
184+
}
185+
XCTAssertEqual(store.state, 4)
186+
}
148187
}

0 commit comments

Comments
 (0)