Skip to content

Commit

Permalink
WIP: Rework tests - fix CalculatorTest.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
ILIYANGERMANOV committed Jun 11, 2022
1 parent 810c504 commit c139a09
Show file tree
Hide file tree
Showing 10 changed files with 471 additions and 515 deletions.
17 changes: 2 additions & 15 deletions app/src/androidTest/java/com/ivy/wallet/compose/IvyComposeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import androidx.work.testing.WorkManagerTestInitHelper
import com.ivy.frp.test.TestIdlingResource
import com.ivy.frp.test.TestingContext
import com.ivy.frp.view.navigation.Navigation
import com.ivy.wallet.compose.helpers.*
import com.ivy.wallet.compose.helpers.OnboardingFlow
import com.ivy.wallet.io.network.IvySession
import com.ivy.wallet.io.persistence.IvyRoomDatabase
import com.ivy.wallet.io.persistence.SharedPrefs
Expand Down Expand Up @@ -47,19 +47,6 @@ abstract class IvyComposeTest {
val composeTestRule = createAndroidComposeRule<RootActivity>()
// use createAndroidComposeRule<YourActivity>() if you need access to an activity

//----------------------------
protected val onboarding = OnboardingFlow(composeTestRule)
protected val amountInput = IvyAmountInput(composeTestRule)
protected val accountModal = AccountModal(composeTestRule)
protected val transactionFlow = TransactionFlow(composeTestRule)
protected val homeTab = HomeTab(composeTestRule)
protected val accountsTab = AccountsTab(composeTestRule)
protected val editTransactionScreen = TransactionScreen(composeTestRule)
protected val itemStatisticScreen = ItemStatisticScreen(composeTestRule)
protected val reorderModal = ReorderModal(composeTestRule)
protected val deleteConfirmationModal = DeleteConfirmationModal(composeTestRule)
//----------------------------

private var idlingResource: IdlingResource? = null

@Inject
Expand Down Expand Up @@ -143,7 +130,7 @@ abstract class IvyComposeTest {
test: OnboardingFlow.() -> Unit
) {
try {
onboarding.test()
OnboardingFlow(composeTestRule).test()
} catch (e: Throwable) {
if (attempt < maxAttempts) {
//reset state && retry test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,61 @@ import com.ivy.wallet.compose.IvyComposeTestRule

class BudgetModal(
private val composeTestRule: IvyComposeTestRule
) {
) : AmountInput<BudgetModal>, DeleteItem<BudgetsScreen> {
private val amountInput = IvyAmountInput(composeTestRule)

fun enterName(budgetName: String) {
fun enterName(budgetName: String): BudgetModal {
composeTestRule.onNodeWithTag("base_input")
.performTextReplacement(budgetName)
return this
}

fun enterAmount(amount: String) {
private fun clickBudgetAmount(): IvyAmountInput {
composeTestRule.onNodeWithTag("amount_balance")
.performClick()

amountInput.enterNumber(amount)
return IvyAmountInput(composeTestRule)
}

fun clickCategory(category: String) {
fun clickCategory(category: String): BudgetModal {
composeTestRule.onNode(
hasText(category)
.and(hasAnyAncestor(hasTestTag("budget_categories_row"))),
useUnmergedTree = true
).performClick()
return this
}

fun clickAdd() {
fun clickAdd(): BudgetsScreen {
composeTestRule.onNodeWithText("Add")
.performClick()
return BudgetsScreen(composeTestRule)
}

fun clickSave() {
fun clickSave(): BudgetsScreen {
composeTestRule.onNodeWithText("Save")
.performClick()
return BudgetsScreen(composeTestRule)
}

fun clickDelete() {
private fun clickDelete(): DeleteConfirmationModal {
composeTestRule.onNodeWithTag("modal_delete")
.performClick()
return DeleteConfirmationModal(composeTestRule)
}

fun clickClose() {
fun clickClose(): BudgetsScreen {
composeTestRule.onNodeWithContentDescription("close")
.performClick()
return BudgetsScreen(composeTestRule)
}

override fun enterAmount(number: String): BudgetModal {
return clickBudgetAmount()
.enterNumber(number, next = this)
}

override fun deleteWithConfirmation(): BudgetsScreen {
return clickDelete()
.confirmDelete(next = BudgetsScreen(composeTestRule))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import com.ivy.wallet.compose.IvyComposeTestRule
class BudgetsScreen(
private val composeTestRule: IvyComposeTestRule
) {
fun clickAddBudget() {
fun clickAddBudget(): BudgetModal {
composeTestRule.onNodeWithText("Add budget")
.performClick()
return BudgetModal(composeTestRule)
}

fun assertBudgetsInfo(
appBudget: String?,
categoryBudget: String?,
currency: String = "USD"
) {
): BudgetsScreen {
val budgetInfoNode = composeTestRule.onNodeWithTag("budgets_info_text")

when {
Expand All @@ -33,21 +34,27 @@ class BudgetsScreen(
}
else -> error("Unexpected case")
}

return this
}

fun clickBudget(
budgetName: String
) {
): BudgetModal {
composeTestRule.onNodeWithText(budgetName)
.performScrollTo()
.performClick()

return BudgetModal(composeTestRule)
}

fun assertBudgetDoesNotExist(
budgetName: String
) {
): BudgetsScreen {
composeTestRule.onNodeWithText(budgetName)
.assertDoesNotExist()

return this
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.ivy.wallet.compose.helpers

import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performClick
import com.ivy.wallet.compose.IvyComposeTestRule

class CalculatorAmountInput(
composeTestRule: IvyComposeTestRule
) : IvyAmountInput(composeTestRule) {
fun enterNumber(
number: String,
): CalculatorAmountInput {
return super.enterNumber(
number = number,
next = CalculatorAmountInput(composeTestRule),
onCalculator = true,
autoPressNonCalculator = false
)
}

fun pressPlus(): CalculatorAmountInput {
composeTestRule.onNodeWithTag("key_+")
.performClick()
return this
}

fun pressMinus(): CalculatorAmountInput {
composeTestRule.onNodeWithTag("key_-")
.performClick()
return this
}

fun pressMultiplication(): CalculatorAmountInput {
composeTestRule.onNodeWithTag("key_*")
.performClick()
return this
}

fun pressDivision(): CalculatorAmountInput {
composeTestRule.onNodeWithTag("key_/")
.performClick()
return this
}

fun pressLeftBracket(): CalculatorAmountInput {
composeTestRule.onNodeWithTag("key_(")
.performClick()
return this
}

fun pressRightBracket(): CalculatorAmountInput {
composeTestRule.onNodeWithTag("key_)")
.performClick()
return this
}

fun pressCalcEqual(): CalculatorAmountInput {
composeTestRule.onNodeWithTag("key_=")
.performClick()
return this
}

fun clickCalcSet(): CalculatorAmountInput {
composeTestRule.onNodeWithTag("calc_set")
.performClick()
return this
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ class HomeMoreMenu(
.performClick()
}

fun clickBudgets() {
fun clickBudgets(): BudgetsScreen {
composeTestRule.onNodeWithText("Budgets")
.performClick()
return BudgetsScreen(composeTestRule)
}

fun clickCategories() {
Expand Down
39 changes: 27 additions & 12 deletions app/src/androidTest/java/com/ivy/wallet/compose/helpers/HomeTab.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ class HomeTab(
): HomeTab {
composeTestRule.onNodeWithTag("home_balance")
.assertTextEquals(currency, amount, amountDecimal)
return HomeTab(composeTestRule)
return this
}

fun clickTransaction(
amount: String,
title: String? = null,
account: String? = null,
category: String? = null
) {
): TransactionScreen {
var matcher = hasTestTag("type_amount_currency")
.and(hasAnyDescendant(hasText(amount)))

Expand Down Expand Up @@ -70,80 +70,95 @@ class HomeTab(
)
.assertIsDisplayed()
.performClick()

return TransactionScreen(composeTestRule)
}

fun assertTransactionNotExists(
amount: String
) {
): HomeTab {
composeTestRule.onNode(
hasTestTag("transaction_card")
.and(hasText(amount))
).assertDoesNotExist()

return this
}

fun assertUpcomingIncome(
amount: String,
currency: String
) {
): HomeTab {
composeTestRule.onNodeWithTag(
testTag = "upcoming_income",
useUnmergedTree = true
).assertTextEquals("$amount $currency")

return this
}

fun assertUpcomingExpense(
amount: String,
currency: String
) {
): HomeTab {
composeTestRule.onNodeWithTag(
testTag = "upcoming_expense",
useUnmergedTree = true
).assertTextEquals("$amount $currency")

return this
}

fun dismissPrompt() {
fun dismissPrompt(): HomeTab {
composeTestRule.onNodeWithContentDescription("prompt_dismiss")
.performClick()
return this
}

fun assertUpcomingDoesNotExist() {
fun assertUpcomingDoesNotExist(): HomeTab {
composeTestRule.onNodeWithTag(
testTag = "upcoming_title",
useUnmergedTree = true
).assertDoesNotExist()
return this
}

fun clickUpcoming() {
fun clickUpcoming(): HomeTab {
composeTestRule.onNodeWithTag(
testTag = "upcoming_title",
useUnmergedTree = true
).performClick()
return this
}

fun clickTransactionPay() {
fun clickTransactionPay(): HomeTab {
composeTestRule.onNode(
hasText("Pay")
.and(hasAnyAncestor(hasTestTag("transaction_card")))
)
.performScrollTo()
.performClick()
return this
}

fun assertGreeting(
greeting: String
) {
): HomeTab {
composeTestRule.onNodeWithTag("home_greeting_text", useUnmergedTree = true)
.assertTextEquals(greeting)
return this
}

fun clickIncomeCard() {
fun clickIncomeCard(): PieChartScreen {
composeTestRule.onNodeWithTag("home_card_income")
.performClick()
return PieChartScreen(composeTestRule)
}

fun clickExpenseCard() {
fun clickExpenseCard(): PieChartScreen {
composeTestRule.onNodeWithTag("home_card_expense")
.performClick()
return PieChartScreen(composeTestRule)
}

override fun clickAddFAB(): AddFABMenu {
Expand Down
Loading

0 comments on commit c139a09

Please sign in to comment.