Skip to content

Commit 4140442

Browse files
Add unit tests for booking refund button
Test that the refund callback is present for paid and partially refunded bookings, null for unpaid bookings or bookings without an order, and that clicking it triggers the NavigateToIssueRefund event with the correct orderId.
1 parent c467a8f commit 4140442

File tree

1 file changed

+75
-2
lines changed

1 file changed

+75
-2
lines changed

WooCommerce/src/test/kotlin/com/woocommerce/android/ui/bookings/details/BookingDetailsViewModelTest.kt

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import com.woocommerce.android.ui.bookings.BookingsRepository
1313
import com.woocommerce.android.ui.bookings.PaymentStatus
1414
import com.woocommerce.android.ui.bookings.PaymentStatusResolver
1515
import com.woocommerce.android.ui.bookings.compose.BookingLocationStatus
16+
import com.woocommerce.android.ui.orders.details.OrderDetailRepository
1617
import com.woocommerce.android.ui.bookings.compose.BookingStaffMemberStatus
1718
import com.woocommerce.android.ui.compose.DialogState
1819
import com.woocommerce.android.util.CurrencyFormatter
@@ -72,6 +73,7 @@ class BookingDetailsViewModelTest : BaseUnitTest() {
7273
onBlocking { resolve(any()) } doReturn PaymentStatus.UNPAID
7374
}
7475
private val analyticsTrackerWrapper: AnalyticsTrackerWrapper = mock()
76+
private val orderDetailRepository = mock<OrderDetailRepository>()
7577

7678
@Before
7779
fun setup() {
@@ -484,6 +486,76 @@ class BookingDetailsViewModelTest : BaseUnitTest() {
484486
assertThat(state.toolbarTitle).isEmpty()
485487
}
486488

489+
@Test
490+
fun `given booking is paid with order, when state observed, then refund callback is present`() = testBlocking {
491+
// GIVEN
492+
whenever(paymentStatusResolver.resolve(any())).thenReturn(PaymentStatus.PAID)
493+
val viewModel = createViewModel()
494+
495+
// WHEN
496+
val state = viewModel.state.getOrAwaitValue()
497+
498+
// THEN
499+
assertThat(state.bookingUiState?.onIssueRefundClicked != null).isTrue()
500+
}
501+
502+
@Test
503+
fun `given booking is partially refunded with order, when state observed, then refund callback is present`() = testBlocking {
504+
// GIVEN
505+
whenever(paymentStatusResolver.resolve(any())).thenReturn(PaymentStatus.PARTIALLY_REFUNDED)
506+
val viewModel = createViewModel()
507+
508+
// WHEN
509+
val state = viewModel.state.getOrAwaitValue()
510+
511+
// THEN
512+
assertThat(state.bookingUiState?.onIssueRefundClicked != null).isTrue()
513+
}
514+
515+
@Test
516+
fun `given booking is unpaid, when state observed, then refund callback is null`() = testBlocking {
517+
// GIVEN
518+
whenever(paymentStatusResolver.resolve(any())).thenReturn(PaymentStatus.UNPAID)
519+
val viewModel = createViewModel()
520+
521+
// WHEN
522+
val state = viewModel.state.getOrAwaitValue()
523+
524+
// THEN
525+
assertThat(state.bookingUiState?.onIssueRefundClicked == null).isTrue()
526+
}
527+
528+
@Test
529+
fun `given booking has no associated order, when state observed, then refund callback is null`() = testBlocking {
530+
// GIVEN
531+
whenever(paymentStatusResolver.resolve(any())).thenReturn(PaymentStatus.PAID)
532+
bookingFlow.value = getSampleBooking(bookingId, orderId = 0L)
533+
val viewModel = createViewModel()
534+
535+
// WHEN
536+
val state = viewModel.state.getOrAwaitValue()
537+
538+
// THEN
539+
assertThat(state.bookingUiState?.onIssueRefundClicked == null).isTrue()
540+
}
541+
542+
@Test
543+
fun `when refund button clicked, then NavigateToIssueRefund event is triggered with orderId`() = testBlocking {
544+
// GIVEN
545+
val orderId = 99L
546+
bookingFlow.value = getSampleBooking(bookingId, orderId = orderId)
547+
whenever(paymentStatusResolver.resolve(any())).thenReturn(PaymentStatus.PAID)
548+
val viewModel = createViewModel()
549+
val state = viewModel.state.getOrAwaitValue()
550+
551+
// WHEN
552+
state.bookingUiState?.onIssueRefundClicked?.invoke()
553+
554+
// THEN
555+
val event = viewModel.event.getOrAwaitValue()
556+
assertThat(event).isEqualTo(BookingDetailsViewModel.NavigateToIssueRefund(orderId))
557+
}
558+
487559
@Test
488560
fun `when onAttendanceToggle called rapidly, then first request error is suppressed by cancellation`() =
489561
testBlocking {
@@ -519,13 +591,14 @@ class BookingDetailsViewModelTest : BaseUnitTest() {
519591
networkStatus = networkStatus,
520592
paymentStatusResolver = paymentStatusResolver,
521593
analyticsTrackerWrapper = analyticsTrackerWrapper,
594+
orderDetailRepository = orderDetailRepository,
522595
appScope = TestScope(coroutinesTestRule.testDispatcher),
523596
).apply {
524597
state.observeForever { }
525598
}
526599
}
527600

528-
private fun getSampleBooking(id: Long, location: String? = null): Booking {
601+
private fun getSampleBooking(id: Long, orderId: Long = id, location: String? = null): Booking {
529602
return BookingEntity(
530603
id = LocalOrRemoteId.RemoteId(id),
531604
localSiteId = LocalOrRemoteId.LocalId(1),
@@ -541,7 +614,7 @@ class BookingDetailsViewModelTest : BaseUnitTest() {
541614
dateCreated = Instant.now(),
542615
dateModified = Instant.now(),
543616
googleCalendarEventId = "",
544-
orderId = id,
617+
orderId = orderId,
545618
orderItemId = 1L,
546619
parentId = 0L,
547620
personCounts = listOf(1L),

0 commit comments

Comments
 (0)