Skip to content

Commit e0be6ef

Browse files
committed
Adding unit test for repository
1 parent 212021e commit e0be6ef

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

vector/src/main/java/im/vector/app/features/roomprofile/polls/list/data/RoomPollRepository.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import im.vector.app.features.roomprofile.polls.list.ui.PollSummary
2020
import kotlinx.coroutines.flow.Flow
2121
import javax.inject.Inject
2222

23-
// TODO add unit tests
2423
class RoomPollRepository @Inject constructor(
2524
private val roomPollDataSource: RoomPollDataSource,
2625
) {
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright (c) 2023 New Vector Ltd
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package im.vector.app.features.roomprofile.polls.list.data
18+
19+
import im.vector.app.features.roomprofile.polls.list.ui.PollSummary
20+
import io.mockk.coJustRun
21+
import io.mockk.coVerify
22+
import io.mockk.every
23+
import io.mockk.mockk
24+
import io.mockk.verify
25+
import kotlinx.coroutines.flow.firstOrNull
26+
import kotlinx.coroutines.flow.flowOf
27+
import kotlinx.coroutines.test.runTest
28+
import org.amshove.kluent.shouldBeEqualTo
29+
import org.junit.Test
30+
31+
private const val A_ROOM_ID = "room-id"
32+
33+
class RoomPollRepositoryTest {
34+
35+
private val fakeRoomPollDataSource = mockk<RoomPollDataSource>()
36+
37+
private val roomPollRepository = RoomPollRepository(
38+
roomPollDataSource = fakeRoomPollDataSource,
39+
)
40+
41+
@Test
42+
fun `given data source when getting polls then correct method of data source is called`() = runTest {
43+
// Given
44+
val expectedPolls = listOf<PollSummary>()
45+
every { fakeRoomPollDataSource.getPolls(A_ROOM_ID) } returns flowOf(expectedPolls)
46+
47+
// When
48+
val result = roomPollRepository.getPolls(A_ROOM_ID).firstOrNull()
49+
50+
// Then
51+
result shouldBeEqualTo expectedPolls
52+
verify { fakeRoomPollDataSource.getPolls(A_ROOM_ID) }
53+
}
54+
55+
@Test
56+
fun `given data source when getting loaded polls status then correct method of data source is called`() {
57+
// Given
58+
val expectedStatus = LoadedPollsStatus(
59+
canLoadMore = true,
60+
nbLoadedDays = 10,
61+
)
62+
every { fakeRoomPollDataSource.getLoadedPollsStatus(A_ROOM_ID) } returns expectedStatus
63+
64+
// When
65+
val result = roomPollRepository.getLoadedPollsStatus(A_ROOM_ID)
66+
67+
// Then
68+
result shouldBeEqualTo expectedStatus
69+
verify { fakeRoomPollDataSource.getLoadedPollsStatus(A_ROOM_ID) }
70+
}
71+
72+
@Test
73+
fun `given data source when loading more polls then correct method of data source is called`() = runTest {
74+
// Given
75+
coJustRun { fakeRoomPollDataSource.loadMorePolls(A_ROOM_ID) }
76+
77+
// When
78+
roomPollRepository.loadMorePolls(A_ROOM_ID)
79+
80+
// Then
81+
coVerify { fakeRoomPollDataSource.loadMorePolls(A_ROOM_ID) }
82+
}
83+
84+
@Test
85+
fun `given data source when syncing polls then correct method of data source is called`() = runTest {
86+
// Given
87+
coJustRun { fakeRoomPollDataSource.syncPolls(A_ROOM_ID) }
88+
89+
// When
90+
roomPollRepository.syncPolls(A_ROOM_ID)
91+
92+
// Then
93+
coVerify { fakeRoomPollDataSource.syncPolls(A_ROOM_ID) }
94+
}
95+
}

0 commit comments

Comments
 (0)