From 24d59eba87b0cd5397dbe5864d11d5e7679a41bb Mon Sep 17 00:00:00 2001 From: Onuray Sahin Date: Wed, 15 Jun 2022 17:51:13 +0300 Subject: [PATCH] Test poll view events when create poll is requested. --- .../poll/create/CreatePollViewModelTest.kt | 70 ++++++++++++++++++- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/vector/src/test/java/im/vector/app/features/poll/create/CreatePollViewModelTest.kt b/vector/src/test/java/im/vector/app/features/poll/create/CreatePollViewModelTest.kt index 017ed0a31c..fa24d9c4a6 100644 --- a/vector/src/test/java/im/vector/app/features/poll/create/CreatePollViewModelTest.kt +++ b/vector/src/test/java/im/vector/app/features/poll/create/CreatePollViewModelTest.kt @@ -22,25 +22,38 @@ import im.vector.app.features.poll.create.CreatePollViewStates.createPollArgs import im.vector.app.features.poll.create.CreatePollViewStates.editPollArgs import im.vector.app.features.poll.create.CreatePollViewStates.fakeOptions import im.vector.app.features.poll.create.CreatePollViewStates.fakeQuestion +import im.vector.app.features.poll.create.CreatePollViewStates.fakeRoomId import im.vector.app.features.poll.create.CreatePollViewStates.initialCreatePollViewState import im.vector.app.features.poll.create.CreatePollViewStates.pollViewStateWithOnlyQuestion import im.vector.app.features.poll.create.CreatePollViewStates.pollViewStateWithQuestionAndEnoughOptions import im.vector.app.features.poll.create.CreatePollViewStates.pollViewStateWithQuestionAndMaxOptions import im.vector.app.features.poll.create.CreatePollViewStates.pollViewStateWithQuestionAndNotEnoughOptions import im.vector.app.features.poll.create.CreatePollViewStates.pollViewStateWithoutQuestionAndEnoughOptions -import im.vector.app.test.fakes.FakeSession import im.vector.app.test.test +import io.mockk.every +import io.mockk.mockk import kotlinx.coroutines.delay import kotlinx.coroutines.test.runTest +import org.amshove.kluent.shouldBe +import org.junit.Before import org.junit.Rule 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.room.Room +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 { @get:Rule val mvrxTestRule = MvRxTestRule() - private val fakeSession = FakeSession() + private val fakeSession = mockk() + private val fakeRoom = mockk() + private val fakeSendService = mockk() + private val fakeCancellable = mockk() private fun createPollViewModel(pollMode: PollMode): CreatePollViewModel { return if (pollMode == PollMode.EDIT) { @@ -50,6 +63,13 @@ class CreatePollViewModelTest { } } + @Before + fun setup() { + every { fakeSession.getRoom(fakeRoomId) } returns fakeRoom + every { fakeRoom.sendService() } returns fakeSendService + every { fakeSendService.sendPoll(any(), fakeQuestion, any()) } returns fakeCancellable + } + @Test fun `given the view model is initialized then poll cannot be created and more options can be added`() = runTest { val createPollViewModel = createPollViewModel(PollMode.CREATE) @@ -133,4 +153,50 @@ class CreatePollViewModelTest { .assertState(pollViewStateWithQuestionAndMaxOptions) .finish() } + + @Test + fun `given an initial poll state when poll type is changed then view state is updated accordingly`() = runTest { + val createPollViewModel = createPollViewModel(PollMode.CREATE) + createPollViewModel.handle(CreatePollAction.OnPollTypeChanged(PollType.DISCLOSED)) + createPollViewModel.awaitState().pollType shouldBe PollType.DISCLOSED + createPollViewModel.handle(CreatePollAction.OnPollTypeChanged(PollType.UNDISCLOSED)) + createPollViewModel.awaitState().pollType shouldBe PollType.UNDISCLOSED + } + + @Test + fun `given there is not a question and enough options when create poll is requested then error view events are post`() = runTest { + val createPollViewModel = createPollViewModel(PollMode.CREATE) + val test = createPollViewModel.test() + + createPollViewModel.handle(CreatePollAction.OnCreatePoll) + + createPollViewModel.handle(CreatePollAction.OnQuestionChanged(fakeQuestion)) + createPollViewModel.handle(CreatePollAction.OnCreatePoll) + + createPollViewModel.handle(CreatePollAction.OnOptionChanged(0, fakeOptions[0])) + createPollViewModel.handle(CreatePollAction.OnCreatePoll) + + test + .assertEvents( + CreatePollViewEvents.EmptyQuestionError, + CreatePollViewEvents.NotEnoughOptionsError(requiredOptionsCount = CreatePollViewModel.MIN_OPTIONS_COUNT), + CreatePollViewEvents.NotEnoughOptionsError(requiredOptionsCount = CreatePollViewModel.MIN_OPTIONS_COUNT) + ) + } + + @Test + fun `given there is a question and enough options when create poll is requested then success view event is post`() = runTest { + val createPollViewModel = createPollViewModel(PollMode.CREATE) + val test = createPollViewModel.test() + + createPollViewModel.handle(CreatePollAction.OnQuestionChanged(fakeQuestion)) + createPollViewModel.handle(CreatePollAction.OnOptionChanged(0, fakeOptions[0])) + createPollViewModel.handle(CreatePollAction.OnOptionChanged(1, fakeOptions[1])) + createPollViewModel.handle(CreatePollAction.OnCreatePoll) + + test + .assertEvents( + CreatePollViewEvents.Success + ) + } }