Giter Club home page Giter Club logo

Comments (2)

juliensagot avatar juliensagot commented on June 20, 2024

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.

mbrandonw avatar mbrandonw commented on June 20, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.