Skip to content

Commit 24edf78

Browse files
committed
Created instrumented tests for the FavoriteScreen composables.
1 parent 48a7568 commit 24edf78

File tree

4 files changed

+101
-7
lines changed

4 files changed

+101
-7
lines changed

app/src/androidTest/java/nl/nickkoepr/bored/dummy/repositories/DummyRepositories.kt

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package nl.nickkoepr.bored.dummy.repositories
22

33
import kotlinx.coroutines.flow.Flow
4-
import kotlinx.coroutines.flow.flowOf
4+
import kotlinx.coroutines.flow.MutableStateFlow
5+
import kotlinx.coroutines.flow.map
6+
import kotlinx.coroutines.flow.update
57
import nl.nickkoepr.bored.data.database.repository.DatabaseRepository
68
import nl.nickkoepr.bored.data.network.repository.NetworkRepository
79
import nl.nickkoepr.bored.dummy.DummyActivity
@@ -21,20 +23,27 @@ class DummyNetworkRepository : NetworkRepository {
2123
}
2224

2325
class DummyDatabaseRepository : DatabaseRepository {
24-
private val dummyDataList = mutableListOf<Activity>()
26+
private val dummyDataListState = MutableStateFlow<List<Activity>>(emptyList())
27+
2528
override suspend fun addFavorite(activity: Activity) {
26-
dummyDataList.add(activity)
29+
dummyDataListState.update { state ->
30+
state + activity
31+
}
2732
}
2833

2934
override suspend fun removeFavorite(activity: Activity) {
30-
dummyDataList.remove(activity)
35+
dummyDataListState.update { state ->
36+
state.filterNot { it == activity }
37+
}
3138
}
3239

3340
override fun getFavorites(): Flow<List<Activity>> {
34-
return flowOf(dummyDataList)
41+
return dummyDataListState
3542
}
3643

3744
override fun getActivityByKey(key: String): Flow<Activity?> {
38-
return flowOf(dummyDataList.find { it.key == key })
45+
return dummyDataListState.map { activityList ->
46+
activityList.find { activity -> activity.key == key }
47+
}
3948
}
4049
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package nl.nickkoepr.bored.favoritesScreen
2+
3+
/**
4+
* Tests created for testing the FavoriteScreen composables.
5+
*/
6+
import android.content.Context
7+
import androidx.compose.ui.test.assertIsDisplayed
8+
import androidx.compose.ui.test.junit4.createComposeRule
9+
import androidx.compose.ui.test.onNodeWithTag
10+
import androidx.compose.ui.test.onNodeWithText
11+
import androidx.compose.ui.test.performClick
12+
import androidx.test.core.app.ApplicationProvider
13+
import kotlinx.coroutines.ExperimentalCoroutinesApi
14+
import kotlinx.coroutines.test.runTest
15+
import nl.nickkoepr.bored.data.database.repository.DatabaseRepository
16+
import nl.nickkoepr.bored.dummy.DummyActivity
17+
import nl.nickkoepr.bored.dummy.repositories.DummyDatabaseRepository
18+
import nl.nickkoepr.bored.ui.screens.favorites.FavoritesScreen
19+
import nl.nickkoepr.bored.ui.screens.favorites.FavoritesViewModel
20+
import nl.nickkoepr.bored.utils.toPercent
21+
import org.junit.Before
22+
import org.junit.Rule
23+
import org.junit.Test
24+
import nl.nickkoepr.bored.R
25+
26+
class FavoriteScreenTest {
27+
@get:Rule
28+
val composeTestRule = createComposeRule()
29+
private lateinit var dummyDatabase: DatabaseRepository
30+
31+
@Before
32+
fun initScreen() {
33+
dummyDatabase = DummyDatabaseRepository()
34+
composeTestRule.setContent {
35+
FavoritesScreen({}, viewModel = FavoritesViewModel(dummyDatabase))
36+
}
37+
}
38+
39+
@OptIn(ExperimentalCoroutinesApi::class)
40+
@Test
41+
fun favoriteScreen_favoriteCard_isCardDisplayedWithInformation() = runTest {
42+
val activity = DummyActivity.activity1
43+
val type = ApplicationProvider.getApplicationContext<Context>()
44+
.getString(activity.type.label)
45+
dummyDatabase.addFavorite(activity)
46+
47+
// The [icon] at the end of the activity is a placeholder for the favorite star icon.
48+
composeTestRule.onNodeWithText("${activity.activity}[icon]").assertIsDisplayed()
49+
50+
composeTestRule.onNodeWithText(activity.price.toPercent()).assertIsDisplayed()
51+
composeTestRule.onNodeWithText(type).assertIsDisplayed()
52+
composeTestRule.onNodeWithText(activity.accessibility.toPercent()).assertIsDisplayed()
53+
composeTestRule.onNodeWithText(activity.participants.toString()).assertIsDisplayed()
54+
}
55+
56+
@OptIn(ExperimentalCoroutinesApi::class)
57+
@Test
58+
fun favoriteScreen_favoriteCard_expandedCardInformation() = runTest {
59+
val context = ApplicationProvider.getApplicationContext<Context>()
60+
val activity = DummyActivity.activity1
61+
dummyDatabase.addFavorite(activity)
62+
63+
composeTestRule.onNodeWithText("${activity.activity}[icon]").performClick()
64+
// Those four strings are displayed when the activity favorite card is expanded. Otherwise
65+
// only the stats and icons are displayed.
66+
composeTestRule.onNodeWithText(context.getString(R.string.accessibility)).assertIsDisplayed()
67+
composeTestRule.onNodeWithText(context.getString(R.string.participants)).assertIsDisplayed()
68+
composeTestRule.onNodeWithText(context.getString(R.string.price)).assertIsDisplayed()
69+
composeTestRule.onNodeWithText(context.getString(R.string.type)).assertIsDisplayed()
70+
}
71+
72+
@OptIn(ExperimentalCoroutinesApi::class)
73+
@Test
74+
fun favoriteScreen_favoriteCard_removeCardWhenPressingOnStar() = runTest {
75+
val context = ApplicationProvider.getApplicationContext<Context>()
76+
val activity = DummyActivity.activity1
77+
dummyDatabase.addFavorite(activity)
78+
79+
composeTestRule.onNodeWithTag("favoriteStarIconButton").performClick()
80+
// Check if the No favorites text is displayed on the screen.
81+
// When this text is displayed, then there are no more activities on screen.
82+
composeTestRule.onNodeWithText(context.getString(R.string.no_favorites)).assertIsDisplayed()
83+
}
84+
}

app/src/main/java/nl/nickkoepr/bored/ui/screens/favorites/FavoritesScreen.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ fun NoFavoriteMessage(modifier: Modifier = Modifier) {
251251
tint = MaterialTheme.colorScheme.primary
252252
)
253253
Text(
254-
text = "No favorites",
254+
text = stringResource(id = R.string.no_favorites),
255255
style = MaterialTheme.typography.displaySmall,
256256
fontSize = 30.sp,
257257
color = MaterialTheme.colorScheme.onSurfaceVariant

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<string name="about">about</string>
88
<string name="close">close</string>
99
<string name="no_activity_found">no activity found</string>
10+
<string name="no_favorites">No favorites</string>
1011
<string name="change_filters">change your filters to find activities again!</string>
1112

1213
<string name="home">Home</string>

0 commit comments

Comments
 (0)