-
Notifications
You must be signed in to change notification settings - Fork 731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CreatePollViewModel unit tests [PSF-1122] #6320
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
cffa327
Create dummy data for poll view states.
onurays 19de215
Test initial poll view state.
onurays 5b35534
Test poll view state without enough options.
onurays 0bf37ab
Test poll views state with enough number of options but without a que…
onurays 1f04e73
Test poll view state with a question and enough number of options.
onurays 0ed9a18
Test poll view state with a question and max number of options.
onurays 24d59eb
Test poll view events when create poll is requested.
onurays 934d860
Changelog added.
onurays ec3248d
Lint fixes.
onurays 94c0a02
Rename test data class.
onurays b558d14
Create fake room services.
onurays 841b63b
Test poll view events when poll is edited.
onurays f13dfb8
Test poll view state when poll option is deleted.
onurays a1d35ae
Move fake class into the right package.
onurays 14a4a8e
Workaround for mavericks bug (https://github.com/airbnb/mavericks/iss…
onurays 712a38e
Apply Maxime's fix to get latest state.
onurays File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
CreatePollViewModel unit tests |
252 changes: 252 additions & 0 deletions
252
vector/src/test/java/im/vector/app/features/poll/create/CreatePollViewModelTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,252 @@ | ||
/* | ||
* Copyright (c) 2022 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.poll.create | ||
|
||
import com.airbnb.mvrx.test.MvRxTestRule | ||
import im.vector.app.features.poll.PollMode | ||
import im.vector.app.test.fakes.FakeCreatePollViewStates.A_FAKE_OPTIONS | ||
import im.vector.app.test.fakes.FakeCreatePollViewStates.A_FAKE_QUESTION | ||
import im.vector.app.test.fakes.FakeCreatePollViewStates.A_FAKE_ROOM_ID | ||
import im.vector.app.test.fakes.FakeCreatePollViewStates.A_POLL_START_TIMELINE_EVENT | ||
import im.vector.app.test.fakes.FakeCreatePollViewStates.createPollArgs | ||
import im.vector.app.test.fakes.FakeCreatePollViewStates.editPollArgs | ||
import im.vector.app.test.fakes.FakeCreatePollViewStates.editedPollViewState | ||
import im.vector.app.test.fakes.FakeCreatePollViewStates.initialCreatePollViewState | ||
import im.vector.app.test.fakes.FakeCreatePollViewStates.pollViewStateWithOnlyQuestion | ||
import im.vector.app.test.fakes.FakeCreatePollViewStates.pollViewStateWithQuestionAndEnoughOptions | ||
import im.vector.app.test.fakes.FakeCreatePollViewStates.pollViewStateWithQuestionAndEnoughOptionsButDeletedLastOption | ||
import im.vector.app.test.fakes.FakeCreatePollViewStates.pollViewStateWithQuestionAndMaxOptions | ||
import im.vector.app.test.fakes.FakeCreatePollViewStates.pollViewStateWithQuestionAndNotEnoughOptions | ||
import im.vector.app.test.fakes.FakeCreatePollViewStates.pollViewStateWithoutQuestionAndEnoughOptions | ||
import im.vector.app.test.fakes.FakeSession | ||
import im.vector.app.test.test | ||
import io.mockk.unmockkAll | ||
import kotlinx.coroutines.test.UnconfinedTestDispatcher | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.After | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.matrix.android.sdk.api.session.room.model.message.PollType | ||
|
||
class CreatePollViewModelTest { | ||
|
||
private val testDispatcher = UnconfinedTestDispatcher() | ||
|
||
@get:Rule | ||
val mvRxTestRule = MvRxTestRule( | ||
testDispatcher = testDispatcher // See https://github.com/airbnb/mavericks/issues/599 | ||
) | ||
|
||
private val fakeSession = FakeSession() | ||
|
||
private fun createPollViewModel(pollMode: PollMode): CreatePollViewModel { | ||
return if (pollMode == PollMode.EDIT) { | ||
CreatePollViewModel(CreatePollViewState(editPollArgs), fakeSession) | ||
} else { | ||
CreatePollViewModel(CreatePollViewState(createPollArgs), fakeSession) | ||
} | ||
} | ||
|
||
@Before | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should unmock mocked objects after each test in general. We can add:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
fun setup() { | ||
fakeSession | ||
.roomService() | ||
.getRoom(A_FAKE_ROOM_ID) | ||
.timelineService() | ||
.givenTimelineEvent(A_POLL_START_TIMELINE_EVENT) | ||
} | ||
|
||
@After | ||
fun tearDown() { | ||
unmockkAll() | ||
} | ||
|
||
@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) | ||
val test = createPollViewModel.test() | ||
|
||
test | ||
.assertLatestState(initialCreatePollViewState) | ||
.finish() | ||
} | ||
|
||
@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 { | ||
val createPollViewModel = createPollViewModel(PollMode.CREATE) | ||
val test = createPollViewModel.test() | ||
|
||
createPollViewModel.handle(CreatePollAction.OnQuestionChanged(A_FAKE_QUESTION)) | ||
|
||
test | ||
.assertLatestState(pollViewStateWithOnlyQuestion) | ||
.finish() | ||
} | ||
|
||
@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 { | ||
val createPollViewModel = createPollViewModel(PollMode.CREATE) | ||
val test = createPollViewModel.test() | ||
|
||
createPollViewModel.handle(CreatePollAction.OnQuestionChanged(A_FAKE_QUESTION)) | ||
repeat(CreatePollViewModel.MIN_OPTIONS_COUNT - 1) { | ||
createPollViewModel.handle(CreatePollAction.OnOptionChanged(it, A_FAKE_OPTIONS[it])) | ||
} | ||
|
||
test | ||
.assertLatestState(pollViewStateWithQuestionAndNotEnoughOptions) | ||
.finish() | ||
} | ||
|
||
@Test | ||
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 test = createPollViewModel.test() | ||
|
||
repeat(CreatePollViewModel.MIN_OPTIONS_COUNT) { | ||
createPollViewModel.handle(CreatePollAction.OnOptionChanged(it, A_FAKE_OPTIONS[it])) | ||
} | ||
|
||
test | ||
.assertLatestState(pollViewStateWithoutQuestionAndEnoughOptions) | ||
.finish() | ||
} | ||
|
||
@Test | ||
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 test = createPollViewModel.test() | ||
|
||
createPollViewModel.handle(CreatePollAction.OnQuestionChanged(A_FAKE_QUESTION)) | ||
repeat(CreatePollViewModel.MIN_OPTIONS_COUNT) { | ||
createPollViewModel.handle(CreatePollAction.OnOptionChanged(it, A_FAKE_OPTIONS[it])) | ||
} | ||
|
||
test | ||
.assertLatestState(pollViewStateWithQuestionAndEnoughOptions) | ||
.finish() | ||
} | ||
|
||
@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 { | ||
val createPollViewModel = createPollViewModel(PollMode.CREATE) | ||
val test = createPollViewModel.test() | ||
|
||
createPollViewModel.handle(CreatePollAction.OnQuestionChanged(A_FAKE_QUESTION)) | ||
repeat(CreatePollViewModel.MAX_OPTIONS_COUNT) { | ||
if (it >= CreatePollViewModel.MIN_OPTIONS_COUNT) { | ||
createPollViewModel.handle(CreatePollAction.OnAddOption) | ||
} | ||
createPollViewModel.handle(CreatePollAction.OnOptionChanged(it, A_FAKE_OPTIONS[it])) | ||
} | ||
|
||
test | ||
.assertLatestState(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) | ||
val test = createPollViewModel.test() | ||
|
||
createPollViewModel.handle(CreatePollAction.OnPollTypeChanged(PollType.UNDISCLOSED)) | ||
createPollViewModel.handle(CreatePollAction.OnPollTypeChanged(PollType.DISCLOSED)) | ||
|
||
test | ||
.assertStatesChanges( | ||
initialCreatePollViewState, | ||
{ copy(pollType = PollType.UNDISCLOSED) }, | ||
{ copy(pollType = PollType.DISCLOSED) }, | ||
) | ||
.finish() | ||
} | ||
|
||
@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(A_FAKE_QUESTION)) | ||
createPollViewModel.handle(CreatePollAction.OnCreatePoll) | ||
|
||
createPollViewModel.handle(CreatePollAction.OnOptionChanged(0, A_FAKE_OPTIONS[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(A_FAKE_QUESTION)) | ||
createPollViewModel.handle(CreatePollAction.OnOptionChanged(0, A_FAKE_OPTIONS[0])) | ||
createPollViewModel.handle(CreatePollAction.OnOptionChanged(1, A_FAKE_OPTIONS[1])) | ||
createPollViewModel.handle(CreatePollAction.OnCreatePoll) | ||
|
||
test | ||
.assertEvents( | ||
CreatePollViewEvents.Success, | ||
) | ||
} | ||
|
||
@Test | ||
fun `given there is a question and enough options when the last option is deleted then view state should be updated accordingly`() = runTest { | ||
val createPollViewModel = createPollViewModel(PollMode.CREATE) | ||
val test = createPollViewModel.test() | ||
|
||
createPollViewModel.handle(CreatePollAction.OnQuestionChanged(A_FAKE_QUESTION)) | ||
createPollViewModel.handle(CreatePollAction.OnOptionChanged(0, A_FAKE_OPTIONS[0])) | ||
createPollViewModel.handle(CreatePollAction.OnOptionChanged(1, A_FAKE_OPTIONS[1])) | ||
createPollViewModel.handle(CreatePollAction.OnDeleteOption(1)) | ||
|
||
test.assertLatestState(pollViewStateWithQuestionAndEnoughOptionsButDeletedLastOption) | ||
} | ||
|
||
@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) | ||
val test = createPollViewModel.test() | ||
|
||
test | ||
.assertState(editedPollViewState) | ||
.finish() | ||
} | ||
|
||
@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) | ||
|
||
test | ||
.assertEvents( | ||
CreatePollViewEvents.Success, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we also should add tests for
PollMode.EDIT
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.