|
| 1 | +package com.woocommerce.android.ui.products.details.webview |
| 2 | + |
| 3 | +import androidx.lifecycle.SavedStateHandle |
| 4 | +import com.woocommerce.android.tools.SelectedSite |
| 5 | +import com.woocommerce.android.ui.common.webview.WebViewAuthenticator |
| 6 | +import com.woocommerce.android.ui.products.ProductTestUtils |
| 7 | +import com.woocommerce.android.ui.products.details.ProductDetailRepository |
| 8 | +import com.woocommerce.android.util.captureValues |
| 9 | +import com.woocommerce.android.util.getOrAwaitValue |
| 10 | +import com.woocommerce.android.util.runAndCaptureValues |
| 11 | +import com.woocommerce.android.viewmodel.BaseUnitTest |
| 12 | +import com.woocommerce.android.viewmodel.MultiLiveEvent |
| 13 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 14 | +import org.assertj.core.api.Assertions.assertThat |
| 15 | +import org.junit.Before |
| 16 | +import org.junit.Test |
| 17 | +import org.mockito.kotlin.doReturn |
| 18 | +import org.mockito.kotlin.mock |
| 19 | +import org.mockito.kotlin.whenever |
| 20 | +import org.wordpress.android.fluxc.model.SiteModel |
| 21 | +import org.wordpress.android.fluxc.network.UserAgent |
| 22 | + |
| 23 | +@ExperimentalCoroutinesApi |
| 24 | +class ProductDetailWebViewViewModelTest : BaseUnitTest() { |
| 25 | + |
| 26 | + private val productDetailRepository: ProductDetailRepository = mock() |
| 27 | + private val selectedSite: SelectedSite = mock() |
| 28 | + private val webViewAuthenticator: WebViewAuthenticator = mock() |
| 29 | + private val userAgent: UserAgent = mock() |
| 30 | + |
| 31 | + private lateinit var viewModel: ProductDetailWebViewViewModel |
| 32 | + private lateinit var savedStateHandle: SavedStateHandle |
| 33 | + |
| 34 | + private val testProduct = ProductTestUtils.generateProduct( |
| 35 | + productId = 456L, |
| 36 | + productName = "Test Product" |
| 37 | + ) |
| 38 | + |
| 39 | + private val testSite = SiteModel().apply { |
| 40 | + adminUrl = "https://example.com/wp-admin/" |
| 41 | + } |
| 42 | + |
| 43 | + @Before |
| 44 | + fun setup() { |
| 45 | + whenever(selectedSite.get()).doReturn(testSite) |
| 46 | + } |
| 47 | + |
| 48 | + private suspend fun setupViewModel( |
| 49 | + remoteProductId: Long = testProduct.remoteId, |
| 50 | + setupMocks: suspend () -> Unit = {} |
| 51 | + ) { |
| 52 | + setupMocks() |
| 53 | + |
| 54 | + savedStateHandle = SavedStateHandle().apply { |
| 55 | + set("remoteProductId", remoteProductId) |
| 56 | + } |
| 57 | + |
| 58 | + viewModel = ProductDetailWebViewViewModel( |
| 59 | + savedStateHandle = savedStateHandle, |
| 60 | + productDetailRepository = productDetailRepository, |
| 61 | + selectedSite = selectedSite, |
| 62 | + webViewAuthenticator = webViewAuthenticator, |
| 63 | + userAgent = userAgent |
| 64 | + ) |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + fun `given valid product, when viewState is observed, then correct ViewState is emitted`() = testBlocking { |
| 69 | + // GIVEN |
| 70 | + setupViewModel { |
| 71 | + whenever(productDetailRepository.getProductAsync(testProduct.remoteId)) |
| 72 | + .doReturn(testProduct) |
| 73 | + } |
| 74 | + |
| 75 | + // WHEN |
| 76 | + val viewState = viewModel.viewState.getOrAwaitValue() |
| 77 | + |
| 78 | + // THEN |
| 79 | + assertThat(viewState.title).isEqualTo(testProduct.name) |
| 80 | + assertThat(viewState.urlToLoad).isEqualTo( |
| 81 | + "https://example.com/wp-admin/?page=next-admin&p=/woocommerce/products/edit/${testProduct.remoteId}" |
| 82 | + ) |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + fun `given product not found, when viewState is observed, then navigation back event is triggered`() = testBlocking { |
| 87 | + // GIVEN |
| 88 | + setupViewModel { |
| 89 | + whenever(productDetailRepository.getProductAsync(testProduct.remoteId)) |
| 90 | + .doReturn(null) |
| 91 | + } |
| 92 | + |
| 93 | + // WHEN |
| 94 | + val event = viewModel.event.runAndCaptureValues { |
| 95 | + // Trigger viewState observation |
| 96 | + viewModel.viewState.captureValues() |
| 97 | + }.last() |
| 98 | + |
| 99 | + // THEN |
| 100 | + assertThat(event).isEqualTo(MultiLiveEvent.Event.Exit) |
| 101 | + } |
| 102 | +} |
0 commit comments