-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #168 from badoo/issue-167-fix-serialization-behaviour
[#167] Reverted BaseFeature PostProcessor recursion behaviour change
- Loading branch information
Showing
3 changed files
with
127 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
mvicore/src/test/java/com/badoo/mvicore/feature/BaseFeaturePostProcessorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.badoo.mvicore.feature | ||
|
||
import com.badoo.mvicore.element.Actor | ||
import com.badoo.mvicore.element.NewsPublisher | ||
import com.badoo.mvicore.element.PostProcessor | ||
import com.badoo.mvicore.element.Reducer | ||
import com.badoo.mvicore.feature.PostProcessorTestFeature.* | ||
import io.reactivex.Observable | ||
import org.junit.Test | ||
|
||
class BaseFeaturePostProcessorTest { | ||
@Test | ||
fun `GIVEN feature scheduler provided AND InitialTrigger sent WHEN post processor sends PostProcessorTrigger THEN news is in wish order`() { | ||
val feature = PostProcessorTestFeature(featureScheduler = FeatureSchedulers.TrampolineFeatureScheduler) | ||
val newsTestObserver = Observable.wrap(feature.news).test() | ||
feature.accept(Wish.InitialTrigger) | ||
|
||
newsTestObserver.assertValues(News.TriggerNews, News.PostProcessorNews) | ||
} | ||
|
||
/** | ||
* The post processor is recursively calling the actor, meaning the news is in reverse order in this scenario. | ||
*/ | ||
@Test | ||
fun `GIVEN feature scheduler not provided AND InitialTrigger sent WHEN post processor sends PostProcessorTrigger THEN news is in recursive order`() { | ||
val feature = PostProcessorTestFeature(featureScheduler = null) | ||
val newsTestObserver = Observable.wrap(feature.news).test() | ||
feature.accept(Wish.InitialTrigger) | ||
|
||
newsTestObserver.assertValues(News.PostProcessorNews, News.TriggerNews) | ||
} | ||
} | ||
|
||
private class PostProcessorTestFeature(featureScheduler: FeatureScheduler?) : | ||
BaseFeature<Wish, Wish, Effect, State, News>( | ||
actor = ActorImpl(), | ||
initialState = State, | ||
reducer = ReducerImpl(), | ||
wishToAction = { it }, | ||
newsPublisher = NewsPublisherImpl(), | ||
postProcessor = PostProcessorImpl(), | ||
featureScheduler = featureScheduler | ||
) { | ||
|
||
sealed class Wish { | ||
object InitialTrigger : Wish() | ||
object PostProcessorTrigger : Wish() | ||
} | ||
|
||
sealed class Effect { | ||
object TriggerEffect : Effect() | ||
object PostProcessorEffect : Effect() | ||
} | ||
|
||
object State | ||
|
||
sealed class News { | ||
object TriggerNews : News() | ||
object PostProcessorNews : News() | ||
} | ||
|
||
class ActorImpl : Actor<State, Wish, Effect> { | ||
override fun invoke(state: State, wish: Wish): Observable<out Effect> = | ||
when (wish) { | ||
is Wish.InitialTrigger -> Observable.just(Effect.TriggerEffect) | ||
is Wish.PostProcessorTrigger -> Observable.just(Effect.PostProcessorEffect) | ||
} | ||
} | ||
|
||
class ReducerImpl : Reducer<State, Effect> { | ||
override fun invoke(state: State, effect: Effect): State = state | ||
} | ||
|
||
class NewsPublisherImpl : NewsPublisher<Wish, Effect, State, News> { | ||
override fun invoke(action: Wish, effect: Effect, state: State): News = | ||
when (effect) { | ||
is Effect.TriggerEffect -> News.TriggerNews | ||
is Effect.PostProcessorEffect -> News.PostProcessorNews | ||
} | ||
} | ||
|
||
class PostProcessorImpl : PostProcessor<Wish, Effect, State> { | ||
override fun invoke(action: Wish, effect: Effect, state: State): Wish? = | ||
if (action is Wish.InitialTrigger) { | ||
Wish.PostProcessorTrigger | ||
} else { | ||
null | ||
} | ||
} | ||
} |