From 6cd0fbb614f4778a3f2b6446d44e560d4ae997d5 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Fri, 9 Sep 2022 14:32:35 +0200 Subject: [PATCH] Unit tests for the ViewModel --- .../v2/details/SessionDetailsViewModel.kt | 1 - .../v2/overview/SessionOverviewViewModel.kt | 1 - .../v2/details/SessionDetailsViewModelTest.kt | 71 +++++++++++++++++++ .../overview/SessionOverviewViewModelTest.kt | 8 ++- 4 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 vector/src/test/java/im/vector/app/features/settings/devices/v2/details/SessionDetailsViewModelTest.kt diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/SessionDetailsViewModel.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/SessionDetailsViewModel.kt index 629f9bb839..2064f8ffdd 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/SessionDetailsViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/SessionDetailsViewModel.kt @@ -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, diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModel.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModel.kt index a236814a40..bdcdc40c56 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModel.kt @@ -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 diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/details/SessionDetailsViewModelTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/details/SessionDetailsViewModelTest.kt new file mode 100644 index 0000000000..8444378193 --- /dev/null +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/details/SessionDetailsViewModelTest.kt @@ -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() + + 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() + val deviceInfo = mockk() + 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) } + } +} diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModelTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModelTest.kt index fe33bd8b0b..8d4e49ef85 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModelTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModelTest.kt @@ -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() 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()