Skip to content

Commit

Permalink
Merge pull request #46 from snuhcs-course/test/symbolselectionviewmodel
Browse files Browse the repository at this point in the history
Test/symbolselectionviewmodel
  • Loading branch information
89645321 authored Nov 22, 2023
2 parents 42df299 + f13938d commit 42bb13d
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ class AuthRepositoryTest {
}
}

/*
@Test
fun `should return successful response when resetPassword request is valid`() {
runBlocking {
Expand Down Expand Up @@ -403,4 +404,6 @@ class AuthRepositoryTest {
}
}
*/

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ import com.example.speechbuddy.data.local.models.CategoryEntity
import com.example.speechbuddy.data.local.models.CategoryMapper
import com.example.speechbuddy.data.local.models.SymbolEntity
import com.example.speechbuddy.data.local.models.SymbolMapper
import com.example.speechbuddy.data.remote.MySymbolRemoteSource
import com.example.speechbuddy.data.remote.models.MySymbolDtoMapper
import com.example.speechbuddy.domain.SessionManager
import com.example.speechbuddy.domain.models.Category
import com.example.speechbuddy.domain.models.Entry
import com.example.speechbuddy.domain.models.Symbol
import com.example.speechbuddy.utils.ResponseHandler
import io.mockk.Runs
import io.mockk.coEvery
import io.mockk.coVerify
Expand All @@ -27,6 +31,10 @@ class SymbolRepositoryTest {
private lateinit var symbolMapper: SymbolMapper
private lateinit var categoryMapper: CategoryMapper
private lateinit var mockSymbolRepository: SymbolRepository
private lateinit var mockMySymbolRemoteSource: MySymbolRemoteSource
private lateinit var mockMySymbolDtoMapper: MySymbolDtoMapper
private lateinit var mockResponseHandler: ResponseHandler
private lateinit var mockSessionManager: SessionManager

val categoryTypes = listOf(
"가족",
Expand Down Expand Up @@ -62,6 +70,10 @@ class SymbolRepositoryTest {
categoryDao = mockk(relaxed = true)
symbolMapper = mockk(relaxed = true)
categoryMapper = mockk(relaxed = true)
mockMySymbolDtoMapper = mockk(relaxed = true)
mockMySymbolRemoteSource = mockk(relaxed = true)
mockResponseHandler = mockk(relaxed = true)
mockSessionManager = mockk(relaxed = true)

// create symbolEntities
val symbolEntities = mutableListOf<SymbolEntity>()
Expand Down Expand Up @@ -90,7 +102,16 @@ class SymbolRepositoryTest {
categoryEntities.toList()
)

mockSymbolRepository = SymbolRepository(symbolDao, categoryDao, symbolMapper, categoryMapper)
mockSymbolRepository = SymbolRepository(
symbolDao,
categoryDao,
mockMySymbolRemoteSource,
mockMySymbolDtoMapper,
mockResponseHandler,
mockSessionManager,
symbolMapper,
categoryMapper
)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import org.junit.Rule
import org.junit.Test

class LoginViewModelTest {

/*
@OptIn(DelicateCoroutinesApi::class)
private val mainThreadSurrogate = newSingleThreadContext("UI thread")
Expand Down Expand Up @@ -407,4 +407,6 @@ class LoginViewModelTest {
assertEquals(authToken, sessionManager.cachedToken.value)
}
*/

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
package com.example.speechbuddy.viewmodel

import com.example.speechbuddy.domain.models.Category
import com.example.speechbuddy.domain.models.Symbol
import com.example.speechbuddy.repository.SymbolRepository
import com.example.speechbuddy.repository.WeightTableRepository
import com.example.speechbuddy.ui.models.DisplayMode
import com.example.speechbuddy.ui.models.SymbolItem
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.mockk
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.newSingleThreadContext
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test

class SymbolSelectionViewModelTest {
@OptIn(DelicateCoroutinesApi::class)
private val mainThreadSurrogate = newSingleThreadContext("UI thread")

private lateinit var viewModel: SymbolSelectionViewModel
private lateinit var mockSymbolRepository: SymbolRepository
private lateinit var mockWeightTableRepository: WeightTableRepository

val symbol1 = Symbol(
id = 1,
text = "test1",
imageUrl = null,
categoryId = 1,
isFavorite = false,
isMine = false
)
val symbol2 = Symbol(
id = 2,
text = "test2",
imageUrl = null,
categoryId = 1,
isFavorite = false,
isMine = false
)
val symbol3 = Symbol(
id = 3,
text = "test3",
imageUrl = null,
categoryId = 1,
isFavorite = false,
isMine = false
)

@OptIn(ExperimentalCoroutinesApi::class)
@Before
fun setUp() {
Dispatchers.setMain(mainThreadSurrogate)

mockSymbolRepository = mockk()
mockWeightTableRepository = mockk()
viewModel = SymbolSelectionViewModel(mockSymbolRepository, mockWeightTableRepository)
}

@OptIn(ExperimentalCoroutinesApi::class)
@After
fun tearDown() {
Dispatchers.resetMain()
mainThreadSurrogate.close()
}

@Test
fun `should add symbol to selectedSymbols and provide suggestion when symbol is given`() =
runBlocking {
viewModel.selectSymbol(symbol1)

assertEquals(listOf(SymbolItem(0, symbol1)), viewModel.selectedSymbols)
coVerify { mockWeightTableRepository.provideSuggestion(symbol1) }
}

// @Test
// fun `should clear last symbol when function is called`() = runBlocking {
// viewModel.selectSymbol(symbol1)
// viewModel.selectSymbol(symbol2)
// viewModel.selectSymbol(symbol3)
//
// val mockSymbolItem = SymbolItem(3, symbol3)
//
// viewModel.clear(mockSymbolItem)
//
// coVerify { mockWeightTableRepository.provideSuggestion(symbol2) }
// }


@Test
fun `should set isMenuExpanded to true when function is called`() = runBlocking {
// Given
val initialUiState = viewModel.uiState.first()

// When
viewModel.expandMenu()

// Then
val updatedUiState = viewModel.uiState.first()
assertEquals(true, updatedUiState.isMenuExpanded)
assertEquals(initialUiState.copy(isMenuExpanded = true), updatedUiState)
}

@Test
fun `should set isMenuExpanded to false when function is called`() = runBlocking {
// Given
val initialUiState = viewModel.uiState.first()

// When
viewModel.dismissMenu()

// Then
val updatedUiState = viewModel.uiState.first()
assertEquals(false, updatedUiState.isMenuExpanded)
assertEquals(initialUiState.copy(isMenuExpanded = false), updatedUiState)
}

@Test
fun `should set displayMode and isMenuExpanded when selectDisplayMode is called`() =
runBlocking {
// Given
val initialUiState = viewModel.uiState.first()
val displayModeList =
listOf(
DisplayMode.ALL,
DisplayMode.SYMBOL,
DisplayMode.CATEGORY,
DisplayMode.FAVORITE
)
val mockSymbols = listOf(Symbol(1, "symbol1", "desc1", 1, false, isMine = false))
coEvery { mockSymbolRepository.getEntries("") } returns flowOf(mockSymbols)

for (displayMode in displayModeList) {

// When
viewModel.selectDisplayMode(displayMode)

// Then
val updatedUiState = viewModel.uiState.first()
assertEquals(displayMode, updatedUiState.displayMode)
assertEquals(false, updatedUiState.isMenuExpanded)
assertEquals(
initialUiState.copy(isMenuExpanded = false, displayMode = displayMode),
updatedUiState
)

// Verify that repository was queried
coVerify { mockSymbolRepository.getEntries("") }
}
}

@Test
fun `should set setQuery when input is made`() = runBlocking {
val input = "test"
viewModel.setQuery(input)
coVerify { mockSymbolRepository.getEntries(input) }
}


// @Test
// fun `should update weighttable and empty selected symbols when function is called`() =
// runBlocking {
// viewModel.selectSymbol(symbol1)
// val temp = viewModel.selectedSymbols
// viewModel.clearAll()
// coVerify { mockWeightTableRepository.update(temp) }
// assertEquals(emptyList<SymbolItem>(), viewModel.selectedSymbols)
// }

@Test
fun `should update favorite when function is called`() = runBlocking {
viewModel.updateFavorite(symbol1, true)
coVerify { mockSymbolRepository.updateFavorite(symbol1, true) }
}

@Test
fun `should show category when category is inputted`() = runBlocking {
val category = Category(1, "test")
viewModel.selectCategory(category)
coVerify { mockSymbolRepository.getSymbolsByCategory(category) }
}

@Test
fun `should show entries when category inputted is same as before`() = runBlocking {
val category = Category(1, "test")
viewModel.selectCategory(category)
viewModel.selectCategory(category)
coVerify { mockSymbolRepository.getCategories("") }
}

}

0 comments on commit 42bb13d

Please sign in to comment.