Updating existing unit tests

This commit is contained in:
Maxime NATUREL 2023-01-24 15:37:53 +01:00
parent db2e2916a5
commit 470218ca52
10 changed files with 300 additions and 143 deletions

View File

@ -58,6 +58,7 @@ internal class DefaultFilterAndStoreEventsTask @Inject constructor(
private suspend fun addMissingEventsInDB(roomId: String, events: List<Event>) {
monarchy.awaitTransaction { realm ->
// TODO we should insert TimelineEventEntity as well, how to do that????
val eventIdsToCheck = events.mapNotNull { it.eventId }.filter { it.isNotEmpty() }
if (eventIdsToCheck.isNotEmpty()) {
val existingIds = EventEntity.where(realm, eventIdsToCheck).findAll().toList().map { it.eventId }

View File

@ -25,6 +25,7 @@ class RoomPollRepository @Inject constructor(
private val roomPollDataSource: RoomPollDataSource,
) {
// TODO add unit tests
fun dispose(roomId: String) {
roomPollDataSource.dispose(roomId)
}

View File

@ -26,6 +26,7 @@ class DisposePollHistoryUseCase @Inject constructor(
private val roomPollRepository: RoomPollRepository,
) {
// TODO add unit tests
fun execute(roomId: String) {
roomPollRepository.dispose(roomId)
}

View File

@ -25,6 +25,9 @@ import im.vector.app.features.home.room.detail.timeline.item.ReactionsSummaryDat
import im.vector.app.features.home.room.detail.timeline.style.TimelineMessageLayout
import im.vector.app.features.poll.PollViewState
import im.vector.app.test.fakes.FakeStringProvider
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import org.amshove.kluent.shouldBeEqualTo
import org.junit.Test
import org.matrix.android.sdk.api.session.room.model.message.MessagePollContent
@ -56,88 +59,69 @@ private val A_POLL_CONTENT = MessagePollContent(
unstablePollCreationInfo = PollCreationInfo(
question = PollQuestion(
unstableQuestion = "What is your favourite coffee?"
), kind = PollType.UNDISCLOSED_UNSTABLE, maxSelections = 1, answers = listOf(
PollAnswer(
id = A_POLL_OPTION_IDS[0], unstableAnswer = "Double Espresso"
),
kind = PollType.UNDISCLOSED_UNSTABLE,
maxSelections = 1,
answers = listOf(
PollAnswer(
id = A_POLL_OPTION_IDS[0],
unstableAnswer = "Double Espresso"
),
PollAnswer(
id = A_POLL_OPTION_IDS[1],
unstableAnswer = "Macchiato"
),
PollAnswer(
id = A_POLL_OPTION_IDS[2],
unstableAnswer = "Iced Coffee"
),
)
PollAnswer(
id = A_POLL_OPTION_IDS[1], unstableAnswer = "Macchiato"
),
PollAnswer(
id = A_POLL_OPTION_IDS[2], unstableAnswer = "Iced Coffee"
),
)
)
)
class PollItemViewStateFactoryTest {
private val fakeStringProvider = FakeStringProvider()
private val fakePollOptionViewStateFactory = mockk<PollOptionViewStateFactory>()
private val pollItemViewStateFactory = PollItemViewStateFactory(
stringProvider = fakeStringProvider.instance,
pollOptionViewStateFactory = fakePollOptionViewStateFactory,
)
@Test
fun `given a sending poll state then poll is not votable and option states are PollSending`() {
val stringProvider = FakeStringProvider()
val pollItemViewStateFactory = PollItemViewStateFactory(stringProvider.instance)
// Given
val sendingPollInformationData = A_MESSAGE_INFORMATION_DATA.copy(sendState = SendState.SENDING)
val optionViewStates = listOf(PollOptionViewState.PollSending(optionId = "", optionAnswer = ""))
every { fakePollOptionViewStateFactory.createPollSendingOptions(A_POLL_CONTENT.getBestPollCreationInfo()) } returns optionViewStates
// When
val pollViewState = pollItemViewStateFactory.create(
pollContent = A_POLL_CONTENT,
informationData = sendingPollInformationData,
)
// Then
pollViewState shouldBeEqualTo PollViewState(
question = A_POLL_CONTENT.getBestPollCreationInfo()?.question?.getBestQuestion() ?: "",
votesStatus = stringProvider.instance.getString(R.string.poll_no_votes_cast),
votesStatus = fakeStringProvider.instance.getString(R.string.poll_no_votes_cast),
canVote = false,
optionViewStates = A_POLL_CONTENT.getBestPollCreationInfo()?.answers?.map { answer ->
PollOptionViewState.PollSending(
optionId = answer.id ?: "",
optionAnswer = answer.getBestAnswer() ?: ""
)
},
optionViewStates = optionViewStates,
)
verify { fakePollOptionViewStateFactory.createPollSendingOptions(A_POLL_CONTENT.getBestPollCreationInfo()) }
}
@Test
fun `given a sent poll state when poll is closed then poll is not votable and option states are Ended`() {
val stringProvider = FakeStringProvider()
val pollItemViewStateFactory = PollItemViewStateFactory(stringProvider.instance)
// Given
val closedPollSummary = A_POLL_RESPONSE_DATA.copy(isClosed = true)
val closedPollInformationData = A_MESSAGE_INFORMATION_DATA.copy(pollResponseAggregatedSummary = closedPollSummary)
val pollViewState = pollItemViewStateFactory.create(
pollContent = A_POLL_CONTENT,
informationData = closedPollInformationData,
val optionViewStates = listOf(
PollOptionViewState.PollEnded(
optionId = "", optionAnswer = "", voteCount = 0, votePercentage = 0.0, isWinner = false
)
)
pollViewState shouldBeEqualTo PollViewState(
question = A_POLL_CONTENT.getBestPollCreationInfo()?.question?.getBestQuestion() ?: "",
votesStatus = stringProvider.instance.getQuantityString(R.plurals.poll_total_vote_count_after_ended, 0, 0),
canVote = false,
optionViewStates = A_POLL_CONTENT.getBestPollCreationInfo()?.answers?.map { answer ->
PollOptionViewState.PollEnded(
optionId = answer.id ?: "",
optionAnswer = answer.getBestAnswer() ?: "",
voteCount = 0,
votePercentage = 0.0,
isWinner = false
)
},
)
}
@Test
fun `given a sent poll state with some decryption error when poll is closed then warning message is displayed`() {
// Given
val stringProvider = FakeStringProvider()
val pollItemViewStateFactory = PollItemViewStateFactory(stringProvider.instance)
val closedPollSummary = A_POLL_RESPONSE_DATA.copy(isClosed = true, hasEncryptedRelatedEvents = true)
val closedPollInformationData = A_MESSAGE_INFORMATION_DATA.copy(pollResponseAggregatedSummary = closedPollSummary)
every {
fakePollOptionViewStateFactory.createPollEndedOptions(
A_POLL_CONTENT.getBestPollCreationInfo(),
closedPollInformationData.pollResponseAggregatedSummary,
)
} returns optionViewStates
// When
val pollViewState = pollItemViewStateFactory.create(
@ -146,42 +130,90 @@ class PollItemViewStateFactoryTest {
)
// Then
pollViewState.votesStatus shouldBeEqualTo stringProvider.instance.getString(R.string.unable_to_decrypt_some_events_in_poll)
pollViewState shouldBeEqualTo PollViewState(
question = A_POLL_CONTENT.getBestPollCreationInfo()?.question?.getBestQuestion() ?: "",
votesStatus = fakeStringProvider.instance.getQuantityString(R.plurals.poll_total_vote_count_after_ended, 0, 0),
canVote = false,
optionViewStates = optionViewStates,
)
verify {
fakePollOptionViewStateFactory.createPollEndedOptions(
A_POLL_CONTENT.getBestPollCreationInfo(),
closedPollInformationData.pollResponseAggregatedSummary,
)
}
}
@Test
fun `given a sent poll state with some decryption error when poll is closed then warning message is displayed`() {
// Given
val closedPollSummary = A_POLL_RESPONSE_DATA.copy(isClosed = true, hasEncryptedRelatedEvents = true)
val closedPollInformationData = A_MESSAGE_INFORMATION_DATA.copy(pollResponseAggregatedSummary = closedPollSummary)
val optionViewStates = listOf(
PollOptionViewState.PollEnded(
optionId = "", optionAnswer = "", voteCount = 0, votePercentage = 0.0, isWinner = false
)
)
every {
fakePollOptionViewStateFactory.createPollEndedOptions(
A_POLL_CONTENT.getBestPollCreationInfo(),
closedPollInformationData.pollResponseAggregatedSummary,
)
} returns optionViewStates
// When
val pollViewState = pollItemViewStateFactory.create(
pollContent = A_POLL_CONTENT,
informationData = closedPollInformationData,
)
// Then
pollViewState.votesStatus shouldBeEqualTo fakeStringProvider.instance.getString(R.string.unable_to_decrypt_some_events_in_poll)
}
@Test
fun `given a sent poll when undisclosed poll type is selected then poll is votable and option states are PollUndisclosed`() {
val stringProvider = FakeStringProvider()
val pollItemViewStateFactory = PollItemViewStateFactory(stringProvider.instance)
// Given
val optionViewStates = listOf(
PollOptionViewState.PollUndisclosed(
optionId = "",
optionAnswer = "",
isSelected = false,
)
)
every {
fakePollOptionViewStateFactory.createPollUndisclosedOptions(
A_POLL_CONTENT.getBestPollCreationInfo(),
A_MESSAGE_INFORMATION_DATA.pollResponseAggregatedSummary,
)
} returns optionViewStates
// When
val pollViewState = pollItemViewStateFactory.create(
pollContent = A_POLL_CONTENT,
informationData = A_MESSAGE_INFORMATION_DATA,
)
// Then
pollViewState shouldBeEqualTo PollViewState(
question = A_POLL_CONTENT.getBestPollCreationInfo()?.question?.getBestQuestion() ?: "",
votesStatus = stringProvider.instance.getString(R.string.poll_undisclosed_not_ended),
votesStatus = fakeStringProvider.instance.getString(R.string.poll_undisclosed_not_ended),
canVote = true,
optionViewStates = A_POLL_CONTENT.getBestPollCreationInfo()?.answers?.map { answer ->
PollOptionViewState.PollUndisclosed(
optionId = answer.id ?: "",
optionAnswer = answer.getBestAnswer() ?: "",
isSelected = false
)
},
optionViewStates = optionViewStates,
)
verify {
fakePollOptionViewStateFactory.createPollUndisclosedOptions(
A_POLL_CONTENT.getBestPollCreationInfo(),
A_MESSAGE_INFORMATION_DATA.pollResponseAggregatedSummary,
)
}
}
@Test
fun `given a sent poll when my vote exists then poll is still votable and options states are PollVoted`() {
val stringProvider = FakeStringProvider()
val pollItemViewStateFactory = PollItemViewStateFactory(stringProvider.instance)
// Given
val votedPollData = A_POLL_RESPONSE_DATA.copy(
totalVotes = 1,
myVote = A_POLL_OPTION_IDS[0],
votes = mapOf(A_POLL_OPTION_IDS[0] to PollVoteSummaryData(total = 1, percentage = 1.0))
totalVotes = 1, myVote = A_POLL_OPTION_IDS[0], votes = mapOf(A_POLL_OPTION_IDS[0] to PollVoteSummaryData(total = 1, percentage = 1.0))
)
val disclosedPollContent = A_POLL_CONTENT.copy(
unstablePollCreationInfo = A_POLL_CONTENT.getBestPollCreationInfo()?.copy(
@ -189,33 +221,46 @@ class PollItemViewStateFactoryTest {
),
)
val votedInformationData = A_MESSAGE_INFORMATION_DATA.copy(pollResponseAggregatedSummary = votedPollData)
val optionViewStates = listOf(
PollOptionViewState.PollVoted(
optionId = "",
optionAnswer = "",
voteCount = 0,
votePercentage = 0.0,
isSelected = false,
)
)
every {
fakePollOptionViewStateFactory.createPollVotedOptions(
disclosedPollContent.getBestPollCreationInfo(),
votedInformationData.pollResponseAggregatedSummary,
)
} returns optionViewStates
// When
val pollViewState = pollItemViewStateFactory.create(
pollContent = disclosedPollContent,
informationData = votedInformationData,
)
// Then
pollViewState shouldBeEqualTo PollViewState(
question = A_POLL_CONTENT.getBestPollCreationInfo()?.question?.getBestQuestion() ?: "",
votesStatus = stringProvider.instance.getQuantityString(R.plurals.poll_total_vote_count_before_ended_and_voted, 1, 1),
votesStatus = fakeStringProvider.instance.getQuantityString(R.plurals.poll_total_vote_count_before_ended_and_voted, 1, 1),
canVote = true,
optionViewStates = A_POLL_CONTENT.getBestPollCreationInfo()?.answers?.mapIndexed { index, answer ->
PollOptionViewState.PollVoted(
optionId = answer.id ?: "",
optionAnswer = answer.getBestAnswer() ?: "",
voteCount = if (index == 0) 1 else 0,
votePercentage = if (index == 0) 1.0 else 0.0,
isSelected = index == 0
)
},
optionViewStates = optionViewStates,
)
verify {
fakePollOptionViewStateFactory.createPollVotedOptions(
disclosedPollContent.getBestPollCreationInfo(),
votedInformationData.pollResponseAggregatedSummary,
)
}
}
@Test
fun `given a sent poll with decryption failure when my vote exists then a warning message is displayed`() {
// Given
val stringProvider = FakeStringProvider()
val pollItemViewStateFactory = PollItemViewStateFactory(stringProvider.instance)
val votedPollData = A_POLL_RESPONSE_DATA.copy(
totalVotes = 1,
myVote = A_POLL_OPTION_IDS[0],
@ -228,6 +273,21 @@ class PollItemViewStateFactoryTest {
),
)
val votedInformationData = A_MESSAGE_INFORMATION_DATA.copy(pollResponseAggregatedSummary = votedPollData)
val optionViewStates = listOf(
PollOptionViewState.PollVoted(
optionId = "",
optionAnswer = "",
voteCount = 0,
votePercentage = 0.0,
isSelected = false,
)
)
every {
fakePollOptionViewStateFactory.createPollVotedOptions(
disclosedPollContent.getBestPollCreationInfo(),
votedInformationData.pollResponseAggregatedSummary,
)
} returns optionViewStates
// When
val pollViewState = pollItemViewStateFactory.create(
@ -236,34 +296,46 @@ class PollItemViewStateFactoryTest {
)
// Then
pollViewState.votesStatus shouldBeEqualTo stringProvider.instance.getString(R.string.unable_to_decrypt_some_events_in_poll)
pollViewState.votesStatus shouldBeEqualTo fakeStringProvider.instance.getString(R.string.unable_to_decrypt_some_events_in_poll)
}
@Test
fun `given a sent poll when poll type is disclosed then poll is votable and option view states are PollReady`() {
val stringProvider = FakeStringProvider()
val pollItemViewStateFactory = PollItemViewStateFactory(stringProvider.instance)
// Given
val disclosedPollContent = A_POLL_CONTENT.copy(
unstablePollCreationInfo = A_POLL_CONTENT.getBestPollCreationInfo()?.copy(
kind = PollType.DISCLOSED_UNSTABLE
)
)
val optionViewStates = listOf(
PollOptionViewState.PollReady(
optionId = "",
optionAnswer = "",
)
)
every {
fakePollOptionViewStateFactory.createPollReadyOptions(
disclosedPollContent.getBestPollCreationInfo(),
)
} returns optionViewStates
// When
val pollViewState = pollItemViewStateFactory.create(
pollContent = disclosedPollContent,
informationData = A_MESSAGE_INFORMATION_DATA,
)
// Then
pollViewState shouldBeEqualTo PollViewState(
question = A_POLL_CONTENT.getBestPollCreationInfo()?.question?.getBestQuestion() ?: "",
votesStatus = stringProvider.instance.getString(R.string.poll_no_votes_cast),
votesStatus = fakeStringProvider.instance.getString(R.string.poll_no_votes_cast),
canVote = true,
optionViewStates = A_POLL_CONTENT.getBestPollCreationInfo()?.answers?.map { answer ->
PollOptionViewState.PollReady(
optionId = answer.id ?: "",
optionAnswer = answer.getBestAnswer() ?: ""
)
},
optionViewStates = optionViewStates,
)
verify {
fakePollOptionViewStateFactory.createPollReadyOptions(
disclosedPollContent.getBestPollCreationInfo(),
)
}
}
}

View File

@ -17,23 +17,26 @@
package im.vector.app.features.roomprofile.polls
import com.airbnb.mvrx.test.MavericksTestRule
import im.vector.app.features.roomprofile.polls.list.domain.GetLoadedPollsStatusUseCase
import im.vector.app.features.roomprofile.polls.list.domain.DisposePollHistoryUseCase
import im.vector.app.features.roomprofile.polls.list.domain.GetPollsUseCase
import im.vector.app.features.roomprofile.polls.list.domain.LoadMorePollsUseCase
import im.vector.app.features.roomprofile.polls.list.domain.SyncPollsUseCase
import im.vector.app.features.roomprofile.polls.list.ui.PollSummary
import im.vector.app.features.roomprofile.polls.list.ui.PollSummaryMapper
import im.vector.app.test.test
import im.vector.app.test.testDispatcher
import io.mockk.coEvery
import io.mockk.coJustRun
import io.mockk.coVerify
import io.mockk.every
import io.mockk.justRun
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.flowOf
import org.junit.Rule
import org.junit.Test
import org.matrix.android.sdk.api.session.room.poll.LoadedPollsStatus
import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent
private const val A_ROOM_ID = "room-id"
@ -42,31 +45,35 @@ class RoomPollsViewModelTest {
@get:Rule
val mavericksTestRule = MavericksTestRule(testDispatcher = testDispatcher)
private val initialState = RoomPollsViewState(A_ROOM_ID)
private val fakeGetPollsUseCase = mockk<GetPollsUseCase>()
private val fakeGetLoadedPollsStatusUseCase = mockk<GetLoadedPollsStatusUseCase>()
private val fakeLoadMorePollsUseCase = mockk<LoadMorePollsUseCase>()
private val fakeSyncPollsUseCase = mockk<SyncPollsUseCase>()
private val initialState = RoomPollsViewState(A_ROOM_ID)
private val fakeDisposePollHistoryUseCase = mockk<DisposePollHistoryUseCase>()
private val fakePollSummaryMapper = mockk<PollSummaryMapper>()
private fun createViewModel(): RoomPollsViewModel {
return RoomPollsViewModel(
initialState = initialState,
getPollsUseCase = fakeGetPollsUseCase,
getLoadedPollsStatusUseCase = fakeGetLoadedPollsStatusUseCase,
loadMorePollsUseCase = fakeLoadMorePollsUseCase,
syncPollsUseCase = fakeSyncPollsUseCase,
disposePollHistoryUseCase = fakeDisposePollHistoryUseCase,
pollSummaryMapper = fakePollSummaryMapper,
)
}
@Test
fun `given viewModel when created then polls list is observed, sync is launched and viewState is updated`() {
// Given
val loadedPollsStatus = givenGetLoadedPollsStatusSuccess()
givenSyncPollsWithSuccess()
val polls = listOf(givenAPollSummary())
val loadedPollsStatus = givenSyncPollsWithSuccess()
val aPollEvent = givenAPollEvent()
val aPollSummary = givenAPollSummary()
val polls = listOf(aPollEvent)
every { fakeGetPollsUseCase.execute(A_ROOM_ID) } returns flowOf(polls)
every { fakePollSummaryMapper.map(aPollEvent) } returns aPollSummary
val expectedViewState = initialState.copy(
polls = polls,
polls = listOf(aPollSummary),
canLoadMore = loadedPollsStatus.canLoadMore,
nbSyncedDays = loadedPollsStatus.nbSyncedDays,
)
@ -81,6 +88,7 @@ class RoomPollsViewModelTest {
.finish()
verify {
fakeGetPollsUseCase.execute(A_ROOM_ID)
fakePollSummaryMapper.map(aPollEvent)
}
coVerify { fakeSyncPollsUseCase.execute(A_ROOM_ID) }
}
@ -88,10 +96,8 @@ class RoomPollsViewModelTest {
@Test
fun `given viewModel and error during sync process when created then error is raised in view event`() {
// Given
givenGetLoadedPollsStatusSuccess()
givenSyncPollsWithError(Exception())
val polls = listOf(givenAPollSummary())
every { fakeGetPollsUseCase.execute(A_ROOM_ID) } returns flowOf(polls)
every { fakeGetPollsUseCase.execute(A_ROOM_ID) } returns emptyFlow()
// When
val viewModel = createViewModel()
@ -104,17 +110,30 @@ class RoomPollsViewModelTest {
coVerify { fakeSyncPollsUseCase.execute(A_ROOM_ID) }
}
@Test
fun `given viewModel when calling onCleared then poll history is disposed`() {
// Given
givenSyncPollsWithSuccess()
every { fakeGetPollsUseCase.execute(A_ROOM_ID) } returns emptyFlow()
justRun { fakeDisposePollHistoryUseCase.execute(A_ROOM_ID) }
val viewModel = createViewModel()
// When
viewModel.onCleared()
// Then
verify { fakeDisposePollHistoryUseCase.execute(A_ROOM_ID) }
}
@Test
fun `given viewModel when handle load more action then viewState is updated`() {
// Given
val loadedPollsStatus = givenGetLoadedPollsStatusSuccess()
givenSyncPollsWithSuccess()
val polls = listOf(givenAPollSummary())
every { fakeGetPollsUseCase.execute(A_ROOM_ID) } returns flowOf(polls)
val loadedPollsStatus = givenSyncPollsWithSuccess()
every { fakeGetPollsUseCase.execute(A_ROOM_ID) } returns emptyFlow()
val newLoadedPollsStatus = givenLoadMoreWithSuccess()
val viewModel = createViewModel()
val stateAfterInit = initialState.copy(
polls = polls,
polls = emptyList(),
canLoadMore = loadedPollsStatus.canLoadMore,
nbSyncedDays = loadedPollsStatus.nbSyncedDays,
)
@ -139,8 +158,14 @@ class RoomPollsViewModelTest {
return mockk()
}
private fun givenSyncPollsWithSuccess() {
coJustRun { fakeSyncPollsUseCase.execute(A_ROOM_ID) }
private fun givenAPollEvent(): TimelineEvent {
return mockk()
}
private fun givenSyncPollsWithSuccess(): LoadedPollsStatus {
val loadedPollsStatus = givenALoadedPollsStatus()
coEvery { fakeSyncPollsUseCase.execute(A_ROOM_ID) } returns loadedPollsStatus
return loadedPollsStatus
}
private fun givenSyncPollsWithError(error: Exception) {
@ -153,15 +178,10 @@ class RoomPollsViewModelTest {
return loadedPollsStatus
}
private fun givenGetLoadedPollsStatusSuccess(): LoadedPollsStatus {
val loadedPollsStatus = givenALoadedPollsStatus()
coEvery { fakeGetLoadedPollsStatusUseCase.execute(A_ROOM_ID) } returns loadedPollsStatus
return loadedPollsStatus
}
private fun givenALoadedPollsStatus(canLoadMore: Boolean = true, nbSyncedDays: Int = 10) =
LoadedPollsStatus(
canLoadMore = canLoadMore,
nbSyncedDays = nbSyncedDays,
hasCompletedASyncBackward = false,
)
}

View File

@ -16,7 +16,7 @@
package im.vector.app.features.roomprofile.polls.list.data
import im.vector.app.features.roomprofile.polls.list.ui.PollSummary
import io.mockk.coEvery
import io.mockk.coJustRun
import io.mockk.coVerify
import io.mockk.every
@ -27,6 +27,8 @@ import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
import org.amshove.kluent.shouldBeEqualTo
import org.junit.Test
import org.matrix.android.sdk.api.session.room.poll.LoadedPollsStatus
import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent
private const val A_ROOM_ID = "room-id"
@ -41,7 +43,7 @@ class RoomPollRepositoryTest {
@Test
fun `given data source when getting polls then correct method of data source is called`() = runTest {
// Given
val expectedPolls = listOf<PollSummary>()
val expectedPolls = listOf<TimelineEvent>()
every { fakeRoomPollDataSource.getPolls(A_ROOM_ID) } returns flowOf(expectedPolls)
// When
@ -53,20 +55,21 @@ class RoomPollRepositoryTest {
}
@Test
fun `given data source when getting loaded polls status then correct method of data source is called`() {
fun `given data source when getting loaded polls status then correct method of data source is called`() = runTest {
// Given
val expectedStatus = LoadedPollsStatus(
canLoadMore = true,
nbLoadedDays = 10,
nbSyncedDays = 10,
hasCompletedASyncBackward = false,
)
every { fakeRoomPollDataSource.getLoadedPollsStatus(A_ROOM_ID) } returns expectedStatus
coEvery { fakeRoomPollDataSource.getLoadedPollsStatus(A_ROOM_ID) } returns expectedStatus
// When
val result = roomPollRepository.getLoadedPollsStatus(A_ROOM_ID)
// Then
result shouldBeEqualTo expectedStatus
verify { fakeRoomPollDataSource.getLoadedPollsStatus(A_ROOM_ID) }
coVerify { fakeRoomPollDataSource.getLoadedPollsStatus(A_ROOM_ID) }
}
@Test

View File

@ -17,9 +17,10 @@
package im.vector.app.features.roomprofile.polls.list.domain
import im.vector.app.features.roomprofile.polls.list.data.RoomPollRepository
import io.mockk.every
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.test.runTest
import org.amshove.kluent.shouldBeEqualTo
import org.junit.Test
import org.matrix.android.sdk.api.session.room.poll.LoadedPollsStatus
@ -33,20 +34,21 @@ class GetLoadedPollsStatusUseCaseTest {
)
@Test
fun `given repo when execute then correct method of repo is called`() {
fun `given repo when execute then correct method of repo is called`() = runTest {
// Given
val aRoomId = "roomId"
val expectedStatus = LoadedPollsStatus(
canLoadMore = true,
nbSyncedDays = 10,
hasCompletedASyncBackward = true,
)
every { fakeRoomPollRepository.getLoadedPollsStatus(aRoomId) } returns expectedStatus
coEvery { fakeRoomPollRepository.getLoadedPollsStatus(aRoomId) } returns expectedStatus
// When
val status = getLoadedPollsStatusUseCase.execute(aRoomId)
// Then
status shouldBeEqualTo expectedStatus
verify { fakeRoomPollRepository.getLoadedPollsStatus(aRoomId) }
coVerify { fakeRoomPollRepository.getLoadedPollsStatus(aRoomId) }
}
}

View File

@ -17,8 +17,6 @@
package im.vector.app.features.roomprofile.polls.list.domain
import im.vector.app.features.roomprofile.polls.list.data.RoomPollRepository
import im.vector.app.features.roomprofile.polls.list.ui.PollSummary
import im.vector.app.test.fixtures.RoomPollFixture
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
@ -27,6 +25,7 @@ import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
import org.amshove.kluent.shouldBeEqualTo
import org.junit.Test
import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent
class GetPollsUseCaseTest {
private val fakeRoomPollRepository = mockk<RoomPollRepository>()
@ -39,16 +38,16 @@ class GetPollsUseCaseTest {
fun `given repo when execute then correct method of repo is called and polls are sorted most recent first`() = runTest {
// Given
val aRoomId = "roomId"
val poll1 = RoomPollFixture.anActivePollSummary(timestamp = 1)
val poll2 = RoomPollFixture.anActivePollSummary(timestamp = 2)
val poll3 = RoomPollFixture.anActivePollSummary(timestamp = 3)
val polls = listOf<PollSummary>(
val poll1 = givenTimelineEvent(timestamp = 1)
val poll2 = givenTimelineEvent(timestamp = 2)
val poll3 = givenTimelineEvent(timestamp = 3)
val polls = listOf(
poll1,
poll2,
poll3,
)
every { fakeRoomPollRepository.getPolls(aRoomId) } returns flowOf(polls)
val expectedPolls = listOf<PollSummary>(
val expectedPolls = listOf(
poll3,
poll2,
poll1,
@ -60,4 +59,10 @@ class GetPollsUseCaseTest {
result shouldBeEqualTo expectedPolls
verify { fakeRoomPollRepository.getPolls(aRoomId) }
}
private fun givenTimelineEvent(timestamp: Long): TimelineEvent {
return mockk<TimelineEvent>().also {
every { it.root.originServerTs } returns timestamp
}
}
}

View File

@ -40,6 +40,7 @@ class LoadMorePollsUseCaseTest {
val loadedPollsStatus = LoadedPollsStatus(
canLoadMore = true,
nbSyncedDays = 10,
hasCompletedASyncBackward = true,
)
coEvery { fakeRoomPollRepository.loadMorePolls(aRoomId) } returns loadedPollsStatus

View File

@ -17,30 +17,81 @@
package im.vector.app.features.roomprofile.polls.list.domain
import im.vector.app.features.roomprofile.polls.list.data.RoomPollRepository
import io.mockk.coEvery
import io.mockk.coJustRun
import io.mockk.coVerify
import io.mockk.coVerifyOrder
import io.mockk.mockk
import kotlinx.coroutines.test.runTest
import org.amshove.kluent.shouldBeEqualTo
import org.junit.Test
import org.matrix.android.sdk.api.session.room.poll.LoadedPollsStatus
class SyncPollsUseCaseTest {
private val fakeRoomPollRepository = mockk<RoomPollRepository>()
private val fakeGetLoadedPollsStatusUseCase = mockk<GetLoadedPollsStatusUseCase>()
private val fakeLoadMorePollsUseCase = mockk<LoadMorePollsUseCase>()
private val syncPollsUseCase = SyncPollsUseCase(
roomPollRepository = fakeRoomPollRepository,
getLoadedPollsStatusUseCase = fakeGetLoadedPollsStatusUseCase,
loadMorePollsUseCase = fakeLoadMorePollsUseCase,
)
@Test
fun `given repo when execute then correct method of repo is called`() = runTest {
fun `given it has completed a sync backward when execute then only sync process is called`() = runTest {
// Given
val aRoomId = "roomId"
val aLoadedStatus = LoadedPollsStatus(
canLoadMore = true,
nbSyncedDays = 10,
hasCompletedASyncBackward = true,
)
coJustRun { fakeRoomPollRepository.syncPolls(aRoomId) }
coEvery { fakeGetLoadedPollsStatusUseCase.execute(aRoomId) } returns aLoadedStatus
// When
syncPollsUseCase.execute(aRoomId)
val result = syncPollsUseCase.execute(aRoomId)
// Then
coVerify { fakeRoomPollRepository.syncPolls(aRoomId) }
result shouldBeEqualTo aLoadedStatus
coVerifyOrder {
fakeRoomPollRepository.syncPolls(aRoomId)
fakeGetLoadedPollsStatusUseCase.execute(aRoomId)
}
coVerify(inverse = true) {
fakeLoadMorePollsUseCase.execute(any())
}
}
@Test
fun `given it has not completed a sync backward when execute then sync process and load more is called`() = runTest {
// Given
val aRoomId = "roomId"
val aLoadedStatus = LoadedPollsStatus(
canLoadMore = true,
nbSyncedDays = 10,
hasCompletedASyncBackward = false,
)
val anUpdatedLoadedStatus = LoadedPollsStatus(
canLoadMore = true,
nbSyncedDays = 10,
hasCompletedASyncBackward = true,
)
coJustRun { fakeRoomPollRepository.syncPolls(aRoomId) }
coEvery { fakeGetLoadedPollsStatusUseCase.execute(aRoomId) } returns aLoadedStatus
coEvery { fakeLoadMorePollsUseCase.execute(aRoomId) } returns anUpdatedLoadedStatus
// When
val result = syncPollsUseCase.execute(aRoomId)
// Then
result shouldBeEqualTo anUpdatedLoadedStatus
coVerifyOrder {
fakeRoomPollRepository.syncPolls(aRoomId)
fakeGetLoadedPollsStatusUseCase.execute(aRoomId)
fakeLoadMorePollsUseCase.execute(aRoomId)
}
}
}