Skip to content

Commit 120d13b

Browse files
authored
Update Tests to Use Turbine (#672)
* Update Turbine Signed-off-by: matt-ramotar <[email protected]> * Update FetcherResponseTests to use Turbine Signed-off-by: matt-ramotar <[email protected]> * Update FetcherResponseTests to use Turbine Signed-off-by: matt-ramotar <[email protected]> * Update FlowStoreTests to use Turbine Signed-off-by: matt-ramotar <[email protected]> * Update MapIndexedTests to use Turbine Signed-off-by: matt-ramotar <[email protected]> * Update HotFlowStoreTests to use Turbine Signed-off-by: matt-ramotar <[email protected]> * Update UpdaterTests to use Turbine Signed-off-by: matt-ramotar <[email protected]> * Update SourceOfTruthErrorsTests to use Turbine Signed-off-by: matt-ramotar <[email protected]> * Update ValueFetcherTests to use Turbine Signed-off-by: matt-ramotar <[email protected]> * Update SourceOfTruthWithBarrierTests to use Turbine Signed-off-by: matt-ramotar <[email protected]> * Update StreamWithoutSourceOfTruthTests to use Turbine Signed-off-by: matt-ramotar <[email protected]> * Delete AssertEmitsExactly.kt Signed-off-by: matt-ramotar <[email protected]> * Run gradlew ktlintFormat Signed-off-by: matt-ramotar <[email protected]> --------- Signed-off-by: matt-ramotar <[email protected]>
1 parent 369fd83 commit 120d13b

File tree

12 files changed

+615
-311
lines changed

12 files changed

+615
-311
lines changed

gradle/libs.versions.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ ktlint = "0.39.0"
2323
kover = "0.9.0-RC"
2424
store = "5.1.0-SNAPSHOT"
2525
truth = "1.1.3"
26+
turbine = "1.2.0"
2627
binary-compatibility-validator = "0.15.0-Beta.2"
2728

2829
[libraries]
@@ -56,7 +57,7 @@ kotlinx-coroutines-test = { group = "org.jetbrains.kotlinx", name = "kotlinx-cor
5657
junit = { group = "junit", name = "junit", version.ref = "junit" }
5758
google-truth = { group = "com.google.truth", name = "truth", version.ref = "truth" }
5859
touchlab-kermit = { group = "co.touchlab", name = "kermit", version.ref = "kermit" }
59-
turbine = "app.cash.turbine:turbine:1.2.0"
60+
turbine = { group = "app.cash.turbine", name = "turbine", version.ref = "turbine" }
6061
binary-compatibility-validator = {module = "org.jetbrains.kotlinx:binary-compatibility-validator", version.ref = "binary-compatibility-validator"}
6162

6263
[plugins]

store/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ kotlin {
2626
dependencies {
2727
implementation(libs.junit)
2828
implementation(libs.kotlinx.coroutines.test)
29+
implementation(libs.turbine)
2930
}
3031
}
3132
}

store/src/commonTest/kotlin/org/mobilenativefoundation/store/store5/FetcherResponseTests.kt

Lines changed: 98 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package org.mobilenativefoundation.store.store5
22

3+
import app.cash.turbine.test
34
import kotlinx.coroutines.ExperimentalCoroutinesApi
45
import kotlinx.coroutines.FlowPreview
56
import kotlinx.coroutines.flow.flowOf
67
import kotlinx.coroutines.flow.map
78
import kotlinx.coroutines.flow.toList
89
import kotlinx.coroutines.test.TestScope
910
import kotlinx.coroutines.test.runTest
10-
import org.mobilenativefoundation.store.store5.util.assertEmitsExactly
1111
import kotlin.test.Test
1212
import kotlin.test.assertEquals
1313
import kotlin.test.assertFailsWith
@@ -34,9 +34,9 @@ class FetcherResponseTests {
3434
}
3535

3636
@Test
37-
fun givenAFetcherThatEmitsErrorAndDataWhenSteamingThenItCanEmitValueAfterAnError() {
38-
val exception = RuntimeException("first error")
37+
fun givenAFetcherThatEmitsErrorAndDataWhenSteamingThenItCanEmitValueAfterAnError() =
3938
testScope.runTest {
39+
val exception = RuntimeException("first error")
4040
val store =
4141
StoreBuilder.from(
4242
fetcher =
@@ -48,18 +48,23 @@ class FetcherResponseTests {
4848
},
4949
).buildWithTestScope()
5050

51-
assertEmitsExactly(
52-
store.stream(
53-
StoreReadRequest.fresh(1),
54-
),
55-
listOf(
51+
store.stream(StoreReadRequest.fresh(1)).test {
52+
assertEquals(
5653
StoreReadResponse.Loading(StoreReadResponseOrigin.Fetcher()),
54+
awaitItem(),
55+
)
56+
57+
assertEquals(
5758
StoreReadResponse.Error.Exception(exception, StoreReadResponseOrigin.Fetcher()),
59+
awaitItem(),
60+
)
61+
62+
assertEquals(
5863
StoreReadResponse.Data("1", StoreReadResponseOrigin.Fetcher()),
59-
),
60-
)
64+
awaitItem(),
65+
)
66+
}
6167
}
62-
}
6368

6469
@Test
6570
fun givenTransformerWhenRawValueThenUnwrappedValueReturnedAndValueIsCached() =
@@ -69,27 +74,32 @@ class FetcherResponseTests {
6974
StoreBuilder
7075
.from(fetcher).buildWithTestScope()
7176

72-
assertEmitsExactly(
73-
pipeline.stream(StoreReadRequest.cached(3, refresh = false)),
74-
listOf(
77+
pipeline.stream(StoreReadRequest.cached(3, refresh = false)).test {
78+
assertEquals(
7579
StoreReadResponse.Loading(
7680
origin = StoreReadResponseOrigin.Fetcher(),
7781
),
82+
awaitItem(),
83+
)
84+
85+
assertEquals(
7886
StoreReadResponse.Data(
7987
value = 9,
8088
origin = StoreReadResponseOrigin.Fetcher(),
8189
),
82-
),
83-
)
84-
assertEmitsExactly(
85-
pipeline.stream(StoreReadRequest.cached(3, refresh = false)),
86-
listOf(
90+
awaitItem(),
91+
)
92+
}
93+
94+
pipeline.stream(StoreReadRequest.cached(3, refresh = false)).test {
95+
assertEquals(
8796
StoreReadResponse.Data(
8897
value = 9,
8998
origin = StoreReadResponseOrigin.Cache,
9099
),
91-
),
92-
)
100+
awaitItem(),
101+
)
102+
}
93103
}
94104

95105
@Test
@@ -110,30 +120,39 @@ class FetcherResponseTests {
110120
StoreBuilder.from(fetcher)
111121
.buildWithTestScope()
112122

113-
assertEmitsExactly(
114-
pipeline.stream(StoreReadRequest.fresh(3)),
115-
listOf(
123+
pipeline.stream(StoreReadRequest.fresh(3)).test {
124+
assertEquals(
116125
StoreReadResponse.Loading(
117126
origin = StoreReadResponseOrigin.Fetcher(),
118127
),
128+
awaitItem(),
129+
)
130+
131+
assertEquals(
119132
StoreReadResponse.Error.Message(
120133
message = "zero",
121134
origin = StoreReadResponseOrigin.Fetcher(),
122135
),
123-
),
124-
)
125-
assertEmitsExactly(
126-
pipeline.stream(StoreReadRequest.cached(3, refresh = false)),
127-
listOf(
136+
awaitItem(),
137+
)
138+
}
139+
140+
pipeline.stream(StoreReadRequest.cached(3, refresh = false)).test {
141+
assertEquals(
128142
StoreReadResponse.Loading(
129143
origin = StoreReadResponseOrigin.Fetcher(),
130144
),
145+
awaitItem(),
146+
)
147+
148+
assertEquals(
131149
StoreReadResponse.Data(
132150
value = 1,
133151
origin = StoreReadResponseOrigin.Fetcher(),
134152
),
135-
),
136-
)
153+
awaitItem(),
154+
)
155+
}
137156
}
138157

139158
@Test
@@ -156,30 +175,39 @@ class FetcherResponseTests {
156175
.from(fetcher)
157176
.buildWithTestScope()
158177

159-
assertEmitsExactly(
160-
pipeline.stream(StoreReadRequest.fresh(3)),
161-
listOf(
178+
pipeline.stream(StoreReadRequest.fresh(3)).test {
179+
assertEquals(
162180
StoreReadResponse.Loading(
163181
origin = StoreReadResponseOrigin.Fetcher(),
164182
),
183+
awaitItem(),
184+
)
185+
186+
assertEquals(
165187
StoreReadResponse.Error.Exception(
166188
error = e,
167189
origin = StoreReadResponseOrigin.Fetcher(),
168190
),
169-
),
170-
)
171-
assertEmitsExactly(
172-
pipeline.stream(StoreReadRequest.cached(3, refresh = false)),
173-
listOf(
191+
awaitItem(),
192+
)
193+
}
194+
195+
pipeline.stream(StoreReadRequest.cached(3, refresh = false)).test {
196+
assertEquals(
174197
StoreReadResponse.Loading(
175198
origin = StoreReadResponseOrigin.Fetcher(),
176199
),
200+
awaitItem(),
201+
)
202+
203+
assertEquals(
177204
StoreReadResponse.Data(
178205
value = 1,
179206
origin = StoreReadResponseOrigin.Fetcher(),
180207
),
181-
),
182-
)
208+
awaitItem(),
209+
)
210+
}
183211
}
184212

185213
@Test
@@ -200,36 +228,46 @@ class FetcherResponseTests {
200228
.from(fetcher = fetcher)
201229
.buildWithTestScope()
202230

203-
assertEmitsExactly(
204-
pipeline.stream(StoreReadRequest.fresh(3)),
205-
listOf(
231+
pipeline.stream(StoreReadRequest.fresh(3)).test {
232+
assertEquals(
206233
StoreReadResponse.Loading(
207234
origin = StoreReadResponseOrigin.Fetcher(),
208235
),
236+
awaitItem(),
237+
)
238+
239+
assertEquals(
209240
StoreReadResponse.Error.Exception(
210241
error = e,
211242
origin = StoreReadResponseOrigin.Fetcher(),
212243
),
213-
),
214-
)
215-
assertEmitsExactly(
216-
pipeline.stream(StoreReadRequest.cached(3, refresh = false)),
217-
listOf(
244+
awaitItem(),
245+
)
246+
}
247+
248+
pipeline.stream(StoreReadRequest.cached(3, refresh = false)).test {
249+
assertEquals(
218250
StoreReadResponse.Loading(
219251
origin = StoreReadResponseOrigin.Fetcher(),
220252
),
253+
awaitItem(),
254+
)
255+
256+
assertEquals(
221257
StoreReadResponse.Data(
222258
value = 1,
223259
origin = StoreReadResponseOrigin.Fetcher(),
224260
),
225-
),
226-
)
261+
awaitItem(),
262+
)
263+
}
227264
}
228265

229266
@Test
230267
fun givenAFetcherThatEmitsCustomErrorWhenStreamingThenCustomErrorShouldBeEmitted() =
231268
testScope.runTest {
232269
data class TestCustomError(val errorMessage: String)
270+
233271
val customError = TestCustomError("Test custom error")
234272

235273
val store =
@@ -242,16 +280,20 @@ class FetcherResponseTests {
242280
},
243281
).buildWithTestScope()
244282

245-
assertEmitsExactly(
246-
store.stream(StoreReadRequest.fresh(1)),
247-
listOf(
283+
store.stream(StoreReadRequest.fresh(1)).test {
284+
assertEquals(
248285
StoreReadResponse.Loading(origin = StoreReadResponseOrigin.Fetcher()),
286+
awaitItem(),
287+
)
288+
289+
assertEquals(
249290
StoreReadResponse.Error.Custom(
250291
error = customError,
251292
origin = StoreReadResponseOrigin.Fetcher(),
252293
),
253-
),
254-
)
294+
awaitItem(),
295+
)
296+
}
255297
}
256298

257299
private fun <Key : Any, Output : Any> StoreBuilder<Key, Output>.buildWithTestScope() = scope(testScope).build()

0 commit comments

Comments
 (0)