From 412fda27af501914c7a76c120a75e820dd084502 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Wed, 31 Aug 2022 14:44:01 +0200 Subject: [PATCH] Adding unit tests for viewModel --- .../v2/overview/SessionOverviewViewModel.kt | 1 - .../v2/overview/SessionOverviewViewState.kt | 4 + .../overview/SessionOverviewViewModelTest.kt | 80 +++++++++++++++++++ .../im/vector/app/test/fakes/FakeSession.kt | 5 ++ 4 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModelTest.kt 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 84c15301aa..9c40480270 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 @@ -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, diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewState.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewState.kt index e839348800..8fa19a6eee 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewState.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewState.kt @@ -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 = Uninitialized, ) : MavericksState { constructor(args: SessionOverviewArgs) : this( sessionId = args.sessionId 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 new file mode 100644 index 0000000000..f15bc0860c --- /dev/null +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModelTest.kt @@ -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() + + 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() + 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() + every { sessionParams.deviceId } returns deviceId + fakeSession.givenSessionParams(sessionParams) + return sessionParams + } +} diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeSession.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeSession.kt index ee016ecae3..71bcde5807 100644 --- a/vector/src/test/java/im/vector/app/test/fakes/FakeSession.kt +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeSession.kt @@ -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 {