Closed Muhammed9991 closed 4 months ago
I was about to create an issue for the exact same bug! Here's another sample of code to reproduce it:
@Reducer
struct MainFeature {
@ObservableState
struct State: Equatable {
var childFeature: ChildFeature.State
@Shared var selectedDate: Date
init(selectedDate: Date) {
let sharedSelectedDate = Shared(selectedDate)
self._selectedDate = sharedSelectedDate
self.childFeature = ChildFeature.State(selectedDate: sharedSelectedDate.reader)
}
}
enum Action {
case childFeature(ChildFeature.Action)
}
var body: some ReducerOf<Self> {
Scope(state: \.childFeature, action: \.childFeature) {
ChildFeature()
}
Reduce<State, Action> { state, action in
switch action {
case let .childFeature(.delegate(.didSelectDate(selectedDate))):
state.selectedDate = selectedDate
return .none
case .childFeature:
return .none
}
}
}
}
@Reducer
struct ChildFeature {
@ObservableState
struct State: Equatable {
@SharedReader var selectedDate: Date
}
enum Action {
@CasePathable
enum Delegate {
case didSelectDate(Date)
}
case delegate(Delegate)
case buttonTapped
}
@Dependency(\.date.now) private var now
var body: some ReducerOf<Self> {
Reduce<State, Action> { state, action in
switch action {
case .buttonTapped:
return .send(.delegate(.didSelectDate(now)))
case .delegate:
return .none
}
}
}
}
final class MainFeatureTests: XCTestCase {
@MainActor
func testMainFeature() async throws {
let store = TestStore(initialState: MainFeature.State(selectedDate: Date(timeIntervalSince1970: 0))) {
MainFeature()
} withDependencies: {
$0.date.now = Date(timeIntervalSince1970: 84000)
}
await store.send(\.childFeature.buttonTapped) // ⚠️ State was not expected to change, but a change occurred
await store.receive(\.childFeature.delegate.didSelectDate) {
$0.selectedDate = Date(timeIntervalSince1970: 84000)
}
}
}
Hi @Muhammed9991 and @juliensagot, unfortunately this is intended behavior for the time being. We would like for it to not be, but in order to fix it it will actually potentially cause tests that pass today to fail. Since that's a breaking change we are going to need to wait to 2.0.
The problem is that currently TestStore
eagerly runs the reducer on actions received from effects. So that is why you are seeing share state change from the send
even though the state is actually changed when the effect action is processed.
The fix is to make TestStore
not process effect actions right away, and instead wait until one does store.receive
. But, as mentioned above, that is a breaking change and so will have to wait until 2.0.
For the time being you will just need to assert on shared state a little more eagerly. Luckily this is really only a problem for synchronous effects (like delegate actions), and so hopefully it's not too much of an inconvenience for now.
Since this is not an issue with the library I am going to convert it to a discussion. Feel free to continue the conversation over there.
Description
I've come across a weird issue when trying to write exhaustive testing when using
@Shared
.Example test:
Here's the attached repro https://github.com/Muhammed9991/SharedStateExhaustiveTestingExample.
Checklist
main
branch of this package.Expected behavior
No response
Actual behavior
No response
Steps to reproduce
No response
The Composable Architecture version information
1.10.3
Destination operating system
iOS 17.3
Xcode version information
XCode 15.3
Swift Compiler version information