Adding unit tests for PollOptionViewStateFactory

This commit is contained in:
Maxime NATUREL 2023-01-25 10:54:37 +01:00
parent cfc67d5b67
commit 41bb743cf4
5 changed files with 228 additions and 85 deletions

View file

@ -21,12 +21,10 @@ import im.vector.app.features.home.room.detail.timeline.item.PollResponseData
import org.matrix.android.sdk.api.session.room.model.message.PollCreationInfo
import javax.inject.Inject
// TODO add unit tests
class PollOptionViewStateFactory @Inject constructor() {
fun createPollEndedOptions(pollCreationInfo: PollCreationInfo?, pollResponseData: PollResponseData?): List<PollOptionViewState.PollEnded> {
val winnerVoteCount = pollResponseData?.winnerVoteCount
return pollCreationInfo?.answers?.map { answer ->
val voteSummary = pollResponseData?.getVoteSummaryOfAnOption(answer.id ?: "")
PollOptionViewState.PollEnded(

View file

@ -25,6 +25,10 @@ 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 im.vector.app.test.fixtures.PollFixture.A_MESSAGE_INFORMATION_DATA
import im.vector.app.test.fixtures.PollFixture.A_POLL_CONTENT
import im.vector.app.test.fixtures.PollFixture.A_POLL_OPTION_IDS
import im.vector.app.test.fixtures.PollFixture.A_POLL_RESPONSE_DATA
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
@ -37,42 +41,6 @@ import org.matrix.android.sdk.api.session.room.model.message.PollQuestion
import org.matrix.android.sdk.api.session.room.model.message.PollType
import org.matrix.android.sdk.api.session.room.send.SendState
private val A_MESSAGE_INFORMATION_DATA = MessageInformationData(
eventId = "eventId",
senderId = "senderId",
ageLocalTS = 0,
avatarUrl = "",
sendState = SendState.SENT,
messageLayout = TimelineMessageLayout.Default(showAvatar = true, showDisplayName = true, showTimestamp = true),
reactionsSummary = ReactionsSummaryData(),
sentByMe = true,
)
private val A_POLL_RESPONSE_DATA = PollResponseData(
myVote = null,
votes = emptyMap(),
)
private val A_POLL_OPTION_IDS = listOf("5ef5f7b0-c9a1-49cf-a0b3-374729a43e76", "ec1a4db0-46d8-4d7a-9bb6-d80724715938", "3677ca8e-061b-40ab-bffe-b22e4e88fcad")
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"
),
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()

View file

@ -0,0 +1,157 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.app.features.home.room.detail.timeline.factory
import im.vector.app.features.home.room.detail.timeline.item.PollOptionViewState
import im.vector.app.features.home.room.detail.timeline.item.PollVoteSummaryData
import im.vector.app.test.fixtures.PollFixture.A_POLL_CONTENT
import im.vector.app.test.fixtures.PollFixture.A_POLL_OPTION_IDS
import im.vector.app.test.fixtures.PollFixture.A_POLL_RESPONSE_DATA
import org.amshove.kluent.shouldBeEqualTo
import org.junit.Test
import org.matrix.android.sdk.api.session.room.model.message.PollType
internal class PollOptionViewStateFactoryTest {
private val pollOptionViewStateFactory = PollOptionViewStateFactory()
@Test
fun `given poll data when creating ended poll options then correct options are returned`() {
// Given
val winnerVotesCount = 0
val pollResponseData = A_POLL_RESPONSE_DATA.copy(
isClosed = true,
winnerVoteCount = winnerVotesCount,
)
val pollCreationInfo = A_POLL_CONTENT.getBestPollCreationInfo()
val expectedOptions = pollCreationInfo?.answers?.map { answer ->
PollOptionViewState.PollEnded(
optionId = answer.id.orEmpty(),
optionAnswer = answer.getBestAnswer().orEmpty(),
voteCount = 0,
votePercentage = 0.0,
isWinner = false,
)
}
// When
val result = pollOptionViewStateFactory.createPollEndedOptions(
pollCreationInfo = pollCreationInfo,
pollResponseData = pollResponseData,
)
// Then
result shouldBeEqualTo expectedOptions
}
@Test
fun `given poll data when creating sending poll options then correct options are returned`() {
// Given
val pollCreationInfo = A_POLL_CONTENT.getBestPollCreationInfo()
val expectedOptions = pollCreationInfo?.answers?.map { answer ->
PollOptionViewState.PollSending(
optionId = answer.id.orEmpty(),
optionAnswer = answer.getBestAnswer().orEmpty(),
)
}
// When
val result = pollOptionViewStateFactory.createPollSendingOptions(
pollCreationInfo = pollCreationInfo,
)
// Then
result shouldBeEqualTo expectedOptions
}
@Test
fun `given poll data when creating undisclosed poll options then correct options are returned`() {
// Given
val pollResponseData = A_POLL_RESPONSE_DATA
val pollCreationInfo = A_POLL_CONTENT.getBestPollCreationInfo()
val expectedOptions = pollCreationInfo?.answers?.map { answer ->
PollOptionViewState.PollUndisclosed(
optionId = answer.id.orEmpty(),
optionAnswer = answer.getBestAnswer().orEmpty(),
isSelected = false,
)
}
// When
val result = pollOptionViewStateFactory.createPollUndisclosedOptions(
pollCreationInfo = pollCreationInfo,
pollResponseData = pollResponseData,
)
// Then
result shouldBeEqualTo expectedOptions
}
@Test
fun `given poll data when creating voted poll options then correct options are returned`() {
// Given
val pollResponseData = 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)),
)
val disclosedPollContent = A_POLL_CONTENT.copy(
unstablePollCreationInfo = A_POLL_CONTENT.getBestPollCreationInfo()?.copy(
kind = PollType.DISCLOSED_UNSTABLE,
),
)
val pollCreationInfo = disclosedPollContent.getBestPollCreationInfo()
val expectedOptions = pollCreationInfo?.answers?.mapIndexed { index, answer ->
PollOptionViewState.PollVoted(
optionId = answer.id.orEmpty(),
optionAnswer = answer.getBestAnswer().orEmpty(),
voteCount = if (index == 0) 1 else 0,
votePercentage = if (index == 0) 1.0 else 0.0,
isSelected = index == 0,
)
}
// When
val result = pollOptionViewStateFactory.createPollVotedOptions(
pollCreationInfo = pollCreationInfo,
pollResponseData = pollResponseData,
)
// Then
result shouldBeEqualTo expectedOptions
}
@Test
fun `given poll data when creating ready poll options then correct options are returned`() {
// Given
val pollCreationInfo = A_POLL_CONTENT.getBestPollCreationInfo()
val expectedOptions = pollCreationInfo?.answers?.map { answer ->
PollOptionViewState.PollReady(
optionId = answer.id.orEmpty(),
optionAnswer = answer.getBestAnswer().orEmpty(),
)
}
// When
val result = pollOptionViewStateFactory.createPollReadyOptions(
pollCreationInfo = pollCreationInfo,
)
// Then
result shouldBeEqualTo expectedOptions
}
}

View file

@ -0,0 +1,67 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.app.test.fixtures
import im.vector.app.features.home.room.detail.timeline.item.MessageInformationData
import im.vector.app.features.home.room.detail.timeline.item.PollResponseData
import im.vector.app.features.home.room.detail.timeline.item.ReactionsSummaryData
import im.vector.app.features.home.room.detail.timeline.style.TimelineMessageLayout
import org.matrix.android.sdk.api.session.room.model.message.MessagePollContent
import org.matrix.android.sdk.api.session.room.model.message.PollAnswer
import org.matrix.android.sdk.api.session.room.model.message.PollCreationInfo
import org.matrix.android.sdk.api.session.room.model.message.PollQuestion
import org.matrix.android.sdk.api.session.room.model.message.PollType
import org.matrix.android.sdk.api.session.room.send.SendState
object PollFixture {
val A_MESSAGE_INFORMATION_DATA = MessageInformationData(
eventId = "eventId",
senderId = "senderId",
ageLocalTS = 0,
avatarUrl = "",
sendState = SendState.SENT,
messageLayout = TimelineMessageLayout.Default(showAvatar = true, showDisplayName = true, showTimestamp = true),
reactionsSummary = ReactionsSummaryData(),
sentByMe = true,
)
val A_POLL_RESPONSE_DATA = PollResponseData(
myVote = null,
votes = emptyMap(),
)
val A_POLL_OPTION_IDS = listOf("5ef5f7b0-c9a1-49cf-a0b3-374729a43e76", "ec1a4db0-46d8-4d7a-9bb6-d80724715938", "3677ca8e-061b-40ab-bffe-b22e4e88fcad")
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"
),
PollAnswer(
id = A_POLL_OPTION_IDS[1], unstableAnswer = "Macchiato"
),
PollAnswer(
id = A_POLL_OPTION_IDS[2], unstableAnswer = "Iced Coffee"
),
)
)
)
}

View file

@ -1,47 +0,0 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.app.test.fixtures
import im.vector.app.features.home.room.detail.timeline.item.PollOptionViewState
import im.vector.app.features.roomprofile.polls.list.ui.PollSummary
object RoomPollFixture {
fun anActivePollSummary(
id: String = "",
timestamp: Long,
title: String = "",
) = PollSummary.ActivePoll(
id = id,
creationTimestamp = timestamp,
title = title,
)
fun anEndedPollSummary(
id: String = "",
timestamp: Long,
title: String = "",
totalVotes: Int,
winnerOptions: List<PollOptionViewState.PollEnded>
) = PollSummary.EndedPoll(
id = id,
creationTimestamp = timestamp,
title = title,
totalVotes = totalVotes,
winnerOptions = winnerOptions,
)
}