Skip to content

Commit

Permalink
fix: iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
AKJAW committed Dec 22, 2022
1 parent a5ebf03 commit 9cf040b
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal class CommonTaskListViewModel(
) : TaskListViewModel() {

private val selectionTracker: TaskSelectionTracker = taskSelectionTrackerFactory.create(
viewModelScope = viewModelScope.coroutineScope + dispatcherProvider.io,
viewModelScope = viewModelScope,
originalTaskFlow = getTasks.execute(),
)
override val tasks: StateFlow<List<Task>> = selectionTracker.tasksWithSelection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import com.akjaw.timi.kmp.core.shared.coroutines.DispatcherProvider
import com.akjaw.timi.kmp.feature.task.api.domain.model.Task
import com.rickclephas.kmm.viewmodel.ViewModelScope
import com.rickclephas.kmm.viewmodel.coroutineScope
import com.rickclephas.kmm.viewmodel.stateIn
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.plus

class TaskSelectionTracker(
viewModelScope: CoroutineScope,
viewModelScope: ViewModelScope,
originalTaskFlow: Flow<List<Task>>,
) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import kotlinx.coroutines.flow.Flow
class TaskSelectionTrackerFactory {

fun create(
viewModelScope: CoroutineScope,
viewModelScope: ViewModelScope,
originalTaskFlow: Flow<List<Task>>,
): TaskSelectionTracker =
TaskSelectionTracker(viewModelScope, originalTaskFlow)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ import com.akjaw.timi.kmp.feature.task.dependency.list.composition.taskListModul
import com.akjaw.timi.kmp.feature.task.dependency.list.presentation.selection.TaskSelectionTrackerFactory
import com.squareup.sqldelight.db.SqlDriver
import io.kotest.matchers.shouldBe
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import org.koin.core.component.KoinComponent
import org.koin.core.component.get
import org.koin.core.component.inject
Expand Down Expand Up @@ -52,6 +55,7 @@ internal class CommonTaskListViewModelTest : KoinComponent {

@BeforeTest
fun setUp() {
Dispatchers.setMain(testCoroutineDispatcher)
startKoin {
modules(
databaseModule,
Expand All @@ -72,10 +76,11 @@ internal class CommonTaskListViewModelTest : KoinComponent {

@AfterTest
fun tearDown() {
Dispatchers.resetMain()
stopKoin()
}

@Test
@Test // TODO still flaky but probably cause by StateFlow refactor
fun `Adding a task changes the list`() = runTest {
givenTasks(TASK1)

Expand All @@ -86,7 +91,7 @@ internal class CommonTaskListViewModelTest : KoinComponent {
}
}

@Test
@Test // TODO still flaky but probably cause by StateFlow refactor
fun `Deleting tasks changes the list`() = runTest {
givenTasks(TASK1, TASK2)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ package com.akjaw.timi.kmp.feature.task.dependency.list.presentation.selection

import com.akjaw.timi.kmp.core.shared.coroutines.TestDispatcherProvider
import com.akjaw.timi.kmp.feature.task.api.domain.model.Task
import com.rickclephas.kmm.viewmodel.KMMViewModel
import com.rickclephas.kmm.viewmodel.ViewModelScope
import io.kotest.matchers.collections.shouldHaveSize
import io.kotest.matchers.shouldBe
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test

Expand All @@ -28,8 +33,15 @@ internal class TaskSelectionTrackerTest {

@BeforeTest
fun setUp() {
Dispatchers.setMain(testCoroutineDispatcher)
originalFlow = MutableStateFlow(emptyList())
systemUnderTest = TaskSelectionTracker(CoroutineScope(testCoroutineDispatcher), originalFlow)
val vm = object : KMMViewModel() {}
systemUnderTest = TaskSelectionTracker(vm.viewModelScope, originalFlow)
}

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

@Test
Expand Down

0 comments on commit 9cf040b

Please sign in to comment.