Bug 1858018 - Persist review checker reanalysis state
parent
be5fb77680
commit
692e1e6853
@ -0,0 +1,17 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.components.appstate.shopping
|
||||
|
||||
/**
|
||||
* State for shopping feature that's required to live the lifetime of a session.
|
||||
*
|
||||
* @property productsInAnalysis Set of product ids that are currently being analysed or were being
|
||||
* analysed when the sheet was closed.
|
||||
* @property shoppingSheetExpanded Boolean indicating if the shopping sheet is expanded and visible.
|
||||
*/
|
||||
data class ShoppingState(
|
||||
val productsInAnalysis: Set<String> = emptySet(),
|
||||
val shoppingSheetExpanded: Boolean? = null,
|
||||
)
|
@ -0,0 +1,43 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.components.appstate.shopping
|
||||
|
||||
import mozilla.components.support.base.log.logger.Logger
|
||||
import org.mozilla.fenix.components.appstate.AppAction.ShoppingAction
|
||||
import org.mozilla.fenix.components.appstate.AppState
|
||||
|
||||
/**
|
||||
* Reducer for the shopping state that handles [ShoppingAction]s.
|
||||
*/
|
||||
internal object ShoppingStateReducer {
|
||||
|
||||
private val logger = Logger("ReviewQualityCheckShoppingStateReducer")
|
||||
|
||||
/**
|
||||
* Reduces the given [ShoppingAction] into a new [AppState].
|
||||
*/
|
||||
fun reduce(state: AppState, action: ShoppingAction): AppState =
|
||||
when (action) {
|
||||
is ShoppingAction.ShoppingSheetStateUpdated -> state.copy(
|
||||
shoppingState = state.shoppingState.copy(
|
||||
shoppingSheetExpanded = action.expanded,
|
||||
),
|
||||
)
|
||||
|
||||
is ShoppingAction.AddToProductAnalysed -> state.copy(
|
||||
shoppingState = state.shoppingState.copy(
|
||||
productsInAnalysis = state.shoppingState.productsInAnalysis + action.productPageUrl,
|
||||
),
|
||||
)
|
||||
|
||||
is ShoppingAction.RemoveFromProductAnalysed -> state.copy(
|
||||
shoppingState = state.shoppingState.copy(
|
||||
productsInAnalysis = state.shoppingState.productsInAnalysis - action.productPageUrl,
|
||||
),
|
||||
)
|
||||
}.also {
|
||||
logger.debug("Action processed: $action, updated shopping state: ${it.shoppingState}")
|
||||
}
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.components.appstate
|
||||
|
||||
import mozilla.components.support.test.ext.joinBlocking
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
import org.mozilla.fenix.components.AppStore
|
||||
import org.mozilla.fenix.components.appstate.shopping.ShoppingState
|
||||
|
||||
class ShoppingActionTest {
|
||||
|
||||
@Test
|
||||
fun `WHEN shopping sheet is collapsed THEN state should reflect that`() {
|
||||
val store = AppStore(
|
||||
initialState = AppState(
|
||||
shoppingState = ShoppingState(
|
||||
shoppingSheetExpanded = true,
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
store.dispatch(AppAction.ShoppingAction.ShoppingSheetStateUpdated(false)).joinBlocking()
|
||||
|
||||
val expected = ShoppingState(
|
||||
shoppingSheetExpanded = false,
|
||||
)
|
||||
|
||||
assertEquals(expected, store.state.shoppingState)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN shopping sheet is expanded THEN state should reflect that`() {
|
||||
val store = AppStore()
|
||||
|
||||
store.dispatch(AppAction.ShoppingAction.ShoppingSheetStateUpdated(true)).joinBlocking()
|
||||
|
||||
val expected = ShoppingState(
|
||||
shoppingSheetExpanded = true,
|
||||
)
|
||||
|
||||
assertEquals(expected, store.state.shoppingState)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN product analysed is added THEN state should reflect that`() {
|
||||
val store = AppStore()
|
||||
|
||||
store.dispatch(AppAction.ShoppingAction.AddToProductAnalysed("pdp")).joinBlocking()
|
||||
|
||||
val expected = ShoppingState(
|
||||
productsInAnalysis = setOf("pdp"),
|
||||
)
|
||||
|
||||
assertEquals(expected, store.state.shoppingState)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN product analysed is removed THEN state should reflect that`() {
|
||||
val store = AppStore(
|
||||
initialState = AppState(
|
||||
shoppingState = ShoppingState(
|
||||
productsInAnalysis = setOf("pdp"),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
store.dispatch(AppAction.ShoppingAction.RemoveFromProductAnalysed("pdp")).joinBlocking()
|
||||
|
||||
val expected = ShoppingState(
|
||||
productsInAnalysis = emptySet(),
|
||||
)
|
||||
|
||||
assertEquals(expected, store.state.shoppingState)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue