Skip to content

Commit 5950566

Browse files
committed
TEST: implement unittests for RemindersListViewModel
1 parent 14407bc commit 5950566

File tree

1 file changed

+111
-2
lines changed

1 file changed

+111
-2
lines changed
Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,122 @@
11
package com.udacity.locationreminder.locationreminders.reminderslist
22

3+
import android.app.Application
4+
import android.content.Context
5+
import android.os.Build
6+
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
7+
import androidx.test.core.app.ApplicationProvider
38
import androidx.test.ext.junit.runners.AndroidJUnit4
9+
import com.udacity.locationreminder.locationreminders.MainCoroutineRule
10+
import com.udacity.locationreminder.locationreminders.data.FakeDataSource
11+
import com.udacity.locationreminder.locationreminders.data.dto.ReminderDTO
12+
import com.udacity.locationreminder.locationreminders.getOrAwaitValue
413
import kotlinx.coroutines.ExperimentalCoroutinesApi
14+
import kotlinx.coroutines.runBlocking
15+
import kotlinx.coroutines.test.runBlockingTest
16+
import org.hamcrest.MatcherAssert.assertThat
17+
import org.hamcrest.core.Is.`is`
18+
import org.junit.After
19+
import org.junit.Before
20+
import org.junit.Rule
21+
import org.junit.Test
522
import org.junit.runner.RunWith
23+
import org.koin.core.context.stopKoin
24+
import org.robolectric.annotation.Config
625

726
@RunWith(AndroidJUnit4::class)
27+
@Config(sdk = [Build.VERSION_CODES.P])
828
@ExperimentalCoroutinesApi
929
class RemindersListViewModelTest {
1030

11-
//TODO: provide testing to the RemindersListViewModel and its live data objects
31+
@get:Rule
32+
var mainCoroutineRule = MainCoroutineRule()
1233

13-
}
34+
private lateinit var repository: FakeDataSource
35+
private lateinit var appContext: Context
36+
37+
// Subject under test
38+
private lateinit var viewModel: RemindersListViewModel
39+
40+
@get:Rule
41+
var instantExecutorRule = InstantTaskExecutorRule()
42+
43+
@Before
44+
fun setupViewModel() {
45+
val reminders = mutableListOf(
46+
ReminderDTO(
47+
title = "Title of reminder 1",
48+
description = "Description of reminder 1",
49+
location = "User defined location",
50+
latitude = 10.0,
51+
longitude = 10.0
52+
),
53+
ReminderDTO(
54+
title = "Title of reminder 2",
55+
description = "Description of reminder 2",
56+
location = "User defined location",
57+
latitude = 20.0,
58+
longitude = 20.0
59+
),
60+
)
61+
62+
repository = FakeDataSource(reminders = reminders)
63+
appContext = ApplicationProvider.getApplicationContext()
64+
viewModel = RemindersListViewModel(appContext as Application, repository)
65+
}
66+
67+
@After
68+
fun tearDownViewModel() {
69+
// ensure clean data for tests
70+
runBlocking {
71+
repository.deleteAllReminders()
72+
}
73+
74+
// Koin instance must be stopped between tests
75+
stopKoin()
76+
}
77+
78+
@Test
79+
fun showNoData_noReminders_isTrue() = runBlockingTest {
80+
repository.deleteAllReminders()
81+
viewModel.loadReminders()
82+
assertThat(viewModel.showNoData.getOrAwaitValue(), `is`(true))
83+
}
84+
85+
@Test
86+
fun showNoData_withData_isFalse() = runBlockingTest {
87+
viewModel.loadReminders()
88+
assertThat(viewModel.showNoData.getOrAwaitValue(), `is`(false))
89+
}
90+
91+
@Test
92+
fun showLoading_noData_isTrueWhileLoading() = runBlockingTest {
93+
repository.deleteAllReminders()
94+
mainCoroutineRule.pauseDispatcher()
95+
viewModel.loadReminders()
96+
assertThat(viewModel.showLoading.getOrAwaitValue(), `is`(true))
97+
mainCoroutineRule.resumeDispatcher()
98+
assertThat(viewModel.showLoading.getOrAwaitValue(), `is`(false))
99+
}
100+
101+
@Test
102+
fun showLoading_withData_isTrueWhileLoading() = runBlockingTest {
103+
mainCoroutineRule.pauseDispatcher()
104+
viewModel.loadReminders()
105+
assertThat(viewModel.showLoading.getOrAwaitValue(), `is`(true))
106+
mainCoroutineRule.resumeDispatcher()
107+
assertThat(viewModel.showLoading.getOrAwaitValue(), `is`(false))
108+
}
109+
110+
@Test
111+
fun remindersList_noData_isEmpty() = runBlockingTest {
112+
repository.deleteAllReminders()
113+
viewModel.loadReminders()
114+
assertThat(viewModel.remindersList.getOrAwaitValue().isEmpty(), `is`(true))
115+
}
116+
117+
@Test
118+
fun remindersList_withData_isNotEmpty() = runBlockingTest {
119+
viewModel.loadReminders()
120+
assertThat(viewModel.remindersList.getOrAwaitValue().isEmpty(), `is`(false))
121+
}
122+
}

0 commit comments

Comments
 (0)