Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Commit

Permalink
Add test for the Expense PieChart
Browse files Browse the repository at this point in the history
  • Loading branch information
ILIYANGERMANOV committed Apr 29, 2022
1 parent 0626a28 commit 93ffe8f
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 7 deletions.
10 changes: 10 additions & 0 deletions app/src/androidTest/java/com/ivy/wallet/compose/helpers/HomeTab.kt
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,14 @@ class HomeTab<A : ComponentActivity>(
composeTestRule.onNodeWithTag("home_greeting_text", useUnmergedTree = true)
.assertTextEquals(greeting)
}

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

fun clickExpenseCard() {
composeTestRule.onNodeWithTag("home_card_expense")
.performClick()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.ivy.wallet.compose.helpers

import androidx.activity.ComponentActivity
import androidx.compose.ui.test.*
import androidx.compose.ui.test.junit4.AndroidComposeTestRule
import androidx.test.ext.junit.rules.ActivityScenarioRule

class PieChartScreen<A : ComponentActivity>(
private val composeTestRule: AndroidComposeTestRule<ActivityScenarioRule<A>, A>
) {
fun assertTitle(title: String) {
composeTestRule.onNodeWithTag("piechart_title")
.assertTextContains(title)
}

fun assertTotalAmount(
amountInt: String,
decimalPart: String,
currency: String = "USD"
) {
val matchText: (String) -> SemanticsMatcher = { text ->
hasTestTag("piechart_total_amount")
.and(
hasAnyDescendant(
hasText(text)
)
)
}

composeTestRule.onNode(matchText(amountInt))
.assertIsDisplayed()

composeTestRule.onNode(matchText(decimalPart))
.assertIsDisplayed()

composeTestRule.onNode(matchText(currency))
.assertIsDisplayed()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.ivy.wallet.compose.scenario

import com.ivy.wallet.compose.IvyComposeTest
import com.ivy.wallet.compose.helpers.HomeTab
import com.ivy.wallet.compose.helpers.OnboardingFlow
import com.ivy.wallet.compose.helpers.PieChartScreen
import com.ivy.wallet.compose.helpers.TransactionFlow
import com.ivy.wallet.compose.printTree
import dagger.hilt.android.testing.HiltAndroidTest
import org.junit.Test

@HiltAndroidTest
class PieChartTest : IvyComposeTest() {
private val onboarding = OnboardingFlow(composeTestRule)
private val homeTab = HomeTab(composeTestRule)
private val transactionFlow = TransactionFlow(composeTestRule)
private val pieChartScreen = PieChartScreen(composeTestRule)

@Test
fun expensePieChart_realistic() {
onboarding.quickOnboarding()

transactionFlow.addExpense(
amount = 50.23
)

transactionFlow.addExpense(
amount = 150.72,
category = "Food & Drinks"
)

transactionFlow.addExpense(
amount = 75.0,
category = "Groceries"
)

transactionFlow.addExpense(
amount = 5.0,
title = "Bread",
category = "Groceries"
)
//----------------------------------------------------

homeTab.clickExpenseCard()

composeTestRule.printTree(useUnmergedTree = false)

pieChartScreen.assertTitle("Expenses")
pieChartScreen.assertTotalAmount(
amountInt = "280",
decimalPart = ".95",
currency = "USD"
)
}
}
8 changes: 6 additions & 2 deletions app/src/main/java/com/ivy/wallet/ui/home/HomeHeader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,8 @@ private fun IncomeExpenses(
textColor = White,
label = stringResource(R.string.income),
currency = currency,
amount = monthlyIncome
amount = monthlyIncome,
testTag = "home_card_income"
) {
nav.navigateTo(
PieChartStatistic(
Expand All @@ -289,7 +290,8 @@ private fun IncomeExpenses(
textColor = UI.colors.pure,
label = stringResource(R.string.expenses),
currency = currency,
amount = monthlyExpenses.absoluteValue
amount = monthlyExpenses.absoluteValue,
testTag = "home_card_expense"
) {
nav.navigateTo(
PieChartStatistic(
Expand All @@ -311,6 +313,7 @@ private fun RowScope.HeaderCard(
label: String,
currency: String,
amount: Double,
testTag: String,
onClick: () -> Unit
) {
Column(
Expand All @@ -321,6 +324,7 @@ private fun RowScope.HeaderCard(
}
.clip(UI.shapes.r4)
.background(backgroundGradient.asHorizontalBrush())
.testTag(testTag)
.clickable(
onClick = onClick
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
Expand Down Expand Up @@ -159,10 +160,11 @@ private fun BoxWithConstraintsScope.UI(
Spacer(Modifier.height(20.dp))

Text(
modifier = Modifier.padding(start = 32.dp),
text = if (transactionType == TransactionType.EXPENSE) stringResource(R.string.expenses) else stringResource(
R.string.income
),
modifier = Modifier
.padding(start = 32.dp)
.testTag("piechart_title"),
text = if (transactionType == TransactionType.EXPENSE)
stringResource(R.string.expenses) else stringResource(R.string.income),
style = UI.typo.b1.style(
fontWeight = FontWeight.ExtraBold
)
Expand All @@ -171,7 +173,8 @@ private fun BoxWithConstraintsScope.UI(
BalanceRow(
modifier = Modifier
.padding(start = 32.dp, end = 16.dp)
.alpha(percentExpanded),
.alpha(percentExpanded)
.testTag("piechart_total_amount"),
currency = currency,
balance = totalAmount,
currencyUpfront = false,
Expand Down

0 comments on commit 93ffe8f

Please sign in to comment.