Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.shopify.checkoutkit.androiddemo.accessibility

object AccessibilityIdentifiers {
const val APP_READY = "checkout-kit-sample-ready"
const val PRELOAD_STATE_PREFIX = "preload-state-"

object Cart {
const val CHECKOUT_READY = "cart-checkout-ready"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ fun CartView(
val state = cartViewModel.cartState.collectAsState().value
val loading = cartViewModel.loadingState.collectAsState().value
val checkoutPresentationMode = cartViewModel.checkoutPresentationMode.collectAsState().value
val preloadStateTestId = cartViewModel.preloadStateTestId.collectAsState().value

val activity = LocalActivity.current as ComponentActivity
var mutableQuantity by remember { mutableStateOf<Map<String, Int>>(mutableMapOf()) }
Expand Down Expand Up @@ -90,7 +91,12 @@ fun CartView(
it.title to it.quantity
}

Column(modifier = Modifier.padding(top = 4.dp)) {
// Exposes the current preload state as a preload identifier.
Column(
modifier = Modifier
.padding(top = 4.dp)
.testTag(preloadStateTestId)
) {
CartLines(
lines = state.cartLines,
loading = loading,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.shopify.checkoutkit.CheckoutErrorCode
import com.shopify.checkoutkit.CheckoutException
import com.shopify.checkoutkit.CheckoutPresentation
import com.shopify.checkoutkit.CheckoutProtocol
import com.shopify.checkoutkit.PreloadState
import com.shopify.checkoutkit.ShopifyCheckoutKit
import com.shopify.checkoutkit.androiddemo.MainActivity
import com.shopify.checkoutkit.androiddemo.R
Expand All @@ -22,6 +23,7 @@ import com.shopify.checkoutkit.androiddemo.common.SnackbarEvent
import com.shopify.checkoutkit.androiddemo.common.logs.LogLevel
import com.shopify.checkoutkit.androiddemo.common.logs.Logger
import com.shopify.checkoutkit.androiddemo.common.navigation.Screen
import com.shopify.checkoutkit.androiddemo.e2e.PreloadStateMarker
import com.shopify.checkoutkit.androiddemo.settings.PreferencesManager
import com.shopify.checkoutkit.androiddemo.settings.authentication.data.AuthenticationState
import com.shopify.checkoutkit.androiddemo.settings.authentication.data.CustomerRepository
Expand Down Expand Up @@ -60,6 +62,9 @@ class CartViewModel(
private val _checkoutPresentationMode = MutableStateFlow(CheckoutPresentationMode.CheckoutKitSheet)
val checkoutPresentationMode: StateFlow<CheckoutPresentationMode> = _checkoutPresentationMode.asStateFlow()

private val _preloadStateTestId = MutableStateFlow(PreloadStateMarker.testId(PreloadState.Idle))
val preloadStateTestId: StateFlow<String> = _preloadStateTestId.asStateFlow()

private var demoBuyerIdentityEnabled = false
private var checkoutPreloadingEnabled = true
private var windowOpenHandler = WindowOpenHandler.Default
Expand Down Expand Up @@ -171,6 +176,7 @@ class CartViewModel(
Timber.i("Preloading checkout")
ShopifyCheckoutKit.preload(url, activity) { state ->
Timber.i("Preload state changed to $state")
_preloadStateTestId.value = PreloadStateMarker.testId(state)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.shopify.checkoutkit.androiddemo.e2e

import com.shopify.checkoutkit.PreloadState
import com.shopify.checkoutkit.androiddemo.accessibility.AccessibilityIdentifiers

/** Maps whether [PreloadState] is ready to a preload identifier. */
object PreloadStateMarker {
fun testId(state: PreloadState): String {
val value = if (state is PreloadState.Ready) "ready" else "not-ready"
return "${AccessibilityIdentifiers.PRELOAD_STATE_PREFIX}$value"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.shopify.checkoutkit.androiddemo.e2e

import com.shopify.checkoutkit.PreloadState
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test

class PreloadStateMarkerTest {
@Test
fun `ready marker matches the maestro flow assertion`() {
assertThat(PreloadStateMarker.testId(PreloadState.Ready)).isEqualTo("preload-state-ready")
}

@Test
fun `non-ready state uses the fallback marker`() {
assertThat(PreloadStateMarker.testId(PreloadState.Idle)).isEqualTo("preload-state-not-ready")
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
enum AccessibilityIdentifiers {
static let appReady = "checkout-kit-sample-ready"
static let preloadStatePrefix = "preload-state-"

enum Cart {
static let checkoutReady = "cart-checkout-ready"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import ShopifyCheckoutKit

/// Maps whether ``PreloadState`` is ready to a preload identifier.
enum PreloadStateMarker {
static func testId(for state: PreloadState) -> String {
let value = if case .ready = state { "ready" } else { "not-ready" }
return "\(AccessibilityIdentifiers.preloadStatePrefix)\(value)"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct CartView: View {
@State var isCompleted: Bool = false
@State var showCheckoutSheet: Bool = false
@State private var checkoutPreload: CheckoutPreload?
@State private var preloadStateTestId = PreloadStateMarker.testId(for: .idle)

@ObservedObject var cartManager: CartManager = .shared

Expand All @@ -37,6 +38,8 @@ struct CartView: View {
}
.padding(.bottom, 130)
}
.accessibilityElement(children: .contain)
.accessibilityIdentifier(preloadStateTestId)

VStack(spacing: DesignSystem.buttonSpacing) {
if let cartID = cartManager.cart?.id {
Expand Down Expand Up @@ -159,6 +162,7 @@ struct CartView: View {
ShopifyCheckoutKit.invalidate()
checkoutPreload = ShopifyCheckoutKit.preload(checkout: url)
checkoutPreload?.onStateChange = { state in
preloadStateTestId = PreloadStateMarker.testId(for: state)
print("[Preload] state changed to \(state)")
ShopifyCheckoutKit.configuration.logger.log("Preload state changed to \(state)")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@testable import CheckoutKitSwiftDemo
import ShopifyCheckoutKit
import XCTest

class PreloadStateMarkerTests: XCTestCase {
func testReadyMarkerMatchesTheMaestroFlowAssertion() {
XCTAssertEqual(PreloadStateMarker.testId(for: .ready), "preload-state-ready")
}

func testNonReadyStateUsesTheFallbackMarker() {
XCTAssertEqual(PreloadStateMarker.testId(for: .idle), "preload-state-not-ready")
}
}
Loading