Skip to content

Commit

Permalink
Clean up landing screen test (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
brian-livefront authored and vvolkgang committed Jun 20, 2024
1 parent 3f742cc commit 42d4a5c
Showing 1 changed file with 68 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package com.x8bit.bitwarden.ui.auth.feature.landing

import androidx.compose.ui.test.assertIsEnabled
import androidx.compose.ui.test.assertIsNotEnabled
import androidx.compose.ui.test.assertIsOff
import androidx.compose.ui.test.assertIsOn
import androidx.compose.ui.test.assertTextEquals
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performScrollTo
Expand All @@ -18,16 +21,10 @@ import org.junit.Test
import org.junit.jupiter.api.Assertions.assertEquals

class LandingScreenTest : BaseComposeTest() {

@Test
fun `continue button should be enabled or disabled according to the state`() {
val mutableStateFlow = MutableStateFlow(
LandingState(
emailInput = "",
isContinueButtonEnabled = true,
isRememberMeEnabled = false,
selectedRegion = LandingState.RegionOption.BITWARDEN_US,
),
)
val mutableStateFlow = MutableStateFlow(DEFAULT_STATE)
val viewModel = mockk<LandingViewModel>(relaxed = true) {
every { eventFlow } returns emptyFlow()
every { stateFlow } returns mutableStateFlow
Expand All @@ -50,14 +47,7 @@ class LandingScreenTest : BaseComposeTest() {
fun `continue button click should send ContinueButtonClick action`() {
val viewModel = mockk<LandingViewModel>(relaxed = true) {
every { eventFlow } returns emptyFlow()
every { stateFlow } returns MutableStateFlow(
LandingState(
emailInput = "",
isContinueButtonEnabled = true,
isRememberMeEnabled = false,
selectedRegion = LandingState.RegionOption.BITWARDEN_US,
),
)
every { stateFlow } returns MutableStateFlow(DEFAULT_STATE)
}
composeTestRule.setContent {
LandingScreen(
Expand All @@ -73,18 +63,32 @@ class LandingScreenTest : BaseComposeTest() {
}

@Test
fun `remember me click should send RememberMeToggle action`() {
fun `remember me should be toggled on or off according to the state`() {
val mutableStateFlow = MutableStateFlow(DEFAULT_STATE)
val viewModel = mockk<LandingViewModel>(relaxed = true) {
every { eventFlow } returns emptyFlow()
every { stateFlow } returns MutableStateFlow(
LandingState(
emailInput = "",
isContinueButtonEnabled = true,
isRememberMeEnabled = false,
selectedRegion = LandingState.RegionOption.BITWARDEN_US,
),
every { stateFlow } returns mutableStateFlow
}
composeTestRule.setContent {
LandingScreen(
onNavigateToCreateAccount = {},
onNavigateToLogin = { _ -> },
viewModel = viewModel,
)
}
composeTestRule.onNodeWithText("Remember me").assertIsOff()

mutableStateFlow.update { it.copy(isRememberMeEnabled = true) }

composeTestRule.onNodeWithText("Remember me").assertIsOn()
}

@Test
fun `remember me click should send RememberMeToggle action`() {
val viewModel = mockk<LandingViewModel>(relaxed = true) {
every { eventFlow } returns emptyFlow()
every { stateFlow } returns MutableStateFlow(DEFAULT_STATE)
}
composeTestRule.setContent {
LandingScreen(
onNavigateToCreateAccount = {},
Expand All @@ -104,14 +108,7 @@ class LandingScreenTest : BaseComposeTest() {
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,
selectedRegion = LandingState.RegionOption.BITWARDEN_US,
),
)
every { stateFlow } returns MutableStateFlow(DEFAULT_STATE)
}
composeTestRule.setContent {
LandingScreen(
Expand All @@ -126,19 +123,38 @@ class LandingScreenTest : BaseComposeTest() {
}
}

@Test
fun `email address should change according to state`() {
val mutableStateFlow = MutableStateFlow(DEFAULT_STATE)
val viewModel = mockk<LandingViewModel>(relaxed = true) {
every { eventFlow } returns emptyFlow()
every { stateFlow } returns mutableStateFlow
}
composeTestRule.setContent {
LandingScreen(
onNavigateToCreateAccount = {},
onNavigateToLogin = { _ -> },
viewModel = viewModel,
)
}

composeTestRule
.onNodeWithText("Email address")
.assertTextEquals("Email address", "")

mutableStateFlow.update { it.copy(emailInput = "[email protected]") }

composeTestRule
.onNodeWithText("Email address")
.assertTextEquals("Email address", "[email protected]")
}

@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,
selectedRegion = LandingState.RegionOption.BITWARDEN_US,
),
)
every { stateFlow } returns MutableStateFlow(DEFAULT_STATE)
}
composeTestRule.setContent {
LandingScreen(
Expand All @@ -158,14 +174,7 @@ class LandingScreenTest : BaseComposeTest() {
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,
selectedRegion = LandingState.RegionOption.BITWARDEN_US,
),
)
every { stateFlow } returns MutableStateFlow(DEFAULT_STATE)
}
composeTestRule.setContent {
LandingScreen(
Expand All @@ -185,14 +194,7 @@ class LandingScreenTest : BaseComposeTest() {

val viewModel = mockk<LandingViewModel>(relaxed = true) {
every { eventFlow } returns flowOf(LandingEvent.NavigateToLogin(testEmail))
every { stateFlow } returns MutableStateFlow(
LandingState(
emailInput = "",
isContinueButtonEnabled = true,
isRememberMeEnabled = false,
selectedRegion = LandingState.RegionOption.BITWARDEN_US,
),
)
every { stateFlow } returns MutableStateFlow(DEFAULT_STATE)
}

composeTestRule.setContent {
Expand All @@ -213,14 +215,7 @@ class LandingScreenTest : BaseComposeTest() {
val selectedRegion = LandingState.RegionOption.BITWARDEN_EU
val viewModel = mockk<LandingViewModel>(relaxed = true) {
every { eventFlow } returns emptyFlow()
every { stateFlow } returns MutableStateFlow(
LandingState(
emailInput = "",
isContinueButtonEnabled = true,
isRememberMeEnabled = false,
selectedRegion = LandingState.RegionOption.BITWARDEN_US,
),
)
every { stateFlow } returns MutableStateFlow(DEFAULT_STATE)
}

composeTestRule.setContent {
Expand All @@ -241,4 +236,13 @@ class LandingScreenTest : BaseComposeTest() {
viewModel.trySendAction(LandingAction.RegionOptionSelect(selectedRegion))
}
}

companion object {
val DEFAULT_STATE = LandingState(
emailInput = "",
isContinueButtonEnabled = true,
isRememberMeEnabled = false,
selectedRegion = LandingState.RegionOption.BITWARDEN_US,
)
}
}

0 comments on commit 42d4a5c

Please sign in to comment.