Skip to content

Commit

Permalink
Add ui tests for welcome screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun committed Aug 23, 2023
1 parent 39ffeb5 commit ac0e9fd
Showing 1 changed file with 185 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package net.mullvad.mullvadvpn.compose.screen

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.mockk
import io.mockk.verify
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import net.mullvad.mullvadvpn.compose.state.WelcomeUiState
import net.mullvad.mullvadvpn.viewmodel.WelcomeViewModel
import org.junit.Before
import org.junit.Rule
import org.junit.Test

class WelcomeScreenTest {
@get:Rule val composeTestRule = createComposeRule()

@Before
fun setup() {
MockKAnnotations.init(this)
}

@Test
fun testDefaultState() {
// Arrange
composeTestRule.setContent {
WelcomeScreen(
showSitePayment = true,
uiState = WelcomeUiState(),
viewActions = MutableSharedFlow(),
onSitePaymentClick = {},
onRedeemVoucherClick = {},
onSettingsClick = {},
onAccountClick = {},
openConnectScreen = {}
)
}

// Assert
composeTestRule.apply {
onNodeWithText("Congrats!").assertExists()
onNodeWithText("Here’s your account number. Save it!").assertExists()
}
}

@Test
fun testDisableSitePayment() {
// Arrange
composeTestRule.setContent {
WelcomeScreen(
showSitePayment = false,
uiState = WelcomeUiState(),
viewActions = MutableSharedFlow(),
onSitePaymentClick = {},
onRedeemVoucherClick = {},
onSettingsClick = {},
onAccountClick = {},
openConnectScreen = {}
)
}

// Assert
composeTestRule.apply {
onNodeWithText(
"Either buy credit on our website or redeem a voucher.",
substring = true
)
.assertDoesNotExist()
onNodeWithText("Buy credit").assertDoesNotExist()
}
}

@Test
fun testShowAccountNumber() {
// Arrange
val rawAccountNumber = "1111222233334444"
val expectedAccountNumber = "1111 2222 3333 4444"
composeTestRule.setContent {
WelcomeScreen(
showSitePayment = true,
uiState = WelcomeUiState(accountNumber = rawAccountNumber),
viewActions = MutableSharedFlow(),
onSitePaymentClick = {},
onRedeemVoucherClick = {},
onSettingsClick = {},
onAccountClick = {},
openConnectScreen = {}
)
}

// Assert
composeTestRule.apply { onNodeWithText(expectedAccountNumber).assertExists() }
}

@Test
fun testOpenAccountView() {
// Arrange
composeTestRule.setContent {
WelcomeScreen(
showSitePayment = true,
uiState = WelcomeUiState(),
viewActions = MutableStateFlow(WelcomeViewModel.ViewAction.OpenAccountView("222")),
onSitePaymentClick = {},
onRedeemVoucherClick = {},
onSettingsClick = {},
onAccountClick = {},
openConnectScreen = {}
)
}

// Assert
composeTestRule.apply { onNodeWithText("Congrats!").assertDoesNotExist() }
}

@Test
fun testOpenConnectScreen() {
// Arrange
val mockClickListener: () -> Unit = mockk(relaxed = true)
composeTestRule.setContent {
WelcomeScreen(
showSitePayment = true,
uiState = WelcomeUiState(),
viewActions = MutableStateFlow(WelcomeViewModel.ViewAction.OpenConnectScreen),
onSitePaymentClick = {},
onRedeemVoucherClick = {},
onSettingsClick = {},
onAccountClick = {},
openConnectScreen = mockClickListener
)
}

// Assert
verify(exactly = 1) { mockClickListener.invoke() }
}

@Test
fun testClickSitePaymentButton() {
// Arrange
val mockClickListener: () -> Unit = mockk(relaxed = true)
composeTestRule.setContent {
WelcomeScreen(
showSitePayment = true,
uiState = WelcomeUiState(),
viewActions = MutableStateFlow(WelcomeViewModel.ViewAction.OpenConnectScreen),
onSitePaymentClick = mockClickListener,
onRedeemVoucherClick = {},
onSettingsClick = {},
onAccountClick = {},
openConnectScreen = {}
)
}

// Act
composeTestRule.apply { onNodeWithText("Buy credit").performClick() }

// Assert
verify(exactly = 1) { mockClickListener.invoke() }
}

@Test
fun testClickRedeemVoucher() {
// Arrange
val mockClickListener: () -> Unit = mockk(relaxed = true)
composeTestRule.setContent {
WelcomeScreen(
showSitePayment = true,
uiState = WelcomeUiState(),
viewActions = MutableStateFlow(WelcomeViewModel.ViewAction.OpenConnectScreen),
onSitePaymentClick = {},
onRedeemVoucherClick = mockClickListener,
onSettingsClick = {},
onAccountClick = {},
openConnectScreen = {}
)
}

// Act
composeTestRule.apply { onNodeWithText("Redeem voucher").performClick() }

// Assert
verify(exactly = 1) { mockClickListener.invoke() }
}
}

0 comments on commit ac0e9fd

Please sign in to comment.