Created
April 9, 2020 15:27
-
-
Save finestructure/63181809c40710aae4013298cb1fc59a to your computer and use it in GitHub Desktop.
Test for adapting a progress handler to Effect.subject
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Combine | |
import ComposableArchitecture | |
import ComposableArchitectureTestSupport | |
import XCTest | |
class MainViewTests: XCTestCase { | |
func test_run() throws { | |
// Setup | |
struct State: Equatable { | |
var busy = false | |
var stage = Stage.idle | |
} | |
enum Action: Equatable { | |
case runTapped | |
case progressUpdate(Stage) | |
} | |
enum Stage: String { case idle, started, finished } | |
func run(progress: (Stage) -> Void) { | |
// simulate successive stages | |
progress(.started) | |
progress(.finished) | |
} | |
let runEffect = Effect<Stage>._subject { subject in | |
run { | |
subject.send($0) | |
if $0 == .finished { subject.send(completion: .finished) } | |
} | |
return AnyCancellable { | |
subject.send(completion: .finished) // or error? | |
} | |
} | |
let reducer = Reducer<State, Action, Void> { state, action, env in | |
switch action { | |
case .runTapped: | |
state.busy = true | |
return runEffect | |
.map(Action.progressUpdate) | |
case .progressUpdate(let stage): | |
state.stage = stage | |
if stage == .finished { state.busy = false } | |
} | |
return .noop | |
} | |
let store = TestStore(initialState: State(), | |
reducer: reducer, | |
environment: ()) | |
// Test | |
store.assert( | |
.send(.runTapped) { | |
$0.busy = true | |
}, | |
.receive(.progressUpdate(.started)) { | |
$0.stage = .started | |
}, | |
.receive(.progressUpdate(.finished)) { | |
$0.stage = .finished | |
$0.busy = false | |
} | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment