Skip to content

Commit

Permalink
Merge pull request #533 from cinadia/363-errant-behavior-when-hiding-…
Browse files Browse the repository at this point in the history
…currently-selected-category

363: Selects the next non-hidden category to display when the currently-displayed category is hidden
  • Loading branch information
PaulKlauser authored Jun 4, 2024
2 parents badf604 + c7350b5 commit 97e88ce
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class MainScreen {
onView(withText(phrase)).check(matches(isDisplayed()))
}
}

// Taps on the selected phrase
fun tapPhrase(phraseText: String) {
onView(withText(phraseText)).tap()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.willowtree.vocable.tests

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.rule.GrantPermissionRule
import com.willowtree.vocable.screens.MainScreen
import com.willowtree.vocable.utility.assertTextMatches
import org.junit.Ignore
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,20 @@ class PresetsViewModel(
categoriesUseCase.categories(),
liveSelectedCategoryId
) { categories, selectedId ->
categories.find { it.categoryId == selectedId }
val currentCategory = categories.find { it.categoryId == selectedId }
if (currentCategory?.hidden == true) {
val newSortOrder = (currentCategory.sortOrder + 1)
val newCategory = categories.find { it.sortOrder == newSortOrder}
if (newCategory != null) {
liveSelectedCategoryId.update { newCategory.categoryId }
categories.find { it.sortOrder == newSortOrder}
} else {
liveSelectedCategoryId.update { categories.first().categoryId }
categories.first()
}
} else {
currentCategory
}
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000L), null)
val selectedCategoryLiveData: LiveData<Category?> = selectedCategory.asLiveData()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,148 @@ class PresetsViewModelTest {
)
}

@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun `selected category is hidden and next immediate category is shown`() = runTest(UnconfinedTestDispatcher()) {
fakeCategoriesUseCase._categories.update {
listOf(
Category.StoredCategory(
categoryId = "1",
localizedName = LocalesWithText(mapOf("en_US" to "category")),
hidden = true,
sortOrder = 0
),
Category.StoredCategory(
categoryId = "2",
localizedName = LocalesWithText(mapOf("en_US" to "second category")),
hidden = false,
sortOrder = 1
)
)
}

val vm = createViewModel()

vm.onCategorySelected("1")

var category: Category? = null
val job = launch {
vm.selectedCategory.collect {
category = it
}
}
job.cancel()

assertEquals(
Category.StoredCategory(
categoryId = "2",
localizedName = LocalesWithText(mapOf("en_US" to "second category")),
hidden = false,
sortOrder = 1
),
category
)

}

@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun `selected category (last in list) is hidden and first category is shown`() = runTest(UnconfinedTestDispatcher()) {
fakeCategoriesUseCase._categories.update {
listOf(
Category.StoredCategory(
categoryId = "1",
localizedName = LocalesWithText(mapOf("en_US" to "category")),
hidden = false,
sortOrder = 0
),
Category.StoredCategory(
categoryId = "2",
localizedName = LocalesWithText(mapOf("en_US" to "second category")),
hidden = false,
sortOrder = 1
),
Category.StoredCategory(
categoryId = "3",
localizedName = LocalesWithText(mapOf("en_US" to "third category")),
hidden = true,
sortOrder = 2
)
)
}

val vm = createViewModel()

vm.onCategorySelected("3")

var category: Category? = null
val job = launch {
vm.selectedCategory.collect {
category = it
}
}
job.cancel()

assertEquals(
Category.StoredCategory(
categoryId = "1",
localizedName = LocalesWithText(mapOf("en_US" to "category")),
hidden = false,
sortOrder = 0
),
category
)
}

@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun `selected category is hidden and next non-hidden category is shown`() = runTest(UnconfinedTestDispatcher()) {
fakeCategoriesUseCase._categories.update {
listOf(
Category.StoredCategory(
categoryId = "1",
localizedName = LocalesWithText(mapOf("en_US" to "category")),
hidden = true,
sortOrder = 0
),
Category.StoredCategory(
categoryId = "2",
localizedName = LocalesWithText(mapOf("en_US" to "second category")),
hidden = false,
sortOrder = 1
),
Category.StoredCategory(
categoryId = "3",
localizedName = LocalesWithText(mapOf("en_US" to "third category")),
hidden = true,
sortOrder = 2
)
)
}

val vm = createViewModel()

vm.onCategorySelected("3")

var category: Category? = null
val job = launch {
vm.selectedCategory.collect {
category = it
}
}
job.cancel()

assertEquals(
Category.StoredCategory(
categoryId = "2",
localizedName = LocalesWithText(mapOf("en_US" to "second category")),
hidden = false,
sortOrder = 1
),
category
)
}

@Test
fun `current phrases updated when category ID changed`() {
fakePhrasesUseCase._allCategories.update {
Expand Down

0 comments on commit 97e88ce

Please sign in to comment.