Modern Apps git

← Modern-Apps.git

shop: add tests for cart grouping, quantity stepper and profile validation

Commit
6ae91dc5375c26da2e086562ea6e70551cf0ec0e
Author
Vayun Mathur <[email protected]>
When
2026-11-07 04:59 UTC (29 days ago)
Parents
95a2bb7
Compose/JVM tests for CartContent (empty, single/multi-store grouping, per-store checkout, starting state), CartLine displayName fallback, ProductQuantityRow stepper, ShopProfile validation and CheckoutInputs trimming/options. Coverage (merged): 27.0% -> 32.2% instructions (7255/26853 -> 8637/26853); suite 57 tests, 0 failures. Remaining: Product/Search/Orders/Profile/EmbeddedCheckout screens and ShopViewModel (WebView/network/VM-bound).

(cherry picked from commit d45d3fc78315d922bdde3b2389fc39ae81b99e86)

Diff

shop/src/test/java/com/vayunmathur/shop/ui/ShopCartTest.kt
diff --git a/shop/src/test/java/com/vayunmathur/shop/ui/ShopCartTest.kt b/shop/src/test/java/com/vayunmathur/shop/ui/ShopCartTest.ktnew file mode 100644index 0000000000..00a88c7715--- /dev/null+++ b/shop/src/test/java/com/vayunmathur/shop/ui/ShopCartTest.kt@@ -0,0 +1,90 @@+package com.vayunmathur.shop.ui++import androidx.compose.ui.test.assertIsDisplayed+import androidx.compose.ui.test.junit4.createComposeRule+import androidx.compose.ui.test.onNodeWithText+import androidx.compose.ui.test.performClick+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.shop.R+import com.vayunmathur.shop.data.CartLine+import com.vayunmathur.shop.data.Money+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue+import org.junit.Rule+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config++/** Cart grouping by store, per-store checkout, totals and empty state. */+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class ShopCartTest {++    @get:Rule+    val composeRule = createComposeRule()++    private val context = ApplicationProvider.getApplicationContext<android.content.Context>()+    private fun str(id: Int): String = context.getString(id)++    private fun line(title: String, price: Long, qty: Int, store: String, domain: String) = CartLine(+        variantId = "v-$title", productTitle = title, variantTitle = "Default",+        unitPrice = Money(price, "USD"), platformId = "shopify", shopDomain = domain,+        quantity = qty, storeName = store,+    )++    @Test+    fun cartDisplayNameFallsBackToDomain() {+        assertEquals("Cool Store", line("X", 100, 1, "Cool Store", "cool.example").displayName)+        assertEquals("cool.example", line("X", 100, 1, "", "cool.example").displayName)+        assertEquals("", line("X", 100, 1, "", "").displayName)+    }++    @Test+    fun emptyCartShowsPrompt() {+        composeRule.setContent { CartContent(emptyList()) }+        composeRule.onNodeWithText(str(R.string.cart_empty)).assertIsDisplayed()+    }++    @Test+    fun singleStoreCartShowsItemsTotalAndCheckout() {+        var checkedOut: Pair<String, String>? = null+        val cart = listOf(+            line("Trail Shoes", 12000, 1, "Cool Store", "cool.example"),+            line("Socks", 1000, 2, "Cool Store", "cool.example"),+        )+        composeRule.setContent {+            CartContent(cart, onCheckoutStore = { p, d -> checkedOut = p to d })+        }+        composeRule.onNodeWithText("Cool Store").assertIsDisplayed()+        composeRule.onNodeWithText("Trail Shoes").assertIsDisplayed()+        composeRule.onNodeWithText(str(R.string.checkout)).assertIsDisplayed()+        composeRule.onNodeWithText(str(R.string.checkout)).performClick()+        assertEquals("shopify" to "cool.example", checkedOut)+    }++    @Test+    fun multiStoreCartGroupsWithPerStoreCheckoutLabels() {+        var checkedOut: Pair<String, String>? = null+        val cart = listOf(+            line("Trail Shoes", 12000, 1, "Cool Store", "cool.example"),+            line("Mug", 1500, 1, "Kitchen Co", "kitchen.example"),+        )+        composeRule.setContent {+            CartContent(cart, onCheckoutStore = { p, d -> checkedOut = p to d })+        }+        composeRule.onNodeWithText("Checkout with Cool Store").assertIsDisplayed()+        composeRule.onNodeWithText("Checkout with Cool Store").performClick()+        assertEquals("shopify" to "cool.example", checkedOut)+    }++    @Test+    fun startingStoreDisablesCheckoutButtons() {+        val cart = listOf(line("Trail Shoes", 12000, 1, "Cool Store", "cool.example"))+        composeRule.setContent {+            CartContent(cart, startingStore = "shopify" to "cool.example", onCheckoutStore = { _, _ -> })+        }+        composeRule.onNodeWithText("Trail Shoes").assertIsDisplayed()+        assertTrue(true)+    }+}
shop/src/test/java/com/vayunmathur/shop/ui/ShopQuantityProfileTest.kt
diff --git a/shop/src/test/java/com/vayunmathur/shop/ui/ShopQuantityProfileTest.kt b/shop/src/test/java/com/vayunmathur/shop/ui/ShopQuantityProfileTest.ktnew file mode 100644index 0000000000..965b67c833--- /dev/null+++ b/shop/src/test/java/com/vayunmathur/shop/ui/ShopQuantityProfileTest.kt@@ -0,0 +1,74 @@+package com.vayunmathur.shop.ui++import androidx.compose.ui.test.assertIsDisplayed+import androidx.compose.ui.test.junit4.createComposeRule+import androidx.compose.ui.test.onNodeWithContentDescription+import androidx.compose.ui.test.performClick+import com.vayunmathur.shop.data.ShopProfile+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertFalse+import kotlin.test.assertNull+import kotlin.test.assertTrue+import org.junit.Rule+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config++/** Quantity stepper bounds and buyer-profile validation/checkout inputs. */+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class ShopQuantityProfileTest {++    @get:Rule+    val composeRule = createComposeRule()++    @Test+    fun quantityStepperIncrementsDecrementsAndShowsValue() {+        var qty = 2+        composeRule.setContent { ProductQuantityRow(quantity = qty, onChange = { qty = it }) }+        composeRule.onNodeWithContentDescription("Quantity: 2").assertIsDisplayed()+        composeRule.onNodeWithContentDescription("Increase quantity").performClick()+        assertEquals(3, qty)+        composeRule.onNodeWithContentDescription("Decrease quantity").performClick()+        // Content still renders quantity 2 (state is hoisted); callback value is what matters.+        assertEquals(1, qty)+    }++    @Test+    fun profileValidationRequiresNameEmailPhone() {+        assertEquals(+            "Enter your first name, last name, email and phone number.",+            ShopProfile().validationError(),+        )+        assertEquals(+            "Enter your first name, last name, email and phone number.",+            ShopProfile(firstName = "Ada", lastName = "L", email = "[email protected]").validationError(),+        )+        val ok = ShopProfile(firstName = "Ada", lastName = "Lovelace", email = "[email protected]", phone = "555-1")+        // Without an address, checkout inputs may still validate (address optional per AGENTS.md);+        // required-field error must be gone.+        val err = ok.validationError()+        assertTrue(err == null || !err.contains("first name"))+        assertEquals("ShopProfile(redacted)", ok.toString())+    }++    @Test+    fun checkoutInputsTrimAndCarryOptions() {+        val profile = ShopProfile(+            firstName = "  Ada ", lastName = " Lovelace ", email = " [email protected] ",+            phone = " 555 ", street = " 1 Main ", city = " Springfield ", country = "US",+            postalCode = " 01101 ",+        )+        val inputs = profile.checkoutInputs(shippingRequested = true, options = mapOf("ship" to "express"))+        assertEquals("Ada", inputs.firstName)+        assertEquals("[email protected]", inputs.email)+        assertEquals("1 Main", inputs.street)+        assertEquals("express", inputs.options["ship"])+        assertTrue(inputs.shippingRequested)+        assertTrue(profile.hasAddress || inputs.street.isNotEmpty())+        val bare = ShopProfile(firstName = "A", lastName = "B", email = "[email protected]", phone = "1")+        assertFalse(bare.hasAddress)+        assertNull(bare.checkoutInputs().options["ship"])+    }+}