Apply Maxime's fix to get latest state.

This commit is contained in:
Onuray Sahin 2022-06-16 18:27:00 +03:00
parent 14a4a8edd7
commit 712a38e26a
3 changed files with 57 additions and 37 deletions

View File

@ -35,10 +35,8 @@ import im.vector.app.test.fakes.FakeCreatePollViewStates.pollViewStateWithoutQue
import im.vector.app.test.fakes.FakeSession import im.vector.app.test.fakes.FakeSession
import im.vector.app.test.test import im.vector.app.test.test
import io.mockk.unmockkAll import io.mockk.unmockkAll
import kotlinx.coroutines.delay
import kotlinx.coroutines.test.UnconfinedTestDispatcher import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest
import org.amshove.kluent.shouldBe
import org.junit.After import org.junit.After
import org.junit.Before import org.junit.Before
import org.junit.Rule import org.junit.Rule
@ -47,9 +45,11 @@ import org.matrix.android.sdk.api.session.room.model.message.PollType
class CreatePollViewModelTest { class CreatePollViewModelTest {
private val testDispatcher = UnconfinedTestDispatcher()
@get:Rule @get:Rule
val mvrxTestRule = MvRxTestRule( val mvRxTestRule = MvRxTestRule(
testDispatcher = UnconfinedTestDispatcher() // See https://github.com/airbnb/mavericks/issues/599 testDispatcher = testDispatcher // See https://github.com/airbnb/mavericks/issues/599
) )
private val fakeSession = FakeSession() private val fakeSession = FakeSession()
@ -79,71 +79,74 @@ class CreatePollViewModelTest {
@Test @Test
fun `given the view model is initialized then poll cannot be created and more options can be added`() = runTest { 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 createPollViewModel = createPollViewModel(PollMode.CREATE)
createPollViewModel val test = createPollViewModel.test()
.test()
.assertState(initialCreatePollViewState) test
.assertLatestState(initialCreatePollViewState)
.finish() .finish()
} }
@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)
val test = createPollViewModel.test()
createPollViewModel.handle(CreatePollAction.OnQuestionChanged(A_FAKE_QUESTION)) createPollViewModel.handle(CreatePollAction.OnQuestionChanged(A_FAKE_QUESTION))
// We need to wait for createPollViewModel.onChange is triggered test
delay(10) .assertLatestState(pollViewStateWithOnlyQuestion)
createPollViewModel
.test()
.assertState(pollViewStateWithOnlyQuestion)
.finish() .finish()
} }
@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)
val test = createPollViewModel.test()
createPollViewModel.handle(CreatePollAction.OnQuestionChanged(A_FAKE_QUESTION)) createPollViewModel.handle(CreatePollAction.OnQuestionChanged(A_FAKE_QUESTION))
repeat(CreatePollViewModel.MIN_OPTIONS_COUNT - 1) { repeat(CreatePollViewModel.MIN_OPTIONS_COUNT - 1) {
createPollViewModel.handle(CreatePollAction.OnOptionChanged(it, A_FAKE_OPTIONS[it])) createPollViewModel.handle(CreatePollAction.OnOptionChanged(it, A_FAKE_OPTIONS[it]))
} }
delay(10) test
createPollViewModel .assertLatestState(pollViewStateWithQuestionAndNotEnoughOptions)
.test()
.assertState(pollViewStateWithQuestionAndNotEnoughOptions)
.finish() .finish()
} }
@Test @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 { 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)
val test = createPollViewModel.test()
repeat(CreatePollViewModel.MIN_OPTIONS_COUNT) { repeat(CreatePollViewModel.MIN_OPTIONS_COUNT) {
createPollViewModel.handle(CreatePollAction.OnOptionChanged(it, A_FAKE_OPTIONS[it])) createPollViewModel.handle(CreatePollAction.OnOptionChanged(it, A_FAKE_OPTIONS[it]))
} }
delay(10) test
createPollViewModel .assertLatestState(pollViewStateWithoutQuestionAndEnoughOptions)
.test()
.assertState(pollViewStateWithoutQuestionAndEnoughOptions)
.finish() .finish()
} }
@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)
val test = createPollViewModel.test()
createPollViewModel.handle(CreatePollAction.OnQuestionChanged(A_FAKE_QUESTION)) createPollViewModel.handle(CreatePollAction.OnQuestionChanged(A_FAKE_QUESTION))
repeat(CreatePollViewModel.MIN_OPTIONS_COUNT) { repeat(CreatePollViewModel.MIN_OPTIONS_COUNT) {
createPollViewModel.handle(CreatePollAction.OnOptionChanged(it, A_FAKE_OPTIONS[it])) createPollViewModel.handle(CreatePollAction.OnOptionChanged(it, A_FAKE_OPTIONS[it]))
} }
//delay(10) test
createPollViewModel.test() .assertLatestState(pollViewStateWithQuestionAndEnoughOptions)
.assertState(pollViewStateWithQuestionAndEnoughOptions)
.finish() .finish()
} }
@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)
val test = createPollViewModel.test()
createPollViewModel.handle(CreatePollAction.OnQuestionChanged(A_FAKE_QUESTION)) 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) {
@ -152,20 +155,26 @@ class CreatePollViewModelTest {
createPollViewModel.handle(CreatePollAction.OnOptionChanged(it, A_FAKE_OPTIONS[it])) createPollViewModel.handle(CreatePollAction.OnOptionChanged(it, A_FAKE_OPTIONS[it]))
} }
delay(10) test
createPollViewModel .assertLatestState(pollViewStateWithQuestionAndMaxOptions)
.test()
.assertState(pollViewStateWithQuestionAndMaxOptions)
.finish() .finish()
} }
@Test @Test
fun `given an initial poll state when poll type is changed then view state is updated accordingly`() = runTest { fun `given an initial poll state when poll type is changed then view state is updated accordingly`() = runTest {
val createPollViewModel = createPollViewModel(PollMode.CREATE) val createPollViewModel = createPollViewModel(PollMode.CREATE)
createPollViewModel.handle(CreatePollAction.OnPollTypeChanged(PollType.DISCLOSED)) val test = createPollViewModel.test()
createPollViewModel.awaitState().pollType shouldBe PollType.DISCLOSED
createPollViewModel.handle(CreatePollAction.OnPollTypeChanged(PollType.UNDISCLOSED)) createPollViewModel.handle(CreatePollAction.OnPollTypeChanged(PollType.UNDISCLOSED))
createPollViewModel.awaitState().pollType shouldBe PollType.UNDISCLOSED createPollViewModel.handle(CreatePollAction.OnPollTypeChanged(PollType.DISCLOSED))
test
.assertStatesChanges(
initialCreatePollViewState,
{ copy(pollType = PollType.UNDISCLOSED) },
{ copy(pollType = PollType.DISCLOSED) },
)
.finish()
} }
@Test @Test
@ -185,7 +194,7 @@ class CreatePollViewModelTest {
.assertEvents( .assertEvents(
CreatePollViewEvents.EmptyQuestionError, CreatePollViewEvents.EmptyQuestionError,
CreatePollViewEvents.NotEnoughOptionsError(requiredOptionsCount = CreatePollViewModel.MIN_OPTIONS_COUNT), CreatePollViewEvents.NotEnoughOptionsError(requiredOptionsCount = CreatePollViewModel.MIN_OPTIONS_COUNT),
CreatePollViewEvents.NotEnoughOptionsError(requiredOptionsCount = CreatePollViewModel.MIN_OPTIONS_COUNT) CreatePollViewEvents.NotEnoughOptionsError(requiredOptionsCount = CreatePollViewModel.MIN_OPTIONS_COUNT),
) )
} }
@ -201,29 +210,31 @@ class CreatePollViewModelTest {
test test
.assertEvents( .assertEvents(
CreatePollViewEvents.Success CreatePollViewEvents.Success,
) )
} }
@Test @Test
fun `given there is a question and enough options when the last option is deleted then view state should be updated accordingly`() = runTest { 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 createPollViewModel = createPollViewModel(PollMode.CREATE)
val test = createPollViewModel.test()
createPollViewModel.handle(CreatePollAction.OnQuestionChanged(A_FAKE_QUESTION)) createPollViewModel.handle(CreatePollAction.OnQuestionChanged(A_FAKE_QUESTION))
createPollViewModel.handle(CreatePollAction.OnOptionChanged(0, A_FAKE_OPTIONS[0])) createPollViewModel.handle(CreatePollAction.OnOptionChanged(0, A_FAKE_OPTIONS[0]))
createPollViewModel.handle(CreatePollAction.OnOptionChanged(1, A_FAKE_OPTIONS[1])) createPollViewModel.handle(CreatePollAction.OnOptionChanged(1, A_FAKE_OPTIONS[1]))
createPollViewModel.handle(CreatePollAction.OnDeleteOption(1)) createPollViewModel.handle(CreatePollAction.OnDeleteOption(1))
delay(10) test.assertLatestState(pollViewStateWithQuestionAndEnoughOptionsButDeletedLastOption)
createPollViewModel.test().assertState(pollViewStateWithQuestionAndEnoughOptionsButDeletedLastOption)
} }
@Test @Test
fun `given an edited poll event when question and options are changed then view state is updated accordingly`() = runTest { 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 createPollViewModel = createPollViewModel(PollMode.EDIT)
val test = createPollViewModel.test()
delay(10) test
createPollViewModel.test().assertState(editedPollViewState) .assertState(editedPollViewState)
.finish()
} }
@Test @Test
@ -235,7 +246,7 @@ class CreatePollViewModelTest {
test test
.assertEvents( .assertEvents(
CreatePollViewEvents.Success CreatePollViewEvents.Success,
) )
} }
} }

View File

@ -86,6 +86,11 @@ class ViewModelTest<S, VE>(
return this return this
} }
fun assertLatestState(expected: S): ViewModelTest<S, VE> {
states.assertLatestValue(expected)
return this
}
fun finish() { fun finish() {
states.finish() states.finish()
viewEvents.finish() viewEvents.finish()

View File

@ -47,6 +47,10 @@ class FlowTestObserver<T>(
return this return this
} }
fun assertLatestValue(value: T) {
assertTrue(values.last() == value)
}
fun assertValues(values: List<T>): FlowTestObserver<T> { fun assertValues(values: List<T>): FlowTestObserver<T> {
assertEquals(values, this.values) assertEquals(values, this.values)
return this return this