@@ -24,9 +24,9 @@ final class ConcatMapTests: XCTestCase {
24
24
cancellables = [ ]
25
25
}
26
26
27
- func test_publishes_values_inOrder ( ) {
27
+ func test_publishes_values_in_order ( ) {
28
28
var receivedValues = [ Int] ( )
29
- let expectedValues = [ 1 , 2 , 4 , 5 , 6 ]
29
+ let expectedValues = [ 1 , 2 , 3 ]
30
30
31
31
let firstPublisher = P ( )
32
32
let secondPublisher = P ( )
@@ -43,19 +43,89 @@ final class ConcatMapTests: XCTestCase {
43
43
44
44
sut. send ( firstPublisher)
45
45
sut. send ( secondPublisher)
46
+
47
+ firstPublisher. send ( 1 )
48
+ firstPublisher. send ( completion: . finished)
49
+
50
+ secondPublisher. send ( 2 )
46
51
sut. send ( thirdPublisher)
52
+ secondPublisher. send ( completion: . finished)
53
+
54
+ thirdPublisher. send ( 3 )
55
+
56
+ XCTAssertEqual ( expectedValues, receivedValues)
57
+ }
58
+
59
+ func test_ignores_values_of_subsequent_while_previous_hasNot_completed( ) {
60
+ var receivedValues = [ Int] ( )
61
+ let expectedValues = [ 1 , 3 ]
62
+
63
+ let firstPublisher = P ( )
64
+ let secondPublisher = P ( )
65
+
66
+ let sut = PassthroughSubject < P , TestError > ( )
67
+
68
+ sut. concatMap { $0 }
69
+ . sink (
70
+ receiveCompletion: { _ in } ,
71
+ receiveValue: { value in receivedValues. append ( value) }
72
+ )
73
+ . store ( in: & cancellables)
74
+
75
+ sut. send ( firstPublisher)
76
+ sut. send ( secondPublisher)
47
77
48
78
firstPublisher. send ( 1 )
49
- firstPublisher. send ( 2 )
50
- // values sent onto the second publisher will be ignored as long as the first publisher hasn't completed
79
+ secondPublisher. send ( 2 )
80
+ firstPublisher. send ( completion: . finished)
81
+
51
82
secondPublisher. send ( 3 )
83
+ secondPublisher. send ( completion: . finished)
84
+
85
+ XCTAssertEqual ( expectedValues, receivedValues)
86
+ }
87
+
88
+ func test_publishes_values_of_subsequent_publisher_after_emptying_publisher_queue( ) {
89
+ var receivedValues = [ Int] ( )
90
+ let expectedValues = [ 1 , 2 ]
91
+
92
+ let firstPublisher = P ( )
93
+ let secondPublisher = P ( )
94
+
95
+ let sut = PassthroughSubject < P , TestError > ( )
96
+
97
+ sut. concatMap { $0 }
98
+ . sink (
99
+ receiveCompletion: { _ in } ,
100
+ receiveValue: { value in receivedValues. append ( value) }
101
+ )
102
+ . store ( in: & cancellables)
103
+
104
+ sut. send ( firstPublisher)
105
+ firstPublisher. send ( 1 )
52
106
firstPublisher. send ( completion: . finished)
53
107
54
- secondPublisher . send ( 4 )
55
- secondPublisher. send ( 5 )
108
+ sut . send ( secondPublisher )
109
+ secondPublisher. send ( 2 )
56
110
secondPublisher. send ( completion: . finished)
57
111
58
- thirdPublisher. send ( 6 )
112
+ XCTAssertEqual ( expectedValues, receivedValues)
113
+ }
114
+
115
+ func test_synchronous_completion( ) {
116
+ var receivedValues = [ Int] ( )
117
+ let expectedValues = [ 1 , 2 ]
118
+ let firstPublisher = Just < Int > ( 1 )
119
+ let secondPublisher = Just < Int > ( 2 )
120
+
121
+ let sut = PassthroughSubject < Just < Int > , Never > ( )
122
+
123
+ sut. concatMap { $0 }
124
+ . sink { value in receivedValues. append ( value) }
125
+ . store ( in: & cancellables)
126
+
127
+ sut. send ( firstPublisher)
128
+ sut. send ( secondPublisher)
59
129
60
130
XCTAssertEqual ( expectedValues, receivedValues)
61
131
}
0 commit comments