Skip to content

Commit

Permalink
Add some tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaynewstrom-stripe committed Dec 18, 2024
1 parent c82c03e commit e44078b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,18 @@ internal class StripeApiRepositoryTest {
assertThat(detachUrl).isEqualTo(expectedUrl)
}

@Test
fun testGetElementsDetachPaymentMethodUrl() {
val paymentMethodId = "pm_1ETDEa2eZvKYlo2CN5828c52"
val detachUrl = stripeApiRepository.getElementsDetachPaymentMethodUrl(paymentMethodId)
val expectedUrl = arrayOf(
"https://api.stripe.com/v1/elements/payment_methods/",
paymentMethodId,
"/detach"
).joinToString("")
assertThat(detachUrl).isEqualTo(expectedUrl)
}

@Test
fun testGetPaymentMethodsUrl() {
assertThat(StripeApiRepository.paymentMethodsUrl)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,28 @@ internal class CustomerRepositoryTest {
assertThat(result.isFailure).isTrue()
}

@Test
fun `detachPaymentMethod() should call elements endpoint when customerSessionClientSecret exists`() =
runTest {
givenElementsDetachPaymentMethodReturns(
Result.success(
PaymentMethodFixtures.CARD_PAYMENT_METHOD
)
)

val result = repository.detachPaymentMethod(
customerInfo = CustomerRepository.CustomerInfo(
id = "customer_id",
ephemeralKeySecret = "ephemeral_key",
customerSessionClientSecret = "cuss_123",
),
paymentMethodId = "payment_method_id",
canRemoveDuplicates = false,
)

assertThat(result.getOrNull()).isEqualTo(PaymentMethodFixtures.CARD_PAYMENT_METHOD)
}

@Test
fun `detachPaymentMethod() with 'canRemoveDuplicates' as 'true' should remove duplicate payment methods and remove provided payment method ID last`() =
runTest {
Expand Down Expand Up @@ -643,6 +665,21 @@ internal class CustomerRepositoryTest {
}
}

private fun givenElementsDetachPaymentMethodReturns(
result: Result<PaymentMethod>
) {
stripeRepository.stub {
onBlocking {
detachPaymentMethod(
customerSessionClientSecret = any(),
productUsageTokens = any(),
paymentMethodId = anyString(),
requestOptions = any(),
)
}.doReturn(result)
}
}

private fun givenAttachPaymentMethodReturns(
result: Result<PaymentMethod>
) {
Expand Down

0 comments on commit e44078b

Please sign in to comment.