Test poll view events when poll is edited.

This commit is contained in:
Onuray Sahin 2022-06-16 13:40:39 +03:00
parent b558d14a48
commit 841b63b819
1 changed files with 61 additions and 37 deletions

View File

@ -18,42 +18,38 @@ package im.vector.app.features.poll.create
import com.airbnb.mvrx.test.MvRxTestRule import com.airbnb.mvrx.test.MvRxTestRule
import im.vector.app.features.poll.PollMode import im.vector.app.features.poll.PollMode
import im.vector.app.features.poll.create.CreatePollViewStates.createPollArgs import im.vector.app.features.poll.create.FakeCreatePollViewStates.A_FAKE_OPTIONS
import im.vector.app.features.poll.create.CreatePollViewStates.editPollArgs import im.vector.app.features.poll.create.FakeCreatePollViewStates.A_FAKE_QUESTION
import im.vector.app.features.poll.create.CreatePollViewStates.fakeOptions import im.vector.app.features.poll.create.FakeCreatePollViewStates.A_FAKE_ROOM_ID
import im.vector.app.features.poll.create.CreatePollViewStates.fakeQuestion import im.vector.app.features.poll.create.FakeCreatePollViewStates.A_POLL_START_TIMELINE_EVENT
import im.vector.app.features.poll.create.CreatePollViewStates.fakeRoomId import im.vector.app.features.poll.create.FakeCreatePollViewStates.createPollArgs
import im.vector.app.features.poll.create.CreatePollViewStates.initialCreatePollViewState import im.vector.app.features.poll.create.FakeCreatePollViewStates.editPollArgs
import im.vector.app.features.poll.create.CreatePollViewStates.pollViewStateWithOnlyQuestion import im.vector.app.features.poll.create.FakeCreatePollViewStates.editedPollViewState
import im.vector.app.features.poll.create.CreatePollViewStates.pollViewStateWithQuestionAndEnoughOptions import im.vector.app.features.poll.create.FakeCreatePollViewStates.initialCreatePollViewState
import im.vector.app.features.poll.create.CreatePollViewStates.pollViewStateWithQuestionAndMaxOptions import im.vector.app.features.poll.create.FakeCreatePollViewStates.pollViewStateWithOnlyQuestion
import im.vector.app.features.poll.create.CreatePollViewStates.pollViewStateWithQuestionAndNotEnoughOptions import im.vector.app.features.poll.create.FakeCreatePollViewStates.pollViewStateWithQuestionAndEnoughOptions
import im.vector.app.features.poll.create.CreatePollViewStates.pollViewStateWithoutQuestionAndEnoughOptions import im.vector.app.features.poll.create.FakeCreatePollViewStates.pollViewStateWithQuestionAndMaxOptions
import im.vector.app.features.poll.create.FakeCreatePollViewStates.pollViewStateWithQuestionAndNotEnoughOptions
import im.vector.app.features.poll.create.FakeCreatePollViewStates.pollViewStateWithoutQuestionAndEnoughOptions
import im.vector.app.test.fakes.FakeSession
import im.vector.app.test.test import im.vector.app.test.test
import io.mockk.every import io.mockk.unmockkAll
import io.mockk.mockk
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest
import org.amshove.kluent.shouldBe import org.amshove.kluent.shouldBe
import org.junit.After
import org.junit.Before import org.junit.Before
import org.junit.Rule import org.junit.Rule
import org.junit.Test import org.junit.Test
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.getRoom import org.matrix.android.sdk.api.session.getRoom
import org.matrix.android.sdk.api.session.room.Room
import org.matrix.android.sdk.api.session.room.model.message.PollType import org.matrix.android.sdk.api.session.room.model.message.PollType
import org.matrix.android.sdk.api.session.room.send.SendService
import org.matrix.android.sdk.api.util.Cancelable
class CreatePollViewModelTest { class CreatePollViewModelTest {
@get:Rule @get:Rule
val mvrxTestRule = MvRxTestRule() val mvrxTestRule = MvRxTestRule()
private val fakeSession = mockk<Session>() private val fakeSession = FakeSession()
private val fakeRoom = mockk<Room>()
private val fakeSendService = mockk<SendService>()
private val fakeCancellable = mockk<Cancelable>()
private fun createPollViewModel(pollMode: PollMode): CreatePollViewModel { private fun createPollViewModel(pollMode: PollMode): CreatePollViewModel {
return if (pollMode == PollMode.EDIT) { return if (pollMode == PollMode.EDIT) {
@ -65,9 +61,16 @@ class CreatePollViewModelTest {
@Before @Before
fun setup() { fun setup() {
every { fakeSession.getRoom(fakeRoomId) } returns fakeRoom fakeSession
every { fakeRoom.sendService() } returns fakeSendService .roomService()
every { fakeSendService.sendPoll(any(), fakeQuestion, any()) } returns fakeCancellable .getRoom(A_FAKE_ROOM_ID)
.timelineService()
.givenTimelineEvent(A_POLL_START_TIMELINE_EVENT)
}
@After
fun tearDown() {
unmockkAll()
} }
@Test @Test
@ -82,7 +85,7 @@ class CreatePollViewModelTest {
@Test @Test
fun `given there is not any options when the question is added then poll cannot be created and more options can be added`() = runTest { fun `given there is not any options when the question is added then poll cannot be created and more options can be added`() = runTest {
val createPollViewModel = createPollViewModel(PollMode.CREATE) val createPollViewModel = createPollViewModel(PollMode.CREATE)
createPollViewModel.handle(CreatePollAction.OnQuestionChanged(fakeQuestion)) createPollViewModel.handle(CreatePollAction.OnQuestionChanged(A_FAKE_QUESTION))
// We need to wait for createPollViewModel.onChange is triggered // We need to wait for createPollViewModel.onChange is triggered
delay(10) delay(10)
@ -95,9 +98,9 @@ class CreatePollViewModelTest {
@Test @Test
fun `given there is not enough options when the question is added then poll cannot be created and more options can be added`() = runTest { fun `given there is not enough options when the question is added then poll cannot be created and more options can be added`() = runTest {
val createPollViewModel = createPollViewModel(PollMode.CREATE) val createPollViewModel = createPollViewModel(PollMode.CREATE)
createPollViewModel.handle(CreatePollAction.OnQuestionChanged(fakeQuestion)) createPollViewModel.handle(CreatePollAction.OnQuestionChanged(A_FAKE_QUESTION))
repeat(CreatePollViewModel.MIN_OPTIONS_COUNT - 1) { repeat(CreatePollViewModel.MIN_OPTIONS_COUNT - 1) {
createPollViewModel.handle(CreatePollAction.OnOptionChanged(it, fakeOptions[it])) createPollViewModel.handle(CreatePollAction.OnOptionChanged(it, A_FAKE_OPTIONS[it]))
} }
delay(10) delay(10)
@ -111,7 +114,7 @@ class CreatePollViewModelTest {
fun `given there is not a question when enough options are added then poll cannot be created and more options can be added`() = runTest { fun `given there is not a question when enough options are added then poll cannot be created and more options can be added`() = runTest {
val createPollViewModel = createPollViewModel(PollMode.CREATE) val createPollViewModel = createPollViewModel(PollMode.CREATE)
repeat(CreatePollViewModel.MIN_OPTIONS_COUNT) { repeat(CreatePollViewModel.MIN_OPTIONS_COUNT) {
createPollViewModel.handle(CreatePollAction.OnOptionChanged(it, fakeOptions[it])) createPollViewModel.handle(CreatePollAction.OnOptionChanged(it, A_FAKE_OPTIONS[it]))
} }
delay(10) delay(10)
@ -124,9 +127,9 @@ class CreatePollViewModelTest {
@Test @Test
fun `given there is a question when enough options are added then poll can be created and more options can be added`() = runTest { fun `given there is a question when enough options are added then poll can be created and more options can be added`() = runTest {
val createPollViewModel = createPollViewModel(PollMode.CREATE) val createPollViewModel = createPollViewModel(PollMode.CREATE)
createPollViewModel.handle(CreatePollAction.OnQuestionChanged(fakeQuestion)) createPollViewModel.handle(CreatePollAction.OnQuestionChanged(A_FAKE_QUESTION))
repeat(CreatePollViewModel.MIN_OPTIONS_COUNT) { repeat(CreatePollViewModel.MIN_OPTIONS_COUNT) {
createPollViewModel.handle(CreatePollAction.OnOptionChanged(it, fakeOptions[it])) createPollViewModel.handle(CreatePollAction.OnOptionChanged(it, A_FAKE_OPTIONS[it]))
} }
delay(10) delay(10)
@ -139,12 +142,12 @@ class CreatePollViewModelTest {
@Test @Test
fun `given there is a question when max number of options are added then poll can be created and more options cannot be added`() = runTest { fun `given there is a question when max number of options are added then poll can be created and more options cannot be added`() = runTest {
val createPollViewModel = createPollViewModel(PollMode.CREATE) val createPollViewModel = createPollViewModel(PollMode.CREATE)
createPollViewModel.handle(CreatePollAction.OnQuestionChanged(fakeQuestion)) createPollViewModel.handle(CreatePollAction.OnQuestionChanged(A_FAKE_QUESTION))
repeat(CreatePollViewModel.MAX_OPTIONS_COUNT) { repeat(CreatePollViewModel.MAX_OPTIONS_COUNT) {
if (it >= CreatePollViewModel.MIN_OPTIONS_COUNT) { if (it >= CreatePollViewModel.MIN_OPTIONS_COUNT) {
createPollViewModel.handle(CreatePollAction.OnAddOption) createPollViewModel.handle(CreatePollAction.OnAddOption)
} }
createPollViewModel.handle(CreatePollAction.OnOptionChanged(it, fakeOptions[it])) createPollViewModel.handle(CreatePollAction.OnOptionChanged(it, A_FAKE_OPTIONS[it]))
} }
delay(10) delay(10)
@ -170,10 +173,10 @@ class CreatePollViewModelTest {
createPollViewModel.handle(CreatePollAction.OnCreatePoll) createPollViewModel.handle(CreatePollAction.OnCreatePoll)
createPollViewModel.handle(CreatePollAction.OnQuestionChanged(fakeQuestion)) createPollViewModel.handle(CreatePollAction.OnQuestionChanged(A_FAKE_QUESTION))
createPollViewModel.handle(CreatePollAction.OnCreatePoll) createPollViewModel.handle(CreatePollAction.OnCreatePoll)
createPollViewModel.handle(CreatePollAction.OnOptionChanged(0, fakeOptions[0])) createPollViewModel.handle(CreatePollAction.OnOptionChanged(0, A_FAKE_OPTIONS[0]))
createPollViewModel.handle(CreatePollAction.OnCreatePoll) createPollViewModel.handle(CreatePollAction.OnCreatePoll)
test test
@ -189,9 +192,30 @@ class CreatePollViewModelTest {
val createPollViewModel = createPollViewModel(PollMode.CREATE) val createPollViewModel = createPollViewModel(PollMode.CREATE)
val test = createPollViewModel.test() val test = createPollViewModel.test()
createPollViewModel.handle(CreatePollAction.OnQuestionChanged(fakeQuestion)) createPollViewModel.handle(CreatePollAction.OnQuestionChanged(A_FAKE_QUESTION))
createPollViewModel.handle(CreatePollAction.OnOptionChanged(0, fakeOptions[0])) createPollViewModel.handle(CreatePollAction.OnOptionChanged(0, A_FAKE_OPTIONS[0]))
createPollViewModel.handle(CreatePollAction.OnOptionChanged(1, fakeOptions[1])) createPollViewModel.handle(CreatePollAction.OnOptionChanged(1, A_FAKE_OPTIONS[1]))
createPollViewModel.handle(CreatePollAction.OnCreatePoll)
test
.assertEvents(
CreatePollViewEvents.Success
)
}
@Test
fun `given an edited poll event when question and options are changed then view state is updated accordingly`() = runTest {
val createPollViewModel = createPollViewModel(PollMode.EDIT)
delay(10)
createPollViewModel.test().assertState(editedPollViewState)
}
@Test
fun `given an edited poll event then able to be edited`() = runTest {
val createPollViewModel = createPollViewModel(PollMode.EDIT)
val test = createPollViewModel.test()
createPollViewModel.handle(CreatePollAction.OnCreatePoll) createPollViewModel.handle(CreatePollAction.OnCreatePoll)
test test