From 411f2d2ec1961a4f439c7b885e2292d6a4ee0ae0 Mon Sep 17 00:00:00 2001 From: Guri999 <116724657+Guri999@users.noreply.github.com> Date: Mon, 13 Jan 2025 08:56:41 +0900 Subject: [PATCH 1/3] Feat: create BookmarkViewModelTest --- .../java/kr/co/testing/dummy/FileInfoDummy.kt | 27 ++++++++++ .../repository/TestBookmarkRepository.kt | 24 +++++++++ .../kr/co/bookmark/BookmarkViewModelTest.kt | 53 +++++++++++++++++++ .../kr/co/explore/ExploreViewModelTest.kt | 26 +-------- 4 files changed, 106 insertions(+), 24 deletions(-) create mode 100644 core/testing/src/main/java/kr/co/testing/dummy/FileInfoDummy.kt create mode 100644 core/testing/src/main/java/kr/co/testing/repository/TestBookmarkRepository.kt create mode 100644 feature/bookmark/src/test/kotlin/kr/co/bookmark/BookmarkViewModelTest.kt diff --git a/core/testing/src/main/java/kr/co/testing/dummy/FileInfoDummy.kt b/core/testing/src/main/java/kr/co/testing/dummy/FileInfoDummy.kt new file mode 100644 index 0000000..f7e93e0 --- /dev/null +++ b/core/testing/src/main/java/kr/co/testing/dummy/FileInfoDummy.kt @@ -0,0 +1,27 @@ +package kr.co.testing.dummy + +import kr.co.model.FileInfo +import kr.co.model.FileInfo.Type.PDF +import java.time.LocalDateTime + +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/core/testing/src/main/java/kr/co/testing/repository/TestBookmarkRepository.kt b/core/testing/src/main/java/kr/co/testing/repository/TestBookmarkRepository.kt new file mode 100644 index 0000000..9b98018 --- /dev/null +++ b/core/testing/src/main/java/kr/co/testing/repository/TestBookmarkRepository.kt @@ -0,0 +1,24 @@ +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.BookmarkRepository +import kr.co.model.FileInfo + +class TestBookmarkRepository: BookmarkRepository { + + private val bookmarkFilesFlow: MutableStateFlow> = + MutableStateFlow(emptyList()) + + override suspend fun insert(bookmarkFile: FileInfo) { + bookmarkFilesFlow.update { it + bookmarkFile } + } + + override fun get(): Flow> = + bookmarkFilesFlow + + override suspend fun delete(bookmarkFile: FileInfo) { + bookmarkFilesFlow.update { it - bookmarkFile } + } +} \ 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..f9a365f --- /dev/null +++ b/feature/bookmark/src/test/kotlin/kr/co/bookmark/BookmarkViewModelTest.kt @@ -0,0 +1,53 @@ +package kr.co.bookmark + +import app.cash.turbine.test +import kotlinx.coroutines.test.runTest +import kr.co.model.BookmarkSideEffect +import kr.co.model.BookmarkUiIntent +import kr.co.testing.dummy.PDF_DUMMY +import kr.co.testing.repository.TestBookmarkRepository +import kr.co.testing.repository.TestRecentRepository +import kr.co.testing.rule.CoroutineTestRule +import org.junit.Before +import org.junit.Rule +import org.junit.Test + +internal class BookmarkViewModelTest { + + @get: Rule + val coroutineTestRule = CoroutineTestRule() + + private val bookmarkRepository = TestBookmarkRepository() + private val recentRepository = TestRecentRepository() + + private lateinit var viewModel: BookmarkViewModel + + @Before + fun setup() { + viewModel = BookmarkViewModel(bookmarkRepository,recentRepository) + } + + @Test + fun `Given a Unit when Init intent is handled then state is updated`() = runTest { + viewModel.handleIntent(BookmarkUiIntent.Init) + + viewModel.uiState.test { + val state = awaitItem() + assert(state.files.isEmpty()) + } + } + + @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) + } + } + } +} \ 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..4a5e059 100644 --- a/feature/explore/src/test/kotlin/kr/co/explore/ExploreViewModelTest.kt +++ b/feature/explore/src/test/kotlin/kr/co/explore/ExploreViewModelTest.kt @@ -9,6 +9,8 @@ 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.dummy.FOLDER_DUMMY +import kr.co.testing.dummy.PDF_DUMMY import kr.co.testing.repository.TestRecentRepository import kr.co.testing.rule.CoroutineTestRule import kr.co.util.FileManager @@ -93,28 +95,4 @@ class ExploreViewModelTest { } } } - - 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 From 5a3bf6a7b834b8afd6b81942bcdc6abff680b5c4 Mon Sep 17 00:00:00 2001 From: Guri999 <116724657+Guri999@users.noreply.github.com> Date: Mon, 13 Jan 2025 13:54:12 +0900 Subject: [PATCH 2/3] =?UTF-8?q?Refactor:=20FileInfo=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/kr/co/testing/dummy/FileInfoDummy.kt | 27 ------------------ .../kr/co/bookmark/BookmarkViewModelTest.kt | 28 ++++++++++++++++++- .../kr/co/explore/ExploreViewModelTest.kt | 25 +++++++++++++++-- 3 files changed, 50 insertions(+), 30 deletions(-) delete mode 100644 core/testing/src/main/java/kr/co/testing/dummy/FileInfoDummy.kt diff --git a/core/testing/src/main/java/kr/co/testing/dummy/FileInfoDummy.kt b/core/testing/src/main/java/kr/co/testing/dummy/FileInfoDummy.kt deleted file mode 100644 index f7e93e0..0000000 --- a/core/testing/src/main/java/kr/co/testing/dummy/FileInfoDummy.kt +++ /dev/null @@ -1,27 +0,0 @@ -package kr.co.testing.dummy - -import kr.co.model.FileInfo -import kr.co.model.FileInfo.Type.PDF -import java.time.LocalDateTime - -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/bookmark/src/test/kotlin/kr/co/bookmark/BookmarkViewModelTest.kt b/feature/bookmark/src/test/kotlin/kr/co/bookmark/BookmarkViewModelTest.kt index f9a365f..461e746 100644 --- a/feature/bookmark/src/test/kotlin/kr/co/bookmark/BookmarkViewModelTest.kt +++ b/feature/bookmark/src/test/kotlin/kr/co/bookmark/BookmarkViewModelTest.kt @@ -4,13 +4,15 @@ import app.cash.turbine.test import kotlinx.coroutines.test.runTest import kr.co.model.BookmarkSideEffect import kr.co.model.BookmarkUiIntent -import kr.co.testing.dummy.PDF_DUMMY +import kr.co.model.FileInfo +import kr.co.model.FileInfo.Type.PDF import kr.co.testing.repository.TestBookmarkRepository import kr.co.testing.repository.TestRecentRepository import kr.co.testing.rule.CoroutineTestRule import org.junit.Before import org.junit.Rule import org.junit.Test +import java.time.LocalDateTime internal class BookmarkViewModelTest { @@ -50,4 +52,28 @@ internal class BookmarkViewModelTest { } } } + + 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 4a5e059..d8cd947 100644 --- a/feature/explore/src/test/kotlin/kr/co/explore/ExploreViewModelTest.kt +++ b/feature/explore/src/test/kotlin/kr/co/explore/ExploreViewModelTest.kt @@ -9,8 +9,6 @@ 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.dummy.FOLDER_DUMMY -import kr.co.testing.dummy.PDF_DUMMY import kr.co.testing.repository.TestRecentRepository import kr.co.testing.rule.CoroutineTestRule import kr.co.util.FileManager @@ -95,4 +93,27 @@ class ExploreViewModelTest { } } } + 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 From 6f85904726ed212828bdc0071b016686c6a62fac Mon Sep 17 00:00:00 2001 From: Guri999 <116724657+Guri999@users.noreply.github.com> Date: Tue, 14 Jan 2025 09:42:41 +0900 Subject: [PATCH 3/3] Refactor: Delete Test Repository --- .../repository/TestBookmarkRepository.kt | 24 ------------------- .../repository/TestRecentRepository.kt | 24 ------------------- .../kr/co/bookmark/BookmarkViewModelTest.kt | 23 ++++++++++++++---- .../kr/co/explore/ExploreViewModelTest.kt | 5 ++-- 4 files changed, 21 insertions(+), 55 deletions(-) delete mode 100644 core/testing/src/main/java/kr/co/testing/repository/TestBookmarkRepository.kt delete mode 100644 core/testing/src/main/java/kr/co/testing/repository/TestRecentRepository.kt diff --git a/core/testing/src/main/java/kr/co/testing/repository/TestBookmarkRepository.kt b/core/testing/src/main/java/kr/co/testing/repository/TestBookmarkRepository.kt deleted file mode 100644 index 9b98018..0000000 --- a/core/testing/src/main/java/kr/co/testing/repository/TestBookmarkRepository.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.BookmarkRepository -import kr.co.model.FileInfo - -class TestBookmarkRepository: BookmarkRepository { - - private val bookmarkFilesFlow: MutableStateFlow> = - MutableStateFlow(emptyList()) - - override suspend fun insert(bookmarkFile: FileInfo) { - bookmarkFilesFlow.update { it + bookmarkFile } - } - - override fun get(): Flow> = - bookmarkFilesFlow - - override suspend fun delete(bookmarkFile: FileInfo) { - bookmarkFilesFlow.update { it - bookmarkFile } - } -} \ No newline at end of file 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 index 461e746..054fb5e 100644 --- a/feature/bookmark/src/test/kotlin/kr/co/bookmark/BookmarkViewModelTest.kt +++ b/feature/bookmark/src/test/kotlin/kr/co/bookmark/BookmarkViewModelTest.kt @@ -1,41 +1,54 @@ 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.repository.TestBookmarkRepository -import kr.co.testing.repository.TestRecentRepository 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() - private val bookmarkRepository = TestBookmarkRepository() - private val recentRepository = TestRecentRepository() + @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() - assert(state.files.isEmpty()) + assertEquals(state.files, dummy) } } 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 d8cd947..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