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 17, 2023
1 parent 2ca1b20 commit d86a2cf
Showing 1 changed file with 177 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
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 = {},
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 = {},
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 mockAccountNumber = "1111 2222 3333 4444"
composeTestRule.setContent {
WelcomeScreen(
showSitePayment = true,
uiState = WelcomeUiState(accountNumber = mockAccountNumber),
viewActions = MutableSharedFlow(),
onSitePaymentClick = {},
onRedeemVoucherClick = {},
onSettingsClick = {},
openConnectScreen = {}
)
}

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

@Test
fun testOpenAccountView() {
// Arrange
composeTestRule.setContent {
WelcomeScreen(
showSitePayment = true,
uiState = WelcomeUiState(),
viewActions = MutableStateFlow(WelcomeViewModel.ViewAction.OpenAccountView("222")),
onSitePaymentClick = {},
onRedeemVoucherClick = {},
onSettingsClick = {},
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 = {},
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 = {},
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 = {},
openConnectScreen = {}
)
}

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

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

0 comments on commit d86a2cf

Please sign in to comment.