diff --git a/core/testing/src/main/java/kr/co/testing/repository/TestRecentRepository.kt b/core/testing/src/main/java/kr/co/testing/repository/TestRecentRepository.kt deleted file mode 100644 index 72f04c0..0000000 --- a/core/testing/src/main/java/kr/co/testing/repository/TestRecentRepository.kt +++ /dev/null @@ -1,24 +0,0 @@ -package kr.co.testing.repository - -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.MutableStateFlow -import kotlinx.coroutines.flow.update -import kr.co.data.repository.RecentRepository -import kr.co.model.FileInfo - -class TestRecentRepository: RecentRepository { - - private val recentFilesFlow: MutableStateFlow> = - MutableStateFlow(emptyList()) - - override suspend fun insert(recentFile: FileInfo) { - recentFilesFlow.update { it + recentFile } - } - - override fun get(): Flow> = - recentFilesFlow - - override suspend fun delete(recentFile: FileInfo) { - recentFilesFlow.update { it - recentFile } - } -} \ No newline at end of file diff --git a/feature/bookmark/src/test/kotlin/kr/co/bookmark/BookmarkViewModelTest.kt b/feature/bookmark/src/test/kotlin/kr/co/bookmark/BookmarkViewModelTest.kt new file mode 100644 index 0000000..054fb5e --- /dev/null +++ b/feature/bookmark/src/test/kotlin/kr/co/bookmark/BookmarkViewModelTest.kt @@ -0,0 +1,92 @@ +package kr.co.bookmark + +import app.cash.turbine.test +import io.mockk.MockKAnnotations +import io.mockk.coEvery +import io.mockk.impl.annotations.MockK +import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.test.runTest +import kr.co.data.repository.BookmarkRepository +import kr.co.data.repository.RecentRepository +import kr.co.model.BookmarkSideEffect +import kr.co.model.BookmarkUiIntent +import kr.co.model.FileInfo +import kr.co.model.FileInfo.Type.PDF +import kr.co.testing.rule.CoroutineTestRule +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import java.time.LocalDateTime +import kotlin.test.assertEquals + +internal class BookmarkViewModelTest { + + @get: Rule + val coroutineTestRule = CoroutineTestRule() + + @MockK + private lateinit var bookmarkRepository: BookmarkRepository + + @MockK + private lateinit var recentRepository: RecentRepository + + private lateinit var viewModel: BookmarkViewModel + + @Before + fun setup() { + MockKAnnotations.init(this) + viewModel = BookmarkViewModel(bookmarkRepository,recentRepository) + } + + @Test + fun `Given a Unit when Init intent is handled then state is updated`() = runTest { + val dummy = listOf(PDF_DUMMY, PDF_DUMMY) + + coEvery { bookmarkRepository.get() } returns flowOf(dummy) + + viewModel.handleIntent(BookmarkUiIntent.Init) + + viewModel.uiState.test { + val state = awaitItem() + assertEquals(state.files, dummy) + } + } + + @Test + fun `Given a file when ClickFile intent is handled then navigate to pdf`() = runTest { + val file = PDF_DUMMY + + viewModel.handleIntent(BookmarkUiIntent.ClickFile(file)) + + viewModel.sideEffect.test { + awaitItem().also { + assert(it is BookmarkSideEffect.NavigateToPdf) + assert((it as BookmarkSideEffect.NavigateToPdf).path == file.path) + } + } + } + + companion object { + val PDF_DUMMY = FileInfo( + name = "DUMMY.pdf", + path = "", + type = PDF, + isDirectory = false, + isHidden = false, + size = 0, + createdAt = LocalDateTime.now(), + lastModified = LocalDateTime.now() + ) + + val FOLDER_DUMMY = FileInfo( + name = "DUMMY", + path = "", + type = PDF, + isDirectory = true, + isHidden = false, + size = 0, + createdAt = LocalDateTime.now(), + lastModified = LocalDateTime.now() + ) + } +} \ No newline at end of file diff --git a/feature/explore/src/test/kotlin/kr/co/explore/ExploreViewModelTest.kt b/feature/explore/src/test/kotlin/kr/co/explore/ExploreViewModelTest.kt index 8b44da1..4406134 100644 --- a/feature/explore/src/test/kotlin/kr/co/explore/ExploreViewModelTest.kt +++ b/feature/explore/src/test/kotlin/kr/co/explore/ExploreViewModelTest.kt @@ -5,11 +5,11 @@ import io.mockk.MockKAnnotations import io.mockk.coEvery import io.mockk.impl.annotations.MockK import kotlinx.coroutines.test.runTest +import kr.co.data.repository.RecentRepository import kr.co.model.ExploreSideEffect import kr.co.model.ExploreUiIntent import kr.co.model.FileInfo import kr.co.model.FileInfo.Type.PDF -import kr.co.testing.repository.TestRecentRepository import kr.co.testing.rule.CoroutineTestRule import kr.co.util.FileManager import org.junit.Before @@ -26,7 +26,8 @@ class ExploreViewModelTest { private lateinit var viewModel: ExploreViewModel - private val recentRepository = TestRecentRepository() + @MockK + private lateinit var recentRepository: RecentRepository @MockK private lateinit var fileManager: FileManager @@ -93,7 +94,6 @@ class ExploreViewModelTest { } } } - companion object { val PDF_DUMMY = FileInfo( name = "DUMMY.pdf",