Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shared state shows test change in the wrong action #3455

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading