From cb8243f8b1720f7ffaf9e27fd3287d7596cf031a Mon Sep 17 00:00:00 2001 From: Wooyeol Lee Date: Mon, 4 Dec 2023 22:23:28 +0900 Subject: [PATCH] fix: resolve EmojiViewModelTest failure --- .../emojihub/usecases/EmojiUseCaseImplTest.kt | 8 ++--- .../test/java/com/goliath/emojihub/utils.kt | 10 +++---- .../emojihub/viewmodels/EmojiViewModelTest.kt | 29 +++++++++++++++---- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/android/app/src/test/java/com/goliath/emojihub/usecases/EmojiUseCaseImplTest.kt b/android/app/src/test/java/com/goliath/emojihub/usecases/EmojiUseCaseImplTest.kt index 3058110a..596065f3 100644 --- a/android/app/src/test/java/com/goliath/emojihub/usecases/EmojiUseCaseImplTest.kt +++ b/android/app/src/test/java/com/goliath/emojihub/usecases/EmojiUseCaseImplTest.kt @@ -3,7 +3,7 @@ package com.goliath.emojihub.usecases import androidx.paging.PagingData import androidx.paging.map import androidx.paging.testing.asSnapshot -import com.goliath.emojihub.createDeterministicDummyEmojiDtoList +import com.goliath.emojihub.createDeterministicTrendingEmojiDtoList import com.goliath.emojihub.data_sources.ApiErrorController import com.goliath.emojihub.mockLogClass import com.goliath.emojihub.models.CreatedEmoji @@ -72,7 +72,7 @@ class EmojiUseCaseImplTest { @Test fun fetchEmojiList_returnsFlowOfEmojiPagingData() { // given - val sampleEmojiPagingDataFlow = createDeterministicDummyEmojiDtoList(5) + val sampleEmojiPagingDataFlow = createDeterministicTrendingEmojiDtoList(5) val sampleAnswer = sampleEmojiPagingDataFlow.map { it.map { dto -> Emoji(dto) } } coEvery { emojiRepository.fetchEmojiList(1) @@ -96,7 +96,7 @@ class EmojiUseCaseImplTest { @Test fun fetchMyCreatedEmojiList_returnsFlowOfEmojiPagingData() { // given - val sampleEmojiPagingDataFlow = createDeterministicDummyEmojiDtoList(5) + val sampleEmojiPagingDataFlow = createDeterministicTrendingEmojiDtoList(5) val sampleAnswer = sampleEmojiPagingDataFlow.map { it.map { dto -> Emoji(dto) } } coEvery { emojiRepository.fetchMyCreatedEmojiList() @@ -120,7 +120,7 @@ class EmojiUseCaseImplTest { @Test fun fetchMySavedEmojiList_returnsFlowOfEmojiPagingData() { // given - val sampleEmojiPagingDataFlow = createDeterministicDummyEmojiDtoList(5) + val sampleEmojiPagingDataFlow = createDeterministicTrendingEmojiDtoList(5) val sampleAnswer = sampleEmojiPagingDataFlow.map { it.map { dto -> Emoji(dto) } } coEvery { emojiRepository.fetchMySavedEmojiList() diff --git a/android/app/src/test/java/com/goliath/emojihub/utils.kt b/android/app/src/test/java/com/goliath/emojihub/utils.kt index 77048eb1..d2d44873 100644 --- a/android/app/src/test/java/com/goliath/emojihub/utils.kt +++ b/android/app/src/test/java/com/goliath/emojihub/utils.kt @@ -26,14 +26,14 @@ fun mockLogClass() { val dummyUsernames = listOf("channn", "doggydog", "meow_0w0", "mpunchmm", "kick_back") val dummyUnicodes = listOf("U+1F44D", "U+1F600", "U+1F970", "U+1F60E", "U+1F621", "U+1F63A", "U+1F496", "U+1F415") const val dummyMaxSavedCounts = 2000 -fun createDeterministicDummyEmojiDtoList(listSize : Int): Flow> { +fun createDeterministicTrendingEmojiDtoList(listSize : Int): Flow> { val dummyEmojiList = mutableListOf() for (i in 0 until listSize) { dummyEmojiList.add( EmojiDto( createdBy = dummyUsernames[i % dummyUsernames.size], - createdAt = "2023.09.16", - savedCount = dummyMaxSavedCounts % (i + 1), + createdAt = "2023."+i%12+".16", + savedCount = dummyMaxSavedCounts - i*10, videoLink = "", thumbnailLink = "", unicode = dummyUnicodes[i % dummyUnicodes.size], @@ -44,8 +44,8 @@ fun createDeterministicDummyEmojiDtoList(listSize : Int): Flow> { - return createDeterministicDummyEmojiDtoList(listSize).map { it.map { dto -> Emoji(dto) } } +fun createDeterministicTrendingEmojiList(listSize: Int): Flow> { + return createDeterministicTrendingEmojiDtoList(listSize).map { it.map { dto -> Emoji(dto) } } } // POST TESTING UTILS diff --git a/android/app/src/test/java/com/goliath/emojihub/viewmodels/EmojiViewModelTest.kt b/android/app/src/test/java/com/goliath/emojihub/viewmodels/EmojiViewModelTest.kt index 1160ab0d..1c0afe34 100644 --- a/android/app/src/test/java/com/goliath/emojihub/viewmodels/EmojiViewModelTest.kt +++ b/android/app/src/test/java/com/goliath/emojihub/viewmodels/EmojiViewModelTest.kt @@ -1,7 +1,7 @@ package com.goliath.emojihub.viewmodels import android.net.Uri -import com.goliath.emojihub.createDeterministicDummyEmojiList +import com.goliath.emojihub.createDeterministicTrendingEmojiList import com.goliath.emojihub.mockLogClass import com.goliath.emojihub.models.CreatedEmoji import com.goliath.emojihub.usecases.EmojiUseCase @@ -44,24 +44,41 @@ class EmojiViewModelTest { } @Test - fun fetchEmojiList_success_updateEmojiList() = runTest { + fun fetchEmojiList_success_updateTrendingEmojiList() = runTest { // given - val sampleFetchedEmojiList = createDeterministicDummyEmojiList(10) + val sampleFetchedEmojiList = createDeterministicTrendingEmojiList(10) coEvery { - emojiUseCase.fetchEmojiList(1) + emojiUseCase.fetchEmojiList(0) } returns sampleFetchedEmojiList // when emojiViewModel.fetchEmojiList() advanceUntilIdle() // then + coVerify(exactly = 1) { emojiUseCase.fetchEmojiList(0) } + coVerify(exactly = 1) { emojiUseCase.updateEmojiList(any()) } + } + + @Test + fun toggleSortingMode_success_updateTrendingEmojiList() = runTest { + // given + // for simplicity of testing, we return the same list for both cases + val sampleFetchedEmojiList = createDeterministicTrendingEmojiList(10) + coEvery { + emojiUseCase.fetchEmojiList(1) + } returns sampleFetchedEmojiList + // when + emojiViewModel.toggleSortingMode() + advanceUntilIdle() + // then coVerify(exactly = 1) { emojiUseCase.fetchEmojiList(1) } coVerify(exactly = 1) { emojiUseCase.updateEmojiList(any()) } + assertEquals(1, emojiViewModel.sortByDate) } @Test fun fetchMyCreatedEmojiList_success_updateMyCreatedEmojiList() = runTest { // given - val sampleFetchedMyCreatedEmojiList = createDeterministicDummyEmojiList(10) + val sampleFetchedMyCreatedEmojiList = createDeterministicTrendingEmojiList(10) coEvery { emojiUseCase.fetchMyCreatedEmojiList() } returns sampleFetchedMyCreatedEmojiList @@ -76,7 +93,7 @@ class EmojiViewModelTest { @Test fun fetchMySavedEmojiList_success_updateMySavedEmojiList() = runTest { // given - val sampleFetchedMySavedEmojiList = createDeterministicDummyEmojiList(10) + val sampleFetchedMySavedEmojiList = createDeterministicTrendingEmojiList(10) coEvery { emojiUseCase.fetchMySavedEmojiList() } returns sampleFetchedMySavedEmojiList