forked from spotify/Mobius.swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEffectHandlerTests.swift
More file actions
76 lines (66 loc) · 2.3 KB
/
EffectHandlerTests.swift
File metadata and controls
76 lines (66 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright Spotify AB.
// SPDX-License-Identifier: Apache-2.0
import MobiusCore
import Nimble
import Quick
private enum Effect: Equatable {
// Effect 1 is handled
case effect1
// Effect 2 is not handled
case effect2
}
private enum Event {
case eventForEffect1
}
class EffectHandlerTests: QuickSpec {
override class func spec() {
describe("Handling effects with EffectHandler") {
var effectHandler: AnyEffectHandler<Effect, Event>!
var executeEffect: ((Effect) -> Void)!
var receivedEvents: [Event]!
beforeEach {
effectHandler = AnyEffectHandler(handle: handleEffect)
receivedEvents = []
let callback = EffectCallback(
onSend: { event in
receivedEvents.append(event)
},
onEnd: {}
)
executeEffect = { effect in
_ = effectHandler.handle(effect, callback)
}
}
context("When executing effects") {
it("dispatches the expected event for an effect which can be handled") {
_ = executeEffect(.effect1)
expect(receivedEvents).to(equal([.eventForEffect1]))
}
it("dispatches no effects for events which cannot be handled") {
_ = executeEffect(.effect2)
expect(receivedEvents).to(beEmpty())
}
}
}
describe("Disposing EffectHandler") {
it("calls the returned disposable when disposing") {
var disposed = false
let effectHandler = AnyEffectHandler<Effect, Event> { _, _ in
AnonymousDisposable {
disposed = true
}
}
let callback = EffectCallback<Event>(onSend: { _ in }, onEnd: {})
effectHandler.handle(.effect1, callback).dispose()
expect(disposed).to(beTrue())
}
}
}
}
private func handleEffect(effect: Effect, callback: EffectCallback<Event>) -> Disposable {
if effect == .effect1 {
callback.send(.eventForEffect1)
}
callback.end()
return EmptyDisposable()
}