Skip to content

Commit

Permalink
tests(card-browser-view-model): column indices
Browse files Browse the repository at this point in the history
Add tests:
* changing column index 1
* changing column index 2

This does not handle checking for config
changes on init

To improve coverage
  • Loading branch information
david-allison authored and mikehardy committed Jun 1, 2024
1 parent dbd9cbc commit 8d04cc6
Showing 1 changed file with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.ichi2.anki.browser

import androidx.test.ext.junit.runners.AndroidJUnit4
import app.cash.turbine.TurbineTestContext
import app.cash.turbine.test
import com.ichi2.anki.AnkiDroidApp
import com.ichi2.anki.CardBrowser
Expand Down Expand Up @@ -274,6 +275,42 @@ class CardBrowserViewModelTest : JvmTest() {
}
}

@Test
fun `changing column index 1`() = runViewModelTest {
flowOfColumnIndex1.test {
ignoreEventsDuringViewModelInit()

assertThat("default column1Index value", column1Index, equalTo(0))

setColumn1Index(1)

assertThat("flowOfColumnIndex1", awaitItem(), equalTo(1))
assertThat("column1Index", column1Index, equalTo(1))

// expect no change if the value is selected again
setColumn1Index(1)
expectNoEvents()
}
}

@Test
fun `changing column index 2`() = runViewModelTest {
flowOfColumnIndex2.test {
ignoreEventsDuringViewModelInit()

assertThat("default column2Index value", column2Index, equalTo(0))

setColumn2Index(1)

assertThat("flowOfColumnIndex2", awaitItem(), equalTo(1))
assertThat("column2Index", column2Index, equalTo(1))

// expect no change if the value is selected again
setColumn2Index(1)
expectNoEvents()
}
}

private fun runViewModelTest(notes: Int = 0, manualInit: Boolean = true, testBody: suspend CardBrowserViewModel.() -> Unit) = runTest {
for (i in 0 until notes) {
addNoteUsingBasicModel()
Expand Down Expand Up @@ -322,6 +359,21 @@ private fun CardBrowserViewModel.selectRowsWithPositions(vararg positions: Int)
}
}

/**
* Helper for testing flows:
*
* A MutableStateFlow can either emit a value or not emit a value
* depending on whether a consumer subscribes before or after the task launched
* from init is completed
*/
private fun <T> TurbineTestContext<T>.ignoreEventsDuringViewModelInit() {
try {
expectMostRecentItem()
} catch (e: AssertionError) {
// explicitly ignored: no items
}
}

private suspend fun CardBrowserViewModel.waitForSearchResults() {
searchJob?.join()
}
Expand Down

0 comments on commit 8d04cc6

Please sign in to comment.