Skip to content

Commit a2319ff

Browse files
Buffered Actions Failing Test (#662)
* Adds a failing test demonstrating #661 * Don't write to state till `isSending` is false * Update StoreTests.swift * Update StoreTests.swift Co-authored-by: Stephen Celis <[email protected]> Co-authored-by: Stephen Celis <[email protected]>
1 parent e7dda73 commit a2319ff

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

Sources/ComposableArchitecture/Store.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,8 @@ public final class Store<State, Action> {
372372
self.isSending = true
373373
var currentState = self.state.value
374374
defer {
375-
self.state.value = currentState
376375
self.isSending = false
376+
self.state.value = currentState
377377
}
378378

379379
while !self.bufferedActions.isEmpty {

Tests/ComposableArchitectureTests/StoreTests.swift

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,4 +436,73 @@ final class StoreTests: XCTestCase {
436436

437437
XCTAssertEqual(emissions, [0, 3])
438438
}
439+
440+
func testBufferedActionProcessing() {
441+
struct ChildState: Equatable {
442+
var count: Int?
443+
}
444+
445+
let childReducer = Reducer<ChildState, Int?, Void> { state, action, _ in
446+
state.count = action
447+
return .none
448+
}
449+
450+
struct ParentState: Equatable {
451+
var count: Int?
452+
var child: ChildState?
453+
}
454+
455+
enum ParentAction: Equatable {
456+
case button
457+
case child(Int?)
458+
}
459+
460+
var handledActions: [ParentAction] = []
461+
let parentReducer = Reducer.combine([
462+
childReducer
463+
.optional()
464+
.pullback(
465+
state: \.child,
466+
action: /ParentAction.child,
467+
environment: {}
468+
),
469+
Reducer<ParentState, ParentAction, Void> { state, action, _ in
470+
handledActions.append(action)
471+
472+
switch action {
473+
case .button:
474+
state.child = .init(count: nil)
475+
return .none
476+
477+
case .child(let childCount):
478+
state.count = childCount
479+
return .none
480+
}
481+
},
482+
])
483+
484+
let parentStore = Store(
485+
initialState: .init(),
486+
reducer: parentReducer,
487+
environment: ()
488+
)
489+
490+
parentStore
491+
.scope(
492+
state: \.child,
493+
action: ParentAction.child
494+
)
495+
.ifLet { childStore in
496+
ViewStore(childStore).send(2)
497+
}
498+
.store(in: &cancellables)
499+
500+
XCTAssertEqual(handledActions, [])
501+
502+
parentStore.send(.button)
503+
XCTAssertEqual(handledActions, [
504+
.button,
505+
.child(2)
506+
])
507+
}
439508
}

0 commit comments

Comments
 (0)