Skip to content

Commit

Permalink
Add payment ui tests to Welcome Screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun committed Oct 18, 2023
1 parent 5e2f11d commit fe300b2
Showing 1 changed file with 168 additions and 7 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.PaymentState
import net.mullvad.mullvadvpn.compose.state.WelcomeUiState
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.viewmodel.WelcomeViewModel
import org.junit.Before
import org.junit.Rule
Expand All @@ -35,7 +40,10 @@ class WelcomeScreenTest {
onRedeemVoucherClick = {},
onSettingsClick = {},
onAccountClick = {},
openConnectScreen = {}
openConnectScreen = {},
onPurchaseBillingProductClick = {},
onTryVerificationAgain = {},
onTryFetchProductsAgain = {}
)
}

Expand All @@ -58,7 +66,10 @@ class WelcomeScreenTest {
onRedeemVoucherClick = {},
onSettingsClick = {},
onAccountClick = {},
openConnectScreen = {}
openConnectScreen = {},
onPurchaseBillingProductClick = {},
onTryVerificationAgain = {},
onTryFetchProductsAgain = {}
)
}

Expand Down Expand Up @@ -87,7 +98,10 @@ class WelcomeScreenTest {
onRedeemVoucherClick = {},
onSettingsClick = {},
onAccountClick = {},
openConnectScreen = {}
openConnectScreen = {},
onPurchaseBillingProductClick = {},
onTryVerificationAgain = {},
onTryFetchProductsAgain = {}
)
}

Expand All @@ -108,7 +122,10 @@ class WelcomeScreenTest {
onRedeemVoucherClick = {},
onSettingsClick = {},
onAccountClick = {},
openConnectScreen = {}
openConnectScreen = {},
onPurchaseBillingProductClick = {},
onTryVerificationAgain = {},
onTryFetchProductsAgain = {}
)
}

Expand All @@ -129,7 +146,10 @@ class WelcomeScreenTest {
onRedeemVoucherClick = {},
onSettingsClick = {},
onAccountClick = {},
openConnectScreen = mockClickListener
openConnectScreen = mockClickListener,
onPurchaseBillingProductClick = {},
onTryVerificationAgain = {},
onTryFetchProductsAgain = {}
)
}

Expand All @@ -150,7 +170,10 @@ class WelcomeScreenTest {
onRedeemVoucherClick = {},
onSettingsClick = {},
onAccountClick = {},
openConnectScreen = {}
openConnectScreen = {},
onPurchaseBillingProductClick = {},
onTryVerificationAgain = {},
onTryFetchProductsAgain = {}
)
}

Expand All @@ -174,7 +197,10 @@ class WelcomeScreenTest {
onRedeemVoucherClick = mockClickListener,
onSettingsClick = {},
onAccountClick = {},
openConnectScreen = {}
openConnectScreen = {},
onPurchaseBillingProductClick = {},
onTryVerificationAgain = {},
onTryFetchProductsAgain = {}
)
}

Expand All @@ -184,4 +210,139 @@ class WelcomeScreenTest {
// Assert
verify(exactly = 1) { mockClickListener.invoke() }
}

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

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

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

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

@Test
fun testShowBillingErrorDialog() {
// Arrange
composeTestRule.setContent {
WelcomeScreen(
showSitePayment = true,
uiState = WelcomeUiState(billingPaymentState = PaymentState.Error.BillingError),
uiSideEffect = MutableStateFlow(WelcomeViewModel.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 {
WelcomeScreen(
showSitePayment = true,
uiState =
WelcomeUiState(
billingPaymentState =
PaymentState.PaymentAvailable(listOf(mockPaymentProduct))
),
uiSideEffect = MutableStateFlow(WelcomeViewModel.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 {
WelcomeScreen(
showSitePayment = true,
uiState =
WelcomeUiState(
billingPaymentState =
PaymentState.PaymentAvailable(listOf(mockPaymentProduct))
),
uiSideEffect = MutableStateFlow(WelcomeViewModel.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 fe300b2

Please sign in to comment.