Skip to content

Commit

Permalink
fix: resolve EmojiViewModelTest failure
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisWooyeol committed Dec 4, 2023
1 parent 051c24c commit cb8243f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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()
Expand Down
10 changes: 5 additions & 5 deletions android/app/src/test/java/com/goliath/emojihub/utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<PagingData<EmojiDto>> {
fun createDeterministicTrendingEmojiDtoList(listSize : Int): Flow<PagingData<EmojiDto>> {
val dummyEmojiList = mutableListOf<EmojiDto>()
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],
Expand All @@ -44,8 +44,8 @@ fun createDeterministicDummyEmojiDtoList(listSize : Int): Flow<PagingData<EmojiD
}
return flowOf(PagingData.from(dummyEmojiList))
}
fun createDeterministicDummyEmojiList(listSize: Int): Flow<PagingData<Emoji>> {
return createDeterministicDummyEmojiDtoList(listSize).map { it.map { dto -> Emoji(dto) } }
fun createDeterministicTrendingEmojiList(listSize: Int): Flow<PagingData<Emoji>> {
return createDeterministicTrendingEmojiDtoList(listSize).map { it.map { dto -> Emoji(dto) } }
}

// POST TESTING UTILS
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit cb8243f

Please sign in to comment.