-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathInitializeDatabaseActivityKakaoTest.kt
113 lines (92 loc) · 3.35 KB
/
InitializeDatabaseActivityKakaoTest.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
package com.jaimegc.covid19tracker.activity.kakao
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.test.core.app.ActivityScenario
import androidx.test.espresso.intent.Intents
import androidx.test.filters.LargeTest
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
import arrow.core.Either
import com.agoda.kakao.screen.Screen.Companion.onScreen
import com.jaimegc.covid19tracker.ScreenStateFactoryTest
import com.jaimegc.covid19tracker.data.room.Covid19TrackerDatabase
import com.jaimegc.covid19tracker.domain.usecase.GetCovidTracker
import com.jaimegc.covid19tracker.ui.home.InitializeDatabaseActivity
import com.jaimegc.covid19tracker.util.FileUtils
import com.jaimegc.covid19tracker.util.kakao.InitializeDatabaseScreen
import io.mockk.coEvery
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkClass
import kotlinx.coroutines.flow.flow
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.koin.core.context.loadKoinModules
import org.koin.core.context.unloadKoinModules
import org.koin.core.module.Module
import org.koin.dsl.module
import org.koin.test.KoinTest
import org.koin.test.mock.MockProviderRule
import org.koin.test.mock.declareMock
@RunWith(AndroidJUnit4ClassRunner::class)
@LargeTest
class InitializeDatabaseActivityKakaoTest : KoinTest {
@get:Rule
val instantExecutorRule = InstantTaskExecutorRule()
private lateinit var scenario: ActivityScenario<InitializeDatabaseActivity>
private val fileUtils = mockk<FileUtils>()
private lateinit var mockModule: Module
@get:Rule
val mockProvider = MockProviderRule.create { clazz ->
mockkClass(clazz)
}
@Before
fun setUp() {
mockModule = module(createdAtStart = true, override = true) {
single {
Covid19TrackerDatabase.buildTest(get())
}
single {
fileUtils
}
}
mockRequests()
Intents.init()
}
@After
fun tearDown() {
Intents.release()
unloadKoinModules(mockModule)
}
private fun mockRequests() {
// Empty requests for UpdateDatabaseWorker
every { fileUtils.generateCurrentDates() } returns listOf()
declareMock<GetCovidTracker> {
every { getCovidTrackerByDate(any()) } returns flow {
emit(Either.right(ScreenStateFactoryTest.stateCovidTrackerEmptyData))
}
}
}
@Test
fun ifDatabaseExists_shouldOpenMainActivityDirectly() {
onScreen<InitializeDatabaseScreen> {
every { fileUtils.databaseExists() } returns true
loadKoinModules(mockModule)
scenario = ActivityScenario.launch(InitializeDatabaseActivity::class.java)
mainActivityIntent { intended() }
scenario.close()
}
}
@Test
fun ifDatabaseDoesNotExist_shouldOpenMainActivityAfterInitDatabase() {
onScreen<InitializeDatabaseScreen> {
every { fileUtils.databaseExists() } returns false
coEvery { fileUtils.initDatabase() } returns Unit
loadKoinModules(mockModule)
scenario = ActivityScenario.launch(InitializeDatabaseActivity::class.java)
mainActivityIntent { intended() }
scenario.close()
}
}
}