Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ dependencies {
implementation(libs.androidx.compose.ui.tooling.preview)
implementation(libs.androidx.compose.material3)
testImplementation(libs.junit)
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.10.2")
testImplementation("io.mockk:mockk:1.14.4")
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
androidTestImplementation(platform(libs.androidx.compose.bom))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,18 @@ class DashboardViewModel @Inject constructor(
private val _effect = Channel<DashboardEffect>(Channel.BUFFERED)
val effect: Flow<DashboardEffect> = _effect.receiveAsFlow()

private var loadJob: Job? = null
private var observeJob: Job? = null

init {
onEvent(DashboardEvent.LoadData)
}

fun onEvent(event: DashboardEvent) {
when (event) {
DashboardEvent.LoadData,
DashboardEvent.RefreshRequested -> loadDashboard()
DashboardEvent.LoadData -> observeDashboard()
DashboardEvent.RefreshRequested -> {
if (observeJob?.isActive != true) observeDashboard()
}

is DashboardEvent.TastingCardClicked -> viewModelScope.launch {
_effect.send(DashboardEffect.NavigateToTastingDetail(event.tasteId))
Expand Down Expand Up @@ -99,17 +101,17 @@ class DashboardViewModel @Inject constructor(
}
}

private fun loadDashboard() {
loadJob?.cancel()
loadJob = viewModelScope.launch {
private fun observeDashboard() {
if (observeJob?.isActive == true) return
observeJob = viewModelScope.launch {
_state.update { it.copy(isLoading = true, errorMessage = null) }

try {
combine(
getRecentTastings(limit = 5),
getDashboardStats(),
getAppPreferences(),
) { tastings, stats, prefs ->
getRecentTastings(limit = 5),
getDashboardStats(),
getAppPreferences(),
) { tastings, stats, prefs ->
_state.update {
it.copy(
isLoading = false,
Expand All @@ -118,12 +120,11 @@ class DashboardViewModel @Inject constructor(
totalTastings = stats.totalTastings,
averageRating = stats.averageRating,
topRegion = stats.topRegion,
username = prefs.username,
showWelcomeDialog = !prefs.welcomeShown,
username = prefs.username,
showWelcomeDialog = !prefs.welcomeShown,
)
}
}.collect()

} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package hu.toliver.vinotes.ui.screen.dashboard

import android.content.Context
import hu.toliver.vinotes.domain.model.DashboardStats
import hu.toliver.vinotes.domain.model.TasteWithWine
import hu.toliver.vinotes.domain.model.enums.AppLanguage
import hu.toliver.vinotes.domain.model.enums.ThemeMode
import hu.toliver.vinotes.domain.usecases.catalog.PerformFullImportUseCase
import hu.toliver.vinotes.domain.usecases.settings.GetAppPreferencesUseCase
import hu.toliver.vinotes.domain.usecases.settings.SaveWelcomeShownUseCase
import hu.toliver.vinotes.domain.usecases.stats.GetDashboardStatsUseCase
import hu.toliver.vinotes.domain.usecases.taste.GetRecentTastingsUseCase
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test

@OptIn(ExperimentalCoroutinesApi::class)
class DashboardViewModelTest {

private val testDispatcher = UnconfinedTestDispatcher()

private val recentTastings = MutableStateFlow<List<TasteWithWine>>(emptyList())
private val dashboardStats = MutableStateFlow(DashboardStats(0, 0, 0.0, ""))
private val appPrefs = MutableStateFlow(defaultPrefs())

private val getRecentTastings: GetRecentTastingsUseCase = mockk()
private val getDashboardStats: GetDashboardStatsUseCase = mockk()
private val getAppPreferences: GetAppPreferencesUseCase = mockk()
private val context: Context = mockk(relaxed = true)

@Before
fun setUp() {
Dispatchers.setMain(testDispatcher)
every { getRecentTastings(any()) } returns recentTastings
every { getDashboardStats() } returns dashboardStats
every { getAppPreferences() } returns appPrefs
}

@After
fun tearDown() {
Dispatchers.resetMain()
}

@Test
fun clearsLoadingWhenSourcesEmitEmptyData() = runTest {
val viewModel = createViewModel()

assertFalse(viewModel.state.value.isLoading)
assertTrue(viewModel.state.value.recentTastings.isEmpty())
assertEquals(0, viewModel.state.value.totalWines)
}

@Test
fun refreshDoesNotResubscribeAndClearsAfterDataWipe() = runTest {
dashboardStats.value = DashboardStats(2, 3, 4.5, "Tokaj")
val viewModel = createViewModel()

assertFalse(viewModel.state.value.isLoading)
assertEquals(2, viewModel.state.value.totalWines)

repeat(3) {
viewModel.onEvent(DashboardEvent.RefreshRequested)
}

recentTastings.value = emptyList()
dashboardStats.value = DashboardStats(0, 0, 0.0, "")

assertFalse(viewModel.state.value.isLoading)
assertEquals(0, viewModel.state.value.totalWines)
verify(exactly = 1) { getRecentTastings(any()) }
verify(exactly = 1) { getDashboardStats() }
verify(exactly = 1) { getAppPreferences() }
}

private fun createViewModel(): DashboardViewModel =
DashboardViewModel(
context = context,
getRecentTastings = getRecentTastings,
getDashboardStats = getDashboardStats,
getAppPreferences = getAppPreferences,
saveWelcomeShown = mockk(relaxed = true),
performFullImport = mockk(relaxed = true),
)

private fun defaultPrefs() =
GetAppPreferencesUseCase.Prefs(
catalogUrl = "https://example.com/catalog",
username = "tester",
themeMode = ThemeMode.SYSTEM,
appLanguage = AppLanguage.ENGLISH,
welcomeShown = true,
)
}
Loading