Hello guys, I am trying a new approach where I hav...
# orbit-mvi
m
Hello guys, I am trying a new approach where I have lambdas for actionable elements passed to UI in state:
Copy code
data class State(
    val button: Button
) {
    data class Button(
        val text: String,
        val onClick: () -> Unit
    )
}
I am assigning methods of my viewModel to lambdas inside state. I tried to run a unit test but it fails due to lambda inequality. My test looks like this:
Copy code
@Test
    fun `should assign correct lambdas`() = runTest {
        val vm = MyViewModel(backgroundScope)
        vm.test(this) {
            expectInitialState()

            expectState(
                MyViewModel.State(
                    Button(
                        text = "OK",
                        onClick = vm::someAction // test fails here
                    )
                )
            )
        }
    }
Is there some way to check the equality of lambdas in that scenario?
m
Can you paste an example from your ViewModel of how you are creating these lambdas?
m
sure!
Copy code
class MyViewModel(
    coroutineScope: CoroutineScope,
) : ContainerHost<State, SideEffect> {
    override val container: Container<State, SideEffect> = coroutineScope.container(
        State(
            State.Button(
                text = "OK",
                onClick = this::someAction
            )
        )
    )
    val stateFlow: StateFlow<State> = container.stateFlow
    val sideEffectFlow: Flow<SideEffect> = container.sideEffectFlow

    fun someAction() {
        // perform some action
    }
}
m
Well you're using method references everywhere... This is puzzling
in my head, this should work
it certainly wouldn't work if you were creating the lambdas like
onClick = { someAction() }
as this creates a new lambda instance
m
OK, so the issue is that when I assign the action to intent inside
reduce
like this:
Copy code
fun someAction() = intent {
        reduce { state.copy(button = state.button!!.copy("NOT OK", ::someAction)) }
    }
the test fails with no apparent reason. But when I change the return type of my action to
Job
(the return type of
intent
) I start getting compiler error:
Type checking has run into a recursive problem.
😄 Seems like compiler does not care about return type as long as it is
Unit
🤷
Thank you @Mikolaj Leszczynski for your help! 😄
m
Gotcha 🙂
you can always do
fun someAction() { intent { ...
instead
👀 1
if you don't care about using
Job
anywhere
m
Oh thanks! That works 😄