From d258e2e2e60854dbc4e74c33f7fe30b374695261 Mon Sep 17 00:00:00 2001 From: PavloNetrebchuk Date: Tue, 29 Oct 2024 16:09:08 +0200 Subject: [PATCH] feat: LearnViewModelTest --- ...lTest.kt => DashboardListViewModelTest.kt} | 2 +- .../presentation/LearnViewModelTest.kt | 80 +++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) rename dashboard/src/test/java/org/openedx/dashboard/presentation/{DashboardViewModelTest.kt => DashboardListViewModelTest.kt} (99%) create mode 100644 dashboard/src/test/java/org/openedx/dashboard/presentation/LearnViewModelTest.kt diff --git a/dashboard/src/test/java/org/openedx/dashboard/presentation/DashboardViewModelTest.kt b/dashboard/src/test/java/org/openedx/dashboard/presentation/DashboardListViewModelTest.kt similarity index 99% rename from dashboard/src/test/java/org/openedx/dashboard/presentation/DashboardViewModelTest.kt rename to dashboard/src/test/java/org/openedx/dashboard/presentation/DashboardListViewModelTest.kt index 2a1131392..216d8ecbf 100644 --- a/dashboard/src/test/java/org/openedx/dashboard/presentation/DashboardViewModelTest.kt +++ b/dashboard/src/test/java/org/openedx/dashboard/presentation/DashboardListViewModelTest.kt @@ -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() diff --git a/dashboard/src/test/java/org/openedx/dashboard/presentation/LearnViewModelTest.kt b/dashboard/src/test/java/org/openedx/dashboard/presentation/LearnViewModelTest.kt new file mode 100644 index 000000000..33c2edea4 --- /dev/null +++ b/dashboard/src/test/java/org/openedx/dashboard/presentation/LearnViewModelTest.kt @@ -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(relaxed = true) + private val dashboardRouter = mockk(relaxed = true) + private val analytics = mockk(relaxed = true) + private val fragmentManager = mockk(relaxed = true) + + 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 + } + ) + } + } +}