@@ -9,6 +9,7 @@ import androidx.test.core.app.ApplicationProvider
99import androidx.test.ext.junit.runners.AndroidJUnit4
1010import app.cash.turbine.ReceiveTurbine
1111import app.cash.turbine.Turbine
12+ import com.google.firebase.remoteconfig.FirebaseRemoteConfig
1213import com.slack.circuit.backstack.SaveableBackStack
1314import com.slack.circuit.foundation.Circuit
1415import com.slack.circuit.foundation.CircuitCompositionLocals
@@ -37,6 +38,7 @@ import org.ccci.gto.android.common.util.content.equalsIntent
3738import org.cru.godtools.analytics.model.OpenAnalyticsActionEvent
3839import org.cru.godtools.analytics.model.OpenAnalyticsActionEvent.Companion.ACTION_OPEN_LESSON
3940import org.cru.godtools.analytics.model.OpenAnalyticsActionEvent.Companion.SOURCE_LESSONS
41+ import org.cru.godtools.base.CONFIG_UI_DASHBOARD_PERSONALIZATION_ENABLED
4042import org.cru.godtools.base.Settings
4143import org.cru.godtools.base.ui.circuit.screen.dashboard.page.LessonsScreen
4244import org.cru.godtools.db.repository.LanguagesRepository
@@ -48,6 +50,8 @@ import org.cru.godtools.model.Translation
4850import org.cru.godtools.model.randomTool
4951import org.cru.godtools.model.randomTranslation
5052import org.cru.godtools.ui.dashboard.filters.FilterMenu
53+ import org.cru.godtools.ui.dashboard.lessons.LessonsPresenter.UiEvent
54+ import org.cru.godtools.ui.dashboard.lessons.LessonsPresenter.UiState
5155import org.cru.godtools.ui.tools.FakeToolCardPresenter
5256import org.cru.godtools.ui.tools.ToolCardPresenter.ToolCardEvent
5357import org.cru.godtools.util.createToolIntent
@@ -65,11 +69,14 @@ class LessonsPresenterTest {
6569 private val enLessonsFlow = MutableStateFlow (emptyList<Tool >())
6670 private val languagesFlow = MutableStateFlow (emptyList<Language >())
6771 private val translationsFlow = MutableStateFlow (emptyList<Translation >())
72+ private var isPersonalizationEnabled = true
6873
6974 private val testScope = TestScope ()
70-
7175 private val context: Context = ApplicationProvider .getApplicationContext()
7276 private val eventBus: EventBus = mockk(relaxUnitFun = true )
77+ private val remoteConfig: FirebaseRemoteConfig = mockk {
78+ every { getBoolean(CONFIG_UI_DASHBOARD_PERSONALIZATION_ENABLED ) } answers { isPersonalizationEnabled }
79+ }
7380 private val languagesRepository: LanguagesRepository = mockk {
7481 every { findLanguageFlow(any()) } answers { flowOf(Language (firstArg())) }
7582 every { getLanguagesFlow() } returns languagesFlow
@@ -94,6 +101,7 @@ class LessonsPresenterTest {
94101 context = context,
95102 eventBus = eventBus,
96103 languagesRepository = languagesRepository,
104+ remoteConfig = remoteConfig,
97105 settings = settings,
98106 toolCardPresenter = FakeToolCardPresenter (),
99107 toolsRepository = toolsRepository,
@@ -110,12 +118,12 @@ class LessonsPresenterTest {
110118
111119 // This logic is based on the Sample AnsweringNavigatorTest in the circuit library.
112120 // see: https://github.com/slackhq/circuit/blob/main/circuit-foundation/src/jvmTest/kotlin/com/slack/circuit/foundation/AnsweringNavigatorTest.kt
113- private fun testPresenterWithStateRestoration (): ReceiveTurbine <LessonsPresenter . UiState > {
114- val presenterState = Turbine <LessonsPresenter . UiState >()
121+ private fun testPresenterWithStateRestoration (): ReceiveTurbine <UiState > {
122+ val presenterState = Turbine <UiState >()
115123
116124 val circuit = Circuit .Builder ()
117- .addPresenter<LessonsScreen , LessonsPresenter . UiState > { s, n , _ -> presenter }
118- .addUi<LessonsScreen , LessonsPresenter . UiState > { state, _ -> SideEffect { presenterState.add(state) } }
125+ .addPresenter<LessonsScreen , UiState > { _, _ , _ -> presenter }
126+ .addUi<LessonsScreen , UiState > { state, _ -> SideEffect { presenterState.add(state) } }
119127 .build()
120128
121129 stateRestorationTester.setContent {
@@ -140,6 +148,41 @@ class LessonsPresenterTest {
140148 navigator.assertResetRootIsEmpty()
141149 }
142150
151+ // region State.mode
152+ @Test
153+ fun `State - mode - personalization disabled` () = testScope.runTest {
154+ isPersonalizationEnabled = false
155+ presenter.test {
156+ assertEquals(UiState .Mode .ALL_LESSONS , expectMostRecentItem().mode)
157+ }
158+ }
159+
160+ @Test
161+ fun `State - mode - personalization enabled` () = testScope.runTest {
162+ presenter.test {
163+ val state = awaitItem()
164+ assertEquals(UiState .Mode .PERSONALIZATION , state.mode)
165+
166+ state.eventSink(UiEvent .ChangeMode (UiState .Mode .ALL_LESSONS ))
167+ assertEquals(UiState .Mode .ALL_LESSONS , awaitItem().mode)
168+ }
169+ }
170+
171+ @Test
172+ fun `State - mode - persisted through state save & restore` () = testScope.runTest {
173+ testPresenterWithStateRestoration().test {
174+ val state = expectMostRecentItem()
175+ assertEquals(UiState .Mode .PERSONALIZATION , state.mode)
176+ state.eventSink(UiEvent .ChangeMode (UiState .Mode .ALL_LESSONS ))
177+ composeTestRule.waitForIdle()
178+ assertEquals(UiState .Mode .ALL_LESSONS , expectMostRecentItem().mode)
179+
180+ stateRestorationTester.emulateSavedInstanceStateRestore()
181+ assertEquals(UiState .Mode .ALL_LESSONS , awaitItem().mode)
182+ }
183+ }
184+ // endregion State.mode
185+
143186 // region State.languageFilter.selectedItem
144187 @Test
145188 fun `State - languageFilter - selectedItem - default to app language` () = testScope.runTest {
0 commit comments