Skip to content

Commit 2f0af32

Browse files
Add error state test for loadYears in ScheduleViewModel
Co-authored-by: alvin000009238 <107313913+alvin000009238@users.noreply.github.com>
1 parent fd03a1f commit 2f0af32

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

android/app/src/test/java/com/clhs/score/viewmodel/ScheduleViewModelTest.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import kotlinx.coroutines.test.setMain
1818
import org.junit.After
1919
import org.junit.Assert.assertEquals
2020
import org.junit.Assert.assertFalse
21+
import org.junit.Assert.assertTrue
2122
import org.junit.Before
2223
import org.junit.Test
2324

@@ -74,6 +75,21 @@ class ScheduleViewModelTest {
7475
assertEquals("202", viewModel.uiState.value.selectedClassValue)
7576
}
7677

78+
@Test
79+
fun loadYearsErrorUpdatesState() = runTest(dispatcher) {
80+
val repository = ControllableScheduleRepository()
81+
val viewModel = ScheduleViewModel(repository)
82+
runCurrent()
83+
84+
repository.yearsDeferred.completeExceptionally(RuntimeException("Network error"))
85+
runCurrent()
86+
87+
val state = viewModel.uiState.value
88+
assertFalse(state.isLoading)
89+
assertTrue(state.isError)
90+
assertEquals("Network error", state.errorMessage)
91+
}
92+
7793
@Test
7894
fun savingWidgetPreferencesLogsAnonymousAnalytics() = runTest(dispatcher) {
7995
val repository = ControllableScheduleRepository()

0 commit comments

Comments
 (0)