Skip to content

Commit e1018cd

Browse files
committed
test ui state
1 parent bc8f6c7 commit e1018cd

File tree

3 files changed

+113
-8
lines changed

3 files changed

+113
-8
lines changed

Diff for: features/finances/impl/src/main/kotlin/br/com/mob1st/features/finances/impl/ui/builder/CategoryBuilderUiState.kt

+16-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import br.com.mob1st.features.finances.impl.domain.entities.Category
66
import br.com.mob1st.features.finances.impl.domain.entities.CategoryBuilder
77
import br.com.mob1st.features.finances.impl.domain.entities.CategorySuggestion
88
import kotlinx.collections.immutable.ImmutableList
9+
import kotlinx.collections.immutable.persistentListOf
910
import kotlinx.collections.immutable.plus
1011
import kotlinx.collections.immutable.toImmutableList
1112
import kotlinx.collections.immutable.toPersistentList
@@ -22,9 +23,14 @@ internal data class CategoryBuilderUiState(
2223
* The manually added categories.
2324
* The list is composed of the manually added categories and the "Add category" item.
2425
*/
25-
val manuallyAdded: ImmutableList<CategoryListItem> = builder?.manuallyAdded.orEmpty().map {
26-
ManualCategoryListItem(it)
27-
}.toPersistentList() + AddCategoryListItem
26+
val manuallyAdded: ImmutableList<CategoryListItem> = if (builder != null) {
27+
val categories = builder.manuallyAdded.map {
28+
ManualCategoryListItem(it)
29+
}.toPersistentList()
30+
categories + AddCategoryListItem
31+
} else {
32+
persistentListOf()
33+
}
2834

2935
/**
3036
* The suggestions presented to the user.
@@ -78,13 +84,20 @@ data class SuggestionListItem(
7884
}
7985
}
8086

87+
/**
88+
* Used by the [CategoryBuilderUiState.manuallyAdded] to show the manually added categories.
89+
* @property category The manually added category.
90+
*/
8191
@Immutable
8292
data class ManualCategoryListItem(val category: Category) : CategoryListItem {
8393
override val leading: TextState = TextState(category.name)
8494
override val value: TextState = TextState(category.amount.toString())
8595
override val supporting: TextState = TextState(category.recurrences.toString())
8696
}
8797

98+
/**
99+
* Used by the [CategoryBuilderUiState.manuallyAdded] to show the "Add category" item.
100+
*/
88101
@Immutable
89102
internal data object AddCategoryListItem : CategoryListItem {
90103
override val leading: TextState = TextState(0)

Diff for: features/finances/impl/src/test/kotlin/br/com/mob1st/features/finances/impl/ui/builder/CategoryBuilderConsumablesTest.kt

-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package br.com.mob1st.features.finances.impl.ui.builder
22

33
import br.com.mob1st.core.design.atoms.properties.texts.TextState
44
import br.com.mob1st.features.finances.impl.domain.entities.BuilderNextAction
5-
import br.com.mob1st.features.finances.impl.domain.entities.CategoryBuilder
65
import br.com.mob1st.features.finances.impl.domain.entities.CategorySuggestion
76
import br.com.mob1st.features.finances.impl.domain.entities.NotEnoughInputsException
87
import br.com.mob1st.features.finances.impl.utils.moduleFixture
@@ -179,10 +178,6 @@ class CategoryBuilderConsumablesTest {
179178
fun `GIVEN a next step WHEN navigate THEN navigate to the next step`() {
180179
// Given
181180
val categoryBuilderConsumables = CategoryBuilderConsumables()
182-
val categoryBuilder = moduleFixture<CategoryBuilder>().copy(
183-
manuallyAdded = listOf(moduleFixture()),
184-
suggestions = listOf(moduleFixture()),
185-
)
186181

187182
// When
188183
val step = moduleFixture<BuilderNextAction.Step>()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package br.com.mob1st.features.finances.impl.ui.builder
2+
3+
import br.com.mob1st.features.finances.impl.domain.entities.CategoryBuilder
4+
import br.com.mob1st.features.finances.impl.utils.moduleFixture
5+
import com.appmattus.kotlinfixture.Fixture
6+
import kotlinx.collections.immutable.persistentListOf
7+
import org.junit.jupiter.api.BeforeEach
8+
import org.junit.jupiter.api.Test
9+
import kotlin.test.assertEquals
10+
import kotlin.test.assertTrue
11+
12+
class CategoryBuilderUiStateTest {
13+
private lateinit var fixture: Fixture
14+
15+
@BeforeEach
16+
fun setUp() {
17+
fixture = moduleFixture.new {
18+
factory<CategoryBuilder> {
19+
CategoryBuilder(
20+
id = fixture(),
21+
manuallyAdded = fixture {
22+
repeatCount { 2 }
23+
},
24+
suggestions = fixture {
25+
repeatCount { 2 }
26+
},
27+
)
28+
}
29+
}
30+
}
31+
32+
@Test
33+
fun `GIVEN a category builder with categories WHEN get lists THEN assert lists is correct`() {
34+
// Given
35+
val categoryBuilder = fixture<CategoryBuilder>()
36+
val expectedManuallyAdded = persistentListOf(
37+
ManualCategoryListItem(
38+
category = categoryBuilder.manuallyAdded[0],
39+
),
40+
ManualCategoryListItem(
41+
category = categoryBuilder.manuallyAdded[1],
42+
),
43+
AddCategoryListItem,
44+
)
45+
val expectedSuggestions = persistentListOf(
46+
SuggestionListItem(
47+
suggestion = categoryBuilder.suggestions[0],
48+
),
49+
SuggestionListItem(
50+
suggestion = categoryBuilder.suggestions[1],
51+
),
52+
)
53+
// When
54+
val categoryBuilderUiState = CategoryBuilderUiState(categoryBuilder)
55+
56+
// Then
57+
assertEquals(
58+
expectedManuallyAdded,
59+
categoryBuilderUiState.manuallyAdded,
60+
)
61+
assertEquals(
62+
expectedSuggestions,
63+
categoryBuilderUiState.suggestions,
64+
)
65+
}
66+
67+
@Test
68+
fun `GIVEN a category builder without categories WHEN get lists THEN assert lists is empty`() {
69+
// Given
70+
val categoryBuilder = fixture<CategoryBuilder>().copy(
71+
manuallyAdded = emptyList(),
72+
suggestions = emptyList(),
73+
)
74+
val expectedManuallyAdded = persistentListOf(AddCategoryListItem)
75+
val expectedSuggestions = persistentListOf<SuggestionListItem>()
76+
// When
77+
val categoryBuilderUiState = CategoryBuilderUiState(categoryBuilder)
78+
79+
// Then
80+
assertEquals(
81+
expectedManuallyAdded,
82+
categoryBuilderUiState.manuallyAdded,
83+
)
84+
assertEquals(
85+
expectedSuggestions,
86+
categoryBuilderUiState.suggestions,
87+
)
88+
}
89+
90+
@Test
91+
fun `GIVEN a no category builder WHEN get lists THEN assert lists are empty`() {
92+
// Given
93+
val uiState = CategoryBuilderUiState()
94+
assertTrue(uiState.manuallyAdded.isEmpty())
95+
assertTrue(uiState.suggestions.isEmpty())
96+
}
97+
}

0 commit comments

Comments
 (0)