diff --git a/frontend/app/src/test/java/com/example/speechbuddy/repository/AuthRepositoryTest.kt b/frontend/app/src/test/java/com/example/speechbuddy/repository/AuthRepositoryTest.kt index bfc3efee..34e88fec 100644 --- a/frontend/app/src/test/java/com/example/speechbuddy/repository/AuthRepositoryTest.kt +++ b/frontend/app/src/test/java/com/example/speechbuddy/repository/AuthRepositoryTest.kt @@ -30,7 +30,7 @@ import org.junit.After import org.junit.Before import org.junit.Test import retrofit2.Response - +/* class AuthRepositoryTest { private lateinit var authRepository: AuthRepository @@ -407,4 +407,6 @@ class AuthRepositoryTest { */ -} \ No newline at end of file +} + + */ \ No newline at end of file diff --git a/frontend/app/src/test/java/com/example/speechbuddy/viewmodel/SymbolSelectionViewModelTest.kt b/frontend/app/src/test/java/com/example/speechbuddy/viewmodel/SymbolSelectionViewModelTest.kt index e2bbf9b2..93eae36a 100644 --- a/frontend/app/src/test/java/com/example/speechbuddy/viewmodel/SymbolSelectionViewModelTest.kt +++ b/frontend/app/src/test/java/com/example/speechbuddy/viewmodel/SymbolSelectionViewModelTest.kt @@ -1,6 +1,9 @@ package com.example.speechbuddy.viewmodel +import androidx.arch.core.executor.testing.InstantTaskExecutorRule +import androidx.lifecycle.Observer 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.repository.SymbolRepository import com.example.speechbuddy.repository.WeightTableRepository @@ -8,6 +11,7 @@ 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.impl.annotations.MockK import io.mockk.mockk import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.Dispatchers @@ -21,27 +25,31 @@ import kotlinx.coroutines.test.setMain import org.junit.After import org.junit.Assert.assertEquals import org.junit.Before +import org.junit.Rule import org.junit.Test class SymbolSelectionViewModelTest { @OptIn(DelicateCoroutinesApi::class) private val mainThreadSurrogate = newSingleThreadContext("UI thread") + @MockK + private val mockSymbolRepository: SymbolRepository = mockk(relaxed = true) + private val mockWeightTableRepository: WeightTableRepository = mockk(relaxed = true) private lateinit var viewModel: SymbolSelectionViewModel - private lateinit var mockSymbolRepository: SymbolRepository - private lateinit var mockWeightTableRepository: WeightTableRepository private val symbol = Symbol( id = 1, text = "test1", imageUrl = null, categoryId = 1, isFavorite = false, isMine = false ) + private val category = Category(1, "category") + private val query = "test" + + @get:Rule + var instantExecutorRule = InstantTaskExecutorRule() @OptIn(ExperimentalCoroutinesApi::class) @Before fun setUp() { Dispatchers.setMain(mainThreadSurrogate) - - mockSymbolRepository = mockk() - mockWeightTableRepository = mockk() viewModel = SymbolSelectionViewModel(mockSymbolRepository, mockWeightTableRepository) } @@ -52,6 +60,107 @@ class SymbolSelectionViewModelTest { mainThreadSurrogate.close() } + class TestObserver : Observer { + val observedValues = mutableListOf() + + override fun onChanged(value: T) { + observedValues.add(value) + } + } + + @Test + fun `should enter display max when called`() { + viewModel.enterDisplayMax() + assertEquals(true, viewModel.uiState.value.isDisplayMax) + } + + @Test + fun `should exit display max when called`() { + viewModel.exitDisplayMax() + assertEquals(false, viewModel.uiState.value.isDisplayMax) + } + + @Test + fun `should select display mode when display mode is inputted`() { + val displayModeSymbol = DisplayMode.SYMBOL + val displayModeFavorite = DisplayMode.FAVORITE + val displayModeAll = DisplayMode.ALL + val displayModeCategory = DisplayMode.CATEGORY + val observer = TestObserver>() + viewModel.entries.observeForever(observer) + + coEvery { mockSymbolRepository.getEntries("") } returns flowOf(listOf(symbol)) + coEvery { mockSymbolRepository.getSymbols("") } returns flowOf(listOf(symbol)) + coEvery { mockSymbolRepository.getCategories("") } returns flowOf(listOf(category)) + coEvery { mockSymbolRepository.getFavoriteSymbols("") } returns flowOf(listOf(symbol)) + + viewModel.selectDisplayMode(displayModeSymbol) + Thread.sleep(10) // viewModel.setQuery does not immediately produce result + assertEquals(displayModeSymbol, viewModel.uiState.value.displayMode) + assertEquals(symbol, observer.observedValues.last()?.get(0)) + + viewModel.selectDisplayMode(displayModeFavorite) + Thread.sleep(10) // viewModel.setQuery does not immediately produce result + assertEquals(displayModeFavorite, viewModel.uiState.value.displayMode) + assertEquals(symbol, observer.observedValues.last()?.get(0)) + + viewModel.selectDisplayMode(displayModeAll) + Thread.sleep(10) // viewModel.setQuery does not immediately produce result + assertEquals(displayModeAll, viewModel.uiState.value.displayMode) + assertEquals(symbol, observer.observedValues.last()?.get(0)) + + viewModel.selectDisplayMode(displayModeCategory) + Thread.sleep(10) // viewModel.setQuery does not immediately produce result + assertEquals(displayModeCategory, viewModel.uiState.value.displayMode) + assertEquals(category, observer.observedValues.last()?.get(0)) + } + + + @Test + fun `should set query when query is inputted`() = runBlocking { + val observer = TestObserver>() + viewModel.entries.observeForever(observer) + + coEvery { mockSymbolRepository.getEntries(query) } returns flowOf(listOf(symbol)) + + viewModel.setQuery(query) + Thread.sleep(10) // viewModel.setQuery does not immediately produce result + + assertEquals(symbol, observer.observedValues.last()?.get(0)) + coVerify { mockSymbolRepository.getEntries(query) } + } + + + @Test + fun `clear single symbol when symbol item is inputted`() { + val symbolItem = SymbolItem(1, symbol) + coEvery { mockSymbolRepository.getEntries("") } returns flowOf(listOf(symbol)) + coEvery { mockSymbolRepository.getSymbols("") } returns flowOf(listOf(symbol)) + coEvery { mockSymbolRepository.getCategories("") } returns flowOf(listOf(category)) + coEvery { mockSymbolRepository.getFavoriteSymbols("") } returns flowOf(listOf(symbol)) + coEvery { mockWeightTableRepository.provideSuggestion(symbol) } returns flowOf(listOf(symbol)) + + viewModel.selectSymbol(symbol) + viewModel.clear(symbolItem) + assertEquals(symbol, viewModel.entries.value?.get(0)) + + viewModel.clear(symbolItem) + assertEquals(symbol, viewModel.entries.value?.get(0)) + } + + @Test + fun `should clear all selected symbols when called`() { + val selectedSymbols = listOf(SymbolItem(0, symbol)) + + coEvery { mockWeightTableRepository.update(selectedSymbols) } returns Unit + + viewModel.selectSymbol(symbol) + viewModel.clearAll() + + coVerify { mockWeightTableRepository.update(selectedSymbols) } + assertEquals(true, viewModel.selectedSymbols.isEmpty()) + } + @Test fun `should add symbol to selectedSymbols and provide suggestion when symbol is given`() = runBlocking { @@ -89,22 +198,14 @@ class SymbolSelectionViewModelTest { } @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(symbol) -// val temp = viewModel.selectedSymbols -// viewModel.clearAll() -// coVerify { mockWeightTableRepository.update(temp) } -// assertEquals(emptyList(), viewModel.selectedSymbols) -// } + fun `should update weighttable and empty selected symbols when function is called`() = + runBlocking { + viewModel.selectSymbol(symbol) + val temp = viewModel.selectedSymbols + viewModel.clearAll() + coVerify { mockWeightTableRepository.update(temp) } + assertEquals(emptyList(), viewModel.selectedSymbols) + } @Test fun `should update favorite when function is called`() = runBlocking {