-
Notifications
You must be signed in to change notification settings - Fork 657
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a selection holder for embedded. (#9791)
- Loading branch information
1 parent
5fa5b85
commit 4e6c286
Showing
4 changed files
with
143 additions
and
2 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
...sheet/src/main/java/com/stripe/android/paymentelement/embedded/EmbeddedSelectionHolder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.stripe.android.paymentelement.embedded | ||
|
||
import androidx.lifecycle.SavedStateHandle | ||
import com.stripe.android.paymentsheet.model.PaymentSelection | ||
import kotlinx.coroutines.flow.StateFlow | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
internal class EmbeddedSelectionHolder @Inject constructor( | ||
private val savedStateHandle: SavedStateHandle, | ||
) { | ||
val selection: StateFlow<PaymentSelection?> = savedStateHandle.getStateFlow(EMBEDDED_SELECTION_KEY, null) | ||
|
||
fun set(updatedSelection: PaymentSelection?) { | ||
savedStateHandle[EMBEDDED_SELECTION_KEY] = updatedSelection | ||
} | ||
|
||
companion object { | ||
const val EMBEDDED_SELECTION_KEY = "EMBEDDED_SELECTION_KEY" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...t/src/test/java/com/stripe/android/paymentelement/embedded/EmbeddedSelectionHolderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.stripe.android.paymentelement.embedded | ||
|
||
import androidx.lifecycle.SavedStateHandle | ||
import app.cash.turbine.test | ||
import com.google.common.truth.Truth.assertThat | ||
import com.stripe.android.paymentsheet.model.PaymentSelection | ||
import com.stripe.android.paymentsheet.model.paymentMethodType | ||
import kotlinx.coroutines.test.runTest | ||
import kotlin.test.Test | ||
|
||
internal class EmbeddedSelectionHolderTest { | ||
@Test | ||
fun `setting selection emits value in selection state flow`() = testScenario { | ||
selectionHolder.selection.test { | ||
assertThat(awaitItem()).isNull() | ||
selectionHolder.set(PaymentSelection.GooglePay) | ||
assertThat(awaitItem()?.paymentMethodType).isEqualTo("google_pay") | ||
} | ||
} | ||
|
||
@Test | ||
fun `setting selection updates savedStateHandle`() = testScenario { | ||
assertThat(savedStateHandle.get<PaymentSelection?>(EmbeddedSelectionHolder.EMBEDDED_SELECTION_KEY)) | ||
.isNull() | ||
selectionHolder.set(PaymentSelection.GooglePay) | ||
assertThat(savedStateHandle.get<PaymentSelection?>(EmbeddedSelectionHolder.EMBEDDED_SELECTION_KEY)) | ||
.isEqualTo(PaymentSelection.GooglePay) | ||
} | ||
|
||
@Test | ||
fun `initializing with selection in savedStateHandle sets initial value`() = testScenario( | ||
setup = { | ||
set(EmbeddedSelectionHolder.EMBEDDED_SELECTION_KEY, PaymentSelection.GooglePay) | ||
}, | ||
) { | ||
assertThat(savedStateHandle.get<PaymentSelection?>(EmbeddedSelectionHolder.EMBEDDED_SELECTION_KEY)) | ||
.isEqualTo(PaymentSelection.GooglePay) | ||
selectionHolder.set(null) | ||
assertThat(savedStateHandle.get<PaymentSelection?>(EmbeddedSelectionHolder.EMBEDDED_SELECTION_KEY)) | ||
.isNull() | ||
} | ||
|
||
private class Scenario( | ||
val selectionHolder: EmbeddedSelectionHolder, | ||
val savedStateHandle: SavedStateHandle, | ||
) | ||
|
||
private fun testScenario( | ||
setup: SavedStateHandle.() -> Unit = {}, | ||
block: suspend Scenario.() -> Unit, | ||
) = runTest { | ||
val savedStateHandle = SavedStateHandle() | ||
setup(savedStateHandle) | ||
Scenario( | ||
selectionHolder = EmbeddedSelectionHolder(savedStateHandle), | ||
savedStateHandle = savedStateHandle, | ||
).block() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters