Last active
March 17, 2017 15:13
-
-
Save exallium/ff47dad3227c3534ce9703a81d0ba353 to your computer and use it in GitHub Desktop.
debounceEx2
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
interface View { | |
fun clicks(): Observable<Unit> | |
} | |
interface Model { | |
fun submits(): Observable<Unit> | |
} | |
class Presenter(val view: View, val model: Model) { | |
fun setUp() { | |
view | |
.debounce(100, TimeUnit.MILLISECONDS) | |
.flatMap { model.submits() } | |
.susbscribe() | |
} | |
} | |
class PresenterTest { | |
@InjectMocks | |
lateinit var testSubject: Presenter | |
@Mock | |
lateinit var view: View | |
@Mock | |
lateinit var model: Model | |
@Test | |
fun viewClicks_onlySumbitsOneEventPer115Ms() { | |
// GIVEN | |
val scheduler = setUpRxComputationScheduler() | |
val subject = PublishSubject<Unit>() | |
whenever(view.clicks()).thenReturn(subject) | |
testSubject.setUp() | |
// WHEN | |
subject.onNext(Unit) | |
subject.onNext(Unit) | |
subject.onNext(Unit) | |
scheduler.advanceBy(115, TimeUnit.MILLISECONDS) | |
subject.onNext(Unit) | |
subject.onNext(Unit) | |
subject.onNext(Unit) | |
scheduler.advanceBy(230, TimeUnit.MILLISECONDS) | |
// THEN | |
verify(model, times(2)).submit() | |
} | |
fun setUpRxComputationScheduler(): TestScheduler { | |
val scheduler = TestScheduler() | |
RxJavaPlugins.getInstance().reset() | |
RxJavaPlugins.getInstance().registerSchedulersHook(object : RxJavaSchedulersHook() { | |
override fun getComputationScheduler(): Scheduler { | |
return scheduler | |
} | |
}) | |
return scheduler | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment