Skip to content

Commit a4e9094

Browse files
authored
Merge pull request #14914 from woocommerce/woomob-1624-woo-poslocal-catalog-hide-catalog-settings-when-store-too
[Woo POS][Local Catalog] Hide `Local Catalog` setting category if local catalog is not supported
2 parents 84107d6 + b08533a commit a4e9094

File tree

2 files changed

+135
-11
lines changed

2 files changed

+135
-11
lines changed
Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
11
package com.woocommerce.android.ui.woopos.settings.categories
22

3-
import android.content.Context
43
import androidx.lifecycle.ViewModel
5-
import com.woocommerce.android.util.FeatureFlag
4+
import androidx.lifecycle.viewModelScope
5+
import com.woocommerce.android.tools.SelectedSite
6+
import com.woocommerce.android.ui.woopos.localcatalog.WooPosIsLocalCatalogSupported
67
import dagger.hilt.android.lifecycle.HiltViewModel
7-
import dagger.hilt.android.qualifiers.ApplicationContext
88
import kotlinx.coroutines.flow.MutableStateFlow
99
import kotlinx.coroutines.flow.StateFlow
1010
import kotlinx.coroutines.flow.asStateFlow
11+
import kotlinx.coroutines.launch
1112
import javax.inject.Inject
1213

1314
@HiltViewModel
1415
class WooPosSettingsCategoriesViewModel @Inject constructor(
15-
@ApplicationContext private val context: Context
16+
selectedSite: SelectedSite,
17+
private val isLocalCatalogSupported: WooPosIsLocalCatalogSupported,
1618
) : ViewModel() {
1719
private val _state = MutableStateFlow(createInitialState())
1820
val state: StateFlow<WooPosSettingsCategoriesState> = _state.asStateFlow()
19-
private fun createInitialState(): WooPosSettingsCategoriesState {
20-
val allCategories = WooPosSettingsCategory.entries
21-
val visibleCategories = if (FeatureFlag.WOO_POS_LOCAL_CATALOG_M1.isEnabled(context)) {
22-
allCategories
23-
} else {
24-
allCategories.filter { it != WooPosSettingsCategory.LOCAL_CATALOG }
21+
22+
init {
23+
viewModelScope.launch {
24+
val categories = WooPosSettingsCategory.entries.filter {
25+
if (!isLocalCatalogSupported(selectedSite.get().localId())) {
26+
it != WooPosSettingsCategory.LOCAL_CATALOG
27+
} else {
28+
true
29+
}
30+
}
31+
_state.value = WooPosSettingsCategoriesState(categories)
2532
}
26-
return WooPosSettingsCategoriesState(categories = visibleCategories)
33+
}
34+
35+
private fun createInitialState(): WooPosSettingsCategoriesState {
36+
return WooPosSettingsCategoriesState(
37+
categories = WooPosSettingsCategory.entries.filter { it != WooPosSettingsCategory.LOCAL_CATALOG }
38+
)
2739
}
2840
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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

Comments
 (0)