Skip to content

Commit f2f3643

Browse files
committed
remove errors
1 parent 9feff35 commit f2f3643

2 files changed

Lines changed: 15 additions & 49 deletions

File tree

kmvi/src/commonMain/kotlin/io/github/natobytes/kmvi/ViewModel.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import kotlinx.coroutines.flow.SharedFlow
2020
import kotlinx.coroutines.flow.StateFlow
2121
import kotlinx.coroutines.flow.asSharedFlow
2222
import kotlinx.coroutines.flow.asStateFlow
23-
import kotlinx.coroutines.flow.catch
2423
import kotlinx.coroutines.flow.flowOn
2524
import kotlinx.coroutines.flow.launchIn
2625
import kotlinx.coroutines.flow.onEach
@@ -61,7 +60,6 @@ abstract class ViewModel<I : Intent, R : Result, E : Effect, S : State>(
6160
private val reducer: Reducer<R, S>,
6261
private val computationDispatcher: CoroutineDispatcher = Dispatchers.Default,
6362
private val mainDispatcher: CoroutineDispatcher = Dispatchers.Main,
64-
private val onError: ((Throwable) -> Unit)? = null
6563
) : ViewModel() {
6664

6765
private val _state = MutableStateFlow(initialState)

kmvi/src/commonTest/kotlin/io/github/natobytes/kmvi/ViewModelTest.kt

Lines changed: 15 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,12 @@ class TestReducer : Reducer<TestResult, TestState> {
7070
@OptIn(ExperimentalCoroutinesApi::class)
7171
class TestViewModel(
7272
initialState: TestState = TestState(),
73-
onError: ((Throwable) -> Unit)? = null
7473
) : ViewModel<TestIntent, TestResult, TestResult, TestState>(
7574
initialState = initialState,
7675
processor = TestProcessor(),
7776
reducer = TestReducer(),
7877
computationDispatcher = UnconfinedTestDispatcher(),
7978
mainDispatcher = UnconfinedTestDispatcher(),
80-
onError = onError
8179
)
8280

8381
@OptIn(ExperimentalCoroutinesApi::class)
@@ -87,99 +85,69 @@ class ViewModelTest {
8785
fun `initial state is set correctly`() = runTest {
8886
val initialState = TestState(count = 5, message = "initial")
8987
val viewModel = TestViewModel(initialState)
90-
88+
9189
assertEquals(initialState, viewModel.state.value)
9290
}
9391

9492
@Test
9593
fun `process intent updates state through reducer`() = runTest {
9694
val viewModel = TestViewModel()
97-
95+
9896
viewModel.process(TestIntent.Increment)
9997
delay(100) // Give time for processing
100-
98+
10199
assertEquals(1, viewModel.state.value.count)
102100
}
103101

104102
@Test
105103
fun `multiple intents are processed sequentially`() = runTest {
106104
val viewModel = TestViewModel()
107-
105+
108106
viewModel.process(TestIntent.Increment)
109107
viewModel.process(TestIntent.Increment)
110108
viewModel.process(TestIntent.Decrement)
111109
delay(100)
112-
110+
113111
assertEquals(1, viewModel.state.value.count)
114112
}
115113

116114
@Test
117115
fun `state message is updated correctly`() = runTest {
118116
val viewModel = TestViewModel()
119-
117+
120118
viewModel.process(TestIntent.SetMessage("Hello"))
121119
delay(100)
122-
120+
123121
assertEquals("Hello", viewModel.state.value.message)
124122
}
125123

126124
@Test
127125
fun `effects are emitted correctly`() = runTest {
128126
val viewModel = TestViewModel()
129127
val effects = mutableListOf<TestResult>()
130-
128+
131129
// Collect effects in background
132130
val job = kotlinx.coroutines.launch {
133131
viewModel.effects.take(1).toList(effects)
134132
}
135-
133+
136134
viewModel.process(TestIntent.TriggerEffect)
137135
delay(100)
138136
job.cancel()
139-
137+
140138
assertEquals(1, effects.size)
141139
assertTrue(effects.first() is TestResult.ShowToast)
142140
assertEquals("Effect triggered", (effects.first() as TestResult.ShowToast).message)
143141
}
144142

145-
@Test
146-
fun `errors are emitted to error flow`() = runTest {
147-
val viewModel = TestViewModel()
148-
val errors = mutableListOf<Throwable>()
149-
150-
val job = kotlinx.coroutines.launch {
151-
viewModel.errors.take(1).toList(errors)
152-
}
153-
154-
viewModel.process(TestIntent.ThrowError)
155-
delay(100)
156-
job.cancel()
157-
158-
assertEquals(1, errors.size)
159-
assertTrue(errors.first() is IllegalStateException)
160-
assertEquals("Test error", errors.first().message)
161-
}
162-
163-
@Test
164-
fun `custom error handler is invoked`() = runTest {
165-
var capturedError: Throwable? = null
166-
val viewModel = TestViewModel(onError = { capturedError = it })
167-
168-
viewModel.process(TestIntent.ThrowError)
169-
delay(100)
170-
171-
assertNotNull(capturedError)
172-
assertTrue(capturedError is IllegalStateException)
173-
}
174-
175143
@Test
176144
fun `state updates maintain immutability`() = runTest {
177145
val viewModel = TestViewModel(TestState(count = 10))
178146
val initialState = viewModel.state.value
179-
147+
180148
viewModel.process(TestIntent.Increment)
181149
delay(100)
182-
150+
183151
// Initial state should not be modified
184152
assertEquals(10, initialState.count)
185153
// New state should be updated
@@ -190,15 +158,15 @@ class ViewModelTest {
190158
fun `reducer is pure function test`() = runTest {
191159
val reducer = TestReducer()
192160
val initialState = TestState(count = 5, message = "test")
193-
161+
194162
val result1 = reducer.reduce(TestResult.UpdateCount(2), initialState)
195163
val result2 = reducer.reduce(TestResult.UpdateCount(2), initialState)
196-
164+
197165
// Same inputs should produce same outputs
198166
assertEquals(result1, result2)
199167
assertEquals(7, result1.count)
200168
assertEquals(7, result2.count)
201-
169+
202170
// Original state should not be modified
203171
assertEquals(5, initialState.count)
204172
}

0 commit comments

Comments
 (0)