From a61d2be7be9a7d38e5322b29b44549feda54f45f Mon Sep 17 00:00:00 2001 From: Ryan Carver Date: Sat, 19 Oct 2024 20:41:22 -0700 Subject: [PATCH 1/3] test case showing shared action received in the wrong action --- .../SharedTests.swift | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/Tests/ComposableArchitectureTests/SharedTests.swift b/Tests/ComposableArchitectureTests/SharedTests.swift index adf7ca243d39..21189111ca14 100644 --- a/Tests/ComposableArchitectureTests/SharedTests.swift +++ b/Tests/ComposableArchitectureTests/SharedTests.swift @@ -25,6 +25,24 @@ final class SharedTests: XCTestCase { XCTAssertEqual(store.state.profile.stats.count, 1) } + @MainActor + func testSharingWithDelegateAction() async { + 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 testSharing_Failure() async { let store = TestStore( @@ -1070,18 +1088,28 @@ 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 { 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 @@ -1089,6 +1117,8 @@ private struct SharedFeature { 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)) From 910720c0c6de8ca1573d04a6d2892a4b454903d3 Mon Sep 17 00:00:00 2001 From: Ryan Carver Date: Mon, 21 Oct 2024 14:00:14 -0700 Subject: [PATCH 2/3] mark testSharingWithDelegateAction with XCTTODO --- Tests/ComposableArchitectureTests/SharedTests.swift | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Tests/ComposableArchitectureTests/SharedTests.swift b/Tests/ComposableArchitectureTests/SharedTests.swift index 21189111ca14..be447e549adc 100644 --- a/Tests/ComposableArchitectureTests/SharedTests.swift +++ b/Tests/ComposableArchitectureTests/SharedTests.swift @@ -27,6 +27,13 @@ final class SharedTests: XCTestCase { @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()))), From 4cfe10d581553ee739d4cf9e3064aa1ebebf4030 Mon Sep 17 00:00:00 2001 From: Ryan Carver Date: Mon, 21 Oct 2024 14:02:29 -0700 Subject: [PATCH 3/3] Add the version of testSharingWithDelegateAction that works today --- .../SharedTests.swift | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Tests/ComposableArchitectureTests/SharedTests.swift b/Tests/ComposableArchitectureTests/SharedTests.swift index be447e549adc..fc78cef09979 100644 --- a/Tests/ComposableArchitectureTests/SharedTests.swift +++ b/Tests/ComposableArchitectureTests/SharedTests.swift @@ -50,6 +50,25 @@ final class SharedTests: XCTestCase { } } + @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(