Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ abstract class BaseStateMachineViewModel<TState : Any, TEvent : Any, TUiSideEffe
.filter { it.accept(event, newState) }
.forEach { it.handle(event, oldState = currentState, newState) }
}
stateMachine.process(event)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import net.thunderbird.core.logging.Logger
import net.thunderbird.core.logging.testing.TestLogger
import net.thunderbird.core.testing.coroutines.MainDispatcherHelper

class StateMachineViewModelTest {
class BaseStateMachineViewModelTest {

@OptIn(ExperimentalCoroutinesApi::class)
private val mainDispatcher = MainDispatcherHelper(UnconfinedTestDispatcher())
Expand Down Expand Up @@ -136,6 +136,19 @@ class StateMachineViewModelTest {
assertThat(eventTwoHandled).isTrue()
}

@Test
fun `event() should process an event only once when dispatched`() = runTest {
val stateMachine = FakeStateMachine("Initial state")
val viewModel = TestStateMachineViewModel(
initialState = "Initial state",
stateMachine = stateMachine,
)

viewModel.event("Test event")

assertThat(stateMachine.processCount).isEqualTo(1)
}

@Test
fun `should trigger side effect handlers on state change`() = runTest {
var sideEffectTriggered = false
Expand Down Expand Up @@ -183,7 +196,11 @@ class StateMachineViewModelTest {
private val _currentState = MutableStateFlow(initialState)
override val currentState: StateFlow<String> = _currentState.asStateFlow()

var processCount = 0
private set

override suspend fun process(event: String): String {
processCount++
_currentState.value = event
return _currentState.value
}
Expand Down
Loading