Skip to content

Commit

Permalink
change all other unit tests to use junit5
Browse files Browse the repository at this point in the history
  • Loading branch information
saleniuk committed Jun 27, 2024
1 parent cb2ab9d commit f8815ae
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ class EditGuestAccessViewModel @Inject constructor(
conversationDetailsFlow,
isSelfAdminFlow
) { conversationDetails, isSelfAnAdmin ->
isSelfAnAdmin to conversationDetails
}.collect { (isSelfAnAdmin, conversationDetails) ->

val isGuestAllowed =
conversationDetails.conversation.isGuestAllowed() || conversationDetails.conversation.isNonTeamMemberAllowed()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package com.wire.android.mapper
import com.wire.android.ui.home.conversationslist.model.Membership
import com.wire.kalium.logic.data.user.type.UserType
import org.amshove.kluent.internal.assertEquals
import org.junit.Test
import org.junit.jupiter.api.Test

class UserTypeMapperTest {

Expand All @@ -46,9 +46,9 @@ class UserTypeMapperTest {
}

@Test
fun `given internal as a user type correctly map to none as membership`() {
fun `given internal as a user type correctly map to standard as membership`() {
val result = userTypeMapper.toMembership(UserType.INTERNAL)
assertEquals(Membership.None, result)
assertEquals(Membership.Standard, result)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import com.wire.android.config.CoroutineTestExtension
import com.wire.kalium.logic.data.id.QualifiedID
import com.wire.kalium.logic.data.user.UserId
import org.amshove.kluent.internal.assertEquals
import org.junit.Test
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(CoroutineTestExtension::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@ package com.wire.android.ui.home.conversations.details.editguestaccess

import androidx.lifecycle.SavedStateHandle
import com.wire.android.config.CoroutineTestExtension
import com.wire.android.config.NavigationTestExtension
import com.wire.android.config.TestDispatcherProvider
import com.wire.android.framework.TestConversation
import com.wire.android.framework.TestConversationDetails
import com.wire.android.ui.home.conversations.details.participants.model.ConversationParticipantsData
import com.wire.android.ui.home.conversations.details.participants.usecase.ObserveParticipantsForConversationUseCase
import com.wire.android.ui.navArgs
import com.wire.kalium.logic.CoreFailure
import com.wire.kalium.logic.NetworkFailure
import com.wire.kalium.logic.configuration.GuestRoomLinkStatus
import com.wire.kalium.logic.data.conversation.Conversation
import com.wire.kalium.logic.feature.conversation.ObserveConversationDetailsUseCase
import com.wire.kalium.logic.feature.conversation.UpdateConversationAccessRoleUseCase
import com.wire.kalium.logic.feature.conversation.guestroomlink.CanCreatePasswordProtectedLinksUseCase
Expand All @@ -37,22 +42,27 @@ import com.wire.kalium.logic.feature.conversation.guestroomlink.ObserveGuestRoom
import com.wire.kalium.logic.feature.conversation.guestroomlink.RevokeGuestRoomLinkResult
import com.wire.kalium.logic.feature.conversation.guestroomlink.RevokeGuestRoomLinkUseCase
import com.wire.kalium.logic.feature.user.guestroomlink.ObserveGuestRoomLinkFeatureFlagUseCase
import com.wire.kalium.logic.functional.Either
import io.mockk.MockKAnnotations
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.every
import io.mockk.impl.annotations.MockK
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
import org.amshove.kluent.internal.assertEquals
import org.junit.Before
import org.junit.Test
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

// TODO test not working, fix it
@OptIn(ExperimentalCoroutinesApi::class)
@ExtendWith(CoroutineTestExtension::class)
@ExtendWith(CoroutineTestExtension::class, NavigationTestExtension::class)
class EditGuestAccessViewModelTest {

val dispatcher = TestDispatcherProvider()

@MockK
private lateinit var savedStateHandle: SavedStateHandle

Expand All @@ -65,9 +75,6 @@ class EditGuestAccessViewModelTest {
@MockK
lateinit var observeConversationMembers: ObserveParticipantsForConversationUseCase

@MockK
lateinit var updateConversationAccessRole: UpdateConversationAccessRoleUseCase

@MockK
lateinit var generateGuestRoomLink: GenerateGuestRoomLinkUseCase

Expand All @@ -85,33 +92,50 @@ class EditGuestAccessViewModelTest {

private lateinit var editGuestAccessViewModel: EditGuestAccessViewModel

@Before
private val conversationDetailsChannel = Channel<ObserveConversationDetailsUseCase.Result>(capacity = Channel.UNLIMITED)

@BeforeEach
fun setUp() {
MockKAnnotations.init(this, relaxUnitFun = true)
coEvery { savedStateHandle.navArgs<EditGuestAccessNavArgs>() } returns EditGuestAccessNavArgs(
conversationId = TestConversation.ID,
editGuessAccessParams = EditGuestAccessParams(
isGuestAccessAllowed = true,
isServicesAllowed = true,
isUpdatingGuestAccessAllowed = true
)
)
coEvery {
observeConversationDetails(any())
} returns conversationDetailsChannel.consumeAsFlow()
coEvery {
observeConversationMembers(any())
} returns flowOf(ConversationParticipantsData())
coEvery {
observeGuestRoomLink(any())
} returns flowOf(Either.Right(null))
coEvery {
observeGuestRoomLinkFeatureFlag()
} returns flowOf(GuestRoomLinkStatus(null, null))

editGuestAccessViewModel = EditGuestAccessViewModel(
dispatcher = TestDispatcherProvider(),
observeConversationDetails = observeConversationDetails,
observeConversationMembers = observeConversationMembers,
updateConversationAccessRole = updateConversationAccessRole,
updateConversationAccessRole = updateConversationAccessRoleUseCase,
generateGuestRoomLink = generateGuestRoomLink,
revokeGuestRoomLink = revokeGuestRoomLink,
observeGuestRoomLink = observeGuestRoomLink,
savedStateHandle = savedStateHandle,
observeGuestRoomLinkFeatureFlag = observeGuestRoomLinkFeatureFlag,
canCreatePasswordProtectedLinks = canCreatePasswordProtectedLinks
)
every { savedStateHandle.navArgs<EditGuestAccessNavArgs>() } returns EditGuestAccessNavArgs(
conversationId = TestConversation.ID,
editGuessAccessParams = EditGuestAccessParams(
isGuestAccessAllowed = true,
isServicesAllowed = true,
isUpdatingGuestAccessAllowed = true
)
canCreatePasswordProtectedLinks = canCreatePasswordProtectedLinks,
dispatcher = dispatcher,
)
conversationDetailsChannel.trySend(ObserveConversationDetailsUseCase.Result.Success(TestConversationDetails.GROUP))
}

@Test
fun `given updateConversationAccessRole use case runs successfully, when trying to enable guest access, then enable guest access`() =
runTest {
runTest(dispatcher.default()) {
editGuestAccessViewModel.editGuestAccessState = editGuestAccessViewModel.editGuestAccessState.copy(isGuestAccessAllowed = false)
coEvery {
updateConversationAccessRoleUseCase(any(), any(), any())
Expand Down Expand Up @@ -163,7 +187,7 @@ class EditGuestAccessViewModelTest {
}

@Test
fun `given useCase runs with success, when_generating guest link, then invoke it once`() = runTest {
fun `given useCase runs with success, when_generating guest link, then invoke it once`() = runTest(dispatcher.default()) {
coEvery {
generateGuestRoomLink.invoke(any(), any())
} returns GenerateGuestRoomLinkResult.Success
Expand All @@ -176,7 +200,7 @@ class EditGuestAccessViewModelTest {
}

@Test
fun `given useCase runs with failure, when generating guest link, then show dialog error`() = runTest {
fun `given useCase runs with failure, when generating guest link, then show dialog error`() = runTest(dispatcher.default()) {
coEvery {
generateGuestRoomLink(any(), any())
} returns GenerateGuestRoomLinkResult.Failure(NetworkFailure.NoNetworkConnection(null))
Expand All @@ -190,7 +214,7 @@ class EditGuestAccessViewModelTest {
}

@Test
fun `given useCase runs with success, when revoking guest link, then invoke it once`() = runTest {
fun `given useCase runs with success, when revoking guest link, then invoke it once`() = runTest(dispatcher.default()) {
coEvery {
revokeGuestRoomLink(any())
} returns RevokeGuestRoomLinkResult.Success
Expand All @@ -204,7 +228,7 @@ class EditGuestAccessViewModelTest {
}

@Test
fun `given useCase runs with failure when revoking guest link then show dialog error`() = runTest {
fun `given useCase runs with failure when revoking guest link then show dialog error`() = runTest(dispatcher.default()) {
coEvery {
revokeGuestRoomLink(any())
} returns RevokeGuestRoomLinkResult.Failure(CoreFailure.MissingClientRegistration)
Expand All @@ -220,11 +244,19 @@ class EditGuestAccessViewModelTest {

@Test
fun `given updateConversationAccessRole use case runs successfully, when trying to disable guest access, then disable guest access`() =
runTest {
runTest(dispatcher.default()) {
editGuestAccessViewModel.editGuestAccessState = editGuestAccessViewModel.editGuestAccessState.copy(isGuestAccessAllowed = true)
coEvery {
updateConversationAccessRoleUseCase(any(), any(), any())
} returns UpdateConversationAccessRoleUseCase.Result.Success
} coAnswers {
val accessRoles = secondArg<Set<Conversation.AccessRole>>()
val newConversationDetails = TestConversationDetails.GROUP.copy(
conversation = TestConversationDetails.GROUP.conversation.copy(accessRole = accessRoles.toList())
)
// mock emitting updated conversation details with new access roles
conversationDetailsChannel.send(ObserveConversationDetailsUseCase.Result.Success(newConversationDetails))
UpdateConversationAccessRoleUseCase.Result.Success
}

editGuestAccessViewModel.onGuestDialogConfirm()

Expand Down

0 comments on commit f8815ae

Please sign in to comment.