Skip to content

Commit

Permalink
feat: LearnViewModelTest
Browse files Browse the repository at this point in the history
  • Loading branch information
PavloNetrebchuk committed Oct 30, 2024
1 parent 6071d20 commit 41d65f2
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import org.openedx.dashboard.domain.interactor.DashboardInteractor
import java.net.UnknownHostException

@OptIn(ExperimentalCoroutinesApi::class)
class DashboardViewModelTest {
class DashboardListViewModelTest {

@get:Rule
val testInstantTaskExecutorRule: TestRule = InstantTaskExecutorRule()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.openedx.dashboard.presentation

import androidx.fragment.app.FragmentManager
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import junit.framework.TestCase.assertEquals
import junit.framework.TestCase.assertTrue
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.openedx.DashboardNavigator
import org.openedx.core.config.Config
import org.openedx.core.config.DashboardConfig
import org.openedx.learn.presentation.LearnViewModel

class LearnViewModelTest {

private val config = mockk<Config>()
private val dashboardRouter = mockk<DashboardRouter>(relaxed = true)
private val analytics = mockk<DashboardAnalytics>(relaxed = true)
private val fragmentManager = mockk<FragmentManager>()

private val viewModel = LearnViewModel(config, dashboardRouter, analytics)

@Test
fun `onSettingsClick calls navigateToSettings`() = runTest {
viewModel.onSettingsClick(fragmentManager)
verify { dashboardRouter.navigateToSettings(fragmentManager) }
}

@Test
fun `getDashboardFragment returns correct fragment based on dashboardType`() = runTest {
DashboardConfig.DashboardType.entries.forEach { type ->
every { config.getDashboardConfig().getType() } returns type
val dashboardFragment = viewModel.getDashboardFragment
assertEquals(DashboardNavigator(type).getDashboardFragment()::class, dashboardFragment::class)
}
}


@Test
fun `getProgramFragment returns correct program fragment`() = runTest {
viewModel.getProgramFragment
verify { dashboardRouter.getProgramFragment() }
}

@Test
fun `isProgramTypeWebView returns correct view type`() = runTest {
every { config.getProgramConfig().isViewTypeWebView() } returns true
assertTrue(viewModel.isProgramTypeWebView)
}

@Test
fun `logMyCoursesTabClickedEvent logs correct analytics event`() = runTest {
viewModel.logMyCoursesTabClickedEvent()

verify {
analytics.logScreenEvent(
screenName = DashboardAnalyticsEvent.MY_COURSES.eventName,
params = match {
it[DashboardAnalyticsKey.NAME.key] == DashboardAnalyticsEvent.MY_COURSES.biValue
}
)
}
}

@Test
fun `logMyProgramsTabClickedEvent logs correct analytics event`() = runTest {
viewModel.logMyProgramsTabClickedEvent()

verify {
analytics.logScreenEvent(
screenName = DashboardAnalyticsEvent.MY_PROGRAMS.eventName,
params = match {
it[DashboardAnalyticsKey.NAME.key] == DashboardAnalyticsEvent.MY_PROGRAMS.biValue
}
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ class CalendarViewModelTest {
private val calendarNotifier = mockk<CalendarNotifier>(relaxed = true)
private val calendarInteractor = mockk<CalendarInteractor>(relaxed = true)
private val corePreferences = mockk<CorePreferences>(relaxed = true)
private val profileRouter = mockk<ProfileRouter>(relaxed = true)
private val networkConnection = mockk<NetworkConnection>(relaxed = true)
private val permissionLauncher = mockk<ActivityResultLauncher<Array<String>>>(relaxed = true)
private val fragmentManager = mockk<FragmentManager>(relaxed = true)
private val profileRouter = mockk<ProfileRouter>()
private val networkConnection = mockk<NetworkConnection>()
private val permissionLauncher = mockk<ActivityResultLauncher<Array<String>>>()
private val fragmentManager = mockk<FragmentManager>()

@Before
fun setup() {
Expand Down Expand Up @@ -76,6 +76,7 @@ class CalendarViewModelTest {

@Test
fun `setUpCalendarSync launches permission request`() = runTest(dispatcher) {
every { permissionLauncher.launch(calendarManager.permissions) } returns Unit
viewModel.setUpCalendarSync(permissionLauncher)
coVerify { permissionLauncher.launch(calendarManager.permissions) }
}
Expand Down

0 comments on commit 41d65f2

Please sign in to comment.