Skip to content

Commit 89cd75a

Browse files
authored
Merge pull request #13232 from woocommerce/task/bulk-order-update-unit-tests
[Bulk Update Orders] Unit tests
2 parents 4708660 + d1d54ec commit 89cd75a

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

WooCommerce/src/test/kotlin/com/woocommerce/android/ui/orders/OrderListViewModelTest.kt

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import com.woocommerce.android.analytics.AnalyticsTrackerWrapper
1111
import com.woocommerce.android.extensions.NotificationReceivedEvent
1212
import com.woocommerce.android.extensions.takeIfNotEqualTo
1313
import com.woocommerce.android.model.FeatureFeedbackSettings
14+
import com.woocommerce.android.model.Order
1415
import com.woocommerce.android.model.RequestResult
1516
import com.woocommerce.android.notifications.NotificationChannelType
1617
import com.woocommerce.android.notifications.NotificationChannelsHandler
@@ -997,6 +998,98 @@ class OrderListViewModelTest : BaseUnitTest() {
997998
assertFalse(isFetchingFirstPage!!)
998999
}
9991000

1001+
@Test
1002+
fun `when selection count changes to greater than 0, then enter selection mode`() = testBlocking {
1003+
viewModel.onSelectionChanged(2)
1004+
1005+
assertThat(viewModel.isSelecting()).isTrue()
1006+
assertThat(viewModel.viewState.selectionCount).isEqualTo(2)
1007+
assertThat(viewModel.viewState.isAddOrderButtonVisible).isFalse()
1008+
assertThat(viewModel.viewState.orderListState).isEqualTo(OrderListViewModel.ViewState.OrderListState.Selecting)
1009+
}
1010+
1011+
@Test
1012+
fun `when selection count changes to 0, then exit selection mode`() = testBlocking {
1013+
// First enter selection mode
1014+
viewModel.onSelectionChanged(2)
1015+
assertThat(viewModel.isSelecting()).isTrue()
1016+
1017+
// Then exit
1018+
viewModel.onSelectionChanged(0)
1019+
1020+
assertThat(viewModel.isSelecting()).isFalse()
1021+
assertThat(viewModel.viewState.selectionCount).isNull()
1022+
assertThat(viewModel.viewState.isAddOrderButtonVisible).isTrue()
1023+
assertThat(viewModel.viewState.orderListState).isEqualTo(OrderListViewModel.ViewState.OrderListState.Browsing)
1024+
}
1025+
1026+
@Test
1027+
fun `when in selection mode and count changes but stays above 0, then update count only`() = testBlocking {
1028+
// Enter selection mode
1029+
viewModel.onSelectionChanged(2)
1030+
val initialState = viewModel.viewState.orderListState
1031+
1032+
// Change count
1033+
viewModel.onSelectionChanged(3)
1034+
1035+
assertThat(viewModel.viewState.selectionCount).isEqualTo(3)
1036+
assertThat(viewModel.viewState.orderListState).isEqualTo(initialState)
1037+
assertThat(viewModel.isSelecting()).isTrue()
1038+
}
1039+
1040+
@Test
1041+
fun `when bulk update clicked, then trigger dialog event with status options`() = testBlocking {
1042+
// Given
1043+
val statusOptions = listOf(
1044+
Order.OrderStatus(CoreOrderStatus.COMPLETED.value, "Completed"),
1045+
Order.OrderStatus(CoreOrderStatus.PROCESSING.value, "Processing")
1046+
)
1047+
whenever(orderDetailRepository.getOrderStatusOptions()).thenReturn(statusOptions)
1048+
1049+
// When
1050+
viewModel.onBulkUpdateStatusClicked()
1051+
1052+
// Then
1053+
assertThat(viewModel.event.value).isInstanceOf(OrderListEvent.ShowUpdateStatusDialog::class.java)
1054+
}
1055+
1056+
@Test
1057+
fun `given offline, when bulk update status requested, then show offline error and exit selection mode`() = testBlocking {
1058+
whenever(networkStatus.isConnected()).thenReturn(false)
1059+
1060+
viewModel.onBulkOrderStatusChanged(listOf(1L, 2L), Order.Status.Completed)
1061+
1062+
assertThat(viewModel.event.value).isInstanceOf(Event.ShowSnackbar::class.java)
1063+
assertThat((viewModel.event.value as Event.ShowSnackbar).message).isEqualTo(R.string.offline_error)
1064+
assertThat(viewModel.isSelecting()).isFalse()
1065+
}
1066+
1067+
@Test
1068+
fun `when bulk update fails, then show error message and exit selection`() = testBlocking {
1069+
whenever(networkStatus.isConnected()).thenReturn(true)
1070+
whenever(orderListRepository.bulkUpdateOrderStatus(any(), any())).thenReturn(Result.failure(Exception()))
1071+
1072+
viewModel.onBulkOrderStatusChanged(listOf(1L), Order.Status.Completed)
1073+
1074+
assertThat(viewModel.event.value).isInstanceOf(Event.ShowSnackbar::class.java)
1075+
assertThat((viewModel.event.value as Event.ShowSnackbar).message).isEqualTo(R.string.error_generic)
1076+
assertThat(viewModel.isSelecting()).isFalse()
1077+
}
1078+
1079+
@Test
1080+
fun `when bulk update completes successfully, then exit selection mode`() = testBlocking {
1081+
whenever(networkStatus.isConnected()).thenReturn(true)
1082+
whenever(orderListRepository.bulkUpdateOrderStatus(any(), any())).thenReturn(Result.success(Unit))
1083+
1084+
// First enter selection mode
1085+
viewModel.onSelectionChanged(1)
1086+
assertThat(viewModel.isSelecting()).isTrue()
1087+
1088+
viewModel.onBulkOrderStatusChanged(listOf(1L), Order.Status.Completed)
1089+
1090+
assertThat(viewModel.isSelecting()).isFalse()
1091+
}
1092+
10001093
@Test
10011094
fun `when selection count reaches limit, then show error message`() {
10021095
// when

0 commit comments

Comments
 (0)