Adding unit tests for viewModel

This commit is contained in:
Maxime NATUREL 2022-08-31 14:44:01 +02:00
parent 295ae55142
commit 412fda27af
4 changed files with 89 additions and 1 deletions

View File

@ -30,7 +30,6 @@ import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.flow.onEach
import org.matrix.android.sdk.api.session.Session
// TODO add unit tests
class SessionOverviewViewModel @AssistedInject constructor(
@Assisted val initialState: SessionOverviewViewState,
session: Session,

View File

@ -16,11 +16,15 @@
package im.vector.app.features.settings.devices.v2.overview
import com.airbnb.mvrx.Async
import com.airbnb.mvrx.MavericksState
import com.airbnb.mvrx.Uninitialized
import im.vector.app.features.settings.devices.DeviceFullInfo
data class SessionOverviewViewState(
val sessionId: String,
val isCurrentSession: Boolean = false,
val deviceInfo: Async<DeviceFullInfo> = Uninitialized,
) : MavericksState {
constructor(args: SessionOverviewArgs) : this(
sessionId = args.sessionId

View File

@ -0,0 +1,80 @@
/*
* 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.settings.devices.v2.overview
import com.airbnb.mvrx.Success
import com.airbnb.mvrx.test.MvRxTestRule
import im.vector.app.features.settings.devices.DeviceFullInfo
import im.vector.app.test.fakes.FakeSession
import im.vector.app.test.test
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import org.junit.Rule
import org.junit.Test
import org.matrix.android.sdk.api.auth.data.SessionParams
import org.matrix.android.sdk.api.util.Optional
private const val A_SESSION_ID = "session-id"
class SessionOverviewViewModelTest {
@get:Rule
val mvRxTestRule = MvRxTestRule(testDispatcher = UnconfinedTestDispatcher())
private val args = SessionOverviewArgs(
sessionId = A_SESSION_ID
)
private val fakeSession = FakeSession()
private val getDeviceFullInfoUseCase = mockk<GetDeviceFullInfoUseCase>()
private fun createViewModel() = SessionOverviewViewModel(
initialState = SessionOverviewViewState(args),
session = fakeSession,
getDeviceFullInfoUseCase = getDeviceFullInfoUseCase
)
@Test
fun `given the viewModel has been initialized then viewState is updated with session info`() = runTest {
val sessionParams = givenIdForSession(A_SESSION_ID)
val deviceFullInfo = mockk<DeviceFullInfo>()
every { getDeviceFullInfoUseCase.execute(A_SESSION_ID) } returns flowOf(Optional(deviceFullInfo))
val expectedState = SessionOverviewViewState(
sessionId = A_SESSION_ID,
isCurrentSession = true,
deviceInfo = Success(deviceFullInfo)
)
val viewModel = createViewModel()
viewModel.test()
.assertLatestState { state -> state == expectedState }
.finish()
verify { sessionParams.deviceId }
verify { getDeviceFullInfoUseCase.execute(A_SESSION_ID) }
}
private fun givenIdForSession(deviceId: String): SessionParams {
val sessionParams = mockk<SessionParams>()
every { sessionParams.deviceId } returns deviceId
fakeSession.givenSessionParams(sessionParams)
return sessionParams
}
}

View File

@ -26,6 +26,7 @@ import io.mockk.coJustRun
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkStatic
import org.matrix.android.sdk.api.auth.data.SessionParams
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.getRoomSummary
import org.matrix.android.sdk.api.session.homeserver.HomeServerCapabilitiesService
@ -71,6 +72,10 @@ class FakeSession(
}
}
fun givenSessionParams(sessionParams: SessionParams) {
every { this@FakeSession.sessionParams } returns sessionParams
}
companion object {
fun withRoomSummary(roomSummary: RoomSummary) = FakeSession().apply {