Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejo committed Dec 11, 2024
1 parent b256baa commit 063540b
Showing 1 changed file with 98 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ import org.mockito.kotlin.any
import org.mockito.kotlin.doAnswer
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import java.math.BigDecimal
import kotlin.test.assertEquals
import kotlin.test.assertIs

@OptIn(ExperimentalCoroutinesApi::class)
class WooShippingLabelCreationViewModelTest : BaseUnitTest() {
Expand Down Expand Up @@ -65,6 +68,8 @@ class WooShippingLabelCreationViewModelTest : BaseUnitTest() {
isVerified = true
)
)
private val defaultShippingRates = emptyMap<Carrier, List<ShippingRateUI>>()

private val orderDetailRepository: OrderDetailRepository = mock()
private val getShippableItems: GetShippableItems = mock()
private val currencyFormatter: CurrencyFormatter = mock {
Expand All @@ -77,6 +82,7 @@ class WooShippingLabelCreationViewModelTest : BaseUnitTest() {
WooShippingLabelCreationFragmentArgs(orderId = orderId).toSavedStateHandle()

private val observeOriginAddresses: ObserveOriginAddresses = mock()
private val getShippingRates: GetShippingRates = mock()

private lateinit var sut: WooShippingLabelCreationViewModel

Expand All @@ -86,6 +92,7 @@ class WooShippingLabelCreationViewModelTest : BaseUnitTest() {
getShippableItems = getShippableItems,
currencyFormatter = currencyFormatter,
observeOriginAddresses = observeOriginAddresses,
getShippingRates = getShippingRates,
savedState = savedState
)
}
Expand All @@ -98,6 +105,7 @@ class WooShippingLabelCreationViewModelTest : BaseUnitTest() {
whenever(orderDetailRepository.getOrderById(any())) doReturn order
whenever(getShippableItems(any())) doReturn defaultShippableItems
whenever(observeOriginAddresses()) doReturn flowOf(defaultOriginAddresses)
whenever(getShippingRates(any(), any())) doReturn Result.success(defaultShippingRates)

createViewModel()

Expand All @@ -115,6 +123,7 @@ class WooShippingLabelCreationViewModelTest : BaseUnitTest() {
whenever(orderDetailRepository.getOrderById(any())) doReturn order
whenever(getShippableItems(any())) doReturn defaultShippableItems
whenever(observeOriginAddresses()) doReturn flowOf(defaultOriginAddresses)
whenever(getShippingRates(any(), any())) doReturn Result.success(defaultShippingRates)

createViewModel()

Expand All @@ -130,6 +139,7 @@ class WooShippingLabelCreationViewModelTest : BaseUnitTest() {
val order: Order? = null
whenever(orderDetailRepository.getOrderById(any())) doReturn order
whenever(observeOriginAddresses()) doReturn flowOf(defaultOriginAddresses)
whenever(getShippingRates(any(), any())) doReturn Result.success(defaultShippingRates)

createViewModel()

Expand All @@ -144,6 +154,7 @@ class WooShippingLabelCreationViewModelTest : BaseUnitTest() {
)
whenever(orderDetailRepository.getOrderById(any())) doReturn order
whenever(observeOriginAddresses()) doReturn flowOf(emptyList())
whenever(getShippingRates(any(), any())) doReturn Result.success(defaultShippingRates)

createViewModel()

Expand All @@ -159,6 +170,7 @@ class WooShippingLabelCreationViewModelTest : BaseUnitTest() {
whenever(orderDetailRepository.getOrderById(any())) doReturn order
whenever(getShippableItems(any())) doReturn defaultShippableItems
whenever(observeOriginAddresses()) doReturn flowOf(defaultOriginAddresses)
whenever(getShippingRates(any(), any())) doReturn Result.success(defaultShippingRates)

createViewModel()

Expand All @@ -169,4 +181,90 @@ class WooShippingLabelCreationViewModelTest : BaseUnitTest() {
val ids = dataState.shippingAddresses.originAddresses.map { it.id }
assert(ids.containsAll(defaultOriginAddresses.map { it.id }))
}

@Test
fun `when shipping rates succeed then display the shipping rates`() = testBlocking {
val order = OrderTestUtils.generateTestOrder(orderId = orderId).copy(
shippingLines = defaultShippingLines
)
whenever(orderDetailRepository.getOrderById(any())) doReturn order
whenever(getShippableItems(any())) doReturn defaultShippableItems
whenever(observeOriginAddresses()) doReturn flowOf(defaultOriginAddresses)
whenever(getShippingRates(any(), any())) doReturn Result.success(defaultShippingRates)

createViewModel()

val currentViewState = sut.viewState.value
assert(currentViewState is WooShippingViewState.DataState)
val dataState = currentViewState as WooShippingViewState.DataState
assertIs<WooShippingLabelCreationViewModel.ShippingRatesState.DataState>(dataState.shippingRates)
}

@Test
fun `when shipping rates fail then display an error`() = testBlocking {
val order = OrderTestUtils.generateTestOrder(orderId = orderId).copy(
shippingLines = defaultShippingLines
)
whenever(orderDetailRepository.getOrderById(any())) doReturn order
whenever(getShippableItems(any())) doReturn defaultShippableItems
whenever(observeOriginAddresses()) doReturn flowOf(defaultOriginAddresses)
whenever(getShippingRates(any(), any())) doReturn Result.failure(Exception("Random error"))

createViewModel()

val currentViewState = sut.viewState.value
assert(currentViewState is WooShippingViewState.DataState)
val dataState = currentViewState as WooShippingViewState.DataState
assertIs<WooShippingLabelCreationViewModel.ShippingRatesState.Error>(dataState.shippingRates)
}

@Test
fun `when refresh rates is triggered then refresh shipping rates`() = testBlocking {
val order = OrderTestUtils.generateTestOrder(orderId = orderId).copy(
shippingLines = defaultShippingLines
)
whenever(orderDetailRepository.getOrderById(any())) doReturn order
whenever(getShippableItems(any())) doReturn defaultShippableItems
whenever(observeOriginAddresses()) doReturn flowOf(defaultOriginAddresses)
whenever(getShippingRates(any(), any())) doReturn Result.success(defaultShippingRates)

createViewModel()
sut.onRefreshShippingRates()

verify(getShippingRates, times(2)).invoke(any(), any())
}

@Test
fun `when rates sort order is changed then refresh shipping rates`() = testBlocking {
val order = OrderTestUtils.generateTestOrder(orderId = orderId).copy(
shippingLines = defaultShippingLines
)
whenever(orderDetailRepository.getOrderById(any())) doReturn order
whenever(getShippableItems(any())) doReturn defaultShippableItems
whenever(observeOriginAddresses()) doReturn flowOf(defaultOriginAddresses)
whenever(getShippingRates(any(), any())) doReturn Result.success(defaultShippingRates)

createViewModel()

sut.onSelectedRateSortOrderChanged(ShippingSortOption.CHEAPEST)

verify(getShippingRates, times(2)).invoke(any(), any())
}

@Test
fun `when rates sort order is NOT changed then DON'T refresh shipping rates`() = testBlocking {
val order = OrderTestUtils.generateTestOrder(orderId = orderId).copy(
shippingLines = defaultShippingLines
)
whenever(orderDetailRepository.getOrderById(any())) doReturn order
whenever(getShippableItems(any())) doReturn defaultShippableItems
whenever(observeOriginAddresses()) doReturn flowOf(defaultOriginAddresses)
whenever(getShippingRates(any(), any())) doReturn Result.success(defaultShippingRates)

createViewModel()

sut.onSelectedRateSortOrderChanged(ShippingSortOption.FASTEST)

verify(getShippingRates, times(1)).invoke(any(), any())
}
}

0 comments on commit 063540b

Please sign in to comment.