|
1 | 1 | package br.com.mob1st.features.finances.impl.ui.category.detail
|
2 | 2 |
|
| 3 | +import app.cash.turbine.test |
| 4 | +import app.cash.turbine.turbineScope |
| 5 | +import br.com.mob1st.core.kotlinx.coroutines.DefaultCoroutineDispatcher |
| 6 | +import br.com.mob1st.core.kotlinx.structures.Money |
| 7 | +import br.com.mob1st.core.state.managers.ConsumableDelegate |
| 8 | +import br.com.mob1st.features.finances.impl.domain.entities.CalculatorPreferences |
| 9 | +import br.com.mob1st.features.finances.impl.domain.entities.CategoryDetail |
| 10 | +import br.com.mob1st.features.finances.impl.domain.fixtures.categoryDetail |
| 11 | +import br.com.mob1st.features.finances.impl.domain.usecases.GetCategoryDetailUseCase |
| 12 | +import br.com.mob1st.features.finances.impl.domain.usecases.SetCalculatorPreferencesUseCase |
| 13 | +import br.com.mob1st.features.finances.impl.domain.usecases.SetCategoryUseCase |
| 14 | +import br.com.mob1st.features.finances.impl.ui.fixtures.categoryEntry |
| 15 | +import br.com.mob1st.tests.featuresutils.MainDispatcherTestExtension |
| 16 | +import io.kotest.property.Arb |
| 17 | +import io.kotest.property.arbitrary.bind |
| 18 | +import io.kotest.property.arbitrary.next |
| 19 | +import io.mockk.every |
| 20 | +import io.mockk.mockk |
| 21 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 22 | +import kotlinx.coroutines.flow.Flow |
| 23 | +import kotlinx.coroutines.flow.MutableStateFlow |
| 24 | +import kotlinx.coroutines.flow.emptyFlow |
| 25 | +import kotlinx.coroutines.flow.flowOf |
| 26 | +import kotlinx.coroutines.test.TestDispatcher |
| 27 | +import kotlinx.coroutines.test.TestScope |
| 28 | +import kotlinx.coroutines.test.UnconfinedTestDispatcher |
| 29 | +import kotlinx.coroutines.test.runTest |
| 30 | +import org.junit.jupiter.api.BeforeEach |
3 | 31 | import org.junit.jupiter.api.Test
|
| 32 | +import org.junit.jupiter.api.extension.ExtendWith |
| 33 | +import kotlin.test.assertEquals |
| 34 | +import kotlin.test.assertFalse |
4 | 35 |
|
| 36 | +@ExtendWith(MainDispatcherTestExtension::class) |
5 | 37 | class CategoryViewModelTest {
|
| 38 | + private lateinit var getDetail: GetCategoryDetailUseCase |
| 39 | + private lateinit var setCategory: SetCategoryUseCase |
| 40 | + private lateinit var setPreferencesUseCase: SetCalculatorPreferencesUseCase |
| 41 | + private lateinit var categoryStateHandleTest: CategoryStateHandle |
| 42 | + |
| 43 | + @BeforeEach |
| 44 | + fun setUp() { |
| 45 | + getDetail = mockk() |
| 46 | + setCategory = mockk(relaxed = true) |
| 47 | + setPreferencesUseCase = mockk(relaxed = true) |
| 48 | + categoryStateHandleTest = mockk() |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + fun `GIVEN arguments And a saved state WHEN get initial state THEN saved data is used`() = runTest { |
| 53 | + val detail = Arb.categoryDetail().next() |
| 54 | + val entry = CategoryEntry(detail.category) |
| 55 | + givenInitialState(flowOf(detail), MutableStateFlow(entry)) |
| 56 | + val viewModel = initViewModel() |
| 57 | + val expectedUiState = CategoryDetailUiState.Loaded( |
| 58 | + detail = detail, |
| 59 | + entry = entry, |
| 60 | + ) |
| 61 | + turbineScope { |
| 62 | + val receivedUiState = viewModel.uiState.testIn(backgroundScope) |
| 63 | + val receivedConsumables = viewModel.consumablesState.testIn(backgroundScope) |
| 64 | + val actualUiState = receivedUiState.awaitItem() |
| 65 | + val actualConsumables = receivedConsumables.awaitItem() |
| 66 | + assertEquals(expectedUiState, actualUiState) |
| 67 | + assertEquals(CategoryDetailConsumables(), actualConsumables) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + fun `GIVEN a zeroed state WHEN get initial state THEN assert loading state is emitted`() = runTest { |
| 73 | + every { getDetail[any()] } returns emptyFlow() |
| 74 | + val viewModel = initViewModel() |
| 75 | + viewModel.uiState.test { |
| 76 | + val state = awaitItem() |
| 77 | + assertEquals(CategoryDetailUiState.Loading, state) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + fun `GIVEN a number WHEN type THEN assert ui state changed And new value is saved`() = runTest { |
| 83 | + val detail = Arb.categoryDetail().next().copy( |
| 84 | + preferences = CalculatorPreferences(isEditCentsEnabled = false), |
| 85 | + ) |
| 86 | + val entry = Arb.categoryEntry().next().copy(amount = Money.Zero) |
| 87 | + givenInitialState(flowOf(detail), MutableStateFlow(entry)) |
| 88 | + val viewModel = initViewModel() |
| 89 | + viewModel.uiState.test { |
| 90 | + skipItems(1) |
| 91 | + viewModel.type(1) |
| 92 | + val state = awaitItem() |
| 93 | + val expectedEntry = entry.copy( |
| 94 | + amount = Money(100), |
| 95 | + ) |
| 96 | + assertEquals(CategoryDetailUiState.Loaded(detail, expectedEntry), state) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + fun `GIVEN a state with number WHEN type THEN assert value is erased`() = runTest { |
| 102 | + val detail = Arb.categoryDetail().next().copy() |
| 103 | + val entry = Arb.categoryEntry().next().copy(amount = Money(1)) |
| 104 | + givenInitialState(flowOf(detail), MutableStateFlow(entry)) |
| 105 | + val viewModel = initViewModel() |
| 106 | + viewModel.uiState.test { |
| 107 | + skipItems(1) |
| 108 | + viewModel.deleteNumber() |
| 109 | + val state = awaitItem() |
| 110 | + val expectedEntry = entry.copy( |
| 111 | + amount = Money.Zero, |
| 112 | + ) |
| 113 | + assertEquals(CategoryDetailUiState.Loaded(detail, expectedEntry), state) |
| 114 | + } |
| 115 | + } |
| 116 | + |
6 | 117 | @Test
|
7 | 118 | fun test() {
|
8 |
| - assert(CategoryViewModel::class.equals("")) |
| 119 | + // test show enter name dialog |
| 120 | + // test show icon picker dialog |
| 121 | + // test show all recurrences dialog |
| 122 | + // test type category name |
| 123 | + // test submit dialogs |
| 124 | + // test enable/disable edit cents |
| 125 | + // test submission |
| 126 | + assertFalse(true) |
| 127 | + } |
| 128 | + |
| 129 | + private fun givenInitialState( |
| 130 | + categoryDetailFlow: Flow<CategoryDetail> = flowOf(Arb.categoryDetail().next()), |
| 131 | + entryFlow: MutableStateFlow<CategoryEntry> = MutableStateFlow(Arb.categoryEntry().next()), |
| 132 | + ) { |
| 133 | + every { getDetail[any()] } returns categoryDetailFlow |
| 134 | + every { categoryStateHandleTest.entry(any()) } returns entryFlow |
| 135 | + every { categoryStateHandleTest.update(any()) } answers { |
| 136 | + entryFlow.value = firstArg() |
| 137 | + } |
9 | 138 | }
|
| 139 | + |
| 140 | + @OptIn(ExperimentalCoroutinesApi::class) |
| 141 | + private fun TestScope.initViewModel( |
| 142 | + args: CategoryDetailArgs = Arb.bind<CategoryDetailArgs>().next(), |
| 143 | + testDispatcher: TestDispatcher = UnconfinedTestDispatcher(testScheduler), |
| 144 | + ) = CategoryViewModel( |
| 145 | + default = DefaultCoroutineDispatcher( |
| 146 | + testDispatcher, |
| 147 | + ), |
| 148 | + consumableDelegate = ConsumableDelegate(CategoryDetailConsumables()), |
| 149 | + categoryStateHandle = categoryStateHandleTest, |
| 150 | + getCategoryDetail = getDetail, |
| 151 | + setCategory = setCategory, |
| 152 | + setPreferences = setPreferencesUseCase, |
| 153 | + args = args, |
| 154 | + ) |
10 | 155 | }
|
0 commit comments