generated from bitwarden/template
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example test for MainViewModel (#113)
- Loading branch information
1 parent
c819454
commit d1b5f30
Showing
7 changed files
with
203 additions
and
3 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
60 changes: 60 additions & 0 deletions
60
app/src/test/java/com/bitwarden/authenticator/MainViewModelTest.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,60 @@ | ||
package com.bitwarden.authenticator | ||
|
||
import com.bitwarden.authenticator.data.platform.repository.SettingsRepository | ||
import com.bitwarden.authenticator.ui.platform.base.BaseViewModelTest | ||
import com.bitwarden.authenticator.ui.platform.feature.settings.appearance.model.AppTheme | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import io.mockk.verify | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import org.junit.jupiter.api.AfterEach | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
|
||
class MainViewModelTest : BaseViewModelTest() { | ||
|
||
private val mutableAppThemeFlow = MutableStateFlow(AppTheme.DEFAULT) | ||
private val mutableScreenCaptureAllowedFlow = MutableStateFlow(false) | ||
private val settingsRepository = mockk<SettingsRepository> { | ||
every { appTheme } returns AppTheme.DEFAULT | ||
every { appThemeStateFlow } returns mutableAppThemeFlow | ||
every { isScreenCaptureAllowedStateFlow } returns mutableScreenCaptureAllowedFlow | ||
} | ||
private lateinit var mainViewModel: MainViewModel | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
mainViewModel = MainViewModel(settingsRepository) | ||
} | ||
|
||
@AfterEach | ||
fun tearDown() { | ||
} | ||
|
||
@Test | ||
fun `on AppThemeChanged should update state`() { | ||
assertEquals( | ||
MainState( | ||
theme = AppTheme.DEFAULT, | ||
), | ||
mainViewModel.stateFlow.value, | ||
) | ||
mainViewModel.trySendAction( | ||
MainAction.Internal.ThemeUpdate( | ||
theme = AppTheme.DARK, | ||
), | ||
) | ||
assertEquals( | ||
MainState( | ||
theme = AppTheme.DARK, | ||
), | ||
mainViewModel.stateFlow.value, | ||
) | ||
|
||
verify { | ||
settingsRepository.appTheme | ||
settingsRepository.appThemeStateFlow | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
app/src/test/java/com/bitwarden/authenticator/ui/platform/base/BaseViewModelTest.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,29 @@ | ||
package com.bitwarden.authenticator.ui.platform.base | ||
|
||
import app.cash.turbine.ReceiveTurbine | ||
import app.cash.turbine.TurbineContext | ||
import app.cash.turbine.turbineScope | ||
import kotlinx.coroutines.CoroutineScope | ||
import org.junit.jupiter.api.extension.RegisterExtension | ||
|
||
abstract class BaseViewModelTest { | ||
@Suppress("unused", "JUnitMalformedDeclaration") | ||
@RegisterExtension | ||
protected open val mainDispatcherExtension = MainDispatcherExtension() | ||
|
||
protected suspend fun <S : Any, E : Any, T : BaseViewModel<S, E, *>> T.stateEventFlow( | ||
backgroundScope: CoroutineScope, | ||
validate: suspend TurbineContext.( | ||
stateFlow: ReceiveTurbine<S>, | ||
eventFlow: ReceiveTurbine<E>, | ||
) -> Unit, | ||
) { | ||
turbineScope { | ||
this.validate( | ||
this@stateEventFlow.stateFlow.testIn(backgroundScope), | ||
this@stateEventFlow.eventFlow.testIn(backgroundScope), | ||
) | ||
} | ||
} | ||
} | ||
|
45 changes: 45 additions & 0 deletions
45
app/src/test/java/com/bitwarden/authenticator/ui/platform/base/MainDispatcherExtension.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,45 @@ | ||
package com.bitwarden.authenticator.ui.platform.base | ||
|
||
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( | ||
private 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) | ||
} | ||
} |
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