From e2c1bbe75486cd54cfe3001a986375d839f16156 Mon Sep 17 00:00:00 2001 From: Adetunji Dahunsi Date: Mon, 2 May 2022 17:00:01 -0400 Subject: [PATCH] Fix TopicViewModel id Change-Id: I5c58b58566f78abffb6621d5753986fe7d11e4f6 --- .../feature/topic/TopicViewModel.kt | 4 +++- .../feature/topic/TopicViewModelTest.kt | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/feature-topic/src/main/java/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModel.kt b/feature-topic/src/main/java/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModel.kt index bf7806a39..f96b8d765 100644 --- a/feature-topic/src/main/java/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModel.kt +++ b/feature-topic/src/main/java/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModel.kt @@ -53,7 +53,9 @@ class TopicViewModel @Inject constructor( // Observe the News for this topic private val newsStream: Flow>> = - newsRepository.getNewsResourcesStream(setOf(topicId), emptySet()).asResult() + newsRepository.getNewsResourcesStream( + filterTopicIds = setOf(element = topicId), + ).asResult() val uiState: StateFlow = combine( diff --git a/feature-topic/src/test/java/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModelTest.kt b/feature-topic/src/test/java/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModelTest.kt index bec7ed35c..891fbea0f 100644 --- a/feature-topic/src/test/java/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModelTest.kt +++ b/feature-topic/src/test/java/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModelTest.kt @@ -26,6 +26,7 @@ import com.google.samples.apps.nowinandroid.core.testing.repository.TestNewsRepo import com.google.samples.apps.nowinandroid.core.testing.repository.TestTopicsRepository import com.google.samples.apps.nowinandroid.core.testing.util.TestDispatcherRule import com.google.samples.apps.nowinandroid.feature.topic.TopicDestinationsArgs.TOPIC_ID_ARG +import kotlinx.coroutines.flow.first import kotlinx.coroutines.test.runTest import kotlinx.datetime.Instant import org.junit.Assert.assertEquals @@ -52,6 +53,25 @@ class TopicViewModelTest { ) } + @Test + fun uiStateAuthor_whenSuccess_matchesTopicFromRepository() = runTest { + viewModel.uiState.test { + awaitItem() + topicsRepository.sendTopics(testInputTopics.map(FollowableTopic::topic)) + topicsRepository.setFollowedTopicIds(setOf(testInputTopics[1].topic.id)) + val item = awaitItem() + assertTrue(item.topicState is TopicUiState.Success) + + val successTopicState = item.topicState as TopicUiState.Success + val topicFromRepository = topicsRepository.getTopic( + testInputTopics[0].topic.id + ).first() + + assertEquals(topicFromRepository, successTopicState.followableTopic.topic) + cancel() + } + } + @Test fun uiStateNews_whenInitialized_thenShowLoading() = runTest { viewModel.uiState.test {