Skip to content

Commit

Permalink
MBL-1833: Compose UI tests for PledgeItemizedDetails composable (#2161)
Browse files Browse the repository at this point in the history
  • Loading branch information
leighdouglas authored Oct 30, 2024
1 parent c708225 commit 5ab6976
Show file tree
Hide file tree
Showing 2 changed files with 294 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import com.kickstarter.R
Expand Down Expand Up @@ -71,6 +72,21 @@ fun ItemizedRewardListContainerPreview() {
}
}

enum class PledgeItemizedDetailsTestTag {
DELIVERY_DATE,
PAGE_TITLE,
ITEM_NAME,
ITEM_COST,
PLEDGE_AMOUNT_TITLE,
TOTAL_AMOUNT,
BONUS_SUPPORT_TITLE,
DISCLAIMER_TEXT,
BONUS_SUPPORT,
SHIPPING_TITLE,
SHIPPING_AMOUNT,
CURRENCY_CONVERSION,
}

@Composable
fun ItemizedRewardListContainer(
modifier: Modifier = Modifier,
Expand Down Expand Up @@ -99,6 +115,7 @@ fun ItemizedRewardListContainer(
)
) {
Text(
modifier = Modifier.testTag(PledgeItemizedDetailsTestTag.PAGE_TITLE.name),
text = stringResource(id = R.string.Your_pledge),
style = typography.headline,
color = colors.textPrimary
Expand All @@ -108,6 +125,7 @@ fun ItemizedRewardListContainer(
Spacer(modifier = Modifier.height(dimensions.paddingXSmall))

Text(
modifier = Modifier.testTag(PledgeItemizedDetailsTestTag.DELIVERY_DATE.name),
text = deliveryDateString,
style = typography.caption1,
color = colors.textSecondary
Expand All @@ -123,6 +141,7 @@ fun ItemizedRewardListContainer(

Row {
Text(
modifier = Modifier.testTag(PledgeItemizedDetailsTestTag.ITEM_NAME.name),
text = it.first,
style = typography.subheadlineMedium,
color = colors.textSecondary
Expand All @@ -131,6 +150,7 @@ fun ItemizedRewardListContainer(
Spacer(modifier = Modifier.weight(1f))

Text(
modifier = Modifier.testTag(PledgeItemizedDetailsTestTag.ITEM_COST.name),
text = it.second,
style = typography.subheadlineMedium,
color = colors.textSecondary
Expand All @@ -147,6 +167,7 @@ fun ItemizedRewardListContainer(

Row {
Text(
modifier = Modifier.testTag(PledgeItemizedDetailsTestTag.SHIPPING_TITLE.name),
text = ksString?.format(
stringResource(id = R.string.Shipping_to_country),
"country",
Expand All @@ -159,6 +180,7 @@ fun ItemizedRewardListContainer(
Spacer(modifier = Modifier.weight(1f))

Text(
modifier = Modifier.testTag(PledgeItemizedDetailsTestTag.SHIPPING_AMOUNT.name),
text = shippingAmountString,
style = typography.subheadlineMedium,
color = colors.textSecondary
Expand All @@ -175,6 +197,7 @@ fun ItemizedRewardListContainer(

Row {
Text(
modifier = Modifier.testTag(PledgeItemizedDetailsTestTag.BONUS_SUPPORT_TITLE.name),
text = stringResource(
id =
if (rewardsList.isNotEmpty()) R.string.Bonus_support
Expand All @@ -187,6 +210,7 @@ fun ItemizedRewardListContainer(
Spacer(modifier = Modifier.weight(1f))

Text(
modifier = Modifier.testTag(PledgeItemizedDetailsTestTag.BONUS_SUPPORT.name),
text = totalBonusSupport,
style = typography.subheadlineMedium,
color = colors.textSecondary
Expand All @@ -202,6 +226,7 @@ fun ItemizedRewardListContainer(

Row {
Text(
modifier = Modifier.testTag(PledgeItemizedDetailsTestTag.PLEDGE_AMOUNT_TITLE.name),
text = stringResource(id = R.string.Pledge_amount),
style = typography.calloutMedium,
color = colors.textPrimary
Expand All @@ -211,6 +236,7 @@ fun ItemizedRewardListContainer(

Column(horizontalAlignment = Alignment.End) {
Text(
modifier = Modifier.testTag(PledgeItemizedDetailsTestTag.TOTAL_AMOUNT.name),
text = totalAmount,
style = typography.subheadlineMedium,
color = colors.textPrimary
Expand All @@ -220,6 +246,7 @@ fun ItemizedRewardListContainer(
Spacer(modifier = Modifier.height(dimensions.paddingXSmall))

Text(
modifier = Modifier.testTag(PledgeItemizedDetailsTestTag.CURRENCY_CONVERSION.name),
text = totalAmountCurrencyConverted,
style = typography.footnote,
color = colors.textPrimary
Expand All @@ -232,6 +259,7 @@ fun ItemizedRewardListContainer(
Spacer(modifier = Modifier.height(dimensions.paddingMedium))
Row {
Text(
modifier = Modifier.testTag(PledgeItemizedDetailsTestTag.DISCLAIMER_TEXT.name),
text = disclaimerText,
style = typography.footnote,
color = colors.textPrimary
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
package com.kickstarter.ui.activities.compose

import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertTextEquals
import androidx.compose.ui.test.onAllNodesWithTag
import androidx.compose.ui.test.onNodeWithTag
import androidx.test.platform.app.InstrumentationRegistry
import com.kickstarter.KSRobolectricTestCase
import com.kickstarter.R
import com.kickstarter.ui.compose.designsystem.KSTheme
import com.kickstarter.ui.views.compose.checkout.ItemizedRewardListContainer
import com.kickstarter.ui.views.compose.checkout.PledgeItemizedDetailsTestTag
import org.junit.Test

class PledgeItemizedDetailsTest : KSRobolectricTestCase() {
val context = InstrumentationRegistry.getInstrumentation().targetContext

private val pageTitle =
composeTestRule.onNodeWithTag(PledgeItemizedDetailsTestTag.PAGE_TITLE.name)

private val deliveryDate =
composeTestRule.onNodeWithTag(PledgeItemizedDetailsTestTag.DELIVERY_DATE.name)

private val pledgeAmountTitle =
composeTestRule.onNodeWithTag(PledgeItemizedDetailsTestTag.PLEDGE_AMOUNT_TITLE.name)

private val totalAmount =
composeTestRule.onNodeWithTag(PledgeItemizedDetailsTestTag.TOTAL_AMOUNT.name)

private val bonusSupportTitle =
composeTestRule.onNodeWithTag(PledgeItemizedDetailsTestTag.BONUS_SUPPORT_TITLE.name)

private val bonusSupport =
composeTestRule.onNodeWithTag(PledgeItemizedDetailsTestTag.BONUS_SUPPORT.name)

private val disclaimerText =
composeTestRule.onNodeWithTag(PledgeItemizedDetailsTestTag.DISCLAIMER_TEXT.name)

private val shippingTitle =
composeTestRule.onNodeWithTag(PledgeItemizedDetailsTestTag.SHIPPING_TITLE.name)

private val shippingAmount =
composeTestRule.onNodeWithTag(PledgeItemizedDetailsTestTag.SHIPPING_AMOUNT.name)

private val currencyConversion =
composeTestRule.onNodeWithTag(PledgeItemizedDetailsTestTag.CURRENCY_CONVERSION.name)

@Test
fun `test view init`() {
val rewardsList = listOf(Pair("T-shirt", "$22"), Pair("Pin", "$10"))

composeTestRule.setContent {
KSTheme {
ItemizedRewardListContainer(
ksString = null,
rewardsList = rewardsList,
shippingAmount = 20.0,
shippingAmountString = "",
initialShippingLocation = "",
totalAmount = "50$",
totalAmountCurrencyConverted = "",
initialBonusSupport = "0",
totalBonusSupport = "0",
deliveryDateString = "",
rewardsHaveShippables = false,
disclaimerText = ""
)
}
}
val pageTitleText = context.getString(R.string.Your_pledge)
val pledgeAmountTitleText = context.getString(R.string.Pledge_amount)

pageTitle.assertIsDisplayed()
pageTitle.assertTextEquals(pageTitleText)
deliveryDate.assertDoesNotExist()

composeTestRule.onAllNodesWithTag("ITEM_NAME")[0].assertIsDisplayed()
composeTestRule.onAllNodesWithTag("ITEM_NAME")[1].assertIsDisplayed()
composeTestRule.onAllNodesWithTag("ITEM_NAME")[0].assertTextEquals("T-shirt")
composeTestRule.onAllNodesWithTag("ITEM_NAME")[1].assertTextEquals("Pin")

composeTestRule.onAllNodesWithTag("ITEM_COST")[0].assertIsDisplayed()
composeTestRule.onAllNodesWithTag("ITEM_COST")[1].assertIsDisplayed()
composeTestRule.onAllNodesWithTag("ITEM_COST")[0].assertTextEquals("$22")
composeTestRule.onAllNodesWithTag("ITEM_COST")[1].assertTextEquals("$10")

pledgeAmountTitle.assertIsDisplayed()
pledgeAmountTitle.assertTextEquals(pledgeAmountTitleText)

totalAmount.assertIsDisplayed()
totalAmount.assertTextEquals("50$")

bonusSupportTitle.assertDoesNotExist()
bonusSupport.assertDoesNotExist()
disclaimerText.assertDoesNotExist()
shippingAmount.assertDoesNotExist()
shippingTitle.assertDoesNotExist()
currencyConversion.assertDoesNotExist()
}

@Test
fun `test bonus support, when no rewards and initial is not the same as total bonus, displays pledge without reward copy`() {
composeTestRule.setContent {
KSTheme {
ItemizedRewardListContainer(
ksString = null,
rewardsList = emptyList<Pair<String, String>>(),
shippingAmount = 20.0,
shippingAmountString = "",
initialShippingLocation = "",
totalAmount = "50$",
totalAmountCurrencyConverted = "About CA\$ 1.38",
initialBonusSupport = "1",
totalBonusSupport = "10",
deliveryDateString = "",
rewardsHaveShippables = false,
disclaimerText = ""
)
}
}
val bonusSupportTitleText = context.getString(R.string.Pledge_without_a_reward)

bonusSupportTitle.assertIsDisplayed()
bonusSupportTitle.assertTextEquals(bonusSupportTitleText)
bonusSupport.assertIsDisplayed()
bonusSupport.assertTextEquals("10")
}

@Test
fun `test bonus support, when rewards exist and initial is not the same as total bonus, displays bonus support copy`() {
val rewardsList = listOf(Pair("T-shirt", "$22"), Pair("Pin", "$10"))

composeTestRule.setContent {
KSTheme {
ItemizedRewardListContainer(
ksString = null,
rewardsList = rewardsList,
shippingAmount = 20.0,
shippingAmountString = "",
initialShippingLocation = "",
totalAmount = "50$",
totalAmountCurrencyConverted = "About CA\$ 1.38",
initialBonusSupport = "1",
totalBonusSupport = "10",
deliveryDateString = "",
rewardsHaveShippables = false,
disclaimerText = ""
)
}
}
val bonusSupportTitleText = context.getString(R.string.Bonus_support)

bonusSupportTitle.assertIsDisplayed()
bonusSupportTitle.assertTextEquals(bonusSupportTitleText)
bonusSupport.assertIsDisplayed()
bonusSupport.assertTextEquals("10")
}

@Test
fun `test shipping, when rewards are shippable and backer has shipping location, should show shipping location ui`() {
val rewardsList = listOf(Pair("T-shirt", "$22"), Pair("Pin", "$10"))

composeTestRule.setContent {
KSTheme {
ItemizedRewardListContainer(
ksString = null,
rewardsList = rewardsList,
shippingAmount = 20.0,
shippingAmountString = "$20.0",
initialShippingLocation = "USA",
totalAmount = "50$",
totalAmountCurrencyConverted = "About CA\$ 1.38",
initialBonusSupport = "1",
totalBonusSupport = "10",
deliveryDateString = "",
rewardsHaveShippables = true,
disclaimerText = ""
)
}
}

shippingTitle.assertIsDisplayed()
shippingAmount.assertIsDisplayed()
shippingAmount.assertTextEquals("$20.0")
}

@Test
fun `test currency conversion, when currency conversion not null, should show currency conversion text`() {
val rewardsList = listOf(Pair("T-shirt", "$22"), Pair("Pin", "$10"))

composeTestRule.setContent {
KSTheme {
ItemizedRewardListContainer(
ksString = null,
rewardsList = rewardsList,
shippingAmount = 20.0,
shippingAmountString = "$20.0",
initialShippingLocation = "USA",
totalAmount = "50$",
totalAmountCurrencyConverted = "About CA\$ 1.38",
initialBonusSupport = "1",
totalBonusSupport = "10",
deliveryDateString = "",
rewardsHaveShippables = true,
disclaimerText = ""
)
}
}

currencyConversion.assertIsDisplayed()
currencyConversion.assertTextEquals("About CA\$ 1.38")
}

@Test
fun `test disclaimer, when disclaimer not null, should show disclaimer text`() {
val rewardsList = listOf(Pair("T-shirt", "$22"), Pair("Pin", "$10"))
val disclaimer = context.getString(R.string.If_the_project_reaches_its_funding_goal_you_will_be_charged_total_on_project_deadline_and_receive_proof_of_pledge)

composeTestRule.setContent {
KSTheme {
ItemizedRewardListContainer(
ksString = null,
rewardsList = rewardsList,
shippingAmount = 20.0,
shippingAmountString = "$20.0",
initialShippingLocation = "USA",
totalAmount = "50$",
totalAmountCurrencyConverted = "About CA\$ 1.38",
initialBonusSupport = "1",
totalBonusSupport = "10",
deliveryDateString = "",
rewardsHaveShippables = true,
disclaimerText = disclaimer
)
}
}
disclaimerText.assertIsDisplayed()
disclaimerText.assertTextEquals(disclaimer)
}

@Test
fun `test delivery date, when delivery date not null, should show delivery date`() {
val rewardsList = listOf(Pair("T-shirt", "$22"), Pair("Pin", "$10"))

composeTestRule.setContent {
KSTheme {
ItemizedRewardListContainer(
ksString = null,
rewardsList = rewardsList,
shippingAmount = 20.0,
shippingAmountString = "$20.0",
initialShippingLocation = "USA",
totalAmount = "50$",
totalAmountCurrencyConverted = "About CA\$ 1.38",
initialBonusSupport = "1",
totalBonusSupport = "10",
deliveryDateString = "April 10",
rewardsHaveShippables = true,
disclaimerText = ""
)
}
}
deliveryDate.assertIsDisplayed()
deliveryDate.assertTextEquals("April 10")
}
}

0 comments on commit 5ab6976

Please sign in to comment.