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

TestStore changes to shared state, fire publisher in the reducer body #3605

Closed
2 of 3 tasks
JOyo246 opened this issue Feb 21, 2025 · 3 comments · Fixed by pointfreeco/swift-sharing#120
Closed
2 of 3 tasks
Labels
bug Something isn't working due to a bug in the library.

Comments

@JOyo246
Copy link
Contributor

JOyo246 commented Feb 21, 2025

Description

I might be missing something here, but when bumping our TCA version and migrating to Sharing 2.0, some of our tests began to fail.

It appears that shared state changes from our teststore assertions bleed back into the reducer?

Here's a repro repo in the UserDefaults case studies

here's a pic of the test output. (Note, i'd expect a single failed action here, but getting 2. Adding a breakpoint reveals that the source of the second fire is the TestStore assertion closure.)
Image

Checklist

  • I have determined whether this bug is also reproducible in a vanilla SwiftUI project.
  • If possible, I've reproduced the issue using the main branch of this package.
  • This issue hasn't been addressed in an existing GitHub issue or discussion.

Expected behavior

TestStore changes to Shared State don't affect the Reducer body

Actual behavior

TestStore changes to Shared State affect the Reducer body

Reproducing project

https://github.com/JOyo246/swift-composable-architecture

The Composable Architecture version information

05601a7

Destination operating system

iOS 18.1

Xcode version information

Xcode 16.2

Swift Compiler version information

6.0.2
@JOyo246 JOyo246 added the bug Something isn't working due to a bug in the library. label Feb 21, 2025
@JOyo246
Copy link
Contributor Author

JOyo246 commented Feb 21, 2025

here's a pic of the test output. (Note, i'd expect a single failed action here, but getting 2. Adding a breakpoint reveals that the source of the second fire is the TestStore assertion closure.)

Also, to "fix this", i can receive both publisher actions, but in our production app, the publisher fires off a chain of events that are asserted, so it would require receiving all of those duplicate actions.

Image

@OguzYuuksel
Copy link

OguzYuuksel commented Feb 21, 2025

Hi, I debugged issue but don't have a fix yet.

in _BoxReference.swift

    private var value: Value {
      willSet {
        subject.send(newValue)
      }
    }

willSet is triggered when below state assertion made.

    await store.send(.counter(.incrementButtonTapped)) { // first willSet is triggered as the result of action.
      $0.counter.$count.withLock { $0 = 1 } // Second willSet is triggered, when we assert.
    }

in TestStore.swift there is expectedStateShouldMatch method that applies mutation to the state. We might need to lose the reference to the Shared value while mutating expectedState not to trigger subject.send

@OguzYuuksel
Copy link

I have provided a fix, please see pointfreeco/swift-sharing#120

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working due to a bug in the library.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants