-
Notifications
You must be signed in to change notification settings - Fork 9
/
GetRegionKotestTest.kt
156 lines (123 loc) · 5.1 KB
/
GetRegionKotestTest.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package com.jaimegc.covid19tracker.usecase.kotest
import app.cash.turbine.test
import arrow.core.Either
import com.jaimegc.covid19tracker.domain.usecase.GetRegion
import com.jaimegc.covid19tracker.ScreenStateFactoryTest.stateListRegionLoading
import com.jaimegc.covid19tracker.ScreenStateFactoryTest.stateListRegionEmptySuccess
import com.jaimegc.covid19tracker.ScreenStateFactoryTest.stateListRegionSuccess
import com.jaimegc.covid19tracker.data.repository.CovidTrackerRepository
import dev.olog.flow.test.observer.test
import io.kotest.assertions.arrow.either.shouldBeRight
import io.kotest.core.spec.style.ShouldSpec
import io.mockk.MockKAnnotations
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.flow.collectIndexed
import kotlinx.coroutines.flow.flow
class GetRegionKotestTest : ShouldSpec({
val ID_COUNTRY = "id_country"
lateinit var getRegion: GetRegion
val repository: CovidTrackerRepository = mockk()
beforeTest {
MockKAnnotations.init(this)
getRegion = GetRegion(repository)
}
/**********
* Mockk *
**********/
should("get regions by country should return loading and success") {
val flow = flow {
emit(Either.right(stateListRegionLoading))
emit(Either.right(stateListRegionSuccess))
}
every { repository.getRegionsByCountry(any()) } returns flow
val flowUseCase = getRegion.getRegionsByCountry(ID_COUNTRY)
verify { repository.getRegionsByCountry(any()) }
flowUseCase.collectIndexed { index, data ->
when (index) {
0 -> data.shouldBeRight(stateListRegionLoading)
1 -> data.shouldBeRight(stateListRegionSuccess)
}
}
}
should("get regions by country with empty data should return loading and success") {
val flow = flow {
emit(Either.right(stateListRegionLoading))
emit(Either.right(stateListRegionEmptySuccess))
}
every { repository.getRegionsByCountry(any()) } returns flow
val flowUseCase = getRegion.getRegionsByCountry(ID_COUNTRY)
verify { repository.getRegionsByCountry(any()) }
flowUseCase.collectIndexed { index, data ->
when (index) {
0 -> data.shouldBeRight(stateListRegionLoading)
1 -> data.shouldBeRight(stateListRegionEmptySuccess)
}
}
}
/**************
* Flow Test *
**************/
should("get regions by country should return loading and success using flow test") {
val flow = flow {
emit(Either.right(stateListRegionLoading))
emit(Either.right(stateListRegionSuccess))
}
every { repository.getRegionsByCountry(any()) } returns flow
val flowUseCase = getRegion.getRegionsByCountry(ID_COUNTRY)
verify { repository.getRegionsByCountry(any()) }
flowUseCase.test(this) {
valueAt(0).shouldBeRight(stateListRegionLoading)
valueAt(1).shouldBeRight(stateListRegionSuccess)
assertValueCount(2)
assertComplete()
}
}
should("get regions by country with empty data should return loading and success using flow test") {
val flow = flow {
emit(Either.right(stateListRegionLoading))
emit(Either.right(stateListRegionEmptySuccess))
}
every { repository.getRegionsByCountry(any()) } returns flow
val flowUseCase = getRegion.getRegionsByCountry(ID_COUNTRY)
verify { repository.getRegionsByCountry(any()) }
flowUseCase.test(this) {
valueAt(0).shouldBeRight(stateListRegionLoading)
valueAt(1).shouldBeRight(stateListRegionEmptySuccess)
assertValueCount(2)
assertComplete()
}
}
/************
* Turbine *
************/
should("get regions by country should return loading and success using turbine") {
val flow = flow {
emit(Either.right(stateListRegionLoading))
emit(Either.right(stateListRegionSuccess))
}
every { repository.getRegionsByCountry(any()) } returns flow
val flowUseCase = getRegion.getRegionsByCountry(ID_COUNTRY)
verify { repository.getRegionsByCountry(any()) }
flowUseCase.test {
expectItem().shouldBeRight(stateListRegionLoading)
expectItem().shouldBeRight(stateListRegionSuccess)
expectComplete()
}
}
should("get regions by country with empty data should return loading and success using turbine") {
val flow = flow {
emit(Either.right(stateListRegionLoading))
emit(Either.right(stateListRegionEmptySuccess))
}
every { repository.getRegionsByCountry(any()) } returns flow
val flowUseCase = getRegion.getRegionsByCountry(ID_COUNTRY)
verify { repository.getRegionsByCountry(any()) }
flowUseCase.test {
expectItem().shouldBeRight(stateListRegionLoading)
expectItem().shouldBeRight(stateListRegionEmptySuccess)
expectComplete()
}
}
})