mirror of
https://github.com/bitwarden/android.git
synced 2026-06-02 11:12:00 -05:00
BIT-144: Setup root level app navigation (#8)
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package com.x8bit.bitwarden.example
|
||||
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.TestDispatcher
|
||||
import kotlinx.coroutines.test.UnconfinedTestDispatcher
|
||||
import kotlinx.coroutines.test.resetMain
|
||||
import kotlinx.coroutines.test.setMain
|
||||
import org.junit.jupiter.api.extension.AfterAllCallback
|
||||
import org.junit.jupiter.api.extension.AfterEachCallback
|
||||
import org.junit.jupiter.api.extension.BeforeAllCallback
|
||||
import org.junit.jupiter.api.extension.BeforeEachCallback
|
||||
import org.junit.jupiter.api.extension.ExtendWith
|
||||
import org.junit.jupiter.api.extension.ExtensionContext
|
||||
import org.junit.jupiter.api.extension.RegisterExtension
|
||||
|
||||
/**
|
||||
* JUnit 5 Extension for automatically setting a [testDispatcher] as the "main" dispatcher.
|
||||
*
|
||||
* Note that this may be used as a normal class property with [RegisterExtension] or may be applied
|
||||
* directly to a test class using [ExtendWith].
|
||||
*/
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class MainDispatcherExtension(
|
||||
val testDispatcher: TestDispatcher = UnconfinedTestDispatcher(),
|
||||
) : AfterAllCallback,
|
||||
AfterEachCallback,
|
||||
BeforeAllCallback,
|
||||
BeforeEachCallback {
|
||||
override fun afterAll(context: ExtensionContext?) {
|
||||
Dispatchers.resetMain()
|
||||
}
|
||||
|
||||
override fun afterEach(context: ExtensionContext?) {
|
||||
Dispatchers.resetMain()
|
||||
}
|
||||
|
||||
override fun beforeAll(context: ExtensionContext?) {
|
||||
Dispatchers.setMain(testDispatcher)
|
||||
}
|
||||
|
||||
override fun beforeEach(context: ExtensionContext?) {
|
||||
Dispatchers.setMain(testDispatcher)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.x8bit.bitwarden.example.ui
|
||||
|
||||
import com.x8bit.bitwarden.example.MainDispatcherExtension
|
||||
import org.junit.jupiter.api.extension.RegisterExtension
|
||||
|
||||
abstract class BaseViewModelTest {
|
||||
@RegisterExtension
|
||||
protected open val mainDispatcherExtension = MainDispatcherExtension()
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.x8bit.bitwarden.example.ui.feature.rootnav
|
||||
|
||||
import app.cash.turbine.test
|
||||
import com.x8bit.bitwarden.example.ui.BaseViewModelTest
|
||||
import com.x8bit.bitwarden.ui.feature.rootnav.RootNavState
|
||||
import com.x8bit.bitwarden.ui.feature.rootnav.RootNavViewModel
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class RootNavViewModelTests : BaseViewModelTest() {
|
||||
|
||||
@Test
|
||||
fun `initial state should be splash`() {
|
||||
val viewModel = RootNavViewModel()
|
||||
assert(viewModel.state.value is RootNavState.Splash)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `state should move from splash to login`() = runTest {
|
||||
val viewModel = RootNavViewModel()
|
||||
viewModel.state.test {
|
||||
assert(awaitItem() is RootNavState.Splash)
|
||||
assert(awaitItem() is RootNavState.Login)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user