From 7dd243cd70e843f156229ec103f1a7682a7817cf Mon Sep 17 00:00:00 2001 From: ganfra Date: Fri, 7 Jul 2023 11:30:55 +0200 Subject: [PATCH] Await room: add tests on LoadingRoomStateFlowFactory --- .../room/LoadingRoomStateFlowFactoryTest.kt | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 appnav/src/test/kotlin/io/element/android/appnav/room/LoadingRoomStateFlowFactoryTest.kt diff --git a/appnav/src/test/kotlin/io/element/android/appnav/room/LoadingRoomStateFlowFactoryTest.kt b/appnav/src/test/kotlin/io/element/android/appnav/room/LoadingRoomStateFlowFactoryTest.kt new file mode 100644 index 0000000000..17b6f6deb9 --- /dev/null +++ b/appnav/src/test/kotlin/io/element/android/appnav/room/LoadingRoomStateFlowFactoryTest.kt @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2023 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 io.element.android.appnav.room + +import app.cash.turbine.test +import com.google.common.truth.Truth +import io.element.android.libraries.matrix.api.room.RoomSummaryDataSource +import io.element.android.libraries.matrix.test.A_ROOM_ID +import io.element.android.libraries.matrix.test.A_SESSION_ID +import io.element.android.libraries.matrix.test.FakeMatrixClient +import io.element.android.libraries.matrix.test.room.FakeMatrixRoom +import io.element.android.libraries.matrix.test.room.FakeRoomSummaryDataSource +import kotlinx.coroutines.test.runTest +import org.junit.Test + +class LoadingRoomStateFlowFactoryTest { + + @Test + fun `flow should emit Loading and then Loaded when there is a room in cache`() = runTest { + val room = FakeMatrixRoom(sessionId= A_SESSION_ID, roomId = A_ROOM_ID) + val matrixClient = FakeMatrixClient(A_SESSION_ID).apply { + givenGetRoomResult(A_ROOM_ID, room) + } + val flowFactory = LoadingRoomStateFlowFactory(matrixClient) + flowFactory + .create(this, A_ROOM_ID) + .test { + Truth.assertThat(awaitItem()).isEqualTo(LoadingRoomState.Loading) + Truth.assertThat(awaitItem()).isEqualTo(LoadingRoomState.Loaded(room)) + } + } + + @Test + fun `flow should emit Loading and then Loaded when there is a room in cache after SS is loaded`() = runTest { + val room = FakeMatrixRoom(sessionId= A_SESSION_ID, roomId = A_ROOM_ID) + val roomSummaryDataSource = FakeRoomSummaryDataSource() + val matrixClient = FakeMatrixClient(A_SESSION_ID, roomSummaryDataSource = roomSummaryDataSource) + val flowFactory = LoadingRoomStateFlowFactory(matrixClient) + flowFactory + .create(this, A_ROOM_ID) + .test { + Truth.assertThat(awaitItem()).isEqualTo(LoadingRoomState.Loading) + matrixClient.givenGetRoomResult(A_ROOM_ID, room) + roomSummaryDataSource.postLoadingState(RoomSummaryDataSource.LoadingState.Loaded(1)) + Truth.assertThat(awaitItem()).isEqualTo(LoadingRoomState.Loaded(room)) + } + } + + @Test + fun `flow should emit Loading and then Error when there is no room in cache after SS is loaded`() = runTest { + val roomSummaryDataSource = FakeRoomSummaryDataSource() + val matrixClient = FakeMatrixClient(A_SESSION_ID, roomSummaryDataSource = roomSummaryDataSource) + val flowFactory = LoadingRoomStateFlowFactory(matrixClient) + flowFactory + .create(this, A_ROOM_ID) + .test { + Truth.assertThat(awaitItem()).isEqualTo(LoadingRoomState.Loading) + roomSummaryDataSource.postLoadingState(RoomSummaryDataSource.LoadingState.Loaded(1)) + Truth.assertThat(awaitItem()).isEqualTo(LoadingRoomState.Error) + } + } + + + +}