Skip to content

Commit 701b8f0

Browse files
tgrapperonstephencelis
authored andcommitted
Disable printing by default for DebugReducer when running SwiftUI Previews (#1625)
* Disable printing by default for `DebugReducer` when running SwiftUI Previews * Update Sources/ComposableArchitecture/Internal/SwiftUIPreviews.swift Co-authored-by: Stephen Celis <[email protected]> * Use `@Dependency(\.context)` Co-authored-by: Stephen Celis <[email protected]> (cherry picked from commit 4ac937402d2597ec1ecb4faccdb710fc69b5795c)
1 parent 047742f commit 701b8f0

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

Sources/ComposableArchitecture/Reducer/Reducers/DebugReducer.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public struct _ReducerPrinter<State, Action> {
3131
extension _ReducerPrinter {
3232
public static var customDump: Self {
3333
Self { receivedAction, oldState, newState in
34+
@Dependency(\.context) var context
35+
guard context != .preview else { return }
3436
var target = ""
3537
target.write("received action:\n")
3638
CustomDump.customDump(receivedAction, to: &target, indent: 2)
@@ -42,6 +44,8 @@ extension _ReducerPrinter {
4244

4345
public static var actionLabels: Self {
4446
Self { receivedAction, _, _ in
47+
@Dependency(\.context) var context
48+
guard context != .preview else { return }
4549
print("received action: \(debugCaseOutput(receivedAction))")
4650
}
4751
}

Tests/ComposableArchitectureTests/DebugTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,5 +220,20 @@
220220
let store = TestStore(initialState: 0, reducer: DebuggedReducer()._printChanges())
221221
await store.send(true) { $0 = 1 }
222222
}
223+
224+
@MainActor
225+
func testDebugReducerInPreview() async {
226+
struct DebuggedReducer: ReducerProtocol {
227+
typealias State = Int
228+
typealias Action = Bool
229+
func reduce(into state: inout Int, action: Bool) -> EffectTask<Bool> {
230+
state += action ? 1 : -1
231+
return .none
232+
}
233+
}
234+
let store = TestStore(initialState: 0, reducer: DebuggedReducer()._printChanges())
235+
store.dependencies.context = .preview
236+
await store.send(true) { $0 = 1 }
237+
}
223238
}
224239
#endif

0 commit comments

Comments
 (0)