From e1e1eec58ae320938456c949fb8ac4bfea47bd9a Mon Sep 17 00:00:00 2001 From: Serhii <128455389+hryh27@users.noreply.github.com> Date: Wed, 12 Apr 2023 17:06:25 +0300 Subject: [PATCH] fix tests (#18) --- .../container/CourseContainerViewModelTest.kt | 32 +++- .../detail/CourseDetailsViewModelTest.kt | 13 -- .../handouts/HandoutsViewModelTest.kt | 87 ++++------ .../outline/CourseOutlineViewModelTest.kt | 157 +++++++++++------- .../section/CourseSectionViewModelTest.kt | 35 +++- .../CourseUnitContainerViewModelTest.kt | 99 +++++++---- .../unit/video/VideoUnitViewModelTest.kt | 31 +++- .../units/CourseUnitsViewModelTest.kt | 21 ++- .../videos/CourseVideoViewModelTest.kt | 59 +++++-- .../presentation/DashboardViewModelTest.kt | 83 +++++++-- .../search/CourseSearchViewModelTest.kt | 3 +- .../DiscussionCommentsViewModelTest.kt | 71 +++++++- .../DiscussionResponsesViewModelTest.kt | 9 +- .../DiscussionSearchThreadViewModelTest.kt | 5 +- .../DiscussionAddThreadViewModelTest.kt | 7 +- .../threads/DiscussionThreadsViewModelTest.kt | 110 ++++++++---- .../edit/EditProfileViewModelTest.kt | 44 +++-- .../profile/ProfileViewModelTest.kt | 61 ++++++- 18 files changed, 647 insertions(+), 280 deletions(-) diff --git a/course/src/test/java/com/raccoongang/course/presentation/container/CourseContainerViewModelTest.kt b/course/src/test/java/com/raccoongang/course/presentation/container/CourseContainerViewModelTest.kt index d4de361dc..0265d4750 100644 --- a/course/src/test/java/com/raccoongang/course/presentation/container/CourseContainerViewModelTest.kt +++ b/course/src/test/java/com/raccoongang/course/presentation/container/CourseContainerViewModelTest.kt @@ -3,6 +3,8 @@ package com.raccoongang.course.presentation.container import androidx.arch.core.executor.testing.InstantTaskExecutorRule import com.raccoongang.core.system.connection.NetworkConnection import com.raccoongang.core.R +import com.raccoongang.core.domain.model.CourseStructure +import com.raccoongang.core.domain.model.CoursewareAccess import com.raccoongang.core.system.ResourceManager import com.raccoongang.core.system.notifier.CourseNotifier import com.raccoongang.core.system.notifier.CourseStructureUpdated @@ -35,6 +37,30 @@ class CourseContainerViewModelTest { private val noInternet = "Slow or no internet connection" private val somethingWrong = "Something went wrong" + private val courseStructure = CourseStructure( + root = "", + blockData = listOf(), + id = "id", + name = "Course name", + number = "", + org = "Org", + start = null, + startDisplay = "", + startType = "", + end = null, + coursewareAccess = CoursewareAccess( + true, + "", + "", + "", + "", + "" + ), + media = null, + certificate = null, + isSelfPaced = false + ) + @Before fun setUp() { Dispatchers.setMain(dispatcher) @@ -84,6 +110,7 @@ class CourseContainerViewModelTest { val viewModel = CourseContainerViewModel("", interactor,resourceManager, notifier, networkConnection) every { networkConnection.isOnline() } returns true coEvery { interactor.preloadCourseStructure(any()) } returns Unit + every { interactor.getCourseStructureFromCache() } returns courseStructure viewModel.preloadCourseStructure() advanceUntilIdle() @@ -91,7 +118,7 @@ class CourseContainerViewModelTest { assert(viewModel.errorMessage.value == null) assert(viewModel.showProgress.value == false) - assert(viewModel.dataReady.value == true) + assert(viewModel.dataReady.value != null) } @Test @@ -99,6 +126,7 @@ class CourseContainerViewModelTest { val viewModel = CourseContainerViewModel("", interactor,resourceManager, notifier, networkConnection) every { networkConnection.isOnline() } returns false coEvery { interactor.preloadCourseStructureFromCache(any()) } returns Unit + every { interactor.getCourseStructureFromCache() } returns courseStructure viewModel.preloadCourseStructure() advanceUntilIdle() @@ -107,7 +135,7 @@ class CourseContainerViewModelTest { assert(viewModel.errorMessage.value == null) assert(viewModel.showProgress.value == false) - assert(viewModel.dataReady.value == true) + assert(viewModel.dataReady.value != null) } @Test diff --git a/course/src/test/java/com/raccoongang/course/presentation/detail/CourseDetailsViewModelTest.kt b/course/src/test/java/com/raccoongang/course/presentation/detail/CourseDetailsViewModelTest.kt index e82e75008..d69e22cbc 100644 --- a/course/src/test/java/com/raccoongang/course/presentation/detail/CourseDetailsViewModelTest.kt +++ b/course/src/test/java/com/raccoongang/course/presentation/detail/CourseDetailsViewModelTest.kt @@ -55,11 +55,9 @@ class CourseDetailsViewModelTest { CourseDetailsViewModel("", networkConnection, interactor, resourceManager, notifier) every { networkConnection.isOnline() } returns true coEvery { interactor.getCourseDetails(any()) } throws UnknownHostException() - coEvery { interactor.getEnrolledCourseById(any()) } throws UnknownHostException() advanceUntilIdle() coVerify(exactly = 1) { interactor.getCourseDetails(any()) } - coVerify(exactly = 1) { interactor.getEnrolledCourseById(any()) } val message = viewModel.uiMessage.value as? UIMessage.SnackBarMessage @@ -73,11 +71,9 @@ class CourseDetailsViewModelTest { CourseDetailsViewModel("", networkConnection, interactor, resourceManager, notifier) every { networkConnection.isOnline() } returns true coEvery { interactor.getCourseDetails(any()) } throws Exception() - coEvery { interactor.getEnrolledCourseById(any()) } throws Exception() advanceUntilIdle() coVerify(exactly = 1) { interactor.getCourseDetails(any()) } - coVerify(exactly = 1) { interactor.getEnrolledCourseById(any()) } val message = viewModel.uiMessage.value as? UIMessage.SnackBarMessage @@ -91,12 +87,10 @@ class CourseDetailsViewModelTest { CourseDetailsViewModel("", networkConnection, interactor, resourceManager, notifier) every { networkConnection.isOnline() } returns true coEvery { interactor.getCourseDetails(any()) } returns mockk() - coEvery { interactor.getEnrolledCourseById(any()) } returns mockk() advanceUntilIdle() coVerify(exactly = 1) { interactor.getCourseDetails(any()) } - coVerify(exactly = 1) { interactor.getEnrolledCourseById(any()) } assert(viewModel.uiMessage.value == null) assert(viewModel.uiState.value is CourseDetailsUIState.CourseData) @@ -108,14 +102,11 @@ class CourseDetailsViewModelTest { CourseDetailsViewModel("", networkConnection, interactor, resourceManager, notifier) every { networkConnection.isOnline() } returns false coEvery { interactor.getCourseDetailsFromCache(any()) } returns mockk() - coEvery { interactor.getEnrolledCourseFromCacheById(any()) } returns mockk() advanceUntilIdle() coVerify(exactly = 0) { interactor.getCourseDetails(any()) } - coVerify(exactly = 0) { interactor.getEnrolledCourseById(any()) } coVerify(exactly = 1) { interactor.getCourseDetailsFromCache(any()) } - coVerify(exactly = 1) { interactor.getEnrolledCourseFromCacheById(any()) } assert(viewModel.uiMessage.value == null) assert(viewModel.uiState.value is CourseDetailsUIState.CourseData) @@ -126,7 +117,6 @@ class CourseDetailsViewModelTest { val viewModel = CourseDetailsViewModel("", networkConnection, interactor, resourceManager, notifier) coEvery { interactor.enrollInACourse(any()) } throws UnknownHostException() - coEvery { interactor.getEnrolledCourseById(any()) } returns mockk() coEvery { notifier.send(CourseDashboardUpdate()) } returns Unit every { networkConnection.isOnline() } returns true coEvery { interactor.getCourseDetails(any()) } returns mockk() @@ -147,7 +137,6 @@ class CourseDetailsViewModelTest { val viewModel = CourseDetailsViewModel("", networkConnection, interactor, resourceManager, notifier) coEvery { interactor.enrollInACourse(any()) } throws Exception() - coEvery { interactor.getEnrolledCourseById(any()) } returns mockk() coEvery { notifier.send(CourseDashboardUpdate()) } returns Unit every { networkConnection.isOnline() } returns true coEvery { interactor.getCourseDetails(any()) } returns mockk() @@ -168,7 +157,6 @@ class CourseDetailsViewModelTest { val viewModel = CourseDetailsViewModel("", networkConnection, interactor, resourceManager, notifier) coEvery { interactor.enrollInACourse(any()) } returns Unit - coEvery { interactor.getEnrolledCourseById(any()) } returns mockk() coEvery { notifier.send(CourseDashboardUpdate()) } returns Unit every { networkConnection.isOnline() } returns true coEvery { interactor.getCourseDetails(any()) } returns mockk() @@ -179,7 +167,6 @@ class CourseDetailsViewModelTest { advanceUntilIdle() coVerify(exactly = 1) { interactor.enrollInACourse(any()) } - coVerify(exactly = 2) { interactor.getEnrolledCourseById(any()) } assert(viewModel.uiMessage.value == null) assert(viewModel.uiState.value is CourseDetailsUIState.CourseData) diff --git a/course/src/test/java/com/raccoongang/course/presentation/handouts/HandoutsViewModelTest.kt b/course/src/test/java/com/raccoongang/course/presentation/handouts/HandoutsViewModelTest.kt index 088f0aa16..cbd662459 100644 --- a/course/src/test/java/com/raccoongang/course/presentation/handouts/HandoutsViewModelTest.kt +++ b/course/src/test/java/com/raccoongang/course/presentation/handouts/HandoutsViewModelTest.kt @@ -28,44 +28,10 @@ class HandoutsViewModelTest { private val interactor = mockk() - //region mockEnrolledCourse - - private val mockCourseEnrolled = EnrolledCourse( - auditAccessExpires = Date(), - created = "created", - certificate = Certificate(""), - mode = "mode", - isActive = true, - course = EnrolledCourseData( - id = "id", - name = "name", - number = "", - org = "Org", - start = Date(), - startDisplay = "", - startType = "", - end = null, - dynamicUpgradeDeadline = "", - subscriptionId = "", - coursewareAccess = CoursewareAccess( - true, - "", - "", - "", - "", - "" - ), - media = null, - courseImage = "", - courseAbout = "", - courseSharingUtmParameters = CourseSharingUtmParameters("", ""), - courseUpdates = "", - courseHandouts = "", - discussionUrl = "", - videoOutline = "", - isSelfPaced = false - ) - ) + //region mockHandoutsModel + + private val handoutsModel = HandoutsModel("") + //endregion @Before @@ -80,36 +46,29 @@ class HandoutsViewModelTest { @Test fun `getEnrolledCourse no internet connection exception`() = runTest { - val viewModel = HandoutsViewModel("","Handouts", interactor) - coEvery { interactor.getEnrolledCourseFromCacheById(any()) } throws UnknownHostException() - coEvery { interactor.getAnnouncements(any()) } returns mockk() + val viewModel = HandoutsViewModel("", "Handouts", interactor) + coEvery { interactor.getHandouts(any()) } throws UnknownHostException() advanceUntilIdle() - coVerify(exactly = 1) { interactor.getEnrolledCourseFromCacheById(any()) } assert(viewModel.htmlContent.value == null) } @Test fun `getEnrolledCourse unknown exception`() = runTest { - val viewModel = HandoutsViewModel("","Handouts", interactor) - coEvery { interactor.getEnrolledCourseFromCacheById(any()) } throws Exception() - coEvery { interactor.getAnnouncements(any()) } returns mockk() - + val viewModel = HandoutsViewModel("", "Handouts", interactor) + coEvery { interactor.getHandouts(any()) } throws Exception() advanceUntilIdle() - coVerify(exactly = 1) { interactor.getEnrolledCourseFromCacheById(any()) } assert(viewModel.htmlContent.value == null) } @Test fun `getEnrolledCourse handouts success`() = runTest { - val viewModel = HandoutsViewModel("",HandoutsType.Handouts.name, interactor) - coEvery { interactor.getEnrolledCourseFromCacheById(any()) } returns mockCourseEnrolled + val viewModel = HandoutsViewModel("", HandoutsType.Handouts.name, interactor) coEvery { interactor.getHandouts(any()) } returns HandoutsModel("hello") advanceUntilIdle() - coVerify(exactly = 1) { interactor.getEnrolledCourseFromCacheById(any()) } coVerify(exactly = 1) { interactor.getHandouts(any()) } coVerify(exactly = 0) { interactor.getAnnouncements(any()) } @@ -118,12 +77,15 @@ class HandoutsViewModelTest { @Test fun `getEnrolledCourse announcements success`() = runTest { - val viewModel = HandoutsViewModel("",HandoutsType.Announcements.name, interactor) - coEvery { interactor.getEnrolledCourseFromCacheById(any()) } returns mockCourseEnrolled - coEvery { interactor.getAnnouncements(any()) } returns listOf(AnnouncementModel("date","content")) + val viewModel = HandoutsViewModel("", HandoutsType.Announcements.name, interactor) + coEvery { interactor.getAnnouncements(any()) } returns listOf( + AnnouncementModel( + "date", + "content" + ) + ) advanceUntilIdle() - coVerify(exactly = 1) { interactor.getEnrolledCourseFromCacheById(any()) } coVerify(exactly = 0) { interactor.getHandouts(any()) } coVerify(exactly = 1) { interactor.getAnnouncements(any()) } @@ -132,12 +94,19 @@ class HandoutsViewModelTest { @Test fun `injectDarkMode test`() = runTest { - val viewModel = HandoutsViewModel("",HandoutsType.Announcements.name, interactor) - coEvery { interactor.getEnrolledCourseFromCacheById(any()) } returns mockCourseEnrolled - coEvery { interactor.getAnnouncements(any()) } returns listOf(AnnouncementModel("date","content")) - viewModel.injectDarkMode(viewModel.htmlContent.value.toString(), ULong.MAX_VALUE, ULong.MAX_VALUE) + val viewModel = HandoutsViewModel("", HandoutsType.Announcements.name, interactor) + coEvery { interactor.getAnnouncements(any()) } returns listOf( + AnnouncementModel( + "date", + "content" + ) + ) + viewModel.injectDarkMode( + viewModel.htmlContent.value.toString(), + ULong.MAX_VALUE, + ULong.MAX_VALUE + ) advanceUntilIdle() - coVerify(exactly = 1) { interactor.getEnrolledCourseFromCacheById(any()) } coVerify(exactly = 0) { interactor.getHandouts(any()) } coVerify(exactly = 1) { interactor.getAnnouncements(any()) } diff --git a/course/src/test/java/com/raccoongang/course/presentation/outline/CourseOutlineViewModelTest.kt b/course/src/test/java/com/raccoongang/course/presentation/outline/CourseOutlineViewModelTest.kt index 23b058e08..59e1c7688 100644 --- a/course/src/test/java/com/raccoongang/course/presentation/outline/CourseOutlineViewModelTest.kt +++ b/course/src/test/java/com/raccoongang/course/presentation/outline/CourseOutlineViewModelTest.kt @@ -8,9 +8,7 @@ import com.raccoongang.core.BlockType import com.raccoongang.core.R import com.raccoongang.core.UIMessage import com.raccoongang.core.data.storage.PreferencesManager -import com.raccoongang.core.domain.model.Block -import com.raccoongang.core.domain.model.BlockCounts -import com.raccoongang.core.domain.model.CourseComponentStatus +import com.raccoongang.core.domain.model.* import com.raccoongang.core.module.DownloadWorkerController import com.raccoongang.core.module.db.* import com.raccoongang.core.system.ResourceManager @@ -31,6 +29,7 @@ import org.junit.Rule import org.junit.Test import org.junit.rules.TestRule import java.net.UnknownHostException +import java.util.* @OptIn(ExperimentalCoroutinesApi::class) class CourseOutlineViewModelTest { @@ -100,6 +99,30 @@ class CourseOutlineViewModelTest { ) ) + val courseStructure = CourseStructure( + root = "", + blockData = blocks, + id = "id", + name = "Course name", + number = "", + org = "Org", + start = Date(), + startDisplay = "", + startType = "", + end = Date(), + coursewareAccess = CoursewareAccess( + true, + "", + "", + "", + "", + "" + ), + media = null, + certificate = null, + isSelfPaced = false + ) + private val downloadModel = DownloadModel( "id", "title", @@ -126,6 +149,10 @@ class CourseOutlineViewModelTest { @Test fun `getCourseDataInternal no internet connection exception`() = runTest { + every { interactor.getCourseStructureFromCache() } returns courseStructure + every { networkConnection.isOnline() } returns true + coEvery { interactor.getCourseStatus(any()) } throws UnknownHostException() + val viewModel = CourseOutlineViewModel( "", interactor, @@ -136,11 +163,7 @@ class CourseOutlineViewModelTest { downloadDao, workerController ) - every { interactor.getCourseStructureFromCache() } returns emptyList() - every { networkConnection.isOnline() } returns true - coEvery { interactor.getCourseStatus(any()) } throws UnknownHostException() - viewModel.getCourseData() advanceUntilIdle() verify(exactly = 1) { interactor.getCourseStructureFromCache() } @@ -154,6 +177,9 @@ class CourseOutlineViewModelTest { @Test fun `getCourseDataInternal unknown exception`() = runTest { + every { interactor.getCourseStructureFromCache() } returns courseStructure + every { networkConnection.isOnline() } returns true + coEvery { interactor.getCourseStatus(any()) } throws Exception() val viewModel = CourseOutlineViewModel( "", interactor, @@ -164,11 +190,7 @@ class CourseOutlineViewModelTest { downloadDao, workerController ) - every { interactor.getCourseStructureFromCache() } returns emptyList() - every { networkConnection.isOnline() } returns true - coEvery { interactor.getCourseStatus(any()) } throws Exception() - viewModel.getCourseData() advanceUntilIdle() verify(exactly = 1) { interactor.getCourseStructureFromCache() } @@ -182,17 +204,7 @@ class CourseOutlineViewModelTest { @Test fun `getCourseDataInternal success with internet connection`() = runTest { - val viewModel = CourseOutlineViewModel( - "", - interactor, - resourceManager, - notifier, - networkConnection, - preferencesManager, - downloadDao, - workerController - ) - every { interactor.getCourseStructureFromCache() } returns blocks + every { interactor.getCourseStructureFromCache() } returns courseStructure every { networkConnection.isOnline() } returns true coEvery { downloadDao.readAllData() } returns flow { emit( @@ -205,7 +217,17 @@ class CourseOutlineViewModelTest { } coEvery { interactor.getCourseStatus(any()) } returns CourseComponentStatus("id") - viewModel.getCourseData() + val viewModel = CourseOutlineViewModel( + "", + interactor, + resourceManager, + notifier, + networkConnection, + preferencesManager, + downloadDao, + workerController + ) + advanceUntilIdle() verify(exactly = 1) { interactor.getCourseStructureFromCache() } @@ -218,17 +240,7 @@ class CourseOutlineViewModelTest { @Test fun `getCourseDataInternal success without internet connection`() = runTest { - val viewModel = CourseOutlineViewModel( - "", - interactor, - resourceManager, - notifier, - networkConnection, - preferencesManager, - downloadDao, - workerController - ) - every { interactor.getCourseStructureFromCache() } returns blocks + every { interactor.getCourseStructureFromCache() } returns courseStructure every { networkConnection.isOnline() } returns false coEvery { downloadDao.readAllData() } returns flow { emit( @@ -241,7 +253,17 @@ class CourseOutlineViewModelTest { } coEvery { interactor.getCourseStatus(any()) } returns CourseComponentStatus("id") - viewModel.getCourseData() + val viewModel = CourseOutlineViewModel( + "", + interactor, + resourceManager, + notifier, + networkConnection, + preferencesManager, + downloadDao, + workerController + ) + advanceUntilIdle() verify(exactly = 1) { interactor.getCourseStructureFromCache() } @@ -254,17 +276,7 @@ class CourseOutlineViewModelTest { @Test fun `updateCourseData success with internet connection`() = runTest { - val viewModel = CourseOutlineViewModel( - "", - interactor, - resourceManager, - notifier, - networkConnection, - preferencesManager, - downloadDao, - workerController - ) - every { interactor.getCourseStructureFromCache() } returns blocks + every { interactor.getCourseStructureFromCache() } returns courseStructure every { networkConnection.isOnline() } returns true coEvery { downloadDao.readAllData() } returns flow { emit( @@ -277,11 +289,22 @@ class CourseOutlineViewModelTest { } coEvery { interactor.getCourseStatus(any()) } returns CourseComponentStatus("id") + val viewModel = CourseOutlineViewModel( + "", + interactor, + resourceManager, + notifier, + networkConnection, + preferencesManager, + downloadDao, + workerController + ) + viewModel.updateCourseData(false) advanceUntilIdle() - coVerify(exactly = 1) { interactor.getCourseStructureFromCache() } - coVerify(exactly = 1) { interactor.getCourseStatus(any()) } + coVerify(exactly = 2) { interactor.getCourseStructureFromCache() } + coVerify(exactly = 2) { interactor.getCourseStatus(any()) } assert(viewModel.uiMessage.value == null) assert(viewModel.isUpdating.value == false) @@ -301,7 +324,7 @@ class CourseOutlineViewModelTest { workerController ) coEvery { notifier.notifier } returns flow { emit(CourseStructureUpdated("", false)) } - every { interactor.getCourseStructureFromCache() } returns blocks + every { interactor.getCourseStructureFromCache() } returns courseStructure every { downloadDao.readAllData() } returns flow { repeat(5) { delay(10000) @@ -319,12 +342,20 @@ class CourseOutlineViewModelTest { viewModel.setIsUpdating() advanceUntilIdle() - coVerify(exactly = 1) { interactor.getCourseStructureFromCache() } + coVerify(exactly = 2) { interactor.getCourseStructureFromCache() } coVerify(exactly = 1) { interactor.getCourseStatus(any()) } } @Test fun `saveDownloadModels test`() = runTest { + every { preferencesManager.videoSettings.wifiDownloadOnly } returns false + every { interactor.getCourseStructureFromCache() } returns courseStructure + every { networkConnection.isWifiConnected() } returns true + every { networkConnection.isOnline() } returns true + coEvery { workerController.saveModels(*anyVararg()) } returns Unit + coEvery { interactor.getCourseStatus(any()) } returns CourseComponentStatus("id") + coEvery { downloadDao.readAllData() } returns flow { emit(emptyList()) } + val viewModel = CourseOutlineViewModel( "", interactor, @@ -335,9 +366,6 @@ class CourseOutlineViewModelTest { downloadDao, workerController ) - every { preferencesManager.videoSettings.wifiDownloadOnly } returns false - every { networkConnection.isWifiConnected() } returns true - coEvery { workerController.saveModels(*anyVararg()) } returns Unit viewModel.saveDownloadModels("", "") advanceUntilIdle() @@ -347,6 +375,15 @@ class CourseOutlineViewModelTest { @Test fun `saveDownloadModels only wifi download, with connection`() = runTest { + every { interactor.getCourseStructureFromCache() } returns courseStructure + coEvery { interactor.getCourseStatus(any()) } returns CourseComponentStatus("id") + every { preferencesManager.videoSettings.wifiDownloadOnly } returns true + every { networkConnection.isWifiConnected() } returns true + every { networkConnection.isOnline() } returns true + coEvery { downloadDao.readAllData() } returns mockk() + coEvery { workerController.saveModels(*anyVararg()) } returns Unit + coEvery { downloadDao.readAllData() } returns flow { emit(emptyList()) } + val viewModel = CourseOutlineViewModel( "", interactor, @@ -357,9 +394,6 @@ class CourseOutlineViewModelTest { downloadDao, workerController ) - every { preferencesManager.videoSettings.wifiDownloadOnly } returns true - every { networkConnection.isWifiConnected() } returns true - coEvery { workerController.saveModels(*anyVararg()) } returns Unit viewModel.saveDownloadModels("", "") advanceUntilIdle() @@ -369,6 +403,13 @@ class CourseOutlineViewModelTest { @Test fun `saveDownloadModels only wifi download, without connection`() = runTest { + every { interactor.getCourseStructureFromCache() } returns courseStructure + every { preferencesManager.videoSettings.wifiDownloadOnly } returns true + every { networkConnection.isWifiConnected() } returns false + every { networkConnection.isOnline() } returns false + coEvery { workerController.saveModels(*anyVararg()) } returns Unit + coEvery { downloadDao.readAllData() } returns flow { emit(emptyList()) } + val viewModel = CourseOutlineViewModel( "", interactor, @@ -379,10 +420,6 @@ class CourseOutlineViewModelTest { downloadDao, workerController ) - every { preferencesManager.videoSettings.wifiDownloadOnly } returns true - every { networkConnection.isWifiConnected() } returns false - every { networkConnection.isOnline() } returns false - coEvery { workerController.saveModels(*anyVararg()) } returns Unit viewModel.saveDownloadModels("", "") diff --git a/course/src/test/java/com/raccoongang/course/presentation/section/CourseSectionViewModelTest.kt b/course/src/test/java/com/raccoongang/course/presentation/section/CourseSectionViewModelTest.kt index b0b04edc5..64c3df5c1 100644 --- a/course/src/test/java/com/raccoongang/course/presentation/section/CourseSectionViewModelTest.kt +++ b/course/src/test/java/com/raccoongang/course/presentation/section/CourseSectionViewModelTest.kt @@ -10,6 +10,8 @@ import com.raccoongang.core.UIMessage import com.raccoongang.core.data.storage.PreferencesManager import com.raccoongang.core.domain.model.Block import com.raccoongang.core.domain.model.BlockCounts +import com.raccoongang.core.domain.model.CourseStructure +import com.raccoongang.core.domain.model.CoursewareAccess import com.raccoongang.core.module.DownloadWorkerController import com.raccoongang.core.module.db.* import com.raccoongang.core.presentation.course.CourseViewMode @@ -32,6 +34,7 @@ import org.junit.Rule import org.junit.Test import org.junit.rules.TestRule import java.net.UnknownHostException +import java.util.* @OptIn(ExperimentalCoroutinesApi::class) class CourseSectionViewModelTest { @@ -101,6 +104,30 @@ class CourseSectionViewModelTest { ) ) + private val courseStructure = CourseStructure( + root = "", + blockData = blocks, + id = "id", + name = "Course name", + number = "", + org = "Org", + start = Date(), + startDisplay = "", + startType = "", + end = Date(), + coursewareAccess = CoursewareAccess( + true, + "", + "", + "", + "", + "" + ), + media = null, + certificate = null, + isSelfPaced = false + ) + private val downloadModel = DownloadModel( "id", "title", @@ -192,8 +219,8 @@ class CourseSectionViewModelTest { coEvery { downloadDao.readAllData() } returns flow { emit(listOf(DownloadModelEntity.createFrom(downloadModel))) } - coEvery { interactor.getCourseStructureFromCache() } returns emptyList() - coEvery { interactor.getCourseStructureForVideos() } returns blocks + coEvery { interactor.getCourseStructureFromCache() } returns courseStructure + coEvery { interactor.getCourseStructureForVideos() } returns courseStructure viewModel.getBlocks("id", CourseViewMode.VIDEOS) advanceUntilIdle() @@ -289,8 +316,8 @@ class CourseSectionViewModelTest { emit(emptyList()) } } - coEvery { interactor.getCourseStructureFromCache() } returns emptyList() - coEvery { interactor.getCourseStructureForVideos() } returns blocks + coEvery { interactor.getCourseStructureFromCache() } returns courseStructure + coEvery { interactor.getCourseStructureForVideos() } returns courseStructure val mockLifeCycleOwner: LifecycleOwner = mockk() val lifecycleRegistry = LifecycleRegistry(mockLifeCycleOwner) diff --git a/course/src/test/java/com/raccoongang/course/presentation/unit/container/CourseUnitContainerViewModelTest.kt b/course/src/test/java/com/raccoongang/course/presentation/unit/container/CourseUnitContainerViewModelTest.kt index 4c589b037..af3f4413b 100644 --- a/course/src/test/java/com/raccoongang/course/presentation/unit/container/CourseUnitContainerViewModelTest.kt +++ b/course/src/test/java/com/raccoongang/course/presentation/unit/container/CourseUnitContainerViewModelTest.kt @@ -4,7 +4,10 @@ import androidx.arch.core.executor.testing.InstantTaskExecutorRule import com.raccoongang.core.BlockType import com.raccoongang.core.domain.model.Block import com.raccoongang.core.domain.model.BlockCounts +import com.raccoongang.core.domain.model.CourseStructure +import com.raccoongang.core.domain.model.CoursewareAccess import com.raccoongang.core.presentation.course.CourseViewMode +import com.raccoongang.core.system.notifier.CourseNotifier import com.raccoongang.course.domain.interactor.CourseInteractor import io.mockk.every import io.mockk.mockk @@ -18,6 +21,7 @@ import org.junit.Rule import org.junit.Test import org.junit.rules.TestRule import java.net.UnknownHostException +import java.util.* @OptIn(ExperimentalCoroutinesApi::class) class CourseUnitContainerViewModelTest { @@ -28,6 +32,7 @@ class CourseUnitContainerViewModelTest { private val dispatcher = StandardTestDispatcher() private val interactor = mockk() + private val notifier = mockk() private val blocks = listOf( Block( @@ -93,6 +98,30 @@ class CourseUnitContainerViewModelTest { ) + private val courseStructure = CourseStructure( + root = "", + blockData = blocks, + id = "id", + name = "Course name", + number = "", + org = "Org", + start = Date(), + startDisplay = "", + startType = "", + end = Date(), + coursewareAccess = CoursewareAccess( + true, + "", + "", + "", + "", + "" + ), + media = null, + certificate = null, + isSelfPaced = false + ) + @Before fun setUp() { Dispatchers.setMain(dispatcher) @@ -105,7 +134,7 @@ class CourseUnitContainerViewModelTest { @Test fun `getBlocks no internet connection exception`() = runTest { - val viewModel = CourseUnitContainerViewModel(interactor, "") + val viewModel = CourseUnitContainerViewModel(interactor, notifier, "") every { interactor.getCourseStructureFromCache() } throws UnknownHostException() every { interactor.getCourseStructureForVideos() } throws UnknownHostException() @@ -118,7 +147,7 @@ class CourseUnitContainerViewModelTest { @Test fun `getBlocks unknown exception`() = runTest { - val viewModel = CourseUnitContainerViewModel(interactor, "") + val viewModel = CourseUnitContainerViewModel(interactor, notifier, "") every { interactor.getCourseStructureFromCache() } throws UnknownHostException() every { interactor.getCourseStructureForVideos() } throws UnknownHostException() @@ -131,10 +160,10 @@ class CourseUnitContainerViewModelTest { @Test fun `getBlocks unknown success`() = runTest { - val viewModel = CourseUnitContainerViewModel(interactor, "") + val viewModel = CourseUnitContainerViewModel(interactor, notifier, "") - every { interactor.getCourseStructureFromCache() } returns blocks - every { interactor.getCourseStructureForVideos() } returns blocks + every { interactor.getCourseStructureFromCache() } returns courseStructure + every { interactor.getCourseStructureForVideos() } returns courseStructure viewModel.loadBlocks(CourseViewMode.VIDEOS) @@ -146,9 +175,9 @@ class CourseUnitContainerViewModelTest { @Test fun `setupCurrentIndex`() = runTest { - val viewModel = CourseUnitContainerViewModel(interactor, "") - every { interactor.getCourseStructureFromCache() } returns blocks - every { interactor.getCourseStructureForVideos() } returns blocks + val viewModel = CourseUnitContainerViewModel(interactor, notifier, "") + every { interactor.getCourseStructureFromCache() } returns courseStructure + every { interactor.getCourseStructureForVideos() } returns courseStructure viewModel.loadBlocks(CourseViewMode.VIDEOS) viewModel.setupCurrentIndex("id") @@ -160,9 +189,9 @@ class CourseUnitContainerViewModelTest { @Test fun `prevVertical`() = runTest { - val viewModel = CourseUnitContainerViewModel(interactor, "") - every { interactor.getCourseStructureFromCache() } returns blocks - every { interactor.getCourseStructureForVideos() } returns blocks + val viewModel = CourseUnitContainerViewModel(interactor, notifier, "") + every { interactor.getCourseStructureFromCache() } returns courseStructure + every { interactor.getCourseStructureForVideos() } returns courseStructure viewModel.loadBlocks(CourseViewMode.VIDEOS) viewModel.setupCurrentIndex("id2") @@ -177,9 +206,9 @@ class CourseUnitContainerViewModelTest { @Test fun `nextVertical null`() = runTest { - val viewModel = CourseUnitContainerViewModel(interactor, "") - every { interactor.getCourseStructureFromCache() } returns blocks - every { interactor.getCourseStructureForVideos() } returns blocks + val viewModel = CourseUnitContainerViewModel(interactor, notifier, "") + every { interactor.getCourseStructureFromCache() } returns courseStructure + every { interactor.getCourseStructureForVideos() } returns courseStructure viewModel.loadBlocks(CourseViewMode.VIDEOS) viewModel.setupCurrentIndex("id2") @@ -194,9 +223,9 @@ class CourseUnitContainerViewModelTest { @Test fun `getCurrentBlock test`() = runTest { - val viewModel = CourseUnitContainerViewModel(interactor, "") - every { interactor.getCourseStructureFromCache() } returns blocks - every { interactor.getCourseStructureForVideos() } returns blocks + val viewModel = CourseUnitContainerViewModel(interactor, notifier, "") + every { interactor.getCourseStructureFromCache() } returns courseStructure + every { interactor.getCourseStructureForVideos() } returns courseStructure viewModel.loadBlocks(CourseViewMode.VIDEOS) viewModel.setupCurrentIndex("id") @@ -210,9 +239,9 @@ class CourseUnitContainerViewModelTest { @Test fun `moveToPrevBlock null`() = runTest { - val viewModel = CourseUnitContainerViewModel(interactor, "") - every { interactor.getCourseStructureFromCache() } returns blocks - every { interactor.getCourseStructureForVideos() } returns blocks + val viewModel = CourseUnitContainerViewModel(interactor, notifier, "") + every { interactor.getCourseStructureFromCache() } returns courseStructure + every { interactor.getCourseStructureForVideos() } returns courseStructure viewModel.loadBlocks(CourseViewMode.VIDEOS) viewModel.setupCurrentIndex("id") @@ -226,9 +255,9 @@ class CourseUnitContainerViewModelTest { @Test fun `moveToPrevBlock not null`() = runTest { - val viewModel = CourseUnitContainerViewModel(interactor, "") - every { interactor.getCourseStructureFromCache() } returns blocks - every { interactor.getCourseStructureForVideos() } returns blocks + val viewModel = CourseUnitContainerViewModel(interactor, notifier, "") + every { interactor.getCourseStructureFromCache() } returns courseStructure + every { interactor.getCourseStructureForVideos() } returns courseStructure viewModel.loadBlocks(CourseViewMode.VIDEOS) viewModel.setupCurrentIndex("id3") @@ -242,9 +271,9 @@ class CourseUnitContainerViewModelTest { @Test fun `moveToNextBlock null`() = runTest { - val viewModel = CourseUnitContainerViewModel(interactor, "") - every { interactor.getCourseStructureFromCache() } returns blocks - every { interactor.getCourseStructureForVideos() } returns blocks + val viewModel = CourseUnitContainerViewModel(interactor, notifier, "") + every { interactor.getCourseStructureFromCache() } returns courseStructure + every { interactor.getCourseStructureForVideos() } returns courseStructure viewModel.loadBlocks(CourseViewMode.VIDEOS) viewModel.setupCurrentIndex("id3") @@ -258,9 +287,9 @@ class CourseUnitContainerViewModelTest { @Test fun `moveToNextBlock not null`() = runTest { - val viewModel = CourseUnitContainerViewModel(interactor, "") - every { interactor.getCourseStructureFromCache() } returns blocks - every { interactor.getCourseStructureForVideos() } returns blocks + val viewModel = CourseUnitContainerViewModel(interactor, notifier, "") + every { interactor.getCourseStructureFromCache() } returns courseStructure + every { interactor.getCourseStructureForVideos() } returns courseStructure viewModel.loadBlocks(CourseViewMode.VIDEOS) viewModel.setupCurrentIndex("id") @@ -274,9 +303,9 @@ class CourseUnitContainerViewModelTest { @Test fun `currentIndex equals 0`() = runTest { - val viewModel = CourseUnitContainerViewModel(interactor, "") - every { interactor.getCourseStructureFromCache() } returns blocks - every { interactor.getCourseStructureForVideos() } returns blocks + val viewModel = CourseUnitContainerViewModel(interactor, notifier, "") + every { interactor.getCourseStructureFromCache() } returns courseStructure + every { interactor.getCourseStructureForVideos() } returns courseStructure viewModel.loadBlocks(CourseViewMode.VIDEOS) viewModel.setupCurrentIndex("id") @@ -292,9 +321,9 @@ class CourseUnitContainerViewModelTest { @Test fun `currentIndex isLastIndex`() = runTest { - val viewModel = CourseUnitContainerViewModel(interactor, "") - every { interactor.getCourseStructureFromCache() } returns blocks - every { interactor.getCourseStructureForVideos() } returns blocks + val viewModel = CourseUnitContainerViewModel(interactor, notifier, "") + every { interactor.getCourseStructureFromCache() } returns courseStructure + every { interactor.getCourseStructureForVideos() } returns courseStructure viewModel.loadBlocks(CourseViewMode.VIDEOS) viewModel.setupCurrentIndex("id3") diff --git a/course/src/test/java/com/raccoongang/course/presentation/unit/video/VideoUnitViewModelTest.kt b/course/src/test/java/com/raccoongang/course/presentation/unit/video/VideoUnitViewModelTest.kt index 80cf72840..db9c48f77 100644 --- a/course/src/test/java/com/raccoongang/course/presentation/unit/video/VideoUnitViewModelTest.kt +++ b/course/src/test/java/com/raccoongang/course/presentation/unit/video/VideoUnitViewModelTest.kt @@ -5,6 +5,7 @@ import androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.LifecycleRegistry import com.raccoongang.core.data.storage.PreferencesManager +import com.raccoongang.core.module.TranscriptManager import com.raccoongang.core.system.connection.NetworkConnection import com.raccoongang.core.system.notifier.CourseNotifier import com.raccoongang.core.system.notifier.CourseVideoPositionChanged @@ -34,6 +35,7 @@ class VideoUnitViewModelTest { private val notifier = mockk() private val preferencesManager = mockk() private val networkConnection = mockk() + private val transcriptManager = mockk() @Before @@ -48,7 +50,14 @@ class VideoUnitViewModelTest { @Test fun `markBlockCompleted exception`() = runTest { - val viewModel = VideoUnitViewModel("", courseRepository, preferencesManager, notifier, networkConnection) + val viewModel = VideoUnitViewModel( + "", + courseRepository, + preferencesManager, + notifier, + networkConnection, + transcriptManager + ) coEvery { courseRepository.markBlocksCompletion( any(), @@ -68,7 +77,14 @@ class VideoUnitViewModelTest { @Test fun `markBlockCompleted success`() = runTest { - val viewModel = VideoUnitViewModel("", courseRepository, preferencesManager, notifier, networkConnection) + val viewModel = VideoUnitViewModel( + "", + courseRepository, + preferencesManager, + notifier, + networkConnection, + transcriptManager + ) coEvery { courseRepository.markBlocksCompletion( any(), @@ -88,7 +104,14 @@ class VideoUnitViewModelTest { @Test fun `CourseVideoPositionChanged notifier test`() = runTest { - val viewModel = VideoUnitViewModel("", courseRepository, preferencesManager, notifier, networkConnection) + val viewModel = VideoUnitViewModel( + "", + courseRepository, + preferencesManager, + notifier, + networkConnection, + transcriptManager + ) coEvery { notifier.notifier } returns flow { emit(CourseVideoPositionChanged("", 10)) } val mockLifeCycleOwner: LifecycleOwner = mockk() val lifecycleRegistry = LifecycleRegistry(mockLifeCycleOwner) @@ -97,7 +120,7 @@ class VideoUnitViewModelTest { advanceUntilIdle() - assert(viewModel.currentVideoTime == 10L) + assert(viewModel.currentVideoTime.value == 10L) assert(viewModel.isUpdated.value == true) } diff --git a/course/src/test/java/com/raccoongang/course/presentation/units/CourseUnitsViewModelTest.kt b/course/src/test/java/com/raccoongang/course/presentation/units/CourseUnitsViewModelTest.kt index 2c1e267e6..0cacb2162 100644 --- a/course/src/test/java/com/raccoongang/course/presentation/units/CourseUnitsViewModelTest.kt +++ b/course/src/test/java/com/raccoongang/course/presentation/units/CourseUnitsViewModelTest.kt @@ -111,6 +111,15 @@ class CourseUnitsViewModelTest { ) ) + private val courseStructure = CourseStructure( + "", blocks, "", "", "", "", + null, "", "", null, + CoursewareAccess(false, "", "", "", "", ""), + null, + null, + false + ) + private val downloadModel = DownloadModel( "id", "title", @@ -189,8 +198,8 @@ class CourseUnitsViewModelTest { networkConnection, workerController ) - every { interactor.getCourseStructureFromCache() } returns blocks - every { interactor.getCourseStructureForVideos() } returns blocks + every { interactor.getCourseStructureFromCache() } returns courseStructure + every { interactor.getCourseStructureForVideos() } returns courseStructure coEvery { interactor.getDownloadModels() } returns flow { emit(listOf(downloadModel)) } @@ -213,8 +222,8 @@ class CourseUnitsViewModelTest { networkConnection, workerController ) - every { interactor.getCourseStructureFromCache() } returns blocks - every { interactor.getCourseStructureForVideos() } returns blocks + every { interactor.getCourseStructureFromCache() } returns courseStructure + every { interactor.getCourseStructureForVideos() } returns courseStructure coEvery { interactor.getDownloadModels() } returns flow { emit(listOf(downloadModel)) } @@ -237,8 +246,8 @@ class CourseUnitsViewModelTest { networkConnection, workerController ) - every { interactor.getCourseStructureFromCache() } returns blocks - every { interactor.getCourseStructureForVideos() } returns blocks + every { interactor.getCourseStructureFromCache() } returns courseStructure + every { interactor.getCourseStructureForVideos() } returns courseStructure coEvery { interactor.getDownloadModels() } returns flow { repeat(5) { delay(10000) diff --git a/course/src/test/java/com/raccoongang/course/presentation/videos/CourseVideoViewModelTest.kt b/course/src/test/java/com/raccoongang/course/presentation/videos/CourseVideoViewModelTest.kt index 4f25e69d7..579cf1e76 100644 --- a/course/src/test/java/com/raccoongang/course/presentation/videos/CourseVideoViewModelTest.kt +++ b/course/src/test/java/com/raccoongang/course/presentation/videos/CourseVideoViewModelTest.kt @@ -8,6 +8,8 @@ import com.raccoongang.core.BlockType import com.raccoongang.core.data.storage.PreferencesManager import com.raccoongang.core.domain.model.Block import com.raccoongang.core.domain.model.BlockCounts +import com.raccoongang.core.domain.model.CourseStructure +import com.raccoongang.core.domain.model.CoursewareAccess import com.raccoongang.core.module.DownloadWorkerController import com.raccoongang.core.module.db.DownloadDao import com.raccoongang.core.system.ResourceManager @@ -27,6 +29,7 @@ import org.junit.Before import org.junit.Rule import org.junit.Test import org.junit.rules.TestRule +import java.util.* @OptIn(ExperimentalCoroutinesApi::class) class CourseVideoViewModelTest { @@ -45,7 +48,6 @@ class CourseVideoViewModelTest { private val cantDownload = "You can download content only from Wi-fi" - private val blocks = listOf( Block( id = "id", @@ -94,6 +96,30 @@ class CourseVideoViewModelTest { ) ) + private val courseStructure = CourseStructure( + root = "", + blockData = blocks, + id = "id", + name = "Course name", + number = "", + org = "Org", + start = Date(), + startDisplay = "", + startType = "", + end = Date(), + coursewareAccess = CoursewareAccess( + true, + "", + "", + "", + "", + "" + ), + media = null, + certificate = null, + isSelfPaced = false + ) + @Before fun setUp() { @@ -109,6 +135,9 @@ class CourseVideoViewModelTest { @Test fun `getVideos empty list`() = runTest { + every { interactor.getCourseStructureForVideos() } returns courseStructure.copy(blockData = emptyList()) + every { downloadDao.readAllData() } returns flow { emit(emptyList()) } + val viewModel = CourseVideoViewModel( "", interactor, @@ -120,17 +149,18 @@ class CourseVideoViewModelTest { workerController ) - every { interactor.getCourseStructureForVideos() } returns emptyList() viewModel.getVideos() advanceUntilIdle() - coVerify(exactly = 1) { interactor.getCourseStructureForVideos() } + coVerify(exactly = 2) { interactor.getCourseStructureForVideos() } assert(viewModel.uiState.value is CourseVideosUIState.Empty) } @Test fun `getVideos success`() = runTest { + every { interactor.getCourseStructureForVideos() } returns courseStructure + every { downloadDao.readAllData() } returns flow { emit(emptyList()) } val viewModel = CourseVideoViewModel( "", interactor, @@ -142,17 +172,25 @@ class CourseVideoViewModelTest { workerController ) - every { interactor.getCourseStructureForVideos() } returns blocks + viewModel.getVideos() advanceUntilIdle() - coVerify(exactly = 1) { interactor.getCourseStructureForVideos() } + coVerify(exactly = 2) { interactor.getCourseStructureForVideos() } assert(viewModel.uiState.value is CourseVideosUIState.CourseData) } @Test fun `updateVideos success`() = runTest { + every { interactor.getCourseStructureForVideos() } returns courseStructure + coEvery { notifier.notifier } returns flow { emit(CourseStructureUpdated("", false)) } + every { downloadDao.readAllData() } returns flow { + repeat(5) { + delay(10000) + emit(emptyList()) + } + } val viewModel = CourseVideoViewModel( "", interactor, @@ -164,15 +202,6 @@ class CourseVideoViewModelTest { workerController ) - every { interactor.getCourseStructureForVideos() } returns blocks - coEvery { notifier.notifier } returns flow { emit(CourseStructureUpdated("", false)) } - every { downloadDao.readAllData() } returns flow { - repeat(5) { - delay(10000) - emit(emptyList()) - } - } - val mockLifeCycleOwner: LifecycleOwner = mockk() val lifecycleRegistry = LifecycleRegistry(mockLifeCycleOwner) lifecycleRegistry.addObserver(viewModel) @@ -180,7 +209,7 @@ class CourseVideoViewModelTest { advanceUntilIdle() - coVerify(exactly = 1) { interactor.getCourseStructureForVideos() } + coVerify(exactly = 2) { interactor.getCourseStructureForVideos() } assert(viewModel.uiState.value is CourseVideosUIState.CourseData) assert(viewModel.isUpdating.value == false) diff --git a/dashboard/src/test/java/com/raccoongang/dashboard/presentation/DashboardViewModelTest.kt b/dashboard/src/test/java/com/raccoongang/dashboard/presentation/DashboardViewModelTest.kt index ed761ac12..fc1d94966 100644 --- a/dashboard/src/test/java/com/raccoongang/dashboard/presentation/DashboardViewModelTest.kt +++ b/dashboard/src/test/java/com/raccoongang/dashboard/presentation/DashboardViewModelTest.kt @@ -7,6 +7,8 @@ import androidx.lifecycle.LifecycleRegistry import com.raccoongang.core.system.connection.NetworkConnection import com.raccoongang.core.R import com.raccoongang.core.UIMessage +import com.raccoongang.core.domain.model.DashboardCourseList +import com.raccoongang.core.domain.model.Pagination import com.raccoongang.core.system.ResourceManager import com.raccoongang.core.system.notifier.CourseDashboardUpdate import com.raccoongang.core.system.notifier.CourseNotifier @@ -33,7 +35,6 @@ class DashboardViewModelTest { @get:Rule val testInstantTaskExecutorRule: TestRule = InstantTaskExecutorRule() - private val dispatcher = StandardTestDispatcher() private val resourceManager = mockk() @@ -44,6 +45,11 @@ class DashboardViewModelTest { private val noInternet = "Slow or no internet connection" private val somethingWrong = "Something went wrong" + private val dashboardCourseList = DashboardCourseList( + Pagination(10, "", 3, ""), + listOf(mockk()) + ) + @Before fun setUp() { Dispatchers.setMain(dispatcher) @@ -60,11 +66,11 @@ class DashboardViewModelTest { fun `getCourses no internet connection`() = runTest { val viewModel = DashboardViewModel(networkConnection, interactor, resourceManager, notifier) every { networkConnection.isOnline() } returns true - coEvery { interactor.getEnrolledCourses() } throws UnknownHostException() + coEvery { interactor.getEnrolledCourses(any()) } throws UnknownHostException() advanceUntilIdle() - coVerify(exactly = 1) { interactor.getEnrolledCourses() } + coVerify(exactly = 1) { interactor.getEnrolledCourses(any()) } coVerify(exactly = 0) { interactor.getEnrolledCoursesFromCache() } val message = viewModel.uiMessage.value as? UIMessage.SnackBarMessage @@ -76,11 +82,11 @@ class DashboardViewModelTest { fun `getCourses unknown error`() = runTest { val viewModel = DashboardViewModel(networkConnection, interactor, resourceManager, notifier) every { networkConnection.isOnline() } returns true - coEvery { interactor.getEnrolledCourses() } throws Exception() + coEvery { interactor.getEnrolledCourses(any()) } throws Exception() advanceUntilIdle() - coVerify(exactly = 1) { interactor.getEnrolledCourses() } + coVerify(exactly = 1) { interactor.getEnrolledCourses(any()) } coVerify(exactly = 0) { interactor.getEnrolledCoursesFromCache() } val message = viewModel.uiMessage.value as? UIMessage.SnackBarMessage @@ -92,11 +98,35 @@ class DashboardViewModelTest { fun `getCourses from network`() = runTest { val viewModel = DashboardViewModel(networkConnection, interactor, resourceManager, notifier) every { networkConnection.isOnline() } returns true - coEvery { interactor.getEnrolledCourses() } returns emptyList() + coEvery { interactor.getEnrolledCourses(any()) } returns dashboardCourseList + coEvery { interactor.getEnrolledCoursesFromCache() } returns listOf(mockk()) + + advanceUntilIdle() + + coVerify(exactly = 1) { interactor.getEnrolledCourses(any()) } + coVerify(exactly = 0) { interactor.getEnrolledCoursesFromCache() } + + assert(viewModel.uiMessage.value == null) + assert(viewModel.uiState.value is DashboardUIState.Courses) + } + + @Test + fun `getCourses from network with next page`() = runTest { + val viewModel = DashboardViewModel(networkConnection, interactor, resourceManager, notifier) + every { networkConnection.isOnline() } returns true + coEvery { interactor.getEnrolledCourses(any()) } returns dashboardCourseList.copy( + Pagination( + 10, + "2", + 2, + "" + ) + ) + coEvery { interactor.getEnrolledCoursesFromCache() } returns listOf(mockk()) advanceUntilIdle() - coVerify(exactly = 1) { interactor.getEnrolledCourses() } + coVerify(exactly = 1) { interactor.getEnrolledCourses(any()) } coVerify(exactly = 0) { interactor.getEnrolledCoursesFromCache() } assert(viewModel.uiMessage.value == null) @@ -106,13 +136,13 @@ class DashboardViewModelTest { @Test fun `getCourses from cache`() = runTest { every { networkConnection.isOnline() } returns false - coEvery { interactor.getEnrolledCoursesFromCache() } returns emptyList() + coEvery { interactor.getEnrolledCoursesFromCache() } returns listOf(mockk()) val viewModel = DashboardViewModel(networkConnection, interactor, resourceManager, notifier) advanceUntilIdle() - coVerify(exactly = 0) { interactor.getEnrolledCourses() } + coVerify(exactly = 0) { interactor.getEnrolledCourses(any()) } coVerify(exactly = 1) { interactor.getEnrolledCoursesFromCache() } assert(viewModel.uiMessage.value == null) @@ -122,14 +152,14 @@ class DashboardViewModelTest { @Test fun `updateCourses no internet error`() = runTest { every { networkConnection.isOnline() } returns true - coEvery { interactor.getEnrolledCourses() } returns emptyList() + coEvery { interactor.getEnrolledCourses(any()) } returns dashboardCourseList val viewModel = DashboardViewModel(networkConnection, interactor, resourceManager, notifier) - coEvery { interactor.getEnrolledCourses() } throws UnknownHostException() + coEvery { interactor.getEnrolledCourses(any()) } throws UnknownHostException() viewModel.updateCourses() advanceUntilIdle() - coVerify(exactly = 2) { interactor.getEnrolledCourses() } + coVerify(exactly = 2) { interactor.getEnrolledCourses(any()) } coVerify(exactly = 0) { interactor.getEnrolledCoursesFromCache() } val message = viewModel.uiMessage.value as? UIMessage.SnackBarMessage @@ -141,15 +171,15 @@ class DashboardViewModelTest { @Test fun `updateCourses unknown exception`() = runTest { every { networkConnection.isOnline() } returns true - coEvery { interactor.getEnrolledCourses() } returns emptyList() + coEvery { interactor.getEnrolledCourses(any()) } returns dashboardCourseList val viewModel = DashboardViewModel(networkConnection, interactor, resourceManager, notifier) - coEvery { interactor.getEnrolledCourses() } throws Exception() + coEvery { interactor.getEnrolledCourses(any()) } throws Exception() viewModel.updateCourses() advanceUntilIdle() - coVerify(exactly = 2) { interactor.getEnrolledCourses() } + coVerify(exactly = 2) { interactor.getEnrolledCourses(any()) } coVerify(exactly = 0) { interactor.getEnrolledCoursesFromCache() } val message = viewModel.uiMessage.value as? UIMessage.SnackBarMessage @@ -161,13 +191,30 @@ class DashboardViewModelTest { @Test fun `updateCourses success`() = runTest { every { networkConnection.isOnline() } returns true - coEvery { interactor.getEnrolledCourses() } returns emptyList() + coEvery { interactor.getEnrolledCourses(any()) } returns dashboardCourseList + val viewModel = DashboardViewModel(networkConnection, interactor, resourceManager, notifier) + + viewModel.updateCourses() + advanceUntilIdle() + + coVerify(exactly = 2) { interactor.getEnrolledCourses(any()) } + coVerify(exactly = 0) { interactor.getEnrolledCoursesFromCache() } + + assert(viewModel.uiMessage.value == null) + assert(viewModel.updating.value == false) + assert(viewModel.uiState.value is DashboardUIState.Courses) + } + + @Test + fun `updateCourses success with next page`() = runTest { + every { networkConnection.isOnline() } returns true + coEvery { interactor.getEnrolledCourses(any()) } returns dashboardCourseList.copy(Pagination(10,"2",2,"")) val viewModel = DashboardViewModel(networkConnection, interactor, resourceManager, notifier) viewModel.updateCourses() advanceUntilIdle() - coVerify(exactly = 2) { interactor.getEnrolledCourses() } + coVerify(exactly = 2) { interactor.getEnrolledCourses(any()) } coVerify(exactly = 0) { interactor.getEnrolledCoursesFromCache() } assert(viewModel.uiMessage.value == null) @@ -188,7 +235,7 @@ class DashboardViewModelTest { advanceUntilIdle() - coVerify(exactly = 1) { interactor.getEnrolledCourses() } + coVerify(exactly = 1) { interactor.getEnrolledCourses(any()) } } diff --git a/discovery/src/test/java/com/raccoongang/discovery/presentation/search/CourseSearchViewModelTest.kt b/discovery/src/test/java/com/raccoongang/discovery/presentation/search/CourseSearchViewModelTest.kt index 347575db6..916ca1a2d 100644 --- a/discovery/src/test/java/com/raccoongang/discovery/presentation/search/CourseSearchViewModelTest.kt +++ b/discovery/src/test/java/com/raccoongang/discovery/presentation/search/CourseSearchViewModelTest.kt @@ -60,7 +60,8 @@ class CourseSearchViewModelTest { end = "end", startDisplay = "startDisplay", startType = "startType", - overview = "" + overview = "", + false ) //endregion diff --git a/discussion/src/test/java/com/raccoongang/discussion/presentation/comments/DiscussionCommentsViewModelTest.kt b/discussion/src/test/java/com/raccoongang/discussion/presentation/comments/DiscussionCommentsViewModelTest.kt index b806b0947..dad00d8da 100644 --- a/discussion/src/test/java/com/raccoongang/discussion/presentation/comments/DiscussionCommentsViewModelTest.kt +++ b/discussion/src/test/java/com/raccoongang/discussion/presentation/comments/DiscussionCommentsViewModelTest.kt @@ -73,7 +73,10 @@ class DiscussionCommentsViewModelTest { 4, false, false, - mapOf() + mapOf(), + 0, + false, + false ) //endregion @@ -102,7 +105,8 @@ class DiscussionCommentsViewModelTest { "", 21, emptyList(), - emptyMap() + null, + mapOf() ) //endregion @@ -129,6 +133,7 @@ class DiscussionCommentsViewModelTest { @Test fun `getThreadComments no internet connection exception`() = runTest { coEvery { interactor.getThreadComments(any(), any()) } throws UnknownHostException() + every { resourceManager.getString(eq(mockThread.type.resId)) } returns "" val viewModel = DiscussionCommentsViewModel( interactor, @@ -152,6 +157,7 @@ class DiscussionCommentsViewModelTest { @Test fun `getThreadComments unknown exception`() = runTest { coEvery { interactor.getThreadComments(any(), any()) } throws Exception() + every { resourceManager.getString(eq(DiscussionType.QUESTION.resId)) } returns "" val viewModel = DiscussionCommentsViewModel( interactor, @@ -181,6 +187,8 @@ class DiscussionCommentsViewModelTest { Pagination(10, "2", 4, "1") ) coEvery { interactor.setThreadRead(any()) } returns mockThread + every { resourceManager.getString(eq(DiscussionType.QUESTION.resId)) } returns "" + val viewModel = DiscussionCommentsViewModel( interactor, @@ -209,6 +217,8 @@ class DiscussionCommentsViewModelTest { Pagination(10, "", 4, "1") ) coEvery { interactor.setThreadRead(any()) } returns mockThread + every { resourceManager.getString(eq(mockThread.type.resId)) } returns "" + val viewModel = DiscussionCommentsViewModel( interactor, @@ -241,6 +251,8 @@ class DiscussionCommentsViewModelTest { Pagination(10, "", 4, "1") ) coEvery { interactor.setThreadRead(any()) } returns mockThread + every { resourceManager.getString(eq(mockThread.type.resId)) } returns "" + val viewModel = DiscussionCommentsViewModel( interactor, @@ -275,6 +287,8 @@ class DiscussionCommentsViewModelTest { Pagination(10, "", 4, "1") ) coEvery { interactor.setThreadRead(any()) } throws UnknownHostException() + every { resourceManager.getString(eq(mockThread.type.resId)) } returns "" + val viewModel = DiscussionCommentsViewModel( interactor, @@ -309,6 +323,8 @@ class DiscussionCommentsViewModelTest { comments, Pagination(10, "2", 4, "1") ) + every { resourceManager.getString(eq(mockThread.type.resId)) } returns "" + val viewModel = DiscussionCommentsViewModel( interactor, @@ -341,6 +357,8 @@ class DiscussionCommentsViewModelTest { comments, Pagination(10, "", 4, "1") ) + every { resourceManager.getString(eq(mockThread.type.resId)) } returns "" + val viewModel = DiscussionCommentsViewModel( interactor, @@ -369,6 +387,8 @@ class DiscussionCommentsViewModelTest { comments, Pagination(10, "", 4, "1") ) + every { resourceManager.getString(eq(mockThread.type.resId)) } returns "" + val viewModel = DiscussionCommentsViewModel( interactor, @@ -396,6 +416,8 @@ class DiscussionCommentsViewModelTest { comments, Pagination(10, "", 4, "1") ) + every { resourceManager.getString(eq(mockThread.type.resId)) } returns "" + val viewModel = DiscussionCommentsViewModel( interactor, @@ -422,6 +444,8 @@ class DiscussionCommentsViewModelTest { comments, Pagination(10, "", 4, "1") ) + every { resourceManager.getString(eq(mockThread.type.resId)) } returns "" + val viewModel = DiscussionCommentsViewModel( interactor, @@ -450,6 +474,8 @@ class DiscussionCommentsViewModelTest { comments, Pagination(10, "", 4, "1") ) + every { resourceManager.getString(eq(mockThread.type.resId)) } returns "" + val viewModel = DiscussionCommentsViewModel( interactor, @@ -478,6 +504,8 @@ class DiscussionCommentsViewModelTest { comments, Pagination(10, "", 4, "1") ) + every { resourceManager.getString(eq(mockThread.type.resId)) } returns "" + val viewModel = DiscussionCommentsViewModel( interactor, @@ -505,6 +533,8 @@ class DiscussionCommentsViewModelTest { comments, Pagination(10, "", 4, "1") ) + every { resourceManager.getString(eq(mockThread.type.resId)) } returns "" + val viewModel = DiscussionCommentsViewModel( interactor, @@ -532,6 +562,8 @@ class DiscussionCommentsViewModelTest { comments, Pagination(10, "", 4, "1") ) + every { resourceManager.getString(eq(mockThread.type.resId)) } returns "" + val viewModel = DiscussionCommentsViewModel( interactor, @@ -559,6 +591,8 @@ class DiscussionCommentsViewModelTest { comments, Pagination(10, "", 4, "1") ) + every { resourceManager.getString(eq(mockThread.type.resId)) } returns "" + val viewModel = DiscussionCommentsViewModel( interactor, @@ -585,6 +619,8 @@ class DiscussionCommentsViewModelTest { comments, Pagination(10, "", 4, "1") ) + every { resourceManager.getString(eq(mockThread.type.resId)) } returns "" + val viewModel = DiscussionCommentsViewModel( interactor, @@ -612,6 +648,8 @@ class DiscussionCommentsViewModelTest { comments, Pagination(10, "", 4, "1") ) + every { resourceManager.getString(eq(mockThread.type.resId)) } returns "" + val viewModel = DiscussionCommentsViewModel( interactor, @@ -639,6 +677,8 @@ class DiscussionCommentsViewModelTest { comments, Pagination(10, "", 4, "1") ) + every { resourceManager.getString(eq(mockThread.type.resId)) } returns "" + val viewModel = DiscussionCommentsViewModel( interactor, @@ -666,6 +706,8 @@ class DiscussionCommentsViewModelTest { comments, Pagination(10, "", 4, "1") ) + every { resourceManager.getString(eq(mockThread.type.resId)) } returns "" + val viewModel = DiscussionCommentsViewModel( interactor, @@ -693,6 +735,8 @@ class DiscussionCommentsViewModelTest { comments, Pagination(10, "", 4, "1") ) + every { resourceManager.getString(eq(mockThread.type.resId)) } returns "" + val viewModel = DiscussionCommentsViewModel( interactor, @@ -721,6 +765,8 @@ class DiscussionCommentsViewModelTest { comments, Pagination(10, "", 4, "1") ) + every { resourceManager.getString(eq(mockThread.type.resId)) } returns "" + val viewModel = DiscussionCommentsViewModel( interactor, @@ -747,6 +793,8 @@ class DiscussionCommentsViewModelTest { comments, Pagination(10, "", 4, "1") ) + every { resourceManager.getString(eq(mockThread.type.resId)) } returns "" + val viewModel = DiscussionCommentsViewModel( interactor, @@ -781,6 +829,8 @@ class DiscussionCommentsViewModelTest { comments, Pagination(10, "2", 4, "1") ) + every { resourceManager.getString(eq(mockThread.type.resId)) } returns "" + val viewModel = DiscussionCommentsViewModel( interactor, @@ -815,6 +865,8 @@ class DiscussionCommentsViewModelTest { comments, Pagination(10, "2", 4, "1") ) + every { resourceManager.getString(eq(mockThread.type.resId)) } returns "" + val viewModel = DiscussionCommentsViewModel( interactor, @@ -847,6 +899,8 @@ class DiscussionCommentsViewModelTest { comments, Pagination(10, "2", 4, "1") ) + every { resourceManager.getString(eq(mockThread.type.resId)) } returns "" + val viewModel = DiscussionCommentsViewModel( interactor, @@ -873,6 +927,8 @@ class DiscussionCommentsViewModelTest { comments, Pagination(10, "2", 4, "1") ) + every { resourceManager.getString(eq(mockThread.type.resId)) } returns "" + val viewModel = DiscussionCommentsViewModel( interactor, @@ -900,6 +956,8 @@ class DiscussionCommentsViewModelTest { Pagination(10, "2", 4, "1") ) coEvery { interactor.setThreadRead(any()) } returns mockThread + every { resourceManager.getString(eq(mockThread.type.resId)) } returns "" + val viewModel = DiscussionCommentsViewModel( interactor, @@ -909,14 +967,11 @@ class DiscussionCommentsViewModelTest { mockThread ) coEvery { interactor.createComment(any(), any(), any()) } returns mockComment - every { preferencesManager.profile?.username } returns "" - every { preferencesManager.profile?.profileImage } returns mockk() viewModel.createComment("") advanceUntilIdle() coVerify(exactly = 1) { interactor.createComment(any(), any(), any()) } - verify(exactly = 2) { preferencesManager.profile } assert(viewModel.uiMessage.value != null) assert(viewModel.uiState.value is DiscussionCommentsUIState.Success) @@ -928,6 +983,8 @@ class DiscussionCommentsViewModelTest { comments, Pagination(10, "2", 4, "1") ) + every { resourceManager.getString(eq(mockThread.type.resId)) } returns "" + val viewModel = DiscussionCommentsViewModel( interactor, @@ -950,6 +1007,8 @@ class DiscussionCommentsViewModelTest { comments, Pagination(10, "2", 4, "1") ) + every { resourceManager.getString(eq(mockThread.type.resId)) } returns "" + val viewModel = DiscussionCommentsViewModel( interactor, @@ -973,6 +1032,8 @@ class DiscussionCommentsViewModelTest { comments, Pagination(10, "", 4, "1") ) + every { resourceManager.getString(eq(mockThread.type.resId)) } returns "" + val viewModel = DiscussionCommentsViewModel( interactor, diff --git a/discussion/src/test/java/com/raccoongang/discussion/presentation/responses/DiscussionResponsesViewModelTest.kt b/discussion/src/test/java/com/raccoongang/discussion/presentation/responses/DiscussionResponsesViewModelTest.kt index 6ce6e6773..d586e431d 100644 --- a/discussion/src/test/java/com/raccoongang/discussion/presentation/responses/DiscussionResponsesViewModelTest.kt +++ b/discussion/src/test/java/com/raccoongang/discussion/presentation/responses/DiscussionResponsesViewModelTest.kt @@ -69,7 +69,10 @@ class DiscussionResponsesViewModelTest { 4, false, false, - mapOf() + mapOf(), + 0, + false, + false ) //endregion @@ -98,6 +101,7 @@ class DiscussionResponsesViewModelTest { "", 21, emptyList(), + null, emptyMap() ) @@ -516,14 +520,11 @@ class DiscussionResponsesViewModelTest { mockComment.copy(id = "0") ) coEvery { interactor.createComment(any(), any(), any()) } returns mockComment - every { preferencesManager.profile?.username } returns "" - every { preferencesManager.profile?.profileImage } returns mockk() viewModel.createComment("") advanceUntilIdle() coVerify(exactly = 1) { interactor.createComment(any(), any(), any()) } - verify(exactly = 2) { preferencesManager.profile } assert(viewModel.uiMessage.value != null) diff --git a/discussion/src/test/java/com/raccoongang/discussion/presentation/search/DiscussionSearchThreadViewModelTest.kt b/discussion/src/test/java/com/raccoongang/discussion/presentation/search/DiscussionSearchThreadViewModelTest.kt index cba85846f..fb2753180 100644 --- a/discussion/src/test/java/com/raccoongang/discussion/presentation/search/DiscussionSearchThreadViewModelTest.kt +++ b/discussion/src/test/java/com/raccoongang/discussion/presentation/search/DiscussionSearchThreadViewModelTest.kt @@ -77,7 +77,10 @@ class DiscussionSearchThreadViewModelTest { 4, false, false, - mapOf() + mapOf(), + 0, + false, + false ) //endregion diff --git a/discussion/src/test/java/com/raccoongang/discussion/presentation/threads/DiscussionAddThreadViewModelTest.kt b/discussion/src/test/java/com/raccoongang/discussion/presentation/threads/DiscussionAddThreadViewModelTest.kt index 972346a5e..f0559e59c 100644 --- a/discussion/src/test/java/com/raccoongang/discussion/presentation/threads/DiscussionAddThreadViewModelTest.kt +++ b/discussion/src/test/java/com/raccoongang/discussion/presentation/threads/DiscussionAddThreadViewModelTest.kt @@ -73,7 +73,10 @@ class DiscussionAddThreadViewModelTest { 4, false, false, - mapOf() + mapOf(), + 0, + false, + false ) //endregion @@ -102,6 +105,7 @@ class DiscussionAddThreadViewModelTest { "", 21, emptyList(), + null, emptyMap() ) @@ -127,6 +131,7 @@ class DiscussionAddThreadViewModelTest { "", 21, emptyList(), + null, mapOf("" to DiscussionProfile(ProfileImage("", "", "", "", false))) ) diff --git a/discussion/src/test/java/com/raccoongang/discussion/presentation/threads/DiscussionThreadsViewModelTest.kt b/discussion/src/test/java/com/raccoongang/discussion/presentation/threads/DiscussionThreadsViewModelTest.kt index f377fbf6f..14887621d 100644 --- a/discussion/src/test/java/com/raccoongang/discussion/presentation/threads/DiscussionThreadsViewModelTest.kt +++ b/discussion/src/test/java/com/raccoongang/discussion/presentation/threads/DiscussionThreadsViewModelTest.kt @@ -76,7 +76,10 @@ class DiscussionThreadsViewModelTest { 4, false, false, - mapOf() + mapOf(), + 0, + false, + false ) //endregion @@ -109,11 +112,18 @@ class DiscussionThreadsViewModelTest { "", DiscussionTopicsFragment.ALL_POSTS ) - coEvery { interactor.getAllThreads(any(), any(), any()) } throws UnknownHostException() + coEvery { + interactor.getAllThreads( + any(), + any(), + any(), + any() + ) + } throws UnknownHostException() viewModel.getThreadByType("") advanceUntilIdle() - coVerify(exactly = 1) { interactor.getAllThreads(any(), any(), any()) } + coVerify(exactly = 1) { interactor.getAllThreads(any(), any(), any(), any()) } val message = viewModel.uiMessage.value as? UIMessage.SnackBarMessage assertEquals(noInternet, message?.message) @@ -130,11 +140,11 @@ class DiscussionThreadsViewModelTest { "", DiscussionTopicsFragment.ALL_POSTS ) - coEvery { interactor.getAllThreads(any(), any(), any()) } throws Exception() + coEvery { interactor.getAllThreads(any(), any(), any(), any()) } throws Exception() viewModel.getThreadByType("") advanceUntilIdle() - coVerify(exactly = 1) { interactor.getAllThreads(any(), any(), any()) } + coVerify(exactly = 1) { interactor.getAllThreads(any(), any(), any(), any()) } val message = viewModel.uiMessage.value as? UIMessage.SnackBarMessage assertEquals(somethingWrong, message?.message) @@ -151,12 +161,12 @@ class DiscussionThreadsViewModelTest { "", DiscussionTopicsFragment.ALL_POSTS ) - coEvery { interactor.getAllThreads("", "", range(1, 2)) } returns ThreadsData( + coEvery { interactor.getAllThreads("", "", null, range(1, 2)) } returns ThreadsData( threads, "", Pagination(10, "2", 4, "1") ) - coEvery { interactor.getAllThreads("", "", eq(3)) } returns ThreadsData( + coEvery { interactor.getAllThreads("", "", null, eq(3)) } returns ThreadsData( threads, "", Pagination(10, "", 4, "1") @@ -164,7 +174,7 @@ class DiscussionThreadsViewModelTest { viewModel.getThreadByType("") advanceUntilIdle() - coVerify(exactly = 3) { interactor.getAllThreads(any(), any(), any()) } + coVerify(exactly = 1) { interactor.getAllThreads(any(), any(), any(), any()) } assert(viewModel.uiMessage.value == null) assert(viewModel.isUpdating.value == false) @@ -185,13 +195,14 @@ class DiscussionThreadsViewModelTest { any(), any(), any(), + any(), any() ) } throws UnknownHostException() viewModel.getThreadByType("") advanceUntilIdle() - coVerify(exactly = 1) { interactor.getFollowingThreads(any(), any(), any(), any()) } + coVerify(exactly = 1) { interactor.getFollowingThreads(any(), any(), any(), any(), any()) } val message = viewModel.uiMessage.value as? UIMessage.SnackBarMessage assertEquals(noInternet, message?.message) @@ -208,11 +219,19 @@ class DiscussionThreadsViewModelTest { "", DiscussionTopicsFragment.FOLLOWING_POSTS ) - coEvery { interactor.getFollowingThreads(any(), any(), any(), any()) } throws Exception() + coEvery { + interactor.getFollowingThreads( + any(), + any(), + any(), + any(), + any() + ) + } throws Exception() viewModel.getThreadByType("") advanceUntilIdle() - coVerify(exactly = 1) { interactor.getFollowingThreads(any(), any(), any(), any()) } + coVerify(exactly = 1) { interactor.getFollowingThreads(any(), any(), any(), any(), any()) } val message = viewModel.uiMessage.value as? UIMessage.SnackBarMessage assertEquals(somethingWrong, message?.message) @@ -229,12 +248,20 @@ class DiscussionThreadsViewModelTest { "", DiscussionTopicsFragment.FOLLOWING_POSTS ) - coEvery { interactor.getFollowingThreads("", any(), "", range(1, 2)) } returns ThreadsData( + coEvery { + interactor.getFollowingThreads( + "", + any(), + "", + null, + range(1, 2) + ) + } returns ThreadsData( threads, "", Pagination(10, "2", 4, "1") ) - coEvery { interactor.getFollowingThreads("", any(), "", eq(3)) } returns ThreadsData( + coEvery { interactor.getFollowingThreads("", any(), "", null, eq(3)) } returns ThreadsData( threads, "", Pagination(10, "", 4, "1") @@ -242,7 +269,7 @@ class DiscussionThreadsViewModelTest { viewModel.getThreadByType("") advanceUntilIdle() - coVerify(exactly = 3) { interactor.getFollowingThreads(any(), any(), any(), any()) } + coVerify(exactly = 1) { interactor.getFollowingThreads(any(), any(), any(), any(), any()) } assert(viewModel.uiMessage.value == null) assert(viewModel.isUpdating.value == false) @@ -258,11 +285,19 @@ class DiscussionThreadsViewModelTest { "", DiscussionTopicsFragment.TOPIC ) - coEvery { interactor.getThreads(any(), any(), any(), any()) } throws UnknownHostException() + coEvery { + interactor.getThreads( + any(), + any(), + any(), + any(), + any() + ) + } throws UnknownHostException() viewModel.getThreadByType("") advanceUntilIdle() - coVerify(exactly = 1) { interactor.getThreads(any(), any(), any(), any()) } + coVerify(exactly = 1) { interactor.getThreads(any(), any(), any(), any(), any()) } val message = viewModel.uiMessage.value as? UIMessage.SnackBarMessage assertEquals(noInternet, message?.message) @@ -279,11 +314,11 @@ class DiscussionThreadsViewModelTest { "", DiscussionTopicsFragment.TOPIC ) - coEvery { interactor.getThreads(any(), any(), any(), any()) } throws Exception() + coEvery { interactor.getThreads(any(), any(), any(), any(), any()) } throws Exception() viewModel.getThreadByType("") advanceUntilIdle() - coVerify(exactly = 1) { interactor.getThreads(any(), any(), any(), any()) } + coVerify(exactly = 1) { interactor.getThreads(any(), any(), any(), any(), any()) } val message = viewModel.uiMessage.value as? UIMessage.SnackBarMessage assertEquals(somethingWrong, message?.message) @@ -300,12 +335,12 @@ class DiscussionThreadsViewModelTest { "", DiscussionTopicsFragment.TOPIC ) - coEvery { interactor.getThreads("", any(), "", range(1, 2)) } returns ThreadsData( + coEvery { interactor.getThreads("", any(), "", null, range(1, 2)) } returns ThreadsData( threads, "", Pagination(10, "2", 4, "1") ) - coEvery { interactor.getThreads("", any(), "", eq(3)) } returns ThreadsData( + coEvery { interactor.getThreads("", any(), "", null, eq(3)) } returns ThreadsData( threads, "", Pagination(10, "", 4, "1") @@ -313,7 +348,7 @@ class DiscussionThreadsViewModelTest { viewModel.getThreadByType("") advanceUntilIdle() - coVerify(exactly = 3) { interactor.getThreads(any(), any(), any(), any()) } + coVerify(exactly = 1) { interactor.getThreads(any(), any(), any(), any(), any()) } assert(viewModel.uiMessage.value == null) assert(viewModel.isUpdating.value == false) @@ -329,6 +364,11 @@ class DiscussionThreadsViewModelTest { "", DiscussionTopicsFragment.TOPIC ) + coEvery { interactor.getThreads(any(),any(), any(), any(), any()) } returns ThreadsData( + threads, + "", + pagination = Pagination(10,"", 2,"") + ) viewModel.filterThreads(FilterType.ALL_POSTS.value) advanceUntilIdle() assert(viewModel.uiState.value is DiscussionThreadsUIState.Threads) @@ -343,6 +383,11 @@ class DiscussionThreadsViewModelTest { "", DiscussionTopicsFragment.TOPIC ) + coEvery { interactor.getThreads(any(),any(), any(), any(), any()) } returns ThreadsData( + threads, + "", + pagination = Pagination(10,"", 2,"") + ) viewModel.filterThreads(FilterType.UNREAD.value) advanceUntilIdle() assert(viewModel.uiState.value is DiscussionThreadsUIState.Threads) @@ -357,6 +402,11 @@ class DiscussionThreadsViewModelTest { "", DiscussionTopicsFragment.TOPIC ) + coEvery { interactor.getThreads(any(),any(), any(), any(), any()) } returns ThreadsData( + threads, + "", + pagination = Pagination(10,"", 2,"") + ) viewModel.filterThreads(FilterType.UNANSWERED.value) advanceUntilIdle() assert(viewModel.uiState.value is DiscussionThreadsUIState.Threads) @@ -371,12 +421,12 @@ class DiscussionThreadsViewModelTest { "", DiscussionTopicsFragment.TOPIC ) - coEvery { interactor.getThreads("", any(), "", range(1, 2)) } returns ThreadsData( + coEvery { interactor.getThreads("", any(), "", null, range(1, 2)) } returns ThreadsData( threads, "", Pagination(10, "2", 4, "1") ) - coEvery { interactor.getThreads("", any(), "", eq(3)) } returns ThreadsData( + coEvery { interactor.getThreads("", any(), "", null, eq(3)) } returns ThreadsData( threads, "", Pagination(10, "", 4, "1") @@ -384,7 +434,7 @@ class DiscussionThreadsViewModelTest { viewModel.updateThread("") advanceUntilIdle() - coVerify(exactly = 3) { interactor.getThreads(any(), any(), any(), any()) } + coVerify(exactly = 1) { interactor.getThreads(any(), any(), any(), any(), any()) } assert(viewModel.uiMessage.value == null) assert(viewModel.isUpdating.value == false) @@ -400,12 +450,12 @@ class DiscussionThreadsViewModelTest { "", DiscussionTopicsFragment.TOPIC ) - coEvery { interactor.getThreads("", any(), any(), range(1, 2)) } returns ThreadsData( + coEvery { interactor.getThreads("", any(), any(), null, range(1, 2)) } returns ThreadsData( threads, "", Pagination(10, "2", 4, "1") ) - coEvery { interactor.getThreads("", any(), any(), eq(3)) } returns ThreadsData( + coEvery { interactor.getThreads("", any(), any(), null, eq(3)) } returns ThreadsData( threads, "", Pagination(10, "", 4, "1") @@ -425,7 +475,7 @@ class DiscussionThreadsViewModelTest { viewModel.updateThread("date") advanceUntilIdle() - coVerify(exactly = 6) { interactor.getThreads(any(), any(), any(), any()) } + coVerify(exactly = 2) { interactor.getThreads(any(), any(), any(), any(), any()) } } @Test @@ -437,12 +487,12 @@ class DiscussionThreadsViewModelTest { "", DiscussionTopicsFragment.TOPIC ) - coEvery { interactor.getThreads("", any(), any(), range(1, 2)) } returns ThreadsData( + coEvery { interactor.getThreads("", any(), any(), null,range(1, 2)) } returns ThreadsData( threads, "", Pagination(10, "2", 4, "1") ) - coEvery { interactor.getThreads("", any(), any(), eq(3)) } returns ThreadsData( + coEvery { interactor.getThreads("", any(), any(), null,eq(3)) } returns ThreadsData( threads, "", Pagination(10, "", 4, "1") @@ -462,7 +512,7 @@ class DiscussionThreadsViewModelTest { viewModel.updateThread("date") advanceUntilIdle() - coVerify(exactly = 3) { interactor.getThreads(any(), any(), any(), any()) } + coVerify(exactly = 1) { interactor.getThreads(any(), any(), any(), any(), any()) } } diff --git a/profile/src/test/java/com/raccoongang/profile/presentation/edit/EditProfileViewModelTest.kt b/profile/src/test/java/com/raccoongang/profile/presentation/edit/EditProfileViewModelTest.kt index b687b798b..41ec01e2e 100644 --- a/profile/src/test/java/com/raccoongang/profile/presentation/edit/EditProfileViewModelTest.kt +++ b/profile/src/test/java/com/raccoongang/profile/presentation/edit/EditProfileViewModelTest.kt @@ -4,13 +4,12 @@ import androidx.arch.core.executor.testing.InstantTaskExecutorRule import com.raccoongang.core.R import com.raccoongang.core.UIMessage import com.raccoongang.core.domain.model.Account +import com.raccoongang.core.domain.model.ProfileImage import com.raccoongang.core.system.ResourceManager import com.raccoongang.profile.domain.interactor.ProfileInteractor +import com.raccoongang.profile.system.notifier.AccountUpdated import com.raccoongang.profile.system.notifier.ProfileNotifier -import io.mockk.coEvery -import io.mockk.coVerify -import io.mockk.every -import io.mockk.mockk +import io.mockk.* import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.* @@ -35,7 +34,24 @@ class EditProfileViewModelTest { private val interactor = mockk() private val notifier = mockk() - private val account = mockk() + private val account = Account( + username = "thom84", + bio = "He as compliment unreserved projecting. Between had observe pretend delight for believe. Do newspaper questions consulted sweetness do. Our sportsman his unwilling fulfilled departure law.", + requiresParentalConsent = true, + name = "Thomas", + country = "Ukraine", + isActive = true, + profileImage = ProfileImage("", "", "", "", false), + yearOfBirth = 2000, + levelOfEducation = "Bachelor", + goals = "130", + languageProficiencies = emptyList(), + gender = "male", + mailingAddress = "", + "", + null, + accountPrivacy = Account.Privacy.ALL_USERS + ) private val file = mockk() private val noInternet = "Slow or no internet connection" @@ -57,7 +73,6 @@ class EditProfileViewModelTest { fun `updateAccount no internet connection`() = runTest { val viewModel = EditProfileViewModel(interactor, resourceManager, notifier, account) coEvery { interactor.updateAccount(any()) } throws UnknownHostException() - every { account.copy() } returns account viewModel.updateAccount(emptyMap()) advanceUntilIdle() @@ -72,7 +87,7 @@ class EditProfileViewModelTest { fun `updateAccount unknown exception`() = runTest { val viewModel = EditProfileViewModel(interactor, resourceManager, notifier, account) coEvery { interactor.updateAccount(any()) } throws Exception() - every { account.copy() } returns account + viewModel.updateAccount(emptyMap()) advanceUntilIdle() @@ -87,8 +102,7 @@ class EditProfileViewModelTest { fun `updateAccount success`() = runTest { val viewModel = EditProfileViewModel(interactor, resourceManager, notifier, account) coEvery { interactor.updateAccount(any()) } returns account - coEvery { notifier.send(any()) } returns Unit - every { account.copy() } returns account + coEvery { notifier.send(any()) } returns Unit viewModel.updateAccount(emptyMap()) advanceUntilIdle() @@ -103,8 +117,8 @@ class EditProfileViewModelTest { val viewModel = EditProfileViewModel(interactor, resourceManager, notifier, account) coEvery { interactor.setProfileImage(any(), any()) } throws UnknownHostException() coEvery { interactor.updateAccount(any()) } returns account - coEvery { notifier.send(any()) } returns Unit - every { account.copy() } returns account + coEvery { notifier.send(AccountUpdated()) } returns Unit + viewModel.updateAccountAndImage(emptyMap(), file, "") advanceUntilIdle() @@ -122,8 +136,8 @@ class EditProfileViewModelTest { val viewModel = EditProfileViewModel(interactor, resourceManager, notifier, account) coEvery { interactor.setProfileImage(any(), any()) } throws Exception() coEvery { interactor.updateAccount(any()) } returns account - coEvery { notifier.send(any()) } returns Unit - every { account.copy() } returns account + coEvery { notifier.send(AccountUpdated()) } returns Unit + viewModel.updateAccountAndImage(emptyMap(), file, "") advanceUntilIdle() @@ -141,8 +155,8 @@ class EditProfileViewModelTest { val viewModel = EditProfileViewModel(interactor, resourceManager, notifier, account) coEvery { interactor.setProfileImage(any(), any()) } returns Unit coEvery { interactor.updateAccount(any()) } returns account - coEvery { notifier.send(any()) } returns Unit - every { account.copy() } returns account + coEvery { notifier.send(any()) } returns Unit + viewModel.updateAccountAndImage(emptyMap(), file, "") advanceUntilIdle() diff --git a/profile/src/test/java/com/raccoongang/profile/presentation/profile/ProfileViewModelTest.kt b/profile/src/test/java/com/raccoongang/profile/presentation/profile/ProfileViewModelTest.kt index 33fe425e1..ae7a671fe 100644 --- a/profile/src/test/java/com/raccoongang/profile/presentation/profile/ProfileViewModelTest.kt +++ b/profile/src/test/java/com/raccoongang/profile/presentation/profile/ProfileViewModelTest.kt @@ -8,6 +8,7 @@ import com.raccoongang.core.R import com.raccoongang.core.UIMessage import com.raccoongang.core.data.storage.PreferencesManager import com.raccoongang.core.domain.model.Account +import com.raccoongang.core.module.DownloadWorkerController import com.raccoongang.core.system.AppCookieManager import com.raccoongang.core.system.ResourceManager import com.raccoongang.profile.domain.interactor.ProfileInteractor @@ -43,6 +44,7 @@ class ProfileViewModelTest { private val interactor = mockk() private val notifier = mockk() private val cookieManager = mockk() + private val workerController = mockk() private val account = mockk() @@ -64,7 +66,15 @@ class ProfileViewModelTest { @Test fun `getAccount no internetConnection`() = runTest { val viewModel = - ProfileViewModel(interactor, preferencesManager, resourceManager, notifier, dispatcher, cookieManager) + ProfileViewModel( + interactor, + preferencesManager, + resourceManager, + notifier, + dispatcher, + cookieManager, + workerController + ) coEvery { interactor.getAccount() } throws UnknownHostException() advanceUntilIdle() @@ -78,7 +88,15 @@ class ProfileViewModelTest { @Test fun `getAccount unknown exception`() = runTest { val viewModel = - ProfileViewModel(interactor, preferencesManager, resourceManager, notifier, dispatcher, cookieManager) + ProfileViewModel( + interactor, + preferencesManager, + resourceManager, + notifier, + dispatcher, + cookieManager, + workerController + ) coEvery { interactor.getAccount() } throws Exception() advanceUntilIdle() @@ -92,7 +110,15 @@ class ProfileViewModelTest { @Test fun `getAccount success`() = runTest { val viewModel = - ProfileViewModel(interactor, preferencesManager, resourceManager, notifier, dispatcher, cookieManager) + ProfileViewModel( + interactor, + preferencesManager, + resourceManager, + notifier, + dispatcher, + cookieManager, + workerController + ) coEvery { interactor.getAccount() } returns account every { preferencesManager.profile = any() } returns Unit advanceUntilIdle() @@ -106,8 +132,17 @@ class ProfileViewModelTest { @Test fun `logout no internet connection`() = runTest { val viewModel = - ProfileViewModel(interactor, preferencesManager, resourceManager, notifier, dispatcher, cookieManager) + ProfileViewModel( + interactor, + preferencesManager, + resourceManager, + notifier, + dispatcher, + cookieManager, + workerController + ) coEvery { interactor.logout() } throws UnknownHostException() + coEvery { workerController.cancelWork() } returns Unit viewModel.logout() advanceUntilIdle() @@ -122,8 +157,17 @@ class ProfileViewModelTest { @Test fun `logout unknown exception`() = runTest { val viewModel = - ProfileViewModel(interactor, preferencesManager, resourceManager, notifier, dispatcher, cookieManager) + ProfileViewModel( + interactor, + preferencesManager, + resourceManager, + notifier, + dispatcher, + cookieManager, + workerController + ) coEvery { interactor.logout() } throws Exception() + coEvery { workerController.cancelWork() } returns Unit viewModel.logout() advanceUntilIdle() @@ -142,11 +186,13 @@ class ProfileViewModelTest { resourceManager, notifier, dispatcherIO, - cookieManager + cookieManager, + workerController ) coEvery { interactor.getAccount() } returns mockk() every { preferencesManager.profile = any() } returns Unit coEvery { interactor.logout() } returns Unit + coEvery { workerController.cancelWork() } returns Unit every { cookieManager.clearWebViewCookie() } returns Unit viewModel.logout() advanceUntilIdle() @@ -165,7 +211,8 @@ class ProfileViewModelTest { resourceManager, notifier, dispatcherIO, - cookieManager + cookieManager, + workerController ) every { notifier.notifier } returns flow { emit(AccountUpdated()) } val mockLifeCycleOwner: LifecycleOwner = mockk()