-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from adidas/feature/add-tests-to-sample
Added tests to sample
- Loading branch information
Showing
5 changed files
with
104 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
mvi-sample/src/test/kotlin/com/adidas/mvi/sample/login/viewmodel/CoroutineListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.adidas.mvi.sample.login.viewmodel | ||
|
||
import io.kotest.core.listeners.TestListener | ||
import io.kotest.core.test.TestCase | ||
import io.kotest.core.test.TestResult | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.cancel | ||
import kotlinx.coroutines.test.TestDispatcher | ||
import kotlinx.coroutines.test.resetMain | ||
import kotlinx.coroutines.test.setMain | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
internal class CoroutineListener( | ||
private val testCoroutineDispatcher: TestDispatcher, | ||
) : TestListener { | ||
|
||
override suspend fun beforeContainer(testCase: TestCase) { | ||
Dispatchers.setMain(testCoroutineDispatcher) | ||
} | ||
|
||
override suspend fun afterContainer(testCase: TestCase, result: TestResult) { | ||
Dispatchers.resetMain() | ||
testCoroutineDispatcher.scheduler.cancel() | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
mvi-sample/src/test/kotlin/com/adidas/mvi/sample/login/viewmodel/LoginViewModelTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.adidas.mvi.sample.login.viewmodel | ||
|
||
import com.adidas.mvi.kotest.GivenViewModel | ||
import io.kotest.core.spec.IsolationMode | ||
import io.kotest.core.spec.style.BehaviorSpec | ||
import io.kotest.matchers.shouldBe | ||
import io.mockk.mockk | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.test.TestCoroutineScheduler | ||
import kotlinx.coroutines.test.UnconfinedTestDispatcher | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
class LoginViewModelTest : BehaviorSpec({ | ||
|
||
isolationMode = IsolationMode.InstancePerLeaf | ||
|
||
val testCoroutineDispatcher = UnconfinedTestDispatcher(TestCoroutineScheduler()) | ||
listeners(CoroutineListener(testCoroutineDispatcher)) | ||
|
||
fun getViewModel(): LoginViewModel { | ||
return LoginViewModel( | ||
logger = mockk(relaxed = true), | ||
coroutineDispatcher = testCoroutineDispatcher | ||
) | ||
} | ||
|
||
GivenViewModel(::getViewModel) { | ||
When("no intent is executed") { | ||
ThenState<LoginState.LoggedOut>() | ||
} | ||
|
||
WhenIntent(LoginIntent.Close) { | ||
ThenState<LoginState.LoggedOut>(LoginSideEffect.Close) | ||
} | ||
|
||
WhenIntent(LoginIntent.Login("username", "password")) { | ||
|
||
ThenState<LoginState.LoggedOut> { state -> | ||
state.view.isLoggingIn shouldBe true | ||
} | ||
|
||
testCoroutineDispatcher.scheduler.advanceUntilIdle() | ||
|
||
ThenState<LoginState.LoggedIn>() | ||
} | ||
|
||
WhenIntent(LoginIntent.Logout) { | ||
ThenState<LoginState.LoggedOut>() | ||
} | ||
|
||
WhenIntent(LoginIntent.Login("", "")) { | ||
testCoroutineDispatcher.scheduler.advanceUntilIdle() | ||
|
||
ThenState<LoginState.LoggedOut>(LoginSideEffect.ShowInvalidCredentialsError) | ||
} | ||
|
||
} | ||
|
||
}) |