Skip to content

Commit

Permalink
viewmodel contains different paths to same item
Browse files Browse the repository at this point in the history
  • Loading branch information
vextorspace committed Jul 26, 2024
1 parent ebbd7b5 commit cda77d9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
7 changes: 5 additions & 2 deletions composeApp/src/commonMain/kotlin/viewmodel/ViewItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import kotlinx.serialization.Serializable
import model.Item

@Serializable
data class ViewItem(val item: Item = Item()) {
val subItems: MutableList<ViewItem> = item.subItems.map { ViewItem(it) }.toMutableList()
data class ViewItem(
val item: Item = Item(),
val parent: ViewItem? = null
) {
val subItems: MutableList<ViewItem> = item.subItems.map { ViewItem(it, this) }.toMutableList()
}
21 changes: 21 additions & 0 deletions composeApp/src/commonTest/kotlin/viewmodel/ViewItemTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package viewmodel
import io.kotest.matchers.collections.shouldContainAll
import io.kotest.matchers.equals.shouldBeEqual
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.shouldBe
import io.kotest.matchers.types.shouldBeInstanceOf
import model.Item
import kotlin.test.Test
Expand Down Expand Up @@ -30,7 +31,27 @@ class ViewItemTest {
.shouldContainAll(viewModel.item.subItems)
}

@Test
fun `ViewItem remembers parent even when in different branches`() {
val root = Item("Root")
val child1 = Item("Child1")
val child2 = Item("Child2")
val grandChild = Item("GrandChild")
root.add(child1)
root.add(child2)
child1.add(grandChild)
child2.add(grandChild)

val viewModel = DewItViewModel(root)
val viewItem: ViewItem = viewModel.viewRoot()

val gc1 = viewItem.subItems.first().subItems.first()
val gc2 = viewItem.subItems.last().subItems.first()

gc1.item.shouldBe(gc2.item)
gc1.parent!!.item.shouldBe(child1)
gc2.parent!!.item.shouldBe(child2)
}


}

0 comments on commit cda77d9

Please sign in to comment.