Benoît
08/08/2023, 4:58 PMpublic class MyContainerHost(
scope: CoroutineScope,
dataFlow: Flow<String>
) : ContainerHost<MyContainerHost.State, Unit> {
public data class State(val value: String)
override val container: Container<State, Unit> = scope.container(State("")) {
scope.launch {
dataFlow.collectLatest { flowValue ->
intent {
// This is never reached during tests
reduce {
state.copy(value = flowValue)
}
}
}
}
}
}
And I'm testing it like so
@Test
fun the_test() {
runTest {
val ch = MyContainerHost(scope = this, dataFlow = flowOf("Some value"))
val testCH = ch.test(isolateFlow = false)
testCH.runOnCreate()
delay(1000)
val lastState = testCH.stateObserver.values.last()
assertEquals("Some value", lastState.value)
}
}
But the test fails because the intent{}
block is never called. collectLatest
calls intent
but its content, the reduce function, is never called. This is only an issue during testing
Any idea?Vishnu Ravi
08/09/2023, 2:46 AMpublic class MyContainerHost(
scope: CoroutineScope,
dataFlow: Flow<String>
) : ContainerHost<MyContainerHost.State, Unit> {
public data class State(val value: String)
override val container: Container<State, Unit> = scope.container(State("")) {
intent { // This should work
dataFlow.collectLatest { flowValue ->
reduce {
state.copy(value = flowValue)
}
}
}
}
}
Benoît
08/09/2023, 11:10 AMVishnu Ravi
08/09/2023, 3:58 PMrunOnSubscription
not sure if this is a typo 😅)
2. As per the docs "Cancellation happens automatically when the Orbit coroutine scope cancels." ...and we have a custom scope to deal with.