Add test on SpacePresenter
This commit is contained in:
parent
aa63d9420a
commit
884231e1e1
2 changed files with 216 additions and 0 deletions
|
|
@ -0,0 +1,167 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2025 New Vector Ltd.
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||||
|
* Please see LICENSE files in the repository root for full details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
@file:OptIn(ExperimentalCoroutinesApi::class)
|
||||||
|
|
||||||
|
package io.element.android.features.space.impl
|
||||||
|
|
||||||
|
import com.google.common.truth.Truth.assertThat
|
||||||
|
import io.element.android.features.invite.api.SeenInvitesStore
|
||||||
|
import io.element.android.features.invite.test.InMemorySeenInvitesStore
|
||||||
|
import io.element.android.features.space.api.SpaceEntryPoint
|
||||||
|
import io.element.android.libraries.matrix.api.MatrixClient
|
||||||
|
import io.element.android.libraries.matrix.api.spaces.SpaceRoomList
|
||||||
|
import io.element.android.libraries.matrix.test.A_ROOM_ID
|
||||||
|
import io.element.android.libraries.matrix.test.FakeMatrixClient
|
||||||
|
import io.element.android.libraries.matrix.test.spaces.FakeSpaceRoomList
|
||||||
|
import io.element.android.libraries.matrix.test.spaces.FakeSpaceService
|
||||||
|
import io.element.android.libraries.previewutils.room.aSpaceRoom
|
||||||
|
import io.element.android.tests.testutils.lambda.lambdaRecorder
|
||||||
|
import io.element.android.tests.testutils.test
|
||||||
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
|
import kotlinx.coroutines.test.advanceUntilIdle
|
||||||
|
import kotlinx.coroutines.test.runTest
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class SpacePresenterTest {
|
||||||
|
@Test
|
||||||
|
fun `present - initial state`() = runTest {
|
||||||
|
val paginateResult = lambdaRecorder<Result<Unit>> {
|
||||||
|
Result.success(Unit)
|
||||||
|
}
|
||||||
|
val presenter = createSpacePresenter(
|
||||||
|
client = FakeMatrixClient(
|
||||||
|
spaceService = FakeSpaceService(
|
||||||
|
spaceRoomListResult = {
|
||||||
|
FakeSpaceRoomList(
|
||||||
|
paginateResult = paginateResult,
|
||||||
|
)
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
presenter.test {
|
||||||
|
val state = awaitItem()
|
||||||
|
assertThat(state.currentSpace).isNull()
|
||||||
|
assertThat(state.children).isEmpty()
|
||||||
|
assertThat(state.seenSpaceInvites).isEmpty()
|
||||||
|
assertThat(state.hideInvitesAvatar).isFalse()
|
||||||
|
assertThat(state.hasMoreToLoad).isTrue()
|
||||||
|
advanceUntilIdle()
|
||||||
|
paginateResult.assertions().isCalledOnce()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `present - load more`() = runTest {
|
||||||
|
val paginateResult = lambdaRecorder<Result<Unit>> {
|
||||||
|
Result.success(Unit)
|
||||||
|
}
|
||||||
|
val presenter = createSpacePresenter(
|
||||||
|
client = FakeMatrixClient(
|
||||||
|
spaceService = FakeSpaceService(
|
||||||
|
spaceRoomListResult = {
|
||||||
|
FakeSpaceRoomList(
|
||||||
|
paginateResult = paginateResult,
|
||||||
|
)
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
presenter.test {
|
||||||
|
val state = awaitItem()
|
||||||
|
advanceUntilIdle()
|
||||||
|
paginateResult.assertions().isCalledOnce()
|
||||||
|
state.eventSink(SpaceEvents.LoadMore)
|
||||||
|
advanceUntilIdle()
|
||||||
|
paginateResult.assertions().isCalledExactly(2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `present - has more to load value`() = runTest {
|
||||||
|
val fakeSpaceRoomList = FakeSpaceRoomList(
|
||||||
|
paginateResult = { Result.success(Unit) },
|
||||||
|
)
|
||||||
|
val presenter = createSpacePresenter(
|
||||||
|
client = FakeMatrixClient(
|
||||||
|
spaceService = FakeSpaceService(
|
||||||
|
spaceRoomListResult = { fakeSpaceRoomList },
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
presenter.test {
|
||||||
|
val state = awaitItem()
|
||||||
|
advanceUntilIdle()
|
||||||
|
assertThat(state.hasMoreToLoad).isTrue()
|
||||||
|
fakeSpaceRoomList.emitPaginationStatus(
|
||||||
|
SpaceRoomList.PaginationStatus.Idle(hasMoreToLoad = false)
|
||||||
|
)
|
||||||
|
assertThat(awaitItem().hasMoreToLoad).isFalse()
|
||||||
|
fakeSpaceRoomList.emitPaginationStatus(
|
||||||
|
SpaceRoomList.PaginationStatus.Idle(hasMoreToLoad = true)
|
||||||
|
)
|
||||||
|
assertThat(awaitItem().hasMoreToLoad).isTrue()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `present - current space value`() = runTest {
|
||||||
|
val fakeSpaceRoomList = FakeSpaceRoomList(
|
||||||
|
paginateResult = { Result.success(Unit) },
|
||||||
|
)
|
||||||
|
val presenter = createSpacePresenter(
|
||||||
|
client = FakeMatrixClient(
|
||||||
|
spaceService = FakeSpaceService(
|
||||||
|
spaceRoomListResult = { fakeSpaceRoomList },
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
presenter.test {
|
||||||
|
val state = awaitItem()
|
||||||
|
advanceUntilIdle()
|
||||||
|
assertThat(state.currentSpace).isNull()
|
||||||
|
val aSpace = aSpaceRoom()
|
||||||
|
fakeSpaceRoomList.emitCurrentSpace(aSpace)
|
||||||
|
assertThat(awaitItem().currentSpace).isEqualTo(aSpace)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `present - children value`() = runTest {
|
||||||
|
val fakeSpaceRoomList = FakeSpaceRoomList(
|
||||||
|
paginateResult = { Result.success(Unit) },
|
||||||
|
)
|
||||||
|
val presenter = createSpacePresenter(
|
||||||
|
client = FakeMatrixClient(
|
||||||
|
spaceService = FakeSpaceService(
|
||||||
|
spaceRoomListResult = { fakeSpaceRoomList },
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
presenter.test {
|
||||||
|
val state = awaitItem()
|
||||||
|
advanceUntilIdle()
|
||||||
|
assertThat(state.children).isEmpty()
|
||||||
|
val aSpace = aSpaceRoom()
|
||||||
|
fakeSpaceRoomList.emitSpaceRooms(listOf(aSpace))
|
||||||
|
assertThat(awaitItem().children).containsExactly(aSpace)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun createSpacePresenter(
|
||||||
|
inputs: SpaceEntryPoint.Inputs = SpaceEntryPoint.Inputs(A_ROOM_ID),
|
||||||
|
client: MatrixClient = FakeMatrixClient(),
|
||||||
|
seenInvitesStore: SeenInvitesStore = InMemorySeenInvitesStore(),
|
||||||
|
): SpacePresenter {
|
||||||
|
return SpacePresenter(
|
||||||
|
inputs = inputs,
|
||||||
|
client = client,
|
||||||
|
seenInvitesStore = seenInvitesStore,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,49 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2025 New Vector Ltd.
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||||
|
* Please see LICENSE files in the repository root for full details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.element.android.libraries.matrix.test.spaces
|
||||||
|
|
||||||
|
import io.element.android.libraries.matrix.api.spaces.SpaceRoom
|
||||||
|
import io.element.android.libraries.matrix.api.spaces.SpaceRoomList
|
||||||
|
import io.element.android.tests.testutils.lambda.lambdaError
|
||||||
|
import io.element.android.tests.testutils.simulateLongTask
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
|
|
||||||
|
class FakeSpaceRoomList(
|
||||||
|
initialSpaceFlowValue: SpaceRoom? = null,
|
||||||
|
initialSpaceRoomsValue: List<SpaceRoom> = emptyList(),
|
||||||
|
initialSpaceRoomList: SpaceRoomList.PaginationStatus = SpaceRoomList.PaginationStatus.Loading,
|
||||||
|
private val paginateResult: () -> Result<Unit> = { lambdaError() },
|
||||||
|
) : SpaceRoomList {
|
||||||
|
private val _currentSpaceFlow: MutableStateFlow<SpaceRoom?> = MutableStateFlow(initialSpaceFlowValue)
|
||||||
|
override fun currentSpaceFlow(): Flow<SpaceRoom?> = _currentSpaceFlow.asStateFlow()
|
||||||
|
|
||||||
|
fun emitCurrentSpace(value: SpaceRoom?) {
|
||||||
|
_currentSpaceFlow.value = value
|
||||||
|
}
|
||||||
|
|
||||||
|
private val _spaceRoomsFlow: MutableStateFlow<List<SpaceRoom>> = MutableStateFlow(initialSpaceRoomsValue)
|
||||||
|
override val spaceRoomsFlow: Flow<List<SpaceRoom>> = _spaceRoomsFlow.asStateFlow()
|
||||||
|
|
||||||
|
fun emitSpaceRooms(value: List<SpaceRoom>) {
|
||||||
|
_spaceRoomsFlow.value = value
|
||||||
|
}
|
||||||
|
|
||||||
|
private val _paginationStatusFlow: MutableStateFlow<SpaceRoomList.PaginationStatus> = MutableStateFlow(initialSpaceRoomList)
|
||||||
|
override val paginationStatusFlow: StateFlow<SpaceRoomList.PaginationStatus> = _paginationStatusFlow.asStateFlow()
|
||||||
|
|
||||||
|
fun emitPaginationStatus(value: SpaceRoomList.PaginationStatus) {
|
||||||
|
_paginationStatusFlow.value = value
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun paginate(): Result<Unit> = simulateLongTask {
|
||||||
|
paginateResult()
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue