Skip to content

Commit e839e2b

Browse files
committed
Update UpdaterTests to use Turbine
Signed-off-by: matt-ramotar <[email protected]>
1 parent 87c3f43 commit e839e2b

File tree

1 file changed

+47
-40
lines changed
  • store/src/commonTest/kotlin/org/mobilenativefoundation/store/store5

1 file changed

+47
-40
lines changed

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

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.mobilenativefoundation.store.store5
22

3+
import app.cash.turbine.test
34
import kotlinx.coroutines.ExperimentalCoroutinesApi
45
import kotlinx.coroutines.flow.first
56
import kotlinx.coroutines.flow.flow
@@ -9,7 +10,6 @@ import kotlinx.coroutines.test.TestScope
910
import kotlinx.coroutines.test.runTest
1011
import org.mobilenativefoundation.store.core5.ExperimentalStoreApi
1112
import org.mobilenativefoundation.store.store5.impl.extensions.inHours
12-
import org.mobilenativefoundation.store.store5.util.assertEmitsExactly
1313
import org.mobilenativefoundation.store.store5.util.fake.Notes
1414
import org.mobilenativefoundation.store.store5.util.fake.NotesApi
1515
import org.mobilenativefoundation.store.store5.util.fake.NotesBookkeeping
@@ -63,12 +63,12 @@ class UpdaterTests {
6363
MutableStoreBuilder.from<NotesKey, NetworkNote, InputNote, OutputNote>(
6464
fetcher = Fetcher.of { key -> api.get(key, ttl = ttl) },
6565
sourceOfTruth =
66-
SourceOfTruth.of(
67-
nonFlowReader = { key -> notes.get(key) },
68-
writer = { key, sot: InputNote -> notes.put(key, sot) },
69-
delete = { key -> notes.clear(key) },
70-
deleteAll = { notes.clear() },
71-
),
66+
SourceOfTruth.of(
67+
nonFlowReader = { key -> notes.get(key) },
68+
writer = { key, sot: InputNote -> notes.put(key, sot) },
69+
delete = { key -> notes.clear(key) },
70+
deleteAll = { notes.clear() },
71+
),
7272
converter = converter,
7373
)
7474
.validator(validator)
@@ -82,18 +82,20 @@ class UpdaterTests {
8282
val stream = store.stream<NotesWriteResponse>(readRequest)
8383

8484
// Read is success
85-
val expected =
86-
listOf(
85+
stream.test {
86+
assertEquals(
8787
StoreReadResponse.Loading(origin = StoreReadResponseOrigin.Fetcher()),
88+
awaitItem()
89+
)
90+
91+
assertEquals(
8892
StoreReadResponse.Data(
8993
OutputNote(NoteData.Single(Notes.One), ttl = ttl),
9094
StoreReadResponseOrigin.Fetcher(),
9195
),
96+
awaitItem()
9297
)
93-
assertEmitsExactly(
94-
stream,
95-
expected,
96-
)
98+
}
9799

98100
val newNote = Notes.One.copy(title = "New Title-1")
99101
val writeRequest =
@@ -174,12 +176,12 @@ class UpdaterTests {
174176
MutableStoreBuilder.from<NotesKey, NetworkNote, InputNote, OutputNote>(
175177
fetcher = Fetcher.of { key -> api.get(key, ttl = ttl) },
176178
sourceOfTruth =
177-
SourceOfTruth.of(
178-
nonFlowReader = { key -> notes.get(key) },
179-
writer = { key, sot: InputNote -> notes.put(key, sot) },
180-
delete = { key -> notes.clear(key) },
181-
deleteAll = { notes.clear() },
182-
),
179+
SourceOfTruth.of(
180+
nonFlowReader = { key -> notes.get(key) },
181+
writer = { key, sot: InputNote -> notes.put(key, sot) },
182+
delete = { key -> notes.clear(key) },
183+
deleteAll = { notes.clear() },
184+
),
183185
converter = converter,
184186
)
185187
.validator(validator)
@@ -193,18 +195,20 @@ class UpdaterTests {
193195
val stream = store.stream<NotesWriteResponse>(readRequest)
194196

195197
// Fetch is success and validator is not used
196-
val expected =
197-
listOf(
198+
stream.test {
199+
assertEquals(
198200
StoreReadResponse.Loading(origin = StoreReadResponseOrigin.Fetcher()),
201+
awaitItem()
202+
)
203+
204+
assertEquals(
199205
StoreReadResponse.Data(
200206
OutputNote(NoteData.Single(Notes.One), ttl = ttl),
201207
StoreReadResponseOrigin.Fetcher(),
202208
),
209+
awaitItem()
203210
)
204-
assertEmitsExactly(
205-
stream,
206-
expected,
207-
)
211+
}
208212

209213
val cachedReadRequest =
210214
StoreReadRequest.cached(NotesKey.Single(Notes.One.id), refresh = false)
@@ -215,16 +219,19 @@ class UpdaterTests {
215219
// So we do not emit value in cache or SOT
216220
// Instead we get latest from network even though refresh = false
217221

218-
assertEmitsExactly(
219-
cachedStream,
220-
listOf(
222+
cachedStream.test {
223+
assertEquals(
221224
StoreReadResponse.Loading(origin = StoreReadResponseOrigin.Fetcher(name = null)),
225+
awaitItem()
226+
)
227+
assertEquals(
222228
StoreReadResponse.Data(
223229
OutputNote(NoteData.Single(Notes.One), ttl = ttl),
224230
StoreReadResponseOrigin.Fetcher(),
225231
),
226-
),
227-
)
232+
awaitItem()
233+
)
234+
}
228235
}
229236

230237
@Test
@@ -244,17 +251,17 @@ class UpdaterTests {
244251
val store =
245252
MutableStoreBuilder.from<NotesKey, NetworkNote, InputNote, OutputNote>(
246253
fetcher =
247-
Fetcher.ofFlow { key ->
248-
val network = api.get(key)
249-
flow { emit(network) }
250-
},
254+
Fetcher.ofFlow { key ->
255+
val network = api.get(key)
256+
flow { emit(network) }
257+
},
251258
sourceOfTruth =
252-
SourceOfTruth.of(
253-
nonFlowReader = { key -> notes.get(key) },
254-
writer = { key, sot -> notes.put(key, sot) },
255-
delete = { key -> notes.clear(key) },
256-
deleteAll = { notes.clear() },
257-
),
259+
SourceOfTruth.of(
260+
nonFlowReader = { key -> notes.get(key) },
261+
writer = { key, sot -> notes.put(key, sot) },
262+
delete = { key -> notes.clear(key) },
263+
deleteAll = { notes.clear() },
264+
),
258265
converter,
259266
)
260267
.validator(validator)

0 commit comments

Comments
 (0)