File tree Expand file tree Collapse file tree 4 files changed +25
-19
lines changed
core/src/main/java/xyz/kewiany/contactus/core
main/java/xyz/kewiany/contactus/logic
test/java/xyz/kewiany/contactus Expand file tree Collapse file tree 4 files changed +25
-19
lines changed Original file line number Diff line number Diff line change @@ -5,12 +5,10 @@ import kotlinx.coroutines.Dispatchers
5
5
6
6
7
7
interface DispatcherProvider {
8
-
9
8
fun main (): CoroutineDispatcher = Dispatchers .Main
10
9
fun default (): CoroutineDispatcher = Dispatchers .Default
11
10
fun io (): CoroutineDispatcher = Dispatchers .IO
12
11
fun unconfined (): CoroutineDispatcher = Dispatchers .Unconfined
13
-
14
12
}
15
13
16
- class DefaultDispatcherProvider : DispatcherProvider
14
+ class DefaultDispatcherProvider : DispatcherProvider
Original file line number Diff line number Diff line change 1
1
package xyz.kewiany.contactus.logic
2
2
3
- import kotlinx.coroutines.delay
4
- import kotlinx.coroutines.withContext
3
+ import kotlinx.coroutines.*
5
4
import xyz.kewiany.contactus.core.DefaultDispatcherProvider
6
5
import xyz.kewiany.contactus.core.DispatcherProvider
7
6
import xyz.kewiany.contactus.logic.common.MainViewState
7
+ import kotlin.coroutines.CoroutineContext
8
8
9
- suspend fun MainViewState.MainLogic (dispatchers : DispatcherProvider = DefaultDispatcherProvider ()): Boolean {
9
+ suspend fun MainViewState.MainLogic (
10
+ dispatchers : DispatcherProvider = DefaultDispatcherProvider ()
11
+ ): Boolean {
10
12
commonViewState.isLoading = true
11
- withContext(dispatchers.io()) {
12
- delay(5000 )
13
- commonViewState.isLoading = false
14
- }
13
+ withContext(dispatchers.io()) {
14
+ delay(1000 )
15
+ commonViewState.isLoading = false
16
+ }
17
+
15
18
return true
16
19
}
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ import kotlinx.coroutines.CoroutineDispatcher
8
8
import kotlinx.coroutines.Dispatchers
9
9
import kotlinx.coroutines.ExperimentalCoroutinesApi
10
10
import kotlinx.coroutines.test.TestCoroutineDispatcher
11
+ import kotlinx.coroutines.test.TestCoroutineScope
11
12
import kotlinx.coroutines.test.resetMain
12
13
import kotlinx.coroutines.test.setMain
13
14
import org.junit.Rule
@@ -18,6 +19,7 @@ abstract class CustomFreeSpec constructor(
18
19
) : FreeSpec(body as FreeSpec .() -> Unit ) {
19
20
20
21
val testDispatcher = TestCoroutineDispatcher ()
22
+ val testScope = TestCoroutineScope (testDispatcher)
21
23
val testDispatcherProvider = object : DispatcherProvider {
22
24
override fun default (): CoroutineDispatcher = testDispatcher
23
25
override fun io (): CoroutineDispatcher = testDispatcher
@@ -35,4 +37,4 @@ abstract class CustomFreeSpec constructor(
35
37
Dispatchers .resetMain()
36
38
testDispatcher.cleanupTestCoroutines()
37
39
}
38
- }
40
+ }
Original file line number Diff line number Diff line change 1
1
package xyz.kewiany.contactus
2
2
3
3
import io.kotest.matchers.shouldBe
4
- import kotlinx.coroutines.test.TestCoroutineScope
5
- import kotlinx.coroutines.test.runBlockingTest
4
+ import kotlinx.coroutines.launch
6
5
import xyz.kewiany.contactus.logic.MainLogic
7
6
import xyz.kewiany.contactus.logic.common.MainViewState
8
7
9
8
internal class MainLogicTest : CustomFreeSpec ({
10
9
val state = MainViewState ()
11
10
val stateT = MainStateT (state)
12
11
13
- beforeTest {
14
- testDispatcher.runBlockingTest {
12
+ " test loading " - {
13
+ testScope.launch {
15
14
state.MainLogic (testDispatcherProvider)
16
15
}
16
+ " load" {
17
+ stateT.state.commonViewState.isLoading shouldBe true
18
+ }
19
+ testScope.advanceTimeBy(1000)
20
+ " do not load" {
21
+ stateT.state.commonViewState.isLoading shouldBe false
22
+ }
17
23
}
24
+ })
18
25
19
- " test" - {
20
- stateT.state.commonViewState.isLoading shouldBe false
21
- }
22
- })
You can’t perform that action at this time.
0 commit comments