This repository has been archived by the owner on Feb 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
PopularRepositoriesJvmTest.kt
85 lines (71 loc) · 2.73 KB
/
PopularRepositoriesJvmTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.freeletics.rxredux
import android.arch.core.executor.testing.InstantTaskExecutorRule
import com.freeletics.di.TestApplicationModule
import com.freeletics.rxredux.businesslogic.pagination.Action
import com.freeletics.rxredux.businesslogic.pagination.PaginationStateMachine
import io.reactivex.Observable
import io.reactivex.schedulers.Schedulers
import io.reactivex.subjects.ReplaySubject
import io.reactivex.subjects.Subject
import okhttp3.mockwebserver.MockWebServer
import org.junit.Rule
import org.junit.Test
import timber.log.Timber
/**
* Example local unit test, which will execute on the development machine (host).
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
class PopularRepositoriesJvmTest {
class JvmScreen(
private val viewModel: PopularRepositoriesViewModel
) : Screen, StateRecorder {
val stateSubject: Subject<PaginationStateMachine.State> = ReplaySubject.create()
override fun scrollToEndOfList() {
Observable.just(Action.LoadNextPageAction).subscribe(viewModel.input)
}
override fun retryLoadingFirstPage() {
Observable.just(Action.LoadFirstPageAction).subscribe(viewModel.input)
}
override fun loadFirstPage() {
Observable.just(Action.LoadFirstPageAction).subscribe(viewModel.input)
}
override fun renderedStates(): Observable<PaginationStateMachine.State> = stateSubject
}
@Rule
@JvmField
val rule = InstantTaskExecutorRule()
@Test
fun runTests() {
Timber.plant(object : Timber.Tree() {
override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
println(message)
t?.printStackTrace()
}
})
val applicationComponent = DaggerTestComponent.builder().applicationModule(
TestApplicationModule(
baseUrl = "http://127.0.0.1:$MOCK_WEB_SERVER_PORT",
viewBindingInstantiatorMap = emptyMap(),
androidScheduler = Schedulers.trampoline()
)
).build()
val paginationStateMachine = applicationComponent
.paginationStateMachine()
val viewModel =
PopularRepositoriesViewModel(paginationStateMachine, Schedulers.trampoline())
val screen = JvmScreen(viewModel)
viewModel.state.observeForever {
screen.stateSubject.onNext(it!!)
}
val mockWebServer = MockWebServer()
mockWebServer.setupForHttps()
mockWebServer.use {
PopularRepositoriesSpec(
config = ScreenConfig(it),
screen = screen,
stateHistory = StateHistory(screen)
).runTests()
}
}
}