@@ -13,6 +13,7 @@ import XCTest
13
13
class EffectsTests : XCTestCase {
14
14
var action = PassthroughSubject < Action , Never > ( )
15
15
16
+ /// Can we lookup `Effect`s?
16
17
func testEffectsLookup( ) {
17
18
// Given
18
19
struct TestEffects : Effects {
@@ -25,12 +26,13 @@ class EffectsTests: XCTestCase {
25
26
XCTAssertEqual ( testEffects. enabledEffects. count, 1 )
26
27
}
27
28
28
- func testEffectRunDispatching( ) {
29
+ /// Can we run a single dispatching `Effect`?
30
+ func testEffectRunDispatchingOne( ) throws {
29
31
// Given
30
32
let action2 = Test2Action ( )
31
33
let expectation = XCTestExpectation ( description: debugDescription)
32
34
expectation. expectedFulfillmentCount = 1
33
- let effect = Effect . dispatching {
35
+ let effect = Effect . dispatchingOne {
34
36
$0. ofType ( Test1Action . self)
35
37
. map { _ in
36
38
expectation. fulfill ( )
@@ -40,14 +42,41 @@ class EffectsTests: XCTestCase {
40
42
}
41
43
// When
42
44
let action = Test1Action ( )
43
- let actions : [ Action ] = effect. run ( with: action)
45
+ let actions : [ Action ] = try effect. run ( with: action)
44
46
effect. run ( with: action) // Returns early because of wrong type
45
47
// Then
46
48
wait ( for: [ expectation] , timeout: 1 )
47
49
XCTAssertEqual ( actions [ 0 ] as! Test2Action , action2)
50
+ XCTAssertThrowsError ( try effect. run ( with: action, expectedCount: 2 ) )
48
51
}
49
52
50
- func testEffectRunNonDispatching( ) {
53
+ /// Can we run a multi dispatching `Effect`?
54
+ func testEffectRunDispatchingMultiple( ) throws {
55
+ // Given
56
+ let action2 = Test2Action ( )
57
+ let action3 = Test3Action ( )
58
+ let expectation = XCTestExpectation ( description: debugDescription)
59
+ expectation. expectedFulfillmentCount = 1
60
+ let effect = Effect . dispatchingMultiple {
61
+ $0. ofType ( Test1Action . self)
62
+ . map { _ in
63
+ expectation. fulfill ( )
64
+ return [ action2, action3]
65
+ }
66
+ . eraseToAnyPublisher ( )
67
+ }
68
+ // When
69
+ let action = Test1Action ( )
70
+ let actions : [ Action ] = try effect. run ( with: action, expectedCount: 2 )
71
+ effect. run ( with: action) // Returns early because of wrong type
72
+ // Then
73
+ wait ( for: [ expectation] , timeout: 1 )
74
+ XCTAssertEqual ( actions [ 0 ] as! Test2Action , action2)
75
+ XCTAssertEqual ( actions [ 1 ] as! Test3Action , action3)
76
+ }
77
+
78
+ /// Can we run a non dispatching `Effect`?
79
+ func testEffectRunNonDispatching( ) throws {
51
80
// Given
52
81
let expectation = XCTestExpectation ( description: debugDescription)
53
82
expectation. expectedFulfillmentCount = 1
@@ -57,13 +86,33 @@ class EffectsTests: XCTestCase {
57
86
// When
58
87
let action = Test1Action ( )
59
88
effect. run ( with: action)
60
- _ = effect. run ( with: action) // Returns early because of wrong type
89
+ XCTAssertThrowsError ( try effect. run ( with: action, expectedCount : 1 ) ) // Returns early because of wrong type
61
90
// Then
62
91
wait ( for: [ expectation] , timeout: 1 )
63
92
}
93
+
94
+ /// Only here for test coverage of ActionRecorder's empty completion function.
95
+ func testActionRecorderCompletion( ) throws {
96
+ var cancellable : AnyCancellable !
97
+ let effect = Effect . dispatchingOne {
98
+ let publisher = PassthroughSubject < Action , Never > ( )
99
+ cancellable = $0. sink {
100
+ publisher. send ( $0)
101
+ publisher. send ( completion: . finished)
102
+ }
103
+ return publisher. eraseToAnyPublisher ( )
104
+ }
105
+ _ = try effect. run ( with: Test1Action ( ) , expectedCount: 1 )
106
+ XCTAssertNotNil ( cancellable)
107
+ }
64
108
}
65
109
66
110
private struct Test1Action : Action { }
111
+
67
112
private struct Test2Action : Action , Equatable {
68
113
let id = UUID ( )
69
114
}
115
+
116
+ private struct Test3Action : Action , Equatable {
117
+ let id = UUID ( )
118
+ }
0 commit comments