Unit tests for the ViewModel

This commit is contained in:
Maxime NATUREL 2022-09-09 14:32:35 +02:00
parent c1dfa925c0
commit 6cd0fbb614
4 changed files with 76 additions and 5 deletions

View File

@ -29,7 +29,6 @@ import im.vector.app.features.settings.devices.v2.overview.GetDeviceFullInfoUseC
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
// TODO add unit tests
class SessionDetailsViewModel @AssistedInject constructor(
@Assisted val initialState: SessionDetailsViewState,
private val getDeviceFullInfoUseCase: GetDeviceFullInfoUseCase,

View File

@ -26,7 +26,6 @@ import im.vector.app.core.di.hiltMavericksViewModelFactory
import im.vector.app.core.platform.EmptyViewEvents
import im.vector.app.core.platform.VectorViewModel
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.flow.onEach
import org.matrix.android.sdk.api.session.Session

View File

@ -0,0 +1,71 @@
/*
* 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.details
import com.airbnb.mvrx.Success
import com.airbnb.mvrx.test.MvRxTestRule
import im.vector.app.features.settings.devices.v2.DeviceFullInfo
import im.vector.app.features.settings.devices.v2.overview.GetDeviceFullInfoUseCase
import im.vector.app.test.test
import im.vector.app.test.testDispatcher
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.flow.flowOf
import org.junit.Rule
import org.junit.Test
import org.matrix.android.sdk.api.session.crypto.model.DeviceInfo
private const val A_SESSION_ID = "session-id"
class SessionDetailsViewModelTest {
@get:Rule
val mvRxTestRule = MvRxTestRule(testDispatcher = testDispatcher)
private val args = SessionDetailsArgs(
deviceId = A_SESSION_ID
)
private val getDeviceFullInfoUseCase = mockk<GetDeviceFullInfoUseCase>()
private fun createViewModel() = SessionDetailsViewModel(
initialState = SessionDetailsViewState(args),
getDeviceFullInfoUseCase = getDeviceFullInfoUseCase
)
@Test
fun `given the viewModel has been initialized then viewState is updated with session info`() {
// Given
val deviceFullInfo = mockk<DeviceFullInfo>()
val deviceInfo = mockk<DeviceInfo>()
every { deviceFullInfo.deviceInfo } returns deviceInfo
every { getDeviceFullInfoUseCase.execute(A_SESSION_ID) } returns flowOf(deviceFullInfo)
val expectedState = SessionDetailsViewState(
deviceId = A_SESSION_ID,
deviceInfo = Success(deviceInfo)
)
// When
val viewModel = createViewModel()
// Then
viewModel.test()
.assertLatestState { state -> state == expectedState }
.finish()
verify { getDeviceFullInfoUseCase.execute(A_SESSION_ID) }
}
}

View File

@ -21,22 +21,21 @@ import com.airbnb.mvrx.test.MvRxTestRule
import im.vector.app.features.settings.devices.v2.DeviceFullInfo
import im.vector.app.test.fakes.FakeSession
import im.vector.app.test.test
import im.vector.app.test.testDispatcher
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.UnconfinedTestDispatcher
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())
val mvRxTestRule = MvRxTestRule(testDispatcher = testDispatcher)
private val args = SessionOverviewArgs(
deviceId = A_SESSION_ID
@ -52,6 +51,7 @@ class SessionOverviewViewModelTest {
@Test
fun `given the viewModel has been initialized then viewState is updated with session info`() {
// Given
val sessionParams = givenIdForSession(A_SESSION_ID)
val deviceFullInfo = mockk<DeviceFullInfo>()
every { getDeviceFullInfoUseCase.execute(A_SESSION_ID) } returns flowOf(deviceFullInfo)
@ -61,8 +61,10 @@ class SessionOverviewViewModelTest {
deviceInfo = Success(deviceFullInfo)
)
// When
val viewModel = createViewModel()
// Then
viewModel.test()
.assertLatestState { state -> state == expectedState }
.finish()