Skip to content

Commit

Permalink
Add ui tests for account screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun committed Sep 20, 2023
1 parent 74bea8a commit 49ffdb5
Showing 1 changed file with 100 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@ import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import io.mockk.MockKAnnotations
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import net.mullvad.mullvadvpn.compose.state.AccountDialogState
import net.mullvad.mullvadvpn.compose.state.AccountUiState
import net.mullvad.mullvadvpn.compose.state.PaymentState
import net.mullvad.mullvadvpn.lib.payment.PaymentProduct
import net.mullvad.mullvadvpn.viewmodel.AccountViewModel
import org.junit.Before
import org.junit.Rule
import org.junit.Test

@OptIn(ExperimentalMaterial3Api::class)
class AccountScreenTest {
@get:Rule val composeTestRule = createComposeRule()

Expand All @@ -23,7 +28,6 @@ class AccountScreenTest {
MockKAnnotations.init(this)
}

@OptIn(ExperimentalMaterial3Api::class)
@Test
fun testDefaultState() {
// Arrange
Expand All @@ -47,7 +51,6 @@ class AccountScreenTest {
}
}

@OptIn(ExperimentalMaterial3Api::class)
@Test
fun testManageAccountClick() {
// Arrange
Expand All @@ -58,7 +61,8 @@ class AccountScreenTest {
AccountUiState(
deviceName = DUMMY_DEVICE_NAME,
accountNumber = DUMMY_ACCOUNT_NUMBER,
accountExpiry = null
accountExpiry = null,
webPaymentAvailable = true
),
viewActions = MutableSharedFlow<AccountViewModel.ViewAction>().asSharedFlow(),
enterTransitionEndAction = MutableSharedFlow<Unit>().asSharedFlow(),
Expand All @@ -73,7 +77,6 @@ class AccountScreenTest {
verify { mockedClickHandler.invoke() }
}

@OptIn(ExperimentalMaterial3Api::class)
@Test
fun testRedeemVoucherClick() {
// Arrange
Expand All @@ -99,7 +102,6 @@ class AccountScreenTest {
verify { mockedClickHandler.invoke() }
}

@OptIn(ExperimentalMaterial3Api::class)
@Test
fun testLogoutClick() {
// Arrange
Expand All @@ -125,6 +127,99 @@ class AccountScreenTest {
verify { mockedClickHandler.invoke() }
}

@Test
fun testShowPurchaseCompleteDialog() {
// Arrange
composeTestRule.setContent {
AccountScreen(
uiState = AccountUiState(dialogState = AccountDialogState.PurchaseComplete),
viewActions = MutableSharedFlow<AccountViewModel.ViewAction>().asSharedFlow(),
enterTransitionEndAction = MutableSharedFlow<Unit>().asSharedFlow()
)
}

// Assert
composeTestRule.onNodeWithText("Time was successfully added").assertExists()
}

@Test
fun testShowVerificationErrorDialog() {
// Arrange
composeTestRule.setContent {
AccountScreen(
uiState = AccountUiState(dialogState = AccountDialogState.VerificationError),
viewActions = MutableSharedFlow<AccountViewModel.ViewAction>().asSharedFlow(),
enterTransitionEndAction = MutableSharedFlow<Unit>().asSharedFlow()
)
}

// Assert
composeTestRule.onNodeWithText("Payment was unsuccessful").assertExists()
}

@Test
fun testShowBillingErrorDialog() {
// Arrange
composeTestRule.setContent {
AccountScreen(
uiState = AccountUiState(dialogState = AccountDialogState.BillingError),
viewActions = MutableSharedFlow<AccountViewModel.ViewAction>().asSharedFlow(),
enterTransitionEndAction = MutableSharedFlow<Unit>().asSharedFlow()
)
}

// Assert
composeTestRule.onNodeWithText("Google Play services not available").assertExists()
}

@Test
fun testShowBillingPaymentAvailable() {
// Arrange
val mockPaymentProduct: PaymentProduct = mockk()
every { mockPaymentProduct.price } returns "$10"
composeTestRule.setContent {
AccountScreen(
uiState =
AccountUiState(
billingPaymentState =
PaymentState.PaymentAvailable(listOf(mockPaymentProduct))
),
viewActions = MutableSharedFlow<AccountViewModel.ViewAction>().asSharedFlow(),
enterTransitionEndAction = MutableSharedFlow<Unit>().asSharedFlow()
)
}

// Assert
composeTestRule.onNodeWithText("Add 30 days time ($10)").assertExists()
}

@Test
fun testOnPurchaseBillingProductClick() {
// Arrange
val clickHandler: (String) -> Unit = mockk(relaxed = true)
val mockPaymentProduct: PaymentProduct = mockk()
every { mockPaymentProduct.price } returns "$10"
every { mockPaymentProduct.productId } returns "PRODUCT_ID"
composeTestRule.setContent {
AccountScreen(
uiState =
AccountUiState(
billingPaymentState =
PaymentState.PaymentAvailable(listOf(mockPaymentProduct))
),
onPurchaseBillingProductClick = clickHandler,
viewActions = MutableSharedFlow<AccountViewModel.ViewAction>().asSharedFlow(),
enterTransitionEndAction = MutableSharedFlow<Unit>().asSharedFlow()
)
}

//Act
composeTestRule.onNodeWithText("Add 30 days time ($10)").performClick()

// Assert
verify { clickHandler.invoke("PRODUCT_ID") }
}

companion object {
private const val DUMMY_DEVICE_NAME = "fake_name"
private const val DUMMY_ACCOUNT_NUMBER = "fake_number"
Expand Down

0 comments on commit 49ffdb5

Please sign in to comment.