StatisticsViewModelTest.kt
composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/ui/screens/settings/statistics/StatisticsViewModelTest.kt
1package eu.friedcutlet.yokan.ui.screens.settings.statistics23import eu.friedcutlet.yokan.ui.ViewModelTestHost4import eu.friedcutlet.yokan.data.DefaultYokanRepository5import eu.friedcutlet.yokan.data.LibraryIndexStore6import eu.friedcutlet.yokan.data.SettingsStore7import eu.friedcutlet.yokan.database.DbChapter8import eu.friedcutlet.yokan.database.DbItemType9import eu.friedcutlet.yokan.database.DbManga10import eu.friedcutlet.yokan.database.InMemoryYokanDatabase11import eu.friedcutlet.yokan.database.emptySnapshot12import eu.friedcutlet.yokan.domain.ItemType13import eu.friedcutlet.yokan.services.files.NoFiles14import kotlin.test.AfterTest15import kotlin.test.Test16import kotlin.test.assertEquals17import kotlinx.coroutines.CoroutineScope18import kotlinx.coroutines.ExperimentalCoroutinesApi19import kotlinx.coroutines.flow.first20import kotlinx.coroutines.test.runTest2122@OptIn(ExperimentalCoroutinesApi::class)23class StatisticsViewModelTest {24 private val database = InMemoryYokanDatabase(25 emptySnapshot().copy(26 mangas = listOf(27 DbManga(id = 1, name = "Manga", link = "/m", source = "Src", itemType = DbItemType.Manga, favorite = true),28 DbManga(id = 2, name = "Anime", link = "/a", source = "Src", itemType = DbItemType.Anime, favorite = true),29 ),30 chapters = listOf(31 DbChapter(id = 10, mangaId = 1, name = "1"),32 DbChapter(id = 11, mangaId = 1, name = "2"),33 ),34 ),35 )36 private val repository = DefaultYokanRepository(database)37 private val host = ViewModelTestHost()38 private val dispatcher = host.dispatcher3940 @AfterTest41 fun tearDown() = host.close()4243 private fun viewModel(scope: CoroutineScope) = host.create {44 StatisticsViewModel(45 ItemType.Manga,46 repository,47 SettingsStore(repository, scope, onSchedulingChanged = {}),48 LibraryIndexStore(repository, scope),49 NoFiles,50 workDispatcher = dispatcher,51 )52 }5354 @Test55 fun summaryFollowsReadingWithoutARefreshAndSwitchesType() = runTest {56 val vm = viewModel(backgroundScope)5758 val initial = vm.state.first { it.data != null }.data!!59 assertEquals(1, initial.titles)60 assertEquals(2, initial.chapters)61 assertEquals(0, initial.read)6263 database.upsertChapter(database.chapters().first { it.id == 10L }.copy(isRead = true))64 assertEquals(1, vm.state.first { it.data?.read == 1 }.data!!.inProgress)6566 vm.select(ItemType.Anime)67 val anime = vm.state.first { it.type == ItemType.Anime && it.data != null }.data!!68 assertEquals(1, anime.titles)69 assertEquals(0, anime.chapters)70 }71}72