Skip to content

Commit

Permalink
Shared state shows test change in the wrong action (#3455)
Browse files Browse the repository at this point in the history
* test case showing shared action received in the wrong action

* mark testSharingWithDelegateAction with XCTTODO

* Add the version of testSharingWithDelegateAction that works today

---------

Co-authored-by: Stephen Celis <stephen@stephencelis.com>
  • Loading branch information
rcarver and stephencelis authored Oct 22, 2024
1 parent 5614943 commit 59fdf28
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions Tests/ComposableArchitectureTests/SharedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,50 @@ final class SharedTests: XCTestCase {
XCTAssertEqual(store.state.profile.stats.count, 1)
}

@MainActor
func testSharingWithDelegateAction() async {
XCTTODO(
"""
Ideally this test would pass but is a known, but also expected, issue with shared state and
the test store. The fix is to have the test store not eagerly process actions from effects,
but unfortunately that would be a breaking change in 1.0.
""")

let store = TestStore(
initialState: SharedFeature.State(
profile: Shared(Profile(stats: Shared(Stats()))),
sharedCount: Shared(0),
stats: Shared(Stats())
)
) {
SharedFeature()
}
await store.send(.incrementSharedInDelegate)
await store.receive(\.delegate.didIncrement) {
$0.count = 1
$0.stats.count = 1
}
}

@MainActor
func testSharingWithDelegateAction_EagerActionProcessing() async {
let store = TestStore(
initialState: SharedFeature.State(
profile: Shared(Profile(stats: Shared(Stats()))),
sharedCount: Shared(0),
stats: Shared(Stats())
)
) {
SharedFeature()
}
await store.send(.incrementSharedInDelegate) {
$0.stats.count = 1
}
await store.receive(\.delegate.didIncrement) {
$0.count = 1
}
}

@MainActor
func testSharing_Failure() async {
let store = TestStore(
Expand Down Expand Up @@ -1070,25 +1114,37 @@ private struct SharedFeature {
}
}
enum Action {
case delegate(Delegate)
case increment
case incrementStats
case incrementSharedInDelegate
case longLivingEffect
case noop
case request
case sharedIncrement
case toggleIsOn
@CasePathable
enum Delegate {
case didIncrement
}
}
@Dependency(\.mainQueue) var mainQueue
var body: some ReducerOf<Self> {
Reduce { state, action in
switch action {
case .delegate(.didIncrement):
state.count += 1
state.stats.count += 1
return .none
case .increment:
state.count += 1
return .none
case .incrementStats:
state.profile.stats.count += 1
state.stats.count += 1
return .none
case .incrementSharedInDelegate:
return .send(.delegate(.didIncrement))
case .longLivingEffect:
return .run { [sharedCount = state.$sharedCount] _ in
try await self.mainQueue.sleep(for: .seconds(1))
Expand Down

0 comments on commit 59fdf28

Please sign in to comment.