|
| 1 | +package xyz.junerver.compose.hooks.test |
| 2 | + |
| 3 | +import androidx.compose.material3.Text |
| 4 | +import androidx.compose.runtime.SideEffect |
| 5 | +import androidx.compose.runtime.getValue |
| 6 | +import androidx.compose.runtime.setValue |
| 7 | +import androidx.compose.ui.test.ExperimentalTestApi |
| 8 | +import androidx.compose.ui.test.onNodeWithText |
| 9 | +import androidx.compose.ui.test.runComposeUiTest |
| 10 | +import kotlin.test.Test |
| 11 | +import xyz.junerver.compose.hooks.useState |
| 12 | +import xyz.junerver.compose.hooks.usetable.core.column |
| 13 | +import xyz.junerver.compose.hooks.usetable.useTable |
| 14 | + |
| 15 | +/* |
| 16 | + Description: useTable desktop integration tests |
| 17 | + Author: Junerver |
| 18 | + Date: 2026/06/17 |
| 19 | + Email: junerver@gmail.com |
| 20 | + Version: v1.0 |
| 21 | +*/ |
| 22 | + |
| 23 | +@Suppress("DEPRECATION") |
| 24 | +class UseTableTest { |
| 25 | + private data class User(val name: String, val age: Int) |
| 26 | + |
| 27 | + private val users = listOf( |
| 28 | + User("Charlie", 30), |
| 29 | + User("Alice", 25), |
| 30 | + User("Bob", 20), |
| 31 | + ) |
| 32 | + |
| 33 | + private val columns = listOf( |
| 34 | + column<User, String>("name") { it.name }, |
| 35 | + column<User, Int>("age") { it.age }, |
| 36 | + ) |
| 37 | + |
| 38 | + @OptIn(ExperimentalTestApi::class) |
| 39 | + @Test |
| 40 | + fun holder_api_updates_sorting_and_filtering_state() = runComposeUiTest { |
| 41 | + setContent { |
| 42 | + var phase by useState(default = 0) |
| 43 | + val table = useTable( |
| 44 | + data = users, |
| 45 | + columns = columns, |
| 46 | + optionsOf = { |
| 47 | + enableSorting = true |
| 48 | + enableFiltering = true |
| 49 | + }, |
| 50 | + ) |
| 51 | + |
| 52 | + SideEffect { |
| 53 | + when (phase) { |
| 54 | + 0 -> { |
| 55 | + table.toggleSorting("name", false) |
| 56 | + phase = 1 |
| 57 | + } |
| 58 | + |
| 59 | + 1 -> { |
| 60 | + table.setGlobalFilter("o") |
| 61 | + phase = 2 |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + val rows = table.rowModel.value.rows.joinToString(",") { it.original.name } |
| 67 | + Text( |
| 68 | + text = "phase=$phase rows=$rows total=${table.rowModel.value.totalRows} " + |
| 69 | + "sorting=${table.state.value.sorting.sorting.size} filter=${table.state.value.filtering.globalFilter}", |
| 70 | + ) |
| 71 | + } |
| 72 | + |
| 73 | + waitForIdle() |
| 74 | + onNodeWithText("phase=2 rows=Bob total=1 sorting=1 filter=o").assertExists() |
| 75 | + } |
| 76 | + |
| 77 | + @OptIn(ExperimentalTestApi::class) |
| 78 | + @Test |
| 79 | + fun holder_api_updates_pagination_and_row_selection_state() = runComposeUiTest { |
| 80 | + setContent { |
| 81 | + var phase by useState(default = 0) |
| 82 | + val table = useTable( |
| 83 | + data = users, |
| 84 | + columns = columns, |
| 85 | + optionsOf = { |
| 86 | + enablePagination = true |
| 87 | + enableRowSelection = true |
| 88 | + pageSize = 2 |
| 89 | + }, |
| 90 | + ) |
| 91 | + |
| 92 | + SideEffect { |
| 93 | + when (phase) { |
| 94 | + 0 -> { |
| 95 | + table.setPageIndex(1) |
| 96 | + phase = 1 |
| 97 | + } |
| 98 | + |
| 99 | + 1 -> { |
| 100 | + table.toggleRowSelection("2") |
| 101 | + phase = 2 |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + val rows = table.rowModel.value.rows.joinToString(",") { it.original.name } |
| 107 | + Text( |
| 108 | + text = "phase=$phase page=${table.state.value.pagination.pageIndex} rows=$rows " + |
| 109 | + "selected=${table.state.value.rowSelection.selectedRowIds.size}", |
| 110 | + ) |
| 111 | + } |
| 112 | + |
| 113 | + waitForIdle() |
| 114 | + onNodeWithText("phase=2 page=1 rows=Bob selected=1").assertExists() |
| 115 | + } |
| 116 | +} |
0 commit comments