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
52 changes: 52 additions & 0 deletions app/src/test/kotlin/app/hypostats/ui/MainViewModelTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package app.hypostats.ui

import app.hypostats.ui.model.AppTab
import app.hypostats.ui.model.DrawerDestination
import app.hypostats.util.FakeSettingsRepository
import app.hypostats.util.MainDispatcherRule
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test

@OptIn(ExperimentalCoroutinesApi::class)
class MainViewModelTest {
@get:Rule
val mainDispatcherRule = MainDispatcherRule(UnconfinedTestDispatcher())

private fun TestScope.collectState(viewModel: MainViewModel) {
backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) {
viewModel.state.collect {}
}
}

@Test
fun `selectTab updates selected tab`() =
runTest {
val viewModel = MainViewModel(FakeSettingsRepository())
collectState(viewModel)

assertEquals(AppTab.HYPO, viewModel.state.value.selectedTab)

viewModel.selectTab(AppTab.STATS)

assertEquals(AppTab.STATS, viewModel.state.value.selectedTab)
}

@Test
fun `selectDrawerDestination updates selected drawer destination`() =
runTest {
val viewModel = MainViewModel(FakeSettingsRepository())
collectState(viewModel)

assertEquals(DrawerDestination.HOME, viewModel.state.value.selectedDrawerDestination)

viewModel.selectDrawerDestination(DrawerDestination.ABOUT)

assertEquals(DrawerDestination.ABOUT, viewModel.state.value.selectedDrawerDestination)
}
}
37 changes: 37 additions & 0 deletions app/src/test/kotlin/app/hypostats/ui/log/LogViewModelTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package app.hypostats.ui.log

import app.hypostats.domain.model.Treatment
import app.hypostats.util.FakeTreatmentRepository
import app.hypostats.util.MainDispatcherRule
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test
import java.time.Instant

@OptIn(ExperimentalCoroutinesApi::class)
class LogViewModelTest {
@get:Rule
val mainDispatcherRule = MainDispatcherRule(UnconfinedTestDispatcher())

private fun TestScope.collectTreatments(viewModel: LogViewModel) {
backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) {
viewModel.treatments.collect {}
}
}

@Test
fun `treatments are sorted newest first`() =
runTest {
val older = Treatment(Instant.parse("2024-01-01T00:00:00Z"), 10)
val newer = Treatment(Instant.parse("2024-01-02T00:00:00Z"), 10)
val viewModel = LogViewModel(FakeTreatmentRepository(listOf(older, newer)))
collectTreatments(viewModel)

assertEquals(listOf(newer, older), viewModel.treatments.value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import java.time.Instant

class FakeTreatmentRepository : TreatmentRepository {
private val treatmentsFlow = MutableStateFlow<List<Treatment>>(emptyList())
class FakeTreatmentRepository(
initialTreatments: List<Treatment> = emptyList(),
) : TreatmentRepository {
private val treatmentsFlow = MutableStateFlow<List<Treatment>>(initialTreatments)
private val trackingStartDateFlow = MutableStateFlow(Instant.EPOCH)

override fun getAllTreatments(): Flow<List<Treatment>> = treatmentsFlow.asStateFlow()
Expand Down
23 changes: 23 additions & 0 deletions app/src/test/kotlin/app/hypostats/util/MainDispatcherRule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@file:OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)

package app.hypostats.util

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import org.junit.rules.TestWatcher
import org.junit.runner.Description

class MainDispatcherRule(
val testDispatcher: TestDispatcher = StandardTestDispatcher(),
) : TestWatcher() {
override fun starting(description: Description) {
Dispatchers.setMain(testDispatcher)
}

override fun finished(description: Description) {
Dispatchers.resetMain()
}
}