Skip to content

Commit 212021e

Browse files
committed
Adding unit test for use cases
1 parent 98fff95 commit 212021e

File tree

9 files changed

+265
-5
lines changed

9 files changed

+265
-5
lines changed

vector/src/main/java/im/vector/app/features/roomprofile/polls/list/domain/GetLoadedPollsStatusUseCase.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.data.LoadedPollsStatus
2020
import im.vector.app.features.roomprofile.polls.list.data.RoomPollRepository
2121
import javax.inject.Inject
2222

23-
// TODO add unit tests
2423
class GetLoadedPollsStatusUseCase @Inject constructor(
2524
private val roomPollRepository: RoomPollRepository,
2625
) {

vector/src/main/java/im/vector/app/features/roomprofile/polls/list/domain/GetPollsUseCase.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import kotlinx.coroutines.flow.Flow
2222
import kotlinx.coroutines.flow.map
2323
import javax.inject.Inject
2424

25-
// TODO add unit tests
2625
class GetPollsUseCase @Inject constructor(
2726
private val roomPollRepository: RoomPollRepository,
2827
) {

vector/src/main/java/im/vector/app/features/roomprofile/polls/list/domain/LoadMorePollsUseCase.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.data.LoadedPollsStatus
2020
import im.vector.app.features.roomprofile.polls.list.data.RoomPollRepository
2121
import javax.inject.Inject
2222

23-
// TODO add unit tests
2423
class LoadMorePollsUseCase @Inject constructor(
2524
private val roomPollRepository: RoomPollRepository,
2625
) {

vector/src/test/java/im/vector/app/features/roomprofile/polls/RoomPollsViewModelTest.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
package im.vector.app.features.roomprofile.polls
1818

1919
import com.airbnb.mvrx.test.MavericksTestRule
20+
import im.vector.app.features.roomprofile.polls.list.domain.GetLoadedPollsStatusUseCase
2021
import im.vector.app.features.roomprofile.polls.list.ui.PollSummary
2122
import im.vector.app.features.roomprofile.polls.list.domain.GetPollsUseCase
23+
import im.vector.app.features.roomprofile.polls.list.domain.LoadMorePollsUseCase
24+
import im.vector.app.features.roomprofile.polls.list.domain.SyncPollsUseCase
2225
import im.vector.app.test.test
2326
import im.vector.app.test.testDispatcher
2427
import io.mockk.every
@@ -36,20 +39,26 @@ class RoomPollsViewModelTest {
3639
val mavericksTestRule = MavericksTestRule(testDispatcher = testDispatcher)
3740

3841
private val fakeGetPollsUseCase = mockk<GetPollsUseCase>()
42+
private val fakeGetLoadedPollsStatusUseCase = mockk<GetLoadedPollsStatusUseCase>()
43+
private val fakeLoadMorePollsUseCase = mockk<LoadMorePollsUseCase>()
44+
private val fakeSyncPollsUseCase = mockk<SyncPollsUseCase>()
3945
private val initialState = RoomPollsViewState(ROOM_ID)
4046

4147
private fun createViewModel(): RoomPollsViewModel {
4248
return RoomPollsViewModel(
4349
initialState = initialState,
4450
getPollsUseCase = fakeGetPollsUseCase,
51+
getLoadedPollsStatusUseCase = fakeGetLoadedPollsStatusUseCase,
52+
loadMorePollsUseCase = fakeLoadMorePollsUseCase,
53+
syncPollsUseCase = fakeSyncPollsUseCase,
4554
)
4655
}
4756

4857
@Test
4958
fun `given viewModel when created then polls list is observed and viewState is updated`() {
5059
// Given
5160
val polls = listOf(givenAPollSummary())
52-
every { fakeGetPollsUseCase.execute() } returns flowOf(polls)
61+
every { fakeGetPollsUseCase.execute(ROOM_ID) } returns flowOf(polls)
5362
val expectedViewState = initialState.copy(polls = polls)
5463

5564
// When
@@ -61,7 +70,7 @@ class RoomPollsViewModelTest {
6170
.assertLatestState(expectedViewState)
6271
.finish()
6372
verify {
64-
fakeGetPollsUseCase.execute()
73+
fakeGetPollsUseCase.execute(ROOM_ID)
6574
}
6675
}
6776

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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.domain
18+
19+
import im.vector.app.features.roomprofile.polls.list.data.LoadedPollsStatus
20+
import im.vector.app.features.roomprofile.polls.list.data.RoomPollRepository
21+
import io.mockk.every
22+
import io.mockk.mockk
23+
import io.mockk.verify
24+
import org.amshove.kluent.shouldBeEqualTo
25+
import org.junit.Test
26+
27+
class GetLoadedPollsStatusUseCaseTest {
28+
29+
private val fakeRoomPollRepository = mockk<RoomPollRepository>()
30+
31+
private val getLoadedPollsStatusUseCase = GetLoadedPollsStatusUseCase(
32+
roomPollRepository = fakeRoomPollRepository,
33+
)
34+
35+
@Test
36+
fun `given repo when execute then correct method of repo is called`() {
37+
// Given
38+
val aRoomId = "roomId"
39+
val expectedStatus = LoadedPollsStatus(
40+
canLoadMore = true,
41+
nbLoadedDays = 10,
42+
)
43+
every { fakeRoomPollRepository.getLoadedPollsStatus(aRoomId) } returns expectedStatus
44+
45+
// When
46+
val status = getLoadedPollsStatusUseCase.execute(aRoomId)
47+
48+
// Then
49+
status shouldBeEqualTo expectedStatus
50+
verify { fakeRoomPollRepository.getLoadedPollsStatus(aRoomId) }
51+
}
52+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.domain
18+
19+
import im.vector.app.features.roomprofile.polls.list.data.RoomPollRepository
20+
import im.vector.app.features.roomprofile.polls.list.ui.PollSummary
21+
import im.vector.app.test.fixtures.RoomPollFixture
22+
import io.mockk.every
23+
import io.mockk.mockk
24+
import io.mockk.verify
25+
import kotlinx.coroutines.flow.first
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+
class GetPollsUseCaseTest {
32+
private val fakeRoomPollRepository = mockk<RoomPollRepository>()
33+
34+
private val getPollsUseCase = GetPollsUseCase(
35+
roomPollRepository = fakeRoomPollRepository,
36+
)
37+
38+
@Test
39+
fun `given repo when execute then correct method of repo is called and polls are sorted most recent first`() = runTest {
40+
// Given
41+
val aRoomId = "roomId"
42+
val poll1 = RoomPollFixture.anActivePollSummary(timestamp = 1)
43+
val poll2 = RoomPollFixture.anActivePollSummary(timestamp = 2)
44+
val poll3 = RoomPollFixture.anActivePollSummary(timestamp = 3)
45+
val polls = listOf<PollSummary>(
46+
poll1,
47+
poll2,
48+
poll3,
49+
)
50+
every { fakeRoomPollRepository.getPolls(aRoomId) } returns flowOf(polls)
51+
val expectedPolls = listOf<PollSummary>(
52+
poll3,
53+
poll2,
54+
poll1,
55+
)
56+
// When
57+
val result = getPollsUseCase.execute(aRoomId).first()
58+
59+
// Then
60+
result shouldBeEqualTo expectedPolls
61+
verify { fakeRoomPollRepository.getPolls(aRoomId) }
62+
}
63+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.domain
18+
19+
import im.vector.app.features.roomprofile.polls.list.data.RoomPollRepository
20+
import io.mockk.coJustRun
21+
import io.mockk.coVerify
22+
import io.mockk.mockk
23+
import kotlinx.coroutines.test.runTest
24+
import org.junit.Test
25+
26+
class LoadMorePollsUseCaseTest {
27+
28+
private val fakeRoomPollRepository = mockk<RoomPollRepository>()
29+
30+
private val loadMorePollsUseCase = LoadMorePollsUseCase(
31+
roomPollRepository = fakeRoomPollRepository,
32+
)
33+
34+
@Test
35+
fun `given repo when execute then correct method of repo is called`() = runTest {
36+
// Given
37+
val aRoomId = "roomId"
38+
coJustRun { fakeRoomPollRepository.loadMorePolls(aRoomId) }
39+
40+
// When
41+
loadMorePollsUseCase.execute(aRoomId)
42+
43+
// Then
44+
coVerify { fakeRoomPollRepository.loadMorePolls(aRoomId) }
45+
}
46+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.domain
18+
19+
import im.vector.app.features.roomprofile.polls.list.data.RoomPollRepository
20+
import io.mockk.coJustRun
21+
import io.mockk.coVerify
22+
import io.mockk.mockk
23+
import kotlinx.coroutines.test.runTest
24+
import org.junit.Test
25+
26+
class SyncPollsUseCaseTest {
27+
28+
private val fakeRoomPollRepository = mockk<RoomPollRepository>()
29+
30+
private val syncPollsUseCase = SyncPollsUseCase(
31+
roomPollRepository = fakeRoomPollRepository,
32+
)
33+
34+
@Test
35+
fun `given repo when execute then correct method of repo is called`() = runTest {
36+
// Given
37+
val aRoomId = "roomId"
38+
coJustRun { fakeRoomPollRepository.syncPolls(aRoomId) }
39+
40+
// When
41+
syncPollsUseCase.execute(aRoomId)
42+
43+
// Then
44+
coVerify { fakeRoomPollRepository.syncPolls(aRoomId) }
45+
}
46+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.test.fixtures
18+
19+
import im.vector.app.features.home.room.detail.timeline.item.PollOptionViewState
20+
import im.vector.app.features.roomprofile.polls.list.ui.PollSummary
21+
22+
object RoomPollFixture {
23+
24+
fun anActivePollSummary(
25+
id: String = "",
26+
timestamp: Long,
27+
title: String = "",
28+
) = PollSummary.ActivePoll(
29+
id = id,
30+
creationTimestamp = timestamp,
31+
title = title,
32+
)
33+
34+
fun anEndedPollSummary(
35+
id: String = "",
36+
timestamp: Long,
37+
title: String = "",
38+
totalVotes: Int,
39+
winnerOptions: List<PollOptionViewState.PollEnded>
40+
) = PollSummary.EndedPoll(
41+
id = id,
42+
creationTimestamp = timestamp,
43+
title = title,
44+
totalVotes = totalVotes,
45+
winnerOptions = winnerOptions,
46+
)
47+
}

0 commit comments

Comments
 (0)