mirror of
https://github.com/bitwarden/android.git
synced 2026-11-19 17:40:12 -06:00
BIT-141 Setup basic Login and Landing screens (#40)
Co-authored-by: Caleb Derosier <caleb@livefront.com>
This commit is contained in:
committed by
Álison Fernandes
co-authored by
Caleb Derosier
parent
d8de9bb753
commit
024376b0d2
+130
-3
@@ -1,33 +1,37 @@
|
||||
package com.x8bit.bitwarden.ui.auth.feature.landing
|
||||
|
||||
import androidx.compose.ui.test.onNodeWithTag
|
||||
import androidx.compose.ui.test.onNodeWithText
|
||||
import androidx.compose.ui.test.performClick
|
||||
import androidx.compose.ui.test.performTextInput
|
||||
import com.x8bit.bitwarden.ui.platform.base.BaseComposeTest
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.verify
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.emptyFlow
|
||||
import kotlinx.coroutines.flow.flowOf
|
||||
import org.junit.Test
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
|
||||
class LandingScreenTest : BaseComposeTest() {
|
||||
|
||||
@Test
|
||||
fun `continue button click should send ContinueButtonClicked action`() {
|
||||
fun `continue button click should send ContinueButtonClick action`() {
|
||||
val viewModel = mockk<LandingViewModel>(relaxed = true) {
|
||||
every { eventFlow } returns emptyFlow()
|
||||
every { stateFlow } returns MutableStateFlow(
|
||||
LandingState(
|
||||
initialEmailAddress = "",
|
||||
emailInput = "",
|
||||
isContinueButtonEnabled = true,
|
||||
isRememberMeEnabled = false,
|
||||
),
|
||||
)
|
||||
every { trySendAction(LandingAction.ContinueButtonClick) } returns Unit
|
||||
}
|
||||
composeTestRule.setContent {
|
||||
LandingScreen(
|
||||
onNavigateToCreateAccount = {},
|
||||
onNavigateToLogin = {},
|
||||
viewModel = viewModel,
|
||||
)
|
||||
}
|
||||
@@ -36,4 +40,127 @@ class LandingScreenTest : BaseComposeTest() {
|
||||
viewModel.trySendAction(LandingAction.ContinueButtonClick)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `remember me click should send RememberMeToggle action`() {
|
||||
val viewModel = mockk<LandingViewModel>(relaxed = true) {
|
||||
every { eventFlow } returns emptyFlow()
|
||||
every { stateFlow } returns MutableStateFlow(
|
||||
LandingState(
|
||||
emailInput = "",
|
||||
isContinueButtonEnabled = true,
|
||||
isRememberMeEnabled = false,
|
||||
),
|
||||
)
|
||||
}
|
||||
composeTestRule.setContent {
|
||||
LandingScreen(
|
||||
onNavigateToCreateAccount = {},
|
||||
onNavigateToLogin = {},
|
||||
viewModel = viewModel,
|
||||
)
|
||||
}
|
||||
composeTestRule.onNodeWithTag("Remember me").performClick()
|
||||
verify {
|
||||
viewModel.trySendAction(LandingAction.RememberMeToggle(true))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `create account click should send CreateAccountClick action`() {
|
||||
val viewModel = mockk<LandingViewModel>(relaxed = true) {
|
||||
every { eventFlow } returns emptyFlow()
|
||||
every { stateFlow } returns MutableStateFlow(
|
||||
LandingState(
|
||||
emailInput = "",
|
||||
isContinueButtonEnabled = true,
|
||||
isRememberMeEnabled = false,
|
||||
),
|
||||
)
|
||||
}
|
||||
composeTestRule.setContent {
|
||||
LandingScreen(
|
||||
onNavigateToCreateAccount = {},
|
||||
onNavigateToLogin = {},
|
||||
viewModel = viewModel,
|
||||
)
|
||||
}
|
||||
composeTestRule.onNodeWithText("Create account").performClick()
|
||||
verify {
|
||||
viewModel.trySendAction(LandingAction.CreateAccountClick)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `email address change should send EmailInputChanged action`() {
|
||||
val input = "email"
|
||||
val viewModel = mockk<LandingViewModel>(relaxed = true) {
|
||||
every { eventFlow } returns emptyFlow()
|
||||
every { stateFlow } returns MutableStateFlow(
|
||||
LandingState(
|
||||
emailInput = "",
|
||||
isContinueButtonEnabled = true,
|
||||
isRememberMeEnabled = false,
|
||||
),
|
||||
)
|
||||
}
|
||||
composeTestRule.setContent {
|
||||
LandingScreen(
|
||||
onNavigateToCreateAccount = {},
|
||||
onNavigateToLogin = {},
|
||||
viewModel = viewModel,
|
||||
)
|
||||
}
|
||||
composeTestRule.onNodeWithTag("Email address").performTextInput(input)
|
||||
verify {
|
||||
viewModel.trySendAction(LandingAction.EmailInputChanged(input))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `NavigateToCreateAccount event should call onNavigateToCreateAccount`() {
|
||||
var onNavigateToCreateAccountCalled = false
|
||||
val viewModel = mockk<LandingViewModel>(relaxed = true) {
|
||||
every { eventFlow } returns flowOf(LandingEvent.NavigateToCreateAccount)
|
||||
every { stateFlow } returns MutableStateFlow(
|
||||
LandingState(
|
||||
emailInput = "",
|
||||
isContinueButtonEnabled = true,
|
||||
isRememberMeEnabled = false,
|
||||
),
|
||||
)
|
||||
}
|
||||
composeTestRule.setContent {
|
||||
LandingScreen(
|
||||
onNavigateToCreateAccount = { onNavigateToCreateAccountCalled = true },
|
||||
onNavigateToLogin = {},
|
||||
viewModel = viewModel,
|
||||
)
|
||||
}
|
||||
assert(onNavigateToCreateAccountCalled)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `NavigateToLogin event should call onNavigateToLogin`() {
|
||||
val testEmail = "test@test.com"
|
||||
var onNavigateToLoginEmail = ""
|
||||
val viewModel = mockk<LandingViewModel>(relaxed = true) {
|
||||
every { eventFlow } returns flowOf(LandingEvent.NavigateToLogin(testEmail))
|
||||
every { stateFlow } returns MutableStateFlow(
|
||||
LandingState(
|
||||
emailInput = "",
|
||||
isContinueButtonEnabled = true,
|
||||
isRememberMeEnabled = false,
|
||||
),
|
||||
)
|
||||
}
|
||||
composeTestRule.setContent {
|
||||
LandingScreen(
|
||||
onNavigateToCreateAccount = { },
|
||||
onNavigateToLogin = { onNavigateToLoginEmail = it },
|
||||
viewModel = viewModel,
|
||||
)
|
||||
}
|
||||
assertEquals(testEmail, onNavigateToLoginEmail)
|
||||
}
|
||||
}
|
||||
|
||||
+44
-7
@@ -1,5 +1,6 @@
|
||||
package com.x8bit.bitwarden.ui.auth.feature.landing
|
||||
|
||||
import androidx.lifecycle.SavedStateHandle
|
||||
import app.cash.turbine.test
|
||||
import com.x8bit.bitwarden.ui.platform.base.BaseViewModelTest
|
||||
import kotlinx.coroutines.test.runTest
|
||||
@@ -9,20 +10,42 @@ import org.junit.jupiter.api.Test
|
||||
class LandingViewModelTest : BaseViewModelTest() {
|
||||
|
||||
@Test
|
||||
fun `ContinueButtonClick should disable continue button`() = runTest {
|
||||
val viewModel = LandingViewModel()
|
||||
fun `initial state should be correct`() = runTest {
|
||||
val viewModel = LandingViewModel(SavedStateHandle())
|
||||
viewModel.stateFlow.test {
|
||||
assertEquals(DEFAULT_STATE, awaitItem())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `initial state should pull from saved state handle when present`() = runTest {
|
||||
val expectedState = DEFAULT_STATE.copy(
|
||||
emailInput = "test",
|
||||
isContinueButtonEnabled = false,
|
||||
isRememberMeEnabled = true,
|
||||
)
|
||||
val handle = SavedStateHandle(mapOf("state" to expectedState))
|
||||
val viewModel = LandingViewModel(handle)
|
||||
viewModel.stateFlow.test {
|
||||
assertEquals(expectedState, awaitItem())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `ContinueButtonClick should emit NavigateToLogin`() = runTest {
|
||||
val viewModel = LandingViewModel(SavedStateHandle())
|
||||
viewModel.eventFlow.test {
|
||||
viewModel.actionChannel.trySend(LandingAction.ContinueButtonClick)
|
||||
assertEquals(
|
||||
viewModel.stateFlow.value,
|
||||
DEFAULT_STATE.copy(isContinueButtonEnabled = false),
|
||||
LandingEvent.NavigateToLogin(""),
|
||||
awaitItem(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `CreateAccountClick should emit NavigateToCreateAccount`() = runTest {
|
||||
val viewModel = LandingViewModel()
|
||||
val viewModel = LandingViewModel(SavedStateHandle())
|
||||
viewModel.eventFlow.test {
|
||||
viewModel.actionChannel.trySend(LandingAction.CreateAccountClick)
|
||||
assertEquals(
|
||||
@@ -34,7 +57,7 @@ class LandingViewModelTest : BaseViewModelTest() {
|
||||
|
||||
@Test
|
||||
fun `RememberMeToggle should update value of isRememberMeToggled`() = runTest {
|
||||
val viewModel = LandingViewModel()
|
||||
val viewModel = LandingViewModel(SavedStateHandle())
|
||||
viewModel.eventFlow.test {
|
||||
viewModel.actionChannel.trySend(LandingAction.RememberMeToggle(true))
|
||||
assertEquals(
|
||||
@@ -44,9 +67,23 @@ class LandingViewModelTest : BaseViewModelTest() {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `EmailInputUpdated should update value of email input`() = runTest {
|
||||
val input = "input"
|
||||
val viewModel = LandingViewModel(SavedStateHandle())
|
||||
viewModel.stateFlow.test {
|
||||
awaitItem()
|
||||
viewModel.trySendAction(LandingAction.EmailInputChanged(input))
|
||||
assertEquals(
|
||||
DEFAULT_STATE.copy(emailInput = input),
|
||||
awaitItem(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val DEFAULT_STATE = LandingState(
|
||||
initialEmailAddress = "",
|
||||
emailInput = "",
|
||||
isContinueButtonEnabled = true,
|
||||
isRememberMeEnabled = false,
|
||||
)
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
package com.x8bit.bitwarden.ui.auth.feature.login
|
||||
|
||||
import androidx.compose.ui.test.onNodeWithText
|
||||
import androidx.compose.ui.test.performClick
|
||||
import androidx.compose.ui.test.performTextInput
|
||||
import com.x8bit.bitwarden.ui.platform.base.BaseComposeTest
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.verify
|
||||
import junit.framework.TestCase.assertTrue
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.emptyFlow
|
||||
import kotlinx.coroutines.flow.flowOf
|
||||
import org.junit.Test
|
||||
|
||||
class LoginScreenTest : BaseComposeTest() {
|
||||
|
||||
@Test
|
||||
fun `Not you text click should send NotYouButtonClick action`() {
|
||||
val viewModel = mockk<LoginViewModel>(relaxed = true) {
|
||||
every { eventFlow } returns emptyFlow()
|
||||
every { stateFlow } returns MutableStateFlow(
|
||||
LoginState(
|
||||
emailAddress = "",
|
||||
isLoginButtonEnabled = false,
|
||||
passwordInput = "",
|
||||
),
|
||||
)
|
||||
}
|
||||
composeTestRule.setContent {
|
||||
LoginScreen(
|
||||
onNavigateToLanding = {},
|
||||
viewModel = viewModel,
|
||||
)
|
||||
}
|
||||
composeTestRule.onNodeWithText("Not you?").performClick()
|
||||
verify {
|
||||
viewModel.trySendAction(LoginAction.NotYouButtonClick)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `password input change should send PasswordInputChanged action`() {
|
||||
val input = "input"
|
||||
val viewModel = mockk<LoginViewModel>(relaxed = true) {
|
||||
every { eventFlow } returns emptyFlow()
|
||||
every { stateFlow } returns MutableStateFlow(
|
||||
LoginState(
|
||||
emailAddress = "",
|
||||
isLoginButtonEnabled = false,
|
||||
passwordInput = "",
|
||||
),
|
||||
)
|
||||
}
|
||||
composeTestRule.setContent {
|
||||
LoginScreen(
|
||||
onNavigateToLanding = {},
|
||||
viewModel = viewModel,
|
||||
)
|
||||
}
|
||||
composeTestRule.onNodeWithText("Master password").performTextInput(input)
|
||||
verify {
|
||||
viewModel.trySendAction(LoginAction.PasswordInputChanged(input))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `NavigateToLanding should call onNavigateToLanding`() {
|
||||
var onNavigateToLandingCalled = false
|
||||
val viewModel = mockk<LoginViewModel>(relaxed = true) {
|
||||
every { eventFlow } returns flowOf(LoginEvent.NavigateToLanding)
|
||||
every { stateFlow } returns MutableStateFlow(
|
||||
LoginState(
|
||||
emailAddress = "",
|
||||
isLoginButtonEnabled = false,
|
||||
passwordInput = "",
|
||||
),
|
||||
)
|
||||
}
|
||||
composeTestRule.setContent {
|
||||
LoginScreen(
|
||||
onNavigateToLanding = { onNavigateToLandingCalled = true },
|
||||
viewModel = viewModel,
|
||||
)
|
||||
}
|
||||
assertTrue(onNavigateToLandingCalled)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package com.x8bit.bitwarden.ui.auth.feature.login
|
||||
|
||||
import androidx.lifecycle.SavedStateHandle
|
||||
import app.cash.turbine.test
|
||||
import com.x8bit.bitwarden.ui.platform.base.BaseViewModelTest
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class LoginViewModelTest : BaseViewModelTest() {
|
||||
|
||||
private val savedStateHandle = SavedStateHandle().also {
|
||||
it["email_address"] = "test@gmail.com"
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `initial state should be correct`() = runTest {
|
||||
val viewModel = LoginViewModel(savedStateHandle)
|
||||
viewModel.stateFlow.test {
|
||||
assertEquals(DEFAULT_STATE, awaitItem())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `initial state should pull from handle when present`() = runTest {
|
||||
val expectedState = DEFAULT_STATE.copy(
|
||||
passwordInput = "input",
|
||||
isLoginButtonEnabled = true,
|
||||
)
|
||||
val handle = SavedStateHandle(
|
||||
mapOf(
|
||||
"email_address" to "test@gmail.com",
|
||||
"state" to expectedState,
|
||||
),
|
||||
)
|
||||
val viewModel = LoginViewModel(handle)
|
||||
viewModel.stateFlow.test {
|
||||
assertEquals(expectedState, awaitItem())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `LoginButtonClick should do nothing`() = runTest {
|
||||
val viewModel = LoginViewModel(
|
||||
savedStateHandle = savedStateHandle,
|
||||
)
|
||||
viewModel.eventFlow.test {
|
||||
viewModel.actionChannel.trySend(LoginAction.LoginButtonClick)
|
||||
assertEquals(DEFAULT_STATE, viewModel.stateFlow.value)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `SingleSignOnClick should do nothing`() = runTest {
|
||||
val viewModel = LoginViewModel(
|
||||
savedStateHandle = savedStateHandle,
|
||||
)
|
||||
viewModel.eventFlow.test {
|
||||
viewModel.actionChannel.trySend(LoginAction.SingleSignOnClick)
|
||||
assertEquals(DEFAULT_STATE, viewModel.stateFlow.value)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `NotYouButtonClick should emit NavigateToLanding`() = runTest {
|
||||
val viewModel = LoginViewModel(
|
||||
savedStateHandle = savedStateHandle,
|
||||
)
|
||||
viewModel.eventFlow.test {
|
||||
viewModel.actionChannel.trySend(LoginAction.NotYouButtonClick)
|
||||
assertEquals(
|
||||
LoginEvent.NavigateToLanding,
|
||||
awaitItem(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `PasswordInputChanged should update password input`() = runTest {
|
||||
val input = "input"
|
||||
val viewModel = LoginViewModel(
|
||||
savedStateHandle = savedStateHandle,
|
||||
)
|
||||
viewModel.eventFlow.test {
|
||||
viewModel.actionChannel.trySend(LoginAction.PasswordInputChanged(input))
|
||||
assertEquals(
|
||||
DEFAULT_STATE.copy(passwordInput = input),
|
||||
viewModel.stateFlow.value,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val DEFAULT_STATE = LoginState(
|
||||
emailAddress = "test@gmail.com",
|
||||
passwordInput = "",
|
||||
isLoginButtonEnabled = false,
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user