Skip to content

Commit

Permalink
tested MainActivityViewModel
Browse files Browse the repository at this point in the history
Signed-off-by: Basler182 <[email protected]>
  • Loading branch information
Basler182 authored and eldcn committed Jul 1, 2024
1 parent c1fe605 commit e56c0a5
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class MainActivityViewModel @Inject constructor(
if (!it.isAnonymous) {
// If the user is not anonymous, we can check if the PDF has been uploaded
viewModelScope.launch {
if (pdfService.isPdfUploaded().isSuccess) {
if (pdfService.isPdfUploaded().getOrDefault(false)) {
navigator.navigateTo(AppNavigationEvent.BluetoothScreen)
} else {
// User has to consent to the study before proceeding and upload the PDF
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package edu.stanford.bdh.engagehf

import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import edu.stanford.bdh.engagehf.navigation.AppNavigationEvent
import edu.stanford.spezi.core.navigation.Navigator
import edu.stanford.spezi.core.testing.runTestUnconfined
import edu.stanford.spezi.module.account.AccountEvents
import edu.stanford.spezi.module.onboarding.OnboardingNavigationEvent
import edu.stanford.spezi.module.onboarding.consent.PdfService
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.every
import io.mockk.mockk
import io.mockk.slot
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.setMain
import org.junit.Before
import org.junit.Test

@ExperimentalCoroutinesApi
class MainActivityViewModelTest {

private lateinit var viewModel: MainActivityViewModel
private val accountEvents: AccountEvents = mockk(relaxed = true)
private val navigator: Navigator = mockk(relaxed = true)
private val firebaseAuth: FirebaseAuth = mockk(relaxed = true)
private val pdfService: PdfService = mockk(relaxed = true)
private val firebaseUser: FirebaseUser = mockk(relaxed = true)

@Before
fun setUp() {
Dispatchers.setMain(UnconfinedTestDispatcher())
every { accountEvents.events } returns MutableSharedFlow()
viewModel = MainActivityViewModel(accountEvents, navigator, firebaseAuth, pdfService)
}

@Test
fun `when user is logged in and pdf is uploaded, should navigate to BluetoothScreen`() =
runTestUnconfined {
// given
coEvery { firebaseAuth.currentUser } returns firebaseUser
coEvery { firebaseUser.isAnonymous } returns false
coEvery { pdfService.isPdfUploaded() } returns Result.success(true)

// when
getAuthStateListener().onAuthStateChanged(firebaseAuth)

// then
coVerify { navigator.navigateTo(AppNavigationEvent.BluetoothScreen) }
}

@Test
fun `when user is logged in and pdf is not uploaded, should navigate to ConsentScreen`() =
runTestUnconfined {
// given
coEvery { firebaseAuth.currentUser } returns firebaseUser
coEvery { firebaseUser.isAnonymous } returns false
coEvery { pdfService.isPdfUploaded() } returns Result.success(false)

// when
getAuthStateListener().onAuthStateChanged(firebaseAuth)

// then
coVerify { navigator.navigateTo(OnboardingNavigationEvent.ConsentScreen) }
}

@Test
fun `when user is logged in and pdf is uploaded returns failure, should navigate to ConsentScreen`() =
runTestUnconfined {
// given
coEvery { firebaseAuth.currentUser } returns firebaseUser
coEvery { firebaseUser.isAnonymous } returns false
coEvery { pdfService.isPdfUploaded() } returns Result.failure(Exception())

// when
getAuthStateListener().onAuthStateChanged(firebaseAuth)

// then
coVerify { navigator.navigateTo(OnboardingNavigationEvent.ConsentScreen) }
}

@Test
fun `when no user is logged in, should not navigate`() = runTestUnconfined {
// given
coEvery { firebaseAuth.currentUser } returns null

// when
getAuthStateListener().onAuthStateChanged(firebaseAuth)

// then
coVerify(exactly = 0) { navigator.navigateTo(any()) }
}

private fun getAuthStateListener(): FirebaseAuth.AuthStateListener {
val slot = slot<FirebaseAuth.AuthStateListener>()
every { firebaseAuth.addAuthStateListener(capture(slot)) } answers { }
viewModel = MainActivityViewModel(accountEvents, navigator, firebaseAuth, pdfService)
return slot.captured
}
}

0 comments on commit e56c0a5

Please sign in to comment.