Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Garzas committed Oct 2, 2023
1 parent 608b3ac commit 41f23e2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ data class ConversationListState(
val unreadMentionsCount: Long = 0,
val hasEstablishedCall: Boolean = false,
val shouldShowJoinAnywayDialog: Boolean = false,
val shouldShowCallingPermissionDialog: Boolean = false,
val isFromArchive: Boolean = false
val shouldShowCallingPermissionDialog: Boolean = false
) {
fun findConversationById(conversationId: ConversationId): ConversationItem? =
foldersWithConversations.values.flatten().firstOrNull { it.conversationId == conversationId }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

package com.wire.android.ui.home.conversationslist

import androidx.annotation.VisibleForTesting
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
Expand Down Expand Up @@ -186,14 +185,12 @@ class ConversationListViewModel @Inject constructor(
},
hasNoConversations = conversationsWithFolders.isEmpty(),
foldersWithConversations = conversationsWithFolders,
// TODO: missing other lists and counters (for bottom tabs if we decide to bring them back)
searchQuery = searchQuery.text,
isFromArchive = searchQuery.fromArchive
searchQuery = searchQuery.text
)
}
.flowOn(dispatcher.io())
.collect {
conversationListState = it
conversationListState = it
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import com.wire.android.ui.home.conversationslist.model.ConversationFolder
import com.wire.android.ui.home.conversationslist.model.ConversationInfo
import com.wire.android.ui.home.conversationslist.model.ConversationItem
import com.wire.android.ui.home.conversationslist.model.Membership
import com.wire.android.ui.home.conversationslist.model.SearchQuery
import com.wire.android.util.orDefault
import com.wire.android.util.ui.WireSessionImageLoader
import com.wire.kalium.logic.data.conversation.MutedConversationStatus
import com.wire.kalium.logic.data.id.ConversationId
Expand Down Expand Up @@ -66,16 +66,13 @@ import io.mockk.impl.annotations.MockK
import io.mockk.verify
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.android.awaitFrame
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import org.amshove.kluent.internal.assertEquals
import org.amshove.kluent.shouldBeEqualTo
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

Expand Down Expand Up @@ -133,12 +130,12 @@ class ConversationListViewModelTest {

private val dispatcher = StandardTestDispatcher()

init {
init {
MockKAnnotations.init(this, relaxUnitFun = true)
Dispatchers.setMain(dispatcher)

coEvery { observeEstablishedCalls.invoke() } returns emptyFlow()
coEvery { observeConversationListDetailsUseCase.invoke(any()) } returns flowOf(
coEvery { observeConversationListDetailsUseCase.invoke(false) } returns flowOf(
listOf(
TestConversationDetails.CONNECTION,
TestConversationDetails.CONVERSATION_ONE_ONE,
Expand Down Expand Up @@ -174,6 +171,7 @@ class ConversationListViewModelTest {
val searchQueryText = ""

// When
dispatcher.scheduler.advanceUntilIdle()
conversationListViewModel.searchConversation(TextFieldValue(searchQueryText))
dispatcher.scheduler.advanceUntilIdle()

Expand All @@ -182,26 +180,49 @@ class ConversationListViewModelTest {
3,
conversationListViewModel.conversationListState.conversationSearchResult[ConversationFolder.Predefined.Conversations]?.size,
)
assertEquals( searchQueryText, conversationListViewModel.conversationListState.searchQuery)
assertEquals(searchQueryText, conversationListViewModel.conversationListState.searchQuery)
}

@Test
fun `given non-empty search query, when collecting, then update state with filtered conversations`() = runTest {
// Given
val searchQueryText = "testQuery"
val searchQueryText = TestConversationDetails.CONVERSATION_ONE_ONE.conversation.name.orDefault("test")

// When
dispatcher.scheduler.advanceUntilIdle()
conversationListViewModel.searchConversation(TextFieldValue(searchQueryText))
dispatcher.scheduler.advanceUntilIdle()

// Then
assertEquals(
3,
1,
conversationListViewModel.conversationListState.conversationSearchResult[ConversationFolder.Predefined.Conversations]?.size,
)
assertEquals(searchQueryText, conversationListViewModel.conversationListState.searchQuery)
}

@Test
fun `given empty search query, when collecting archived conversations, then update state with only archived conversations`() = runTest {
// Given
coEvery { observeConversationListDetailsUseCase.invoke(true) } returns flowOf(
listOf(
TestConversationDetails.CONVERSATION_ONE_ONE,
TestConversationDetails.GROUP
)
)
// When
dispatcher.scheduler.advanceUntilIdle()
conversationListViewModel.updateConversationsSource(fromArchived = true)
dispatcher.scheduler.advanceUntilIdle()

// Then
assertEquals(
2,
conversationListViewModel.conversationListState.conversationSearchResult[ConversationFolder.WithoutHeader]?.size,
)
coVerify(exactly = 1) { observeConversationListDetailsUseCase.invoke(true) }
}

@Test
fun `given a valid conversation muting state, when calling muteConversation, then should call with call the UseCase`() = runTest {
coEvery { updateConversationMutedStatus(any(), any(), any()) } returns ConversationUpdateStatusResult.Success
Expand Down

0 comments on commit 41f23e2

Please sign in to comment.