-
Notifications
You must be signed in to change notification settings - Fork 629
/
WalletScreenTest.kt
367 lines (319 loc) · 12.7 KB
/
WalletScreenTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package com.stripe.android.link.ui.wallet
import android.content.Intent
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.defaultMinSize
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.ModalBottomSheetLayout
import androidx.compose.material.ModalBottomSheetValue
import androidx.compose.material.rememberModalBottomSheetState
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.test.assertCountEquals
import androidx.compose.ui.test.filter
import androidx.compose.ui.test.filterToOne
import androidx.compose.ui.test.hasContentDescription
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.onChildren
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.onParent
import androidx.compose.ui.test.performClick
import androidx.compose.ui.unit.dp
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.common.truth.Truth.assertThat
import com.stripe.android.PaymentConfiguration
import com.stripe.android.link.LinkActivity
import com.stripe.android.link.LinkActivityContract
import com.stripe.android.link.StripeIntentFixtures
import com.stripe.android.link.createAndroidIntentComposeRule
import com.stripe.android.link.theme.DefaultLinkTheme
import com.stripe.android.link.ui.BottomSheetContent
import com.stripe.android.link.ui.ErrorMessage
import com.stripe.android.model.CardBrand
import com.stripe.android.model.ConsumerPaymentDetails
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@OptIn(ExperimentalMaterialApi::class)
@RunWith(AndroidJUnit4::class)
internal class WalletScreenTest {
@get:Rule
val composeTestRule = createAndroidIntentComposeRule<LinkActivity> {
PaymentConfiguration.init(it, "publishable_key")
Intent(it, LinkActivity::class.java).apply {
putExtra(
LinkActivityContract.EXTRA_ARGS,
LinkActivityContract.Args(
StripeIntentFixtures.PI_SUCCEEDED,
true,
"Merchant, Inc"
)
)
}
}
private val primaryButtonLabel = "Pay $10.99"
private val paymentDetails = listOf(
ConsumerPaymentDetails.Card(
"id1",
true,
2022,
12,
CardBrand.Visa,
"4242"
),
ConsumerPaymentDetails.Card(
"id2",
false,
2023,
11,
CardBrand.MasterCard,
"4444"
),
ConsumerPaymentDetails.Card(
"id3",
false,
2023,
11,
CardBrand.AmericanExpress,
"0005"
)
)
private val paymentDetailsFlow = MutableStateFlow(paymentDetails)
@Test
fun default_payment_method_is_initially_selected() {
var paymentMethod: ConsumerPaymentDetails.PaymentDetails? = null
setContent(
onPayButtonClick = {
paymentMethod = it
}
)
onPrimaryButton().performClick()
assertThat(paymentMethod).isEqualTo(paymentDetails.first())
}
@Test
fun selected_payment_method_is_shown_when_collapsed() {
setContent()
val secondPaymentMethod = paymentDetails[1]
toggleListExpanded()
onPaymentDetailsItem(secondPaymentMethod).performClick()
toggleListExpanded()
composeTestRule.onNodeWithText("Pay with").onParent().onChildren()
.filter(hasText(secondPaymentMethod.last4, substring = true)).assertCountEquals(1)
}
@Test
fun selected_payment_method_is_used_for_payment() {
var paymentMethod: ConsumerPaymentDetails.PaymentDetails? = null
setContent(
onPayButtonClick = {
paymentMethod = it
}
)
val secondPaymentMethod = paymentDetails[1]
toggleListExpanded()
onPaymentDetailsItem(secondPaymentMethod).performClick()
onPrimaryButton().performClick()
assertThat(paymentMethod).isEqualTo(secondPaymentMethod)
}
@Test
fun add_new_payment_method_click_triggers_action() {
var count = 0
setContent(
onAddNewPaymentMethodClick = {
count++
}
)
toggleListExpanded()
composeTestRule.onNodeWithText("Add a new payment method").performClick()
assertThat(count).isEqualTo(1)
}
@Test
fun pay_another_way_click_triggers_action() {
var count = 0
setContent(
onPayAnotherWayClick = {
count++
}
)
toggleListExpanded()
composeTestRule.onNodeWithText("Pay another way").performClick()
assertThat(count).isEqualTo(1)
}
@Test
fun options_menu_sets_bottom_sheet_content() {
var bottomSheetContent: BottomSheetContent? = null
setContent(
showBottomSheetContent = {
bottomSheetContent = it
}
)
val secondPaymentMethod = paymentDetails[1]
toggleListExpanded()
onOptionsForPaymentMethod(secondPaymentMethod).performClick()
assertThat(bottomSheetContent).isNotNull()
}
@Test
fun delete_item_shows_dialog_confirmation() {
setContent()
toggleListExpanded()
onOptionsForPaymentMethod(paymentDetails.first()).performClick()
onRemoveCardButton().assertExists()
onRemoveCardButton().performClick()
onRemoveConfirmationDialog().assertExists()
}
@Test
fun update_item_triggers_callback() {
var paymentMethod: ConsumerPaymentDetails.PaymentDetails? = null
setContent(
onEditPaymentMethod = {
paymentMethod = it
}
)
toggleListExpanded()
onOptionsForPaymentMethod(paymentDetails.first()).performClick()
onUpdateCardButton().assertExists()
onUpdateCardButton().performClick()
assertThat(paymentMethod).isEqualTo(paymentDetails.first())
}
@Test
fun canceling_bottom_sheet_dismisses_it() {
setContent()
toggleListExpanded()
onOptionsForPaymentMethod(paymentDetails.first()).performClick()
onCancelButton().performClick()
onRemoveCardButton().assertDoesNotExist()
}
@Test
fun confirming_delete_item_triggers_callback() {
var paymentMethod: ConsumerPaymentDetails.PaymentDetails? = null
setContent(
onDeletePaymentMethod = {
paymentMethod = it
}
)
toggleListExpanded()
onOptionsForPaymentMethod(paymentDetails.first()).performClick()
onRemoveCardButton().performClick()
onRemoveButton().performClick()
assertThat(paymentMethod).isEqualTo(paymentDetails.first())
}
@Test
fun canceling_delete_item_keeps_item() {
var paymentMethod: ConsumerPaymentDetails.PaymentDetails? = null
setContent(
onDeletePaymentMethod = {
paymentMethod = it
}
)
toggleListExpanded()
onOptionsForPaymentMethod(paymentDetails.first()).performClick()
onRemoveCardButton().performClick()
onCancelButton().performClick()
assertThat(paymentMethod).isNull()
}
@Test
fun canceling_delete_item_then_trying_again_shows_dialog() {
setContent()
toggleListExpanded()
onOptionsForPaymentMethod(paymentDetails.first()).performClick()
onRemoveCardButton().performClick()
onCancelButton().performClick()
onOptionsForPaymentMethod(paymentDetails.first()).performClick()
onRemoveCardButton().performClick()
onRemoveConfirmationDialog().assertExists()
}
@Test
fun when_error_message_is_not_null_then_it_is_visible() {
val errorMessage = "Error message"
setContent(errorMessage = ErrorMessage.Raw(errorMessage))
composeTestRule.onNodeWithText(errorMessage).assertExists()
}
@Test
fun when_selected_item_is_removed_then_default_is_selected() {
setContent()
val secondPaymentMethod = paymentDetails[1]
toggleListExpanded()
onPaymentDetailsItem(secondPaymentMethod).performClick()
toggleListExpanded()
composeTestRule.onNodeWithText("Pay with").onParent().onChildren()
.filter(hasText(secondPaymentMethod.last4, substring = true)).assertCountEquals(1)
val defaultPaymentDetails = paymentDetails.first()
paymentDetailsFlow.tryEmit(listOf(paymentDetails[2], defaultPaymentDetails))
composeTestRule.onNodeWithText("Pay with").onParent().onChildren()
.filter(hasText(defaultPaymentDetails.last4, substring = true)).assertCountEquals(1)
}
private fun setContent(
isProcessing: Boolean = false,
errorMessage: ErrorMessage? = null,
onAddNewPaymentMethodClick: () -> Unit = {},
onEditPaymentMethod: (ConsumerPaymentDetails.PaymentDetails) -> Unit = {},
onDeletePaymentMethod: (ConsumerPaymentDetails.PaymentDetails) -> Unit = {},
onPayButtonClick: (ConsumerPaymentDetails.PaymentDetails) -> Unit = {},
onPayAnotherWayClick: () -> Unit = {},
showBottomSheetContent: ((BottomSheetContent?) -> Unit)? = null,
) = composeTestRule.setContent {
var bottomSheetContent by remember { mutableStateOf<BottomSheetContent?>(null) }
val sheetState = rememberModalBottomSheetState(ModalBottomSheetValue.Hidden)
val coroutineScope = rememberCoroutineScope()
val paymentDetails by paymentDetailsFlow.collectAsState()
if (bottomSheetContent != null) {
DisposableEffect(bottomSheetContent) {
coroutineScope.launch { sheetState.show() }
onDispose {
coroutineScope.launch { sheetState.hide() }
}
}
}
ModalBottomSheetLayout(
sheetContent = bottomSheetContent ?: {
// Must have some content at startup or bottom sheet crashes when
// calculating its initial size
Box(Modifier.defaultMinSize(minHeight = 1.dp)) {}
},
modifier = Modifier.fillMaxHeight(),
sheetState = sheetState,
) {
DefaultLinkTheme {
WalletBody(
isProcessing = isProcessing,
paymentDetails = paymentDetails,
initiallySelectedId = null,
primaryButtonLabel = primaryButtonLabel,
errorMessage = errorMessage,
onAddNewPaymentMethodClick = onAddNewPaymentMethodClick,
onEditPaymentMethod = onEditPaymentMethod,
onDeletePaymentMethod = onDeletePaymentMethod,
onPrimaryButtonClick = onPayButtonClick,
onPayAnotherWayClick = onPayAnotherWayClick,
showBottomSheetContent = showBottomSheetContent ?: {
bottomSheetContent = it
if (it == null) {
coroutineScope.launch { sheetState.hide() }
}
}
)
}
}
}
private fun toggleListExpanded() =
composeTestRule.onNodeWithTag("ChevronIcon", useUnmergedTree = true).performClick()
private fun onPaymentDetailsItem(paymentDetails: ConsumerPaymentDetails.Card) =
composeTestRule.onNodeWithText(paymentDetails.last4, substring = true)
private fun onOptionsForPaymentMethod(paymentDetails: ConsumerPaymentDetails.Card) =
onPaymentDetailsItem(paymentDetails).onChildren().filterToOne(hasContentDescription("Edit"))
private fun onPrimaryButton() = composeTestRule.onNodeWithText(primaryButtonLabel)
private fun onRemoveCardButton() = composeTestRule.onNodeWithText("Remove card")
private fun onUpdateCardButton() = composeTestRule.onNodeWithText("Update card")
private fun onRemoveConfirmationDialog() =
composeTestRule.onNodeWithText("Are you sure you want to remove this card?")
private fun onCancelButton() = composeTestRule.onNodeWithText("Cancel")
private fun onRemoveButton() = composeTestRule.onNodeWithText("Remove")
}