Favorite : rework and add tests
This commit is contained in:
parent
a63d331f36
commit
7ca931c35f
21 changed files with 454 additions and 144 deletions
|
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* Copyright (c) 2024 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.features.roomactions.impl
|
||||
|
||||
import com.squareup.anvil.annotations.ContributesBinding
|
||||
import io.element.android.features.roomactions.api.SetRoomIsFavoriteAction
|
||||
import io.element.android.libraries.di.SessionScope
|
||||
import io.element.android.libraries.matrix.api.MatrixClient
|
||||
import io.element.android.libraries.matrix.api.core.RoomId
|
||||
import io.element.android.libraries.matrix.api.room.MatrixRoom
|
||||
import kotlinx.coroutines.flow.first
|
||||
import javax.inject.Inject
|
||||
import kotlin.coroutines.cancellation.CancellationException
|
||||
|
||||
@ContributesBinding(SessionScope::class)
|
||||
class DefaultSetRoomIsFavoriteAction @Inject constructor(private val client: MatrixClient) : SetRoomIsFavoriteAction {
|
||||
override suspend operator fun invoke(roomId: RoomId, isFavorite: Boolean): SetRoomIsFavoriteAction.Result {
|
||||
return client.getRoom(roomId).use { room ->
|
||||
room?.setIsFavorite(isFavorite) ?: SetRoomIsFavoriteAction.Result.RoomNotFound
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun invoke(room: MatrixRoom, isFavorite: Boolean): SetRoomIsFavoriteAction.Result {
|
||||
return room.setIsFavorite(isFavorite)
|
||||
}
|
||||
|
||||
private suspend fun MatrixRoom.setIsFavorite(isFavorite: Boolean): SetRoomIsFavoriteAction.Result {
|
||||
val notableTags = notableTagsFlow.first().copy(isFavorite = isFavorite)
|
||||
return updateNotableTags(notableTags).fold(
|
||||
onSuccess = {
|
||||
SetRoomIsFavoriteAction.Result.Success
|
||||
},
|
||||
onFailure = { throwable ->
|
||||
if (throwable is Exception && throwable !is CancellationException) {
|
||||
SetRoomIsFavoriteAction.Result.Exception(throwable)
|
||||
} else {
|
||||
throw throwable
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
* Copyright (c) 2024 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.features.roomactions.impl
|
||||
|
||||
import io.element.android.features.roomactions.api.SetRoomIsFavoriteAction
|
||||
import io.element.android.libraries.matrix.test.FakeMatrixClient
|
||||
import io.element.android.libraries.matrix.test.room.FakeMatrixRoom
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.Test
|
||||
|
||||
class DefaultSetRoomIsFavoriteActionTests {
|
||||
private val room = FakeMatrixRoom()
|
||||
|
||||
@Test
|
||||
fun `given a room id and a client without rooms, when action is invoked, then it returns Result_RoomNotFound`() = runTest {
|
||||
val action = DefaultSetRoomIsFavoriteAction(FakeMatrixClient())
|
||||
val result = action(room.roomId, true)
|
||||
assert(result is SetRoomIsFavoriteAction.Result.RoomNotFound)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `given a room, when action is invoked, then it returns Result_Success`() = runTest {
|
||||
val action = DefaultSetRoomIsFavoriteAction(FakeMatrixClient())
|
||||
val result = action(room, true)
|
||||
assert(result is SetRoomIsFavoriteAction.Result.Success)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `given a room id and a client with a room, when action is invoked, then it returns Result_Success`() = runTest {
|
||||
val client = FakeMatrixClient().apply {
|
||||
givenGetRoomResult(room.roomId, room)
|
||||
}
|
||||
val action = DefaultSetRoomIsFavoriteAction(client)
|
||||
val result = action(room.roomId, true)
|
||||
assert(result is SetRoomIsFavoriteAction.Result.Success)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `given a room, when action is invoked and fail, then it returns Result_Exception`() = runTest {
|
||||
val action = DefaultSetRoomIsFavoriteAction(FakeMatrixClient())
|
||||
room.givenUpdateNotableTagsResult(Result.failure(Exception()))
|
||||
val result = action(room, true)
|
||||
assert(result is SetRoomIsFavoriteAction.Result.Exception)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue