From e46503ae821816b98d44cf67d8295cd8c02a961e Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 17 Oct 2024 15:55:45 +0200 Subject: [PATCH] Cover getCanCall with tests. --- .../impl/root/UserProfilePresenter.kt | 4 +- .../impl/UserProfilePresenterTest.kt | 70 +++++++++++++++++++ 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/features/userprofile/impl/src/main/kotlin/io/element/android/features/userprofile/impl/root/UserProfilePresenter.kt b/features/userprofile/impl/src/main/kotlin/io/element/android/features/userprofile/impl/root/UserProfilePresenter.kt index 30209d49e6..e73eef6aa0 100644 --- a/features/userprofile/impl/src/main/kotlin/io/element/android/features/userprofile/impl/root/UserProfilePresenter.kt +++ b/features/userprofile/impl/src/main/kotlin/io/element/android/features/userprofile/impl/root/UserProfilePresenter.kt @@ -50,14 +50,14 @@ class UserProfilePresenter @AssistedInject constructor( } @Composable - fun getDmRoomId(): State { + private fun getDmRoomId(): State { return produceState(initialValue = null) { value = client.findDM(userId) } } @Composable - fun getCanCall(roomId: RoomId?): State { + private fun getCanCall(roomId: RoomId?): State { return produceState(initialValue = false, roomId) { value = if (client.isMe(userId)) { false diff --git a/features/userprofile/impl/src/test/kotlin/io/element/android/features/userprofile/impl/UserProfilePresenterTest.kt b/features/userprofile/impl/src/test/kotlin/io/element/android/features/userprofile/impl/UserProfilePresenterTest.kt index 6dedaa70e5..485ac268a5 100644 --- a/features/userprofile/impl/src/test/kotlin/io/element/android/features/userprofile/impl/UserProfilePresenterTest.kt +++ b/features/userprofile/impl/src/test/kotlin/io/element/android/features/userprofile/impl/UserProfilePresenterTest.kt @@ -20,14 +20,18 @@ import io.element.android.features.userprofile.impl.root.UserProfilePresenter import io.element.android.libraries.architecture.AsyncAction import io.element.android.libraries.architecture.AsyncData import io.element.android.libraries.matrix.api.MatrixClient +import io.element.android.libraries.matrix.api.core.RoomId import io.element.android.libraries.matrix.api.core.UserId import io.element.android.libraries.matrix.test.AN_EXCEPTION import io.element.android.libraries.matrix.test.A_ROOM_ID import io.element.android.libraries.matrix.test.A_THROWABLE import io.element.android.libraries.matrix.test.A_USER_ID +import io.element.android.libraries.matrix.test.A_USER_ID_2 import io.element.android.libraries.matrix.test.FakeMatrixClient +import io.element.android.libraries.matrix.test.room.FakeMatrixRoom import io.element.android.libraries.matrix.ui.components.aMatrixUser import io.element.android.tests.testutils.WarmUpRule +import io.element.android.tests.testutils.awaitLastSequentialItem import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.runTest import org.junit.Rule @@ -60,6 +64,72 @@ class UserProfilePresenterTest { } } + @Test + fun `present - canCall is true when all the conditions are met`() { + testCanCall( + expectedResult = true, + ) + } + + @Test + fun `present - canCall is false when canUserJoinCall returns false`() { + testCanCall( + canUserJoinCallResult = Result.success(false), + expectedResult = false, + ) + } + + @Test + fun `present - canCall is false when canUserJoinCall fails`() { + testCanCall( + canUserJoinCallResult = Result.failure(AN_EXCEPTION), + expectedResult = false, + ) + } + + @Test + fun `present - canCall is false when there is no DM`() { + testCanCall( + dmRoom = null, + expectedResult = false, + ) + } + + @Test + fun `present - canCall is false when room is not found`() { + testCanCall( + canFindRoom = false, + expectedResult = false, + ) + } + + private fun testCanCall( + canUserJoinCallResult: Result = Result.success(true), + dmRoom: RoomId? = A_ROOM_ID, + canFindRoom: Boolean = true, + expectedResult: Boolean, + ) = runTest { + val room = FakeMatrixRoom( + canUserJoinCallResult = { canUserJoinCallResult }, + ) + val client = FakeMatrixClient().apply { + if (canFindRoom) { + givenGetRoomResult(A_ROOM_ID, room) + } + givenFindDmResult(dmRoom) + } + val presenter = createUserProfilePresenter( + userId = A_USER_ID_2, + client = client, + ) + moleculeFlow(RecompositionMode.Immediate) { + presenter.present() + }.test { + val initialState = awaitLastSequentialItem() + assertThat(initialState.canCall).isEqualTo(expectedResult) + } + } + @Test fun `present - returns empty data in case of failure`() = runTest { val client = FakeMatrixClient().apply {