Skip to content

Commit

Permalink
Add payment ui tests to OutOfTimeScreen
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun committed Nov 9, 2023
1 parent d612e12 commit 11fe214
Showing 1 changed file with 140 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ 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.MutableStateFlow
import net.mullvad.mullvadvpn.compose.setContentWithTheme
import net.mullvad.mullvadvpn.compose.state.OutOfTimeUiState
import net.mullvad.mullvadvpn.compose.state.PaymentState
import net.mullvad.mullvadvpn.lib.payment.model.PaymentProduct
import net.mullvad.mullvadvpn.lib.payment.model.PaymentStatus
import net.mullvad.mullvadvpn.lib.payment.model.PurchaseResult
import net.mullvad.mullvadvpn.model.TunnelState
import net.mullvad.mullvadvpn.viewmodel.OutOfTimeViewModel
import org.junit.Before
Expand Down Expand Up @@ -174,4 +179,139 @@ class OutOfTimeScreenTest {
// Assert
verify(exactly = 1) { mockClickListener.invoke() }
}

@Test
fun testShowPurchaseCompleteDialog() {
// Arrange
composeTestRule.setContent {
OutOfTimeScreen(
showSitePayment = true,
uiState = OutOfTimeUiState(purchaseResult = PurchaseResult.PurchaseCompleted),
uiSideEffect = MutableStateFlow(OutOfTimeViewModel.UiSideEffect.OpenConnectScreen),
onSitePaymentClick = {},
onRedeemVoucherClick = {},
onSettingsClick = {},
onAccountClick = {},
openConnectScreen = {},
onPurchaseBillingProductClick = {},
onTryVerificationAgain = {},
onTryFetchProductsAgain = {}
)
}

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

@Test
fun testShowVerificationErrorDialog() {
// Arrange
composeTestRule.setContent {
OutOfTimeScreen(
showSitePayment = true,
uiState =
OutOfTimeUiState(purchaseResult = PurchaseResult.Error.VerificationError(null)),
uiSideEffect = MutableStateFlow(OutOfTimeViewModel.UiSideEffect.OpenConnectScreen),
onSitePaymentClick = {},
onRedeemVoucherClick = {},
onSettingsClick = {},
onAccountClick = {},
openConnectScreen = {},
onPurchaseBillingProductClick = {},
onTryVerificationAgain = {},
onTryFetchProductsAgain = {}
)
}

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

@Test
fun testShowBillingErrorDialog() {
// Arrange
composeTestRule.setContent {
OutOfTimeScreen(
showSitePayment = true,
uiState = OutOfTimeUiState(billingPaymentState = PaymentState.Error.BillingError),
uiSideEffect = MutableStateFlow(OutOfTimeViewModel.UiSideEffect.OpenConnectScreen),
onSitePaymentClick = {},
onRedeemVoucherClick = {},
onSettingsClick = {},
onAccountClick = {},
openConnectScreen = {},
onPurchaseBillingProductClick = {},
onTryVerificationAgain = {},
onTryFetchProductsAgain = {}
)
}

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

@Test
fun testShowBillingPaymentAvailable() {
// Arrange
val mockPaymentProduct: PaymentProduct = mockk()
every { mockPaymentProduct.price } returns "$10"
every { mockPaymentProduct.status } returns PaymentStatus.AVAILABLE
composeTestRule.setContent {
OutOfTimeScreen(
showSitePayment = true,
uiState =
OutOfTimeUiState(
billingPaymentState =
PaymentState.PaymentAvailable(listOf(mockPaymentProduct))
),
uiSideEffect = MutableStateFlow(OutOfTimeViewModel.UiSideEffect.OpenConnectScreen),
onSitePaymentClick = {},
onRedeemVoucherClick = {},
onSettingsClick = {},
onAccountClick = {},
openConnectScreen = {},
onPurchaseBillingProductClick = {},
onTryVerificationAgain = {},
onTryFetchProductsAgain = {}
)
}

// 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"
every { mockPaymentProduct.status } returns PaymentStatus.AVAILABLE
composeTestRule.setContent {
OutOfTimeScreen(
showSitePayment = true,
uiState =
OutOfTimeUiState(
billingPaymentState =
PaymentState.PaymentAvailable(listOf(mockPaymentProduct))
),
uiSideEffect = MutableStateFlow(OutOfTimeViewModel.UiSideEffect.OpenConnectScreen),
onSitePaymentClick = {},
onRedeemVoucherClick = {},
onSettingsClick = {},
onAccountClick = {},
openConnectScreen = {},
onPurchaseBillingProductClick = clickHandler,
onTryVerificationAgain = {},
onTryFetchProductsAgain = {}
)
}

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

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

0 comments on commit 11fe214

Please sign in to comment.