lundi 27 janvier 2020

How to test @Model data classes from Jetpack Compose?

I'm trying to test a mapper function between my UI layer and my Domain layer.

But I get an exception thrown of java.lang.IllegalStateException: Not in a frame

This exception goes away if I change my UIModel to have a val instead of a var but then it is useless with the model updates that Jetpack Compose offers.

Is there a way to test this type of mapper?

import androidx.compose.Model
import org.junit.Assert.assertEquals
import org.junit.Test

class DataModelMapperTest {

    @Test
    fun `data model to ui model`() {
        val model = DataModel(5)
        val uiModel = UIModel(5)

        assertEquals(uiModel, model.toUIModel())
    }

    @Test
    fun `ui model to data model`() {
        val model = DataModel(5)
        val uiModel = UIModel(5)

        assertEquals(model, uiModel.toDataModel())
    }

}

@Model
data class UIModel(var value: Int)

data class DataModel(val value: Int)

fun DataModel.toUIModel(): UIModel = UIModel(this.value)

fun UIModel.toDataModel(): DataModel = DataModel(this.value)

Aucun commentaire:

Enregistrer un commentaire