Skip to content

Commit 6018efe

Browse files
committed
[#20] Add test: clear cache data
1 parent b3ee93f commit 6018efe

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

shared/src/commonMain/kotlin/vn/luongvo/kmm/survey/data/local/datasource/SurveyLocalDataSource.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface SurveyLocalDataSource {
1111

1212
fun getSurveys(): List<SurveyRealmObject>
1313

14-
fun deleteAllSurveys()
14+
fun clear()
1515
}
1616

1717
class SurveyLocalDataSourceImpl(private val realm: Realm) : SurveyLocalDataSource {
@@ -28,7 +28,7 @@ class SurveyLocalDataSourceImpl(private val realm: Realm) : SurveyLocalDataSourc
2828
return realm.query<SurveyRealmObject>().find()
2929
}
3030

31-
override fun deleteAllSurveys() {
31+
override fun clear() {
3232
realm.writeBlocking {
3333
val surveys = query<SurveyRealmObject>().find()
3434
delete(surveys)

shared/src/commonMain/kotlin/vn/luongvo/kmm/survey/data/repository/SurveyRepositoryImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class SurveyRepositoryImpl(
1717

1818
override fun getSurveys(pageNumber: Int, pageSize: Int, isRefresh: Boolean): Flow<List<Survey>> = flowTransform {
1919
if (isRefresh) {
20-
surveyLocalDataSource.deleteAllSurveys()
20+
surveyLocalDataSource.clear()
2121
}
2222

2323
val surveys = surveyRemoteDataSource

shared/src/commonTest/kotlin/vn/luongvo/kmm/survey/data/repository/SurveyRepositoryTest.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,23 @@ class SurveyRepositoryTest {
5555
}
5656
}
5757

58+
@Test
59+
fun `when calling getSurveys with isRefresh = true - it clears the cache`() = runTest {
60+
given(mockRemoteDataSource)
61+
.suspendFunction(mockRemoteDataSource::getSurveys)
62+
.whenInvokedWith(any(), any())
63+
.thenReturn(surveyResponses)
64+
65+
repository.getSurveys(pageNumber = 1, pageSize = 10, isRefresh = true).test {
66+
awaitItem() shouldBe surveys
67+
awaitComplete()
68+
69+
verify(mockLocalDataSource)
70+
.function(mockLocalDataSource::clear)
71+
.wasInvoked(exactly = 1.time)
72+
}
73+
}
74+
5875
@Test
5976
fun `when calling getSurveys fails - it does not cache and throws the corresponding error`() = runTest {
6077
val throwable = Throwable()

0 commit comments

Comments
 (0)