Skip to content

Commit

Permalink
Added Reducer.combine instance method (pointfreeco#85)
Browse files Browse the repository at this point in the history
* Added Reducer.combine instance method

* Changes name to combined(with:)
  • Loading branch information
alexito4 authored May 13, 2020
1 parent 17439d8 commit 1e9af44
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Sources/ComposableArchitecture/Reducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ public struct Reducer<State, Action, Environment> {
}
}

/// Combines a reducer with another given reducer by running each one on the state, and
/// concatenating both of their effects.
///
/// - Parameter other: Another reducer.
/// - Returns: A single reducer.
public func combined(with other: Reducer) -> Reducer {
Reducer.combine(self, other)
}

/// Transforms a reducer that works on local state, action and environment into one that works on
/// global state, action and environment. It accomplishes this by providing 3 transformations to
/// the method:
Expand Down
36 changes: 36 additions & 0 deletions Tests/ComposableArchitectureTests/ReducerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,42 @@ final class ReducerTests: XCTestCase {
)
}

func testCombine() {
enum Action: Equatable {
case increment
}

var childEffectExecuted = false
let childReducer = Reducer<Int, Action, Void> { state, _, _ in
state += 1
return Effect.fireAndForget { childEffectExecuted = true }
.eraseToEffect()
}

var mainEffectExecuted = false
let mainReducer = Reducer<Int, Action, Void> { state, _, _ in
state += 1
return Effect.fireAndForget { mainEffectExecuted = true }
.eraseToEffect()
}
.combined(with: childReducer)

let store = TestStore(
initialState: 0,
reducer: mainReducer,
environment: ()
)

store.assert(
.send(.increment) {
$0 = 2
}
)

XCTAssertTrue(childEffectExecuted)
XCTAssertTrue(mainEffectExecuted)
}

func testPrint() {
struct Unit: Equatable {}
struct State: Equatable { var count = 0 }
Expand Down

0 comments on commit 1e9af44

Please sign in to comment.