|
| 1 | +package com.threegap.bitnagil.presentation.common.mviviewmodel |
| 2 | + |
| 3 | +import androidx.lifecycle.SavedStateHandle |
| 4 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 5 | +import kotlinx.coroutines.test.runTest |
| 6 | +import kotlinx.parcelize.Parcelize |
| 7 | +import org.junit.Before |
| 8 | +import org.junit.Test |
| 9 | +import org.orbitmvi.orbit.syntax.simple.SimpleSyntax |
| 10 | +import org.orbitmvi.orbit.test.test |
| 11 | + |
| 12 | +@ExperimentalCoroutinesApi |
| 13 | +class MviViewModelTest { |
| 14 | + private lateinit var sampleMviViewModel: SampleMviViewModel |
| 15 | + |
| 16 | + @Before |
| 17 | + fun setUp() { |
| 18 | + sampleMviViewModel = SampleMviViewModel(initState = SampleState(), savedStateHandle = SavedStateHandle()) |
| 19 | + } |
| 20 | + |
| 21 | + @Test |
| 22 | + fun `state는 호출 순서대로 갱신되어야 한다`() = |
| 23 | + runTest { |
| 24 | + sampleMviViewModel.test(testScope = this) { |
| 25 | + containerHost.sendIntent(SampleIntent.Increase(number = 1)) |
| 26 | + containerHost.sendIntent(SampleIntent.Decrease(number = 2)) |
| 27 | + containerHost.sendIntent(SampleIntent.Increase(number = 3)) |
| 28 | + |
| 29 | + expectState { SampleState() } |
| 30 | + expectState { SampleState(count = 1) } |
| 31 | + expectState { SampleState(count = -1) } |
| 32 | + expectState { SampleState(count = 2) } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + fun `state와 sideEffect는 호출 순서대로 갱신되어야 한다`() = |
| 38 | + runTest { |
| 39 | + sampleMviViewModel.test(testScope = this) { |
| 40 | + containerHost.sendIntent(SampleIntent.Increase(number = 1)) |
| 41 | + containerHost.sendIntent(SampleIntent.Clear) |
| 42 | + containerHost.sendIntent(SampleIntent.Decrease(number = 2)) |
| 43 | + containerHost.sendIntent(SampleIntent.Increase(number = 3)) |
| 44 | + |
| 45 | + expectState { SampleState() } |
| 46 | + expectState { SampleState(count = 1) } |
| 47 | + expectSideEffect(SampleSideEffect.ShowToast("Clear")) |
| 48 | + expectState { SampleState() } |
| 49 | + expectState { SampleState(count = -2) } |
| 50 | + expectState { SampleState(count = 1) } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // only for test |
| 55 | + private class SampleMviViewModel( |
| 56 | + initState: SampleState, |
| 57 | + savedStateHandle: SavedStateHandle, |
| 58 | + ) : MviViewModel<SampleState, SampleSideEffect, SampleIntent>(initState, savedStateHandle) { |
| 59 | + override suspend fun SimpleSyntax<SampleState, SampleSideEffect>.reduceState( |
| 60 | + intent: SampleIntent, |
| 61 | + state: SampleState, |
| 62 | + ): SampleState? { |
| 63 | + val newState = |
| 64 | + when (intent) { |
| 65 | + is SampleIntent.Decrease -> state.copy(count = state.count - intent.number) |
| 66 | + is SampleIntent.Increase -> state.copy(count = state.count + intent.number) |
| 67 | + SampleIntent.Clear -> { |
| 68 | + sendSideEffect(sideEffect = SampleSideEffect.ShowToast("Clear")) |
| 69 | + state.copy(count = 0) |
| 70 | + } |
| 71 | + } |
| 72 | + return newState |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Parcelize |
| 77 | + private data class SampleState( |
| 78 | + val count: Int = 0, |
| 79 | + ) : MviState |
| 80 | + |
| 81 | + private sealed class SampleSideEffect : MviSideEffect { |
| 82 | + data class ShowToast(val message: String) : SampleSideEffect() |
| 83 | + } |
| 84 | + |
| 85 | + private sealed class SampleIntent : MviIntent { |
| 86 | + data class Increase(val number: Int) : SampleIntent() |
| 87 | + |
| 88 | + data class Decrease(val number: Int) : SampleIntent() |
| 89 | + |
| 90 | + object Clear : SampleIntent() |
| 91 | + } |
| 92 | +} |
0 commit comments