Skip to content

Commit

Permalink
Merge pull request #13232 from woocommerce/task/bulk-order-update-uni…
Browse files Browse the repository at this point in the history
…t-tests

[Bulk Update Orders] Unit tests
  • Loading branch information
hafizrahman authored Jan 7, 2025
2 parents 4708660 + d1d54ec commit 89cd75a
Showing 1 changed file with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.woocommerce.android.analytics.AnalyticsTrackerWrapper
import com.woocommerce.android.extensions.NotificationReceivedEvent
import com.woocommerce.android.extensions.takeIfNotEqualTo
import com.woocommerce.android.model.FeatureFeedbackSettings
import com.woocommerce.android.model.Order
import com.woocommerce.android.model.RequestResult
import com.woocommerce.android.notifications.NotificationChannelType
import com.woocommerce.android.notifications.NotificationChannelsHandler
Expand Down Expand Up @@ -997,6 +998,98 @@ class OrderListViewModelTest : BaseUnitTest() {
assertFalse(isFetchingFirstPage!!)
}

@Test
fun `when selection count changes to greater than 0, then enter selection mode`() = testBlocking {
viewModel.onSelectionChanged(2)

assertThat(viewModel.isSelecting()).isTrue()
assertThat(viewModel.viewState.selectionCount).isEqualTo(2)
assertThat(viewModel.viewState.isAddOrderButtonVisible).isFalse()
assertThat(viewModel.viewState.orderListState).isEqualTo(OrderListViewModel.ViewState.OrderListState.Selecting)
}

@Test
fun `when selection count changes to 0, then exit selection mode`() = testBlocking {
// First enter selection mode
viewModel.onSelectionChanged(2)
assertThat(viewModel.isSelecting()).isTrue()

// Then exit
viewModel.onSelectionChanged(0)

assertThat(viewModel.isSelecting()).isFalse()
assertThat(viewModel.viewState.selectionCount).isNull()
assertThat(viewModel.viewState.isAddOrderButtonVisible).isTrue()
assertThat(viewModel.viewState.orderListState).isEqualTo(OrderListViewModel.ViewState.OrderListState.Browsing)
}

@Test
fun `when in selection mode and count changes but stays above 0, then update count only`() = testBlocking {
// Enter selection mode
viewModel.onSelectionChanged(2)
val initialState = viewModel.viewState.orderListState

// Change count
viewModel.onSelectionChanged(3)

assertThat(viewModel.viewState.selectionCount).isEqualTo(3)
assertThat(viewModel.viewState.orderListState).isEqualTo(initialState)
assertThat(viewModel.isSelecting()).isTrue()
}

@Test
fun `when bulk update clicked, then trigger dialog event with status options`() = testBlocking {
// Given
val statusOptions = listOf(
Order.OrderStatus(CoreOrderStatus.COMPLETED.value, "Completed"),
Order.OrderStatus(CoreOrderStatus.PROCESSING.value, "Processing")
)
whenever(orderDetailRepository.getOrderStatusOptions()).thenReturn(statusOptions)

// When
viewModel.onBulkUpdateStatusClicked()

// Then
assertThat(viewModel.event.value).isInstanceOf(OrderListEvent.ShowUpdateStatusDialog::class.java)
}

@Test
fun `given offline, when bulk update status requested, then show offline error and exit selection mode`() = testBlocking {
whenever(networkStatus.isConnected()).thenReturn(false)

viewModel.onBulkOrderStatusChanged(listOf(1L, 2L), Order.Status.Completed)

assertThat(viewModel.event.value).isInstanceOf(Event.ShowSnackbar::class.java)
assertThat((viewModel.event.value as Event.ShowSnackbar).message).isEqualTo(R.string.offline_error)
assertThat(viewModel.isSelecting()).isFalse()
}

@Test
fun `when bulk update fails, then show error message and exit selection`() = testBlocking {
whenever(networkStatus.isConnected()).thenReturn(true)
whenever(orderListRepository.bulkUpdateOrderStatus(any(), any())).thenReturn(Result.failure(Exception()))

viewModel.onBulkOrderStatusChanged(listOf(1L), Order.Status.Completed)

assertThat(viewModel.event.value).isInstanceOf(Event.ShowSnackbar::class.java)
assertThat((viewModel.event.value as Event.ShowSnackbar).message).isEqualTo(R.string.error_generic)
assertThat(viewModel.isSelecting()).isFalse()
}

@Test
fun `when bulk update completes successfully, then exit selection mode`() = testBlocking {
whenever(networkStatus.isConnected()).thenReturn(true)
whenever(orderListRepository.bulkUpdateOrderStatus(any(), any())).thenReturn(Result.success(Unit))

// First enter selection mode
viewModel.onSelectionChanged(1)
assertThat(viewModel.isSelecting()).isTrue()

viewModel.onBulkOrderStatusChanged(listOf(1L), Order.Status.Completed)

assertThat(viewModel.isSelecting()).isFalse()
}

@Test
fun `when selection count reaches limit, then show error message`() {
// when
Expand Down

0 comments on commit 89cd75a

Please sign in to comment.