Fix unit and UI tests. Apply spotless.

Change-Id: Iab75852a27a44309d349f84e65226911ab59fcd5
pull/507/head
Don Turner 2 years ago
parent ec4bceead3
commit 5638e253ae

@ -22,7 +22,7 @@ import com.google.samples.apps.nowinandroid.core.model.data.previewTopics
/**
* A [topic] with the additional information for whether or not it is followed.
*/
data class FollowableTopic( //TODO consider changing to UserTopic and flattening
data class FollowableTopic( // TODO consider changing to UserTopic and flattening
val topic: Topic,
val isFollowed: Boolean
)

@ -21,12 +21,13 @@ import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType
import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Codelab
import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Unknown
import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Video
import com.google.samples.apps.nowinandroid.core.model.data.previewTopics
import kotlinx.datetime.Instant
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toInstant
/* ktlint-disable max-line-length */
/**
* A [NewsResource] with the additional user information.
*/

@ -70,7 +70,8 @@ class GetUserNewsResourcesUseCaseTest {
isFollowed = false
)
},
true),
true
),
UserNewsResource(
sampleNewsResources[1].id,
sampleNewsResources[1].title,
@ -85,7 +86,8 @@ class GetUserNewsResourcesUseCaseTest {
isFollowed = false
)
},
false),
false
),
UserNewsResource(
sampleNewsResources[2].id,
sampleNewsResources[2].title,
@ -97,10 +99,11 @@ class GetUserNewsResourcesUseCaseTest {
sampleNewsResources[2].topics.map { topic ->
FollowableTopic(
topic = topic,
isFollowed = true
isFollowed = false
)
},
true),
true
),
),
saveableNewsResources.first()
)
@ -120,21 +123,24 @@ class GetUserNewsResourcesUseCaseTest {
assertEquals(
sampleNewsResources
.filter { it.topics.contains(sampleTopic1) }
.map { UserNewsResource(
id = it.id,
title = it.title,
content = it.content,
url = it.url,
headerImageUrl = it.headerImageUrl,
publishDate = it.publishDate,
type = it.type,
topics = it.topics.map { topic ->
FollowableTopic(
topic = topic,
isFollowed = false
)
},
isSaved = false) },
.map {
UserNewsResource(
id = it.id,
title = it.title,
content = it.content,
url = it.url,
headerImageUrl = it.headerImageUrl,
publishDate = it.publishDate,
type = it.type,
topics = it.topics.map { topic ->
FollowableTopic(
topic = topic,
isFollowed = false
)
},
isSaved = false
)
},
saveableNewsResources.first()
)
}

@ -20,7 +20,7 @@ import androidx.activity.ComponentActivity
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithText
import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources
import com.google.samples.apps.nowinandroid.core.domain.model.previewUserNewsResources
import org.junit.Rule
import org.junit.Test
@ -30,7 +30,7 @@ class NewsResourceCardTest {
@Test
fun testMetaDataDisplay_withCodelabResource() {
val newsWithKnownResourceType = previewNewsResources[0]
val newsWithKnownResourceType = previewUserNewsResources[0]
var dateFormatted = ""
composeTestRule.setContent {
@ -57,7 +57,7 @@ class NewsResourceCardTest {
@Test
fun testMetaDataDisplay_withUnknownResource() {
val newsWithUnknownResourceType = previewNewsResources[3]
val newsWithUnknownResourceType = previewUserNewsResources[3]
var dateFormatted = ""
composeTestRule.setContent {

@ -61,7 +61,6 @@ import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource
import com.google.samples.apps.nowinandroid.core.domain.model.previewUserNewsResources
import com.google.samples.apps.nowinandroid.core.model.data.NewsResource
import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType
import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.util.Locale

@ -38,7 +38,7 @@ import com.google.samples.apps.nowinandroid.core.model.data.NewsResource
*/
fun <T> LazyListScope.newsResourceCardItems(
items: List<T>,
newsResourceMapper: (item: T) -> UserNewsResource, //TODO remove this?
newsResourceMapper: (item: T) -> UserNewsResource, // TODO remove this?
isBookmarkedMapper: (item: T) -> Boolean,
onToggleBookmark: (item: T) -> Unit,
onItemClick: ((item: T) -> Unit)? = null,

@ -31,8 +31,7 @@ import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performScrollToNode
import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource
import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources
import com.google.samples.apps.nowinandroid.core.domain.model.previewUserNewsResources
import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState
import kotlin.test.assertEquals
import kotlin.test.assertTrue
@ -70,8 +69,7 @@ class BookmarksScreenTest {
composeTestRule.setContent {
BookmarksScreen(
feedState = NewsFeedUiState.Success(
previewNewsResources.take(2)
.map { UserNewsResource(it, true) }
previewUserNewsResources.take(2)
),
removeFromBookmarks = { }
)
@ -79,7 +77,7 @@ class BookmarksScreenTest {
composeTestRule
.onNodeWithText(
previewNewsResources[0].title,
previewUserNewsResources[0].title,
substring = true
)
.assertExists()
@ -88,14 +86,14 @@ class BookmarksScreenTest {
composeTestRule.onNode(hasScrollToNodeAction())
.performScrollToNode(
hasText(
previewNewsResources[1].title,
previewUserNewsResources[1].title,
substring = true
)
)
composeTestRule
.onNodeWithText(
previewNewsResources[1].title,
previewUserNewsResources[1].title,
substring = true
)
.assertExists()
@ -109,11 +107,10 @@ class BookmarksScreenTest {
composeTestRule.setContent {
BookmarksScreen(
feedState = NewsFeedUiState.Success(
previewNewsResources.take(2)
.map { UserNewsResource(it, true) }
previewUserNewsResources.take(2)
),
removeFromBookmarks = { newsResourceId ->
assertEquals(previewNewsResources[0].id, newsResourceId)
assertEquals(previewUserNewsResources[0].id, newsResourceId)
removeFromBookmarksCalled = true
}
)
@ -127,7 +124,7 @@ class BookmarksScreenTest {
).filter(
hasAnyAncestor(
hasText(
previewNewsResources[0].title,
previewUserNewsResources[0].title,
substring = true
)
)

@ -52,9 +52,7 @@ import androidx.lifecycle.compose.ExperimentalLifecycleComposeApi
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.google.samples.apps.nowinandroid.core.designsystem.component.NiaLoadingWheel
import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme
import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource
import com.google.samples.apps.nowinandroid.core.domain.model.previewUserNewsResources
import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources
import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState
import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState.Loading
import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState.Success

@ -29,9 +29,8 @@ import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performScrollToNode
import com.google.samples.apps.nowinandroid.core.domain.model.FollowableTopic
import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource
import com.google.samples.apps.nowinandroid.core.domain.model.previewUserNewsResources
import com.google.samples.apps.nowinandroid.core.model.data.Topic
import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources
import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState
import org.junit.Rule
import org.junit.Test
@ -225,9 +224,7 @@ class ForYouScreenTest {
isSyncing = false,
onboardingUiState = OnboardingUiState.NotShown,
feedState = NewsFeedUiState.Success(
feed = previewNewsResources.map {
UserNewsResource(it, false)
}
feed = previewUserNewsResources
),
onTopicCheckedChanged = { _, _ -> },
saveFollowedTopics = {},
@ -237,7 +234,7 @@ class ForYouScreenTest {
composeTestRule
.onNodeWithText(
previewNewsResources[0].title,
previewUserNewsResources[0].title,
substring = true
)
.assertExists()
@ -246,14 +243,14 @@ class ForYouScreenTest {
composeTestRule.onNode(hasScrollToNodeAction())
.performScrollToNode(
hasText(
previewNewsResources[1].title,
previewUserNewsResources[1].title,
substring = true
)
)
composeTestRule
.onNodeWithText(
previewNewsResources[1].title,
previewUserNewsResources[1].title,
substring = true
)
.assertExists()

@ -83,9 +83,7 @@ import com.google.samples.apps.nowinandroid.core.designsystem.component.NiaOverl
import com.google.samples.apps.nowinandroid.core.designsystem.icon.NiaIcons
import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme
import com.google.samples.apps.nowinandroid.core.domain.model.FollowableTopic
import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource
import com.google.samples.apps.nowinandroid.core.domain.model.previewUserNewsResources
import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources
import com.google.samples.apps.nowinandroid.core.model.data.previewTopics
import com.google.samples.apps.nowinandroid.core.ui.DevicePreviews
import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState

@ -263,7 +263,9 @@ class ForYouViewModelTest {
val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }
topicsRepository.sendTopics(sampleTopics)
userDataRepository.setFollowedTopicIds(setOf("0", "1"))
val followedTopicIds = setOf("0", "1")
userDataRepository.setFollowedTopicIds(followedTopicIds)
viewModel.dismissOnboarding()
assertEquals(
@ -290,10 +292,10 @@ class ForYouViewModelTest {
headerImageUrl = it.headerImageUrl,
publishDate = it.publishDate,
type = it.type,
topics = it.topics.map{ topic ->
topics = it.topics.map { topic ->
FollowableTopic(
topic = topic,
isFollowed = false
isFollowed = followedTopicIds.contains(topic.id)
)
},
isSaved = false
@ -319,41 +321,9 @@ class ForYouViewModelTest {
assertEquals(
OnboardingUiState.Shown(
topics = listOf(
FollowableTopic(
topic = Topic(
id = "0",
name = "Headlines",
shortDescription = "",
longDescription = "long description",
url = "URL",
imageUrl = "image URL",
),
isFollowed = false
),
FollowableTopic(
topic = Topic(
id = "1",
name = "UI",
shortDescription = "",
longDescription = "long description",
url = "URL",
imageUrl = "image URL",
),
isFollowed = false
),
FollowableTopic(
topic = Topic(
id = "2",
name = "Tools",
shortDescription = "",
longDescription = "long description",
url = "URL",
imageUrl = "image URL",
),
isFollowed = false
)
),
topics = sampleTopics.map {
FollowableTopic(it, false)
}
),
viewModel.onboardingUiState.value
)
@ -364,45 +334,14 @@ class ForYouViewModelTest {
viewModel.feedState.value
)
viewModel.updateTopicSelection("1", isChecked = true)
val followedTopicId = sampleTopics[1].id
viewModel.updateTopicSelection(followedTopicId, isChecked = true)
assertEquals(
OnboardingUiState.Shown(
topics = listOf(
FollowableTopic(
topic = Topic(
id = "0",
name = "Headlines",
shortDescription = "",
longDescription = "long description",
url = "URL",
imageUrl = "image URL",
),
isFollowed = false
),
FollowableTopic(
topic = Topic(
id = "1",
name = "UI",
shortDescription = "",
longDescription = "long description",
url = "URL",
imageUrl = "image URL",
),
isFollowed = true
),
FollowableTopic(
topic = Topic(
id = "2",
name = "Tools",
shortDescription = "",
longDescription = "long description",
url = "URL",
imageUrl = "image URL",
),
isFollowed = false
)
),
topics = sampleTopics.map {
FollowableTopic(it, it.id == followedTopicId)
}
),
viewModel.onboardingUiState.value
)
@ -417,10 +356,10 @@ class ForYouViewModelTest {
headerImageUrl = sampleNewsResources[1].headerImageUrl,
publishDate = sampleNewsResources[1].publishDate,
type = sampleNewsResources[1].type,
topics = sampleNewsResources[1].topics.map{ topic ->
topics = sampleNewsResources[1].topics.map { topic ->
FollowableTopic(
topic = topic,
isFollowed = false
isFollowed = topic.id == followedTopicId
)
},
isSaved = false
@ -433,10 +372,10 @@ class ForYouViewModelTest {
headerImageUrl = sampleNewsResources[2].headerImageUrl,
publishDate = sampleNewsResources[2].publishDate,
type = sampleNewsResources[2].type,
topics = sampleNewsResources[2].topics.map{ topic ->
topics = sampleNewsResources[2].topics.map { topic ->
FollowableTopic(
topic = topic,
isFollowed = false
isFollowed = topic.id == followedTopicId
)
},
isSaved = false
@ -520,8 +459,10 @@ class ForYouViewModelTest {
launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() }
val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }
val followedTopicIds = setOf("1")
topicsRepository.sendTopics(sampleTopics)
userDataRepository.setFollowedTopicIds(setOf("1"))
userDataRepository.setFollowedTopicIds(followedTopicIds)
userDataRepository.setShouldHideOnboarding(true)
newsRepository.sendNewsResources(sampleNewsResources)
viewModel.updateNewsResourceSaved("2", true)
@ -541,10 +482,10 @@ class ForYouViewModelTest {
headerImageUrl = sampleNewsResources[1].headerImageUrl,
publishDate = sampleNewsResources[1].publishDate,
type = sampleNewsResources[1].type,
topics = sampleNewsResources[1].topics.map{ topic ->
topics = sampleNewsResources[1].topics.map { topic ->
FollowableTopic(
topic = topic,
isFollowed = false
isFollowed = followedTopicIds.contains(topic.id)
)
},
isSaved = true
@ -557,10 +498,10 @@ class ForYouViewModelTest {
headerImageUrl = sampleNewsResources[2].headerImageUrl,
publishDate = sampleNewsResources[2].publishDate,
type = sampleNewsResources[2].type,
topics = sampleNewsResources[2].topics.map{ topic ->
topics = sampleNewsResources[2].topics.map { topic ->
FollowableTopic(
topic = topic,
isFollowed = false
isFollowed = followedTopicIds.contains(topic.id)
)
},
isSaved = false

@ -26,7 +26,6 @@ import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performScrollToNode
import com.google.samples.apps.nowinandroid.core.domain.model.FollowableTopic
import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource
import com.google.samples.apps.nowinandroid.core.model.data.NewsResource
import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Video
import com.google.samples.apps.nowinandroid.core.model.data.Topic
import kotlinx.datetime.Instant
@ -99,14 +98,7 @@ class TopicScreenTest {
composeTestRule.setContent {
TopicScreen(
topicUiState = TopicUiState.Loading,
newsUiState = NewsUiState.Success(
sampleNewsResources.mapIndexed { index, newsResource ->
UserNewsResource(
newsResource = newsResource,
isSaved = index % 2 == 0,
)
}
),
newsUiState = NewsUiState.Success(sampleUserNewsResources),
onBackClick = { },
onFollowClick = { },
onBookmarkChanged = { _, _ -> },
@ -126,12 +118,7 @@ class TopicScreenTest {
TopicScreen(
topicUiState = TopicUiState.Success(testTopic),
newsUiState = NewsUiState.Success(
sampleNewsResources.mapIndexed { index, newsResource ->
UserNewsResource(
newsResource = newsResource,
isSaved = index % 2 == 0,
)
}
sampleUserNewsResources
),
onBackClick = { },
onFollowClick = { },
@ -143,7 +130,7 @@ class TopicScreenTest {
composeTestRule
.onAllNodes(hasScrollToNodeAction())
.onFirst()
.performScrollToNode(hasText(sampleNewsResources.first().title))
.performScrollToNode(hasText(sampleUserNewsResources.first().title))
}
}
@ -188,8 +175,8 @@ private val testTopics = listOf(
)
)
private val sampleNewsResources = listOf(
NewsResource(
private val sampleUserNewsResources = listOf(
UserNewsResource(
id = "1",
title = "Thanks for helping us reach 1M YouTube Subscribers",
content = "Thank you everyone for following the Now in Android series and everything the " +
@ -201,14 +188,18 @@ private val sampleNewsResources = listOf(
publishDate = Instant.parse("2021-11-09T00:00:00.000Z"),
type = Video,
topics = listOf(
Topic(
id = "0",
name = "Headlines",
shortDescription = "",
longDescription = TOPIC_DESC,
url = "",
imageUrl = ""
FollowableTopic(
topic = Topic(
id = "0",
name = "Headlines",
shortDescription = "",
longDescription = TOPIC_DESC,
url = "",
imageUrl = ""
),
isFollowed = false
)
)
),
isSaved = true
)
)

@ -53,9 +53,7 @@ import com.google.samples.apps.nowinandroid.core.designsystem.component.NiaLoadi
import com.google.samples.apps.nowinandroid.core.designsystem.icon.NiaIcons
import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme
import com.google.samples.apps.nowinandroid.core.domain.model.FollowableTopic
import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource
import com.google.samples.apps.nowinandroid.core.domain.model.previewUserNewsResources
import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources
import com.google.samples.apps.nowinandroid.core.model.data.previewTopics
import com.google.samples.apps.nowinandroid.core.ui.DevicePreviews
import com.google.samples.apps.nowinandroid.core.ui.TrackScrollJank

Loading…
Cancel
Save