Skip to content

Commit be65afd

Browse files
committed
Ensure enterpriseService.isElementCallAvailable() is not called several times.
And fix unit tests on CI
1 parent f93bc5e commit be65afd

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

features/userprofile/impl/src/main/kotlin/io/element/android/features/userprofile/impl/root/UserProfilePresenter.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,11 @@ class UserProfilePresenter @AssistedInject constructor(
6161

6262
@Composable
6363
private fun getCanCall(roomId: RoomId?): State<Boolean> {
64-
return produceState(initialValue = false, roomId) {
65-
val isElementCallAvailable = enterpriseService.isElementCallAvailable()
64+
val isElementCallAvailable by produceState(initialValue = false, roomId) {
65+
value = enterpriseService.isElementCallAvailable()
66+
}
6667

68+
return produceState(initialValue = false, isElementCallAvailable, roomId) {
6769
value = when {
6870
isElementCallAvailable.not() -> false
6971
client.isMe(userId) -> false

features/userprofile/impl/src/test/kotlin/io/element/android/features/userprofile/impl/UserProfilePresenterTest.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import io.element.android.libraries.matrix.test.encryption.FakeEncryptionService
3838
import io.element.android.libraries.matrix.test.room.FakeBaseRoom
3939
import io.element.android.libraries.matrix.ui.components.aMatrixUser
4040
import io.element.android.tests.testutils.WarmUpRule
41-
import io.element.android.tests.testutils.awaitLastSequentialItem
4241
import io.element.android.tests.testutils.lambda.any
4342
import io.element.android.tests.testutils.lambda.lambdaRecorder
4443
import io.element.android.tests.testutils.lambda.value
@@ -82,6 +81,8 @@ class UserProfilePresenterTest {
8281
fun `present - canCall is true when all the conditions are met`() {
8382
testCanCall(
8483
expectedResult = true,
84+
skipItems = 3,
85+
checkThatRoomIsDestroyed = true,
8586
)
8687
}
8788

@@ -131,6 +132,8 @@ class UserProfilePresenterTest {
131132
dmRoom: RoomId? = A_ROOM_ID,
132133
canFindRoom: Boolean = true,
133134
expectedResult: Boolean,
135+
skipItems: Int = 1,
136+
checkThatRoomIsDestroyed: Boolean = false,
134137
) = runTest {
135138
val room = FakeBaseRoom(
136139
canUserJoinCallResult = { canUserJoinCallResult },
@@ -147,10 +150,10 @@ class UserProfilePresenterTest {
147150
isElementCallAvailable = isElementCallAvailable,
148151
)
149152
presenter.test {
150-
val initialState = awaitLastSequentialItem()
153+
val initialState = awaitFirstItem(skipItems)
151154
assertThat(initialState.canCall).isEqualTo(expectedResult)
152155
}
153-
if (isElementCallAvailable && canFindRoom && dmRoom != null) {
156+
if (checkThatRoomIsDestroyed) {
154157
room.assertDestroyed()
155158
}
156159
}
@@ -216,7 +219,7 @@ class UserProfilePresenterTest {
216219
)
217220
val presenter = createUserProfilePresenter(client = matrixClient)
218221
presenter.test {
219-
val initialState = awaitFirstItem()
222+
val initialState = awaitFirstItem(count = 2)
220223
initialState.eventSink(UserProfileEvents.BlockUser(needsConfirmation = false))
221224
assertThat(awaitItem().isBlocked.isLoading()).isTrue()
222225
val errorState = awaitItem()
@@ -234,7 +237,7 @@ class UserProfilePresenterTest {
234237
)
235238
val presenter = createUserProfilePresenter(client = matrixClient)
236239
presenter.test {
237-
val initialState = awaitFirstItem()
240+
val initialState = awaitFirstItem(count = 2)
238241
initialState.eventSink(UserProfileEvents.UnblockUser(needsConfirmation = false))
239242
assertThat(awaitItem().isBlocked.isLoading()).isTrue()
240243
val errorState = awaitItem()
@@ -377,8 +380,8 @@ class UserProfilePresenterTest {
377380
}
378381
}
379382

380-
private suspend fun <T> ReceiveTurbine<T>.awaitFirstItem(): T {
381-
skipItems(1)
383+
private suspend fun <T> ReceiveTurbine<T>.awaitFirstItem(count: Int = 1): T {
384+
skipItems(count)
382385
return awaitItem()
383386
}
384387

0 commit comments

Comments
 (0)