From e0be6ef09ff4f0909ed1a9573424a09a9b021ffd Mon Sep 17 00:00:00 2001 From: Maxime NATUREL <46314705+mnaturel@users.noreply.github.com> Date: Fri, 13 Jan 2023 10:33:55 +0100 Subject: [PATCH] Adding unit test for repository --- .../polls/list/data/RoomPollRepository.kt | 1 - .../polls/list/data/RoomPollRepositoryTest.kt | 95 +++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 vector/src/test/java/im/vector/app/features/roomprofile/polls/list/data/RoomPollRepositoryTest.kt diff --git a/vector/src/main/java/im/vector/app/features/roomprofile/polls/list/data/RoomPollRepository.kt b/vector/src/main/java/im/vector/app/features/roomprofile/polls/list/data/RoomPollRepository.kt index 7237452a06..d3577df6c1 100644 --- a/vector/src/main/java/im/vector/app/features/roomprofile/polls/list/data/RoomPollRepository.kt +++ b/vector/src/main/java/im/vector/app/features/roomprofile/polls/list/data/RoomPollRepository.kt @@ -20,7 +20,6 @@ import im.vector.app.features.roomprofile.polls.list.ui.PollSummary import kotlinx.coroutines.flow.Flow import javax.inject.Inject -// TODO add unit tests class RoomPollRepository @Inject constructor( private val roomPollDataSource: RoomPollDataSource, ) { diff --git a/vector/src/test/java/im/vector/app/features/roomprofile/polls/list/data/RoomPollRepositoryTest.kt b/vector/src/test/java/im/vector/app/features/roomprofile/polls/list/data/RoomPollRepositoryTest.kt new file mode 100644 index 0000000000..49d9623c04 --- /dev/null +++ b/vector/src/test/java/im/vector/app/features/roomprofile/polls/list/data/RoomPollRepositoryTest.kt @@ -0,0 +1,95 @@ +/* + * 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 im.vector.app.features.roomprofile.polls.list.data + +import im.vector.app.features.roomprofile.polls.list.ui.PollSummary +import io.mockk.coJustRun +import io.mockk.coVerify +import io.mockk.every +import io.mockk.mockk +import io.mockk.verify +import kotlinx.coroutines.flow.firstOrNull +import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.test.runTest +import org.amshove.kluent.shouldBeEqualTo +import org.junit.Test + +private const val A_ROOM_ID = "room-id" + +class RoomPollRepositoryTest { + + private val fakeRoomPollDataSource = mockk() + + private val roomPollRepository = RoomPollRepository( + roomPollDataSource = fakeRoomPollDataSource, + ) + + @Test + fun `given data source when getting polls then correct method of data source is called`() = runTest { + // Given + val expectedPolls = listOf() + every { fakeRoomPollDataSource.getPolls(A_ROOM_ID) } returns flowOf(expectedPolls) + + // When + val result = roomPollRepository.getPolls(A_ROOM_ID).firstOrNull() + + // Then + result shouldBeEqualTo expectedPolls + verify { fakeRoomPollDataSource.getPolls(A_ROOM_ID) } + } + + @Test + fun `given data source when getting loaded polls status then correct method of data source is called`() { + // Given + val expectedStatus = LoadedPollsStatus( + canLoadMore = true, + nbLoadedDays = 10, + ) + every { fakeRoomPollDataSource.getLoadedPollsStatus(A_ROOM_ID) } returns expectedStatus + + // When + val result = roomPollRepository.getLoadedPollsStatus(A_ROOM_ID) + + // Then + result shouldBeEqualTo expectedStatus + verify { fakeRoomPollDataSource.getLoadedPollsStatus(A_ROOM_ID) } + } + + @Test + fun `given data source when loading more polls then correct method of data source is called`() = runTest { + // Given + coJustRun { fakeRoomPollDataSource.loadMorePolls(A_ROOM_ID) } + + // When + roomPollRepository.loadMorePolls(A_ROOM_ID) + + // Then + coVerify { fakeRoomPollDataSource.loadMorePolls(A_ROOM_ID) } + } + + @Test + fun `given data source when syncing polls then correct method of data source is called`() = runTest { + // Given + coJustRun { fakeRoomPollDataSource.syncPolls(A_ROOM_ID) } + + // When + roomPollRepository.syncPolls(A_ROOM_ID) + + // Then + coVerify { fakeRoomPollDataSource.syncPolls(A_ROOM_ID) } + } +}