Comments (2)
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)
}
}
}
from swift-composable-architecture.
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.
from swift-composable-architecture.
Related Issues (20)
- Key path cannot refer to static member '....' Error HOT 1
- TCA holds on to state for longer than expected HOT 1
- Nested enum reducer error: cannot be constructed because it has no accessible initializers HOT 3
- ForEach<_StoreCollection<String, State, Action>, ObjectIdentifier, SingleTaskView>: the ID ObjectIdentifier(0x0000600003fe8b40) occurs multiple times within the collection, this will give undefined results! HOT 2
- Extra Perception Warning HOT 1
- Non-main thread warning when calling using .refreshable from child @ViewBuilder property HOT 1
- Focus state doesn't work as described in the Building SyncUps Tutorial
- UUID creation not working as described in the SyncUps Tutorial HOT 2
- FileStorage persistence sometimes doesn't save data to disk. HOT 1
- Some texts not appearing correctly: body-8lumc
- SharedState with UserDefaults(suiteName:) not working properly HOT 3
- NSLock Memory leaks HOT 1
- CRASH: Could not cast value of type 'Observation.ObservationRegistrar' to 'Perception._PerceptionRegistrar' HOT 2
- SyncUps Tutorial: Removing attendee test does not pass HOT 1
- isPresented dependency: Incorrectly set to 'true' in tests. HOT 7
- Using both Sharing Data and backward-compatible NavigationLink seems to create some strange behaviors. HOT 1
- Incorrect Confirmation Dialog Presentation on iPad HOT 1
- Ios 17 TextField Binding Issue HOT 1
- Stack overflow crash on physical device HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swift-composable-architecture.