|
| 1 | +package com.woocommerce.android.ui.woopos.settings.categories |
| 2 | + |
| 3 | +import com.woocommerce.android.tools.SelectedSite |
| 4 | +import com.woocommerce.android.ui.woopos.localcatalog.WooPosIsLocalCatalogSupported |
| 5 | +import com.woocommerce.android.ui.woopos.util.WooPosCoroutineTestRule |
| 6 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 7 | +import kotlinx.coroutines.test.advanceUntilIdle |
| 8 | +import kotlinx.coroutines.test.runTest |
| 9 | +import org.assertj.core.api.Assertions.assertThat |
| 10 | +import org.junit.Before |
| 11 | +import org.junit.Rule |
| 12 | +import org.junit.Test |
| 13 | +import org.mockito.kotlin.mock |
| 14 | +import org.mockito.kotlin.whenever |
| 15 | +import org.wordpress.android.fluxc.model.SiteModel |
| 16 | + |
| 17 | +@ExperimentalCoroutinesApi |
| 18 | +class WooPosSettingsCategoriesViewModelTest { |
| 19 | + |
| 20 | + @Rule |
| 21 | + @JvmField |
| 22 | + val coroutineTestRule = WooPosCoroutineTestRule() |
| 23 | + |
| 24 | + private val isLocalCatalogSupported: WooPosIsLocalCatalogSupported = mock() |
| 25 | + private val selectedSite: SelectedSite = mock() |
| 26 | + |
| 27 | + private val mockSite = SiteModel().apply { id = 123 } |
| 28 | + |
| 29 | + private lateinit var sut: WooPosSettingsCategoriesViewModel |
| 30 | + |
| 31 | + @Before |
| 32 | + fun setUp() { |
| 33 | + whenever(selectedSite.get()).thenReturn(mockSite) |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + fun `given local catalog not supported, when viewmodel is initialized, then LOCAL_CATALOG is hidden`() = runTest { |
| 38 | + // GIVEN |
| 39 | + whenever(isLocalCatalogSupported(mockSite.localId())).thenReturn(false) |
| 40 | + |
| 41 | + // WHEN |
| 42 | + sut = createViewModel() |
| 43 | + advanceUntilIdle() |
| 44 | + |
| 45 | + // THEN |
| 46 | + assertThat(sut.state.value.categories).doesNotContain(WooPosSettingsCategory.LOCAL_CATALOG) |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + fun `given local catalog supported, when viewmodel is initialized, then LOCAL_CATALOG is visible`() = runTest { |
| 51 | + // GIVEN |
| 52 | + whenever(isLocalCatalogSupported(mockSite.localId())).thenReturn(true) |
| 53 | + |
| 54 | + // WHEN |
| 55 | + sut = createViewModel() |
| 56 | + advanceUntilIdle() |
| 57 | + |
| 58 | + // THEN |
| 59 | + assertThat(sut.state.value.categories).contains(WooPosSettingsCategory.LOCAL_CATALOG) |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + fun `given local catalog not supported, when viewmodel is initialized, then other categories are visible`() = runTest { |
| 64 | + // GIVEN |
| 65 | + whenever(isLocalCatalogSupported(mockSite.localId())).thenReturn(false) |
| 66 | + |
| 67 | + // WHEN |
| 68 | + sut = createViewModel() |
| 69 | + advanceUntilIdle() |
| 70 | + |
| 71 | + // THEN |
| 72 | + assertThat(sut.state.value.categories).contains( |
| 73 | + WooPosSettingsCategory.STORE, |
| 74 | + WooPosSettingsCategory.HARDWARE, |
| 75 | + WooPosSettingsCategory.HELP |
| 76 | + ) |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + fun `given local catalog supported, when viewmodel is initialized, then all categories are visible`() = runTest { |
| 81 | + // GIVEN |
| 82 | + whenever(isLocalCatalogSupported(mockSite.localId())).thenReturn(true) |
| 83 | + |
| 84 | + // WHEN |
| 85 | + sut = createViewModel() |
| 86 | + advanceUntilIdle() |
| 87 | + |
| 88 | + // THEN |
| 89 | + assertThat(sut.state.value.categories).containsExactlyInAnyOrder( |
| 90 | + WooPosSettingsCategory.STORE, |
| 91 | + WooPosSettingsCategory.HARDWARE, |
| 92 | + WooPosSettingsCategory.LOCAL_CATALOG, |
| 93 | + WooPosSettingsCategory.HELP |
| 94 | + ) |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + fun `when default state is created, then LOCAL_CATALOG is initially hidden`() { |
| 99 | + // GIVEN & WHEN |
| 100 | + val initialState = WooPosSettingsCategoriesState( |
| 101 | + categories = WooPosSettingsCategory.entries.filter { it != WooPosSettingsCategory.LOCAL_CATALOG } |
| 102 | + ) |
| 103 | + |
| 104 | + // THEN |
| 105 | + assertThat(initialState.categories).doesNotContain(WooPosSettingsCategory.LOCAL_CATALOG) |
| 106 | + } |
| 107 | + |
| 108 | + private fun createViewModel() = WooPosSettingsCategoriesViewModel( |
| 109 | + selectedSite = selectedSite, |
| 110 | + isLocalCatalogSupported = isLocalCatalogSupported, |
| 111 | + ) |
| 112 | +} |
0 commit comments