change collectJob to backgroundScope coroutine

pull/1585/head
saeedishayan76 4 months ago
parent 5a382e3a45
commit 0356b873e9

@ -64,7 +64,7 @@ class BookmarksViewModelTest {
@Test @Test
fun oneBookmark_showsInFeed() = runTest { fun oneBookmark_showsInFeed() = runTest {
val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.feedUiState.collect() } backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.feedUiState.collect() }
newsRepository.sendNewsResources(newsResourcesTestData) newsRepository.sendNewsResources(newsResourcesTestData)
userDataRepository.setNewsResourceBookmarked(newsResourcesTestData[0].id, true) userDataRepository.setNewsResourceBookmarked(newsResourcesTestData[0].id, true)
@ -72,12 +72,11 @@ class BookmarksViewModelTest {
assertIs<Success>(item) assertIs<Success>(item)
assertEquals(item.feed.size, 1) assertEquals(item.feed.size, 1)
collectJob.cancel()
} }
@Test @Test
fun oneBookmark_whenRemoving_removesFromFeed() = runTest { fun oneBookmark_whenRemoving_removesFromFeed() = runTest {
val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.feedUiState.collect() } backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.feedUiState.collect() }
// Set the news resources to be used by this test // Set the news resources to be used by this test
newsRepository.sendNewsResources(newsResourcesTestData) newsRepository.sendNewsResources(newsResourcesTestData)
// Start with the resource saved // Start with the resource saved
@ -89,6 +88,5 @@ class BookmarksViewModelTest {
assertIs<Success>(item) assertIs<Success>(item)
assertEquals(item.feed.size, 0) assertEquals(item.feed.size, 0)
collectJob.cancel()
} }
} }

@ -98,9 +98,8 @@ class ForYouViewModelTest {
@Test @Test
fun stateIsLoadingWhenFollowedTopicsAreLoading() = runTest { fun stateIsLoadingWhenFollowedTopicsAreLoading() = runTest {
val collectJob1 = backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() }
launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() } backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }
val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }
topicsRepository.sendTopics(sampleTopics) topicsRepository.sendTopics(sampleTopics)
@ -110,30 +109,25 @@ class ForYouViewModelTest {
) )
assertEquals(NewsFeedUiState.Loading, viewModel.feedState.value) assertEquals(NewsFeedUiState.Loading, viewModel.feedState.value)
collectJob1.cancel()
collectJob2.cancel()
} }
@Test @Test
fun stateIsLoadingWhenAppIsSyncingWithNoInterests() = runTest { fun stateIsLoadingWhenAppIsSyncingWithNoInterests() = runTest {
syncManager.setSyncing(true) syncManager.setSyncing(true)
val collectJob = backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.isSyncing.collect() }
launch(UnconfinedTestDispatcher()) { viewModel.isSyncing.collect() }
assertEquals( assertEquals(
true, true,
viewModel.isSyncing.value, viewModel.isSyncing.value,
) )
collectJob.cancel()
} }
@Test @Test
fun onboardingStateIsLoadingWhenTopicsAreLoading() = runTest { fun onboardingStateIsLoadingWhenTopicsAreLoading() = runTest {
val collectJob1 = backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() }
launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() } backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }
val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }
userDataRepository.setFollowedTopicIds(emptySet()) userDataRepository.setFollowedTopicIds(emptySet())
@ -142,16 +136,13 @@ class ForYouViewModelTest {
viewModel.onboardingUiState.value, viewModel.onboardingUiState.value,
) )
assertEquals(NewsFeedUiState.Success(emptyList()), viewModel.feedState.value) assertEquals(NewsFeedUiState.Success(emptyList()), viewModel.feedState.value)
collectJob1.cancel()
collectJob2.cancel()
} }
@Test @Test
fun onboardingIsShownWhenNewsResourcesAreLoading() = runTest { fun onboardingIsShownWhenNewsResourcesAreLoading() = runTest {
val collectJob1 =
launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() } backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() }
val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() } backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }
topicsRepository.sendTopics(sampleTopics) topicsRepository.sendTopics(sampleTopics)
userDataRepository.setFollowedTopicIds(emptySet()) userDataRepository.setFollowedTopicIds(emptySet())
@ -202,16 +193,12 @@ class ForYouViewModelTest {
), ),
viewModel.feedState.value, viewModel.feedState.value,
) )
collectJob1.cancel()
collectJob2.cancel()
} }
@Test @Test
fun onboardingIsShownAfterLoadingEmptyFollowedTopics() = runTest { fun onboardingIsShownAfterLoadingEmptyFollowedTopics() = runTest {
val collectJob1 = backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() }
launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() } backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }
val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }
topicsRepository.sendTopics(sampleTopics) topicsRepository.sendTopics(sampleTopics)
userDataRepository.setFollowedTopicIds(emptySet()) userDataRepository.setFollowedTopicIds(emptySet())
@ -264,15 +251,12 @@ class ForYouViewModelTest {
viewModel.feedState.value, viewModel.feedState.value,
) )
collectJob1.cancel()
collectJob2.cancel()
} }
@Test @Test
fun onboardingIsNotShownAfterUserDismissesOnboarding() = runTest { fun onboardingIsNotShownAfterUserDismissesOnboarding() = runTest {
val collectJob1 = backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() }
launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() } backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }
val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }
topicsRepository.sendTopics(sampleTopics) topicsRepository.sendTopics(sampleTopics)
@ -300,15 +284,13 @@ class ForYouViewModelTest {
viewModel.feedState.value, viewModel.feedState.value,
) )
collectJob1.cancel()
collectJob2.cancel()
} }
@Test @Test
fun topicSelectionUpdatesAfterSelectingTopic() = runTest { fun topicSelectionUpdatesAfterSelectingTopic() = runTest {
val collectJob1 = backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() }
launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() } backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }
val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }
topicsRepository.sendTopics(sampleTopics) topicsRepository.sendTopics(sampleTopics)
userDataRepository.setFollowedTopicIds(emptySet()) userDataRepository.setFollowedTopicIds(emptySet())
@ -352,16 +334,12 @@ class ForYouViewModelTest {
), ),
viewModel.feedState.value, viewModel.feedState.value,
) )
collectJob1.cancel()
collectJob2.cancel()
} }
@Test @Test
fun topicSelectionUpdatesAfterUnselectingTopic() = runTest { fun topicSelectionUpdatesAfterUnselectingTopic() = runTest {
val collectJob1 = backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() }
launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() } backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }
val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }
topicsRepository.sendTopics(sampleTopics) topicsRepository.sendTopics(sampleTopics)
userDataRepository.setFollowedTopicIds(emptySet()) userDataRepository.setFollowedTopicIds(emptySet())
@ -416,16 +394,12 @@ class ForYouViewModelTest {
), ),
viewModel.feedState.value, viewModel.feedState.value,
) )
collectJob1.cancel()
collectJob2.cancel()
} }
@Test @Test
fun newsResourceSelectionUpdatesAfterLoadingFollowedTopics() = runTest { fun newsResourceSelectionUpdatesAfterLoadingFollowedTopics() = runTest {
val collectJob1 = backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() }
launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() } backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }
val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }
val followedTopicIds = setOf("1") val followedTopicIds = setOf("1")
val userData = emptyUserData.copy( val userData = emptyUserData.copy(
@ -461,14 +435,11 @@ class ForYouViewModelTest {
viewModel.feedState.value, viewModel.feedState.value,
) )
collectJob1.cancel()
collectJob2.cancel()
} }
@Test @Test
fun deepLinkedNewsResourceIsFetchedAndResetAfterViewing() = runTest { fun deepLinkedNewsResourceIsFetchedAndResetAfterViewing() = runTest {
val collectJob = backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.deepLinkedNewsResource.collect() }
launch(UnconfinedTestDispatcher()) { viewModel.deepLinkedNewsResource.collect() }
newsRepository.sendNewsResources(sampleNewsResources) newsRepository.sendNewsResources(sampleNewsResources)
userDataRepository.setUserData(emptyUserData) userDataRepository.setUserData(emptyUserData)
@ -504,7 +475,6 @@ class ForYouViewModelTest {
), ),
) )
collectJob.cancel()
} }
@Test @Test

@ -80,17 +80,15 @@ class InterestsViewModelTest {
@Test @Test
fun uiState_whenFollowedTopicsAreLoading_thenShowLoading() = runTest { fun uiState_whenFollowedTopicsAreLoading_thenShowLoading() = runTest {
val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() } backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }
userDataRepository.setFollowedTopicIds(emptySet()) userDataRepository.setFollowedTopicIds(emptySet())
assertEquals(InterestsUiState.Loading, viewModel.uiState.value) assertEquals(InterestsUiState.Loading, viewModel.uiState.value)
collectJob.cancel()
} }
@Test @Test
fun uiState_whenFollowingNewTopic_thenShowUpdatedTopics() = runTest { fun uiState_whenFollowingNewTopic_thenShowUpdatedTopics() = runTest {
val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() } backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }
val toggleTopicId = testOutputTopics[1].topic.id val toggleTopicId = testOutputTopics[1].topic.id
topicsRepository.sendTopics(testInputTopics.map { it.topic }) topicsRepository.sendTopics(testInputTopics.map { it.topic })
@ -115,12 +113,11 @@ class InterestsViewModelTest {
viewModel.uiState.value, viewModel.uiState.value,
) )
collectJob.cancel()
} }
@Test @Test
fun uiState_whenUnfollowingTopics_thenShowUpdatedTopics() = runTest { fun uiState_whenUnfollowingTopics_thenShowUpdatedTopics() = runTest {
val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() } backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }
val toggleTopicId = testOutputTopics[1].topic.id val toggleTopicId = testOutputTopics[1].topic.id
@ -147,8 +144,6 @@ class InterestsViewModelTest {
), ),
viewModel.uiState.value, viewModel.uiState.value,
) )
collectJob.cancel()
} }
} }

@ -85,20 +85,16 @@ class SearchViewModelTest {
fun stateIsEmptyQuery_withEmptySearchQuery() = runTest { fun stateIsEmptyQuery_withEmptySearchQuery() = runTest {
searchContentsRepository.addNewsResources(newsResourcesTestData) searchContentsRepository.addNewsResources(newsResourcesTestData)
searchContentsRepository.addTopics(topicsTestData) searchContentsRepository.addTopics(topicsTestData)
val collectJob = backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.searchResultUiState.collect() }
launch(UnconfinedTestDispatcher()) { viewModel.searchResultUiState.collect() }
viewModel.onSearchQueryChanged("") viewModel.onSearchQueryChanged("")
assertEquals(EmptyQuery, viewModel.searchResultUiState.value) assertEquals(EmptyQuery, viewModel.searchResultUiState.value)
collectJob.cancel()
} }
@Test @Test
fun emptyResultIsReturned_withNotMatchingQuery() = runTest { fun emptyResultIsReturned_withNotMatchingQuery() = runTest {
val collectJob = backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.searchResultUiState.collect() }
launch(UnconfinedTestDispatcher()) { viewModel.searchResultUiState.collect() }
viewModel.onSearchQueryChanged("XXX") viewModel.onSearchQueryChanged("XXX")
searchContentsRepository.addNewsResources(newsResourcesTestData) searchContentsRepository.addNewsResources(newsResourcesTestData)
@ -106,32 +102,27 @@ class SearchViewModelTest {
val result = viewModel.searchResultUiState.value val result = viewModel.searchResultUiState.value
assertIs<SearchResultUiState.Success>(result) assertIs<SearchResultUiState.Success>(result)
collectJob.cancel()
} }
@Test @Test
fun recentSearches_verifyUiStateIsSuccess() = runTest { fun recentSearches_verifyUiStateIsSuccess() = runTest {
val collectJob =
launch(UnconfinedTestDispatcher()) { viewModel.recentSearchQueriesUiState.collect() } backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.recentSearchQueriesUiState.collect() }
viewModel.onSearchTriggered("kotlin") viewModel.onSearchTriggered("kotlin")
val result = viewModel.recentSearchQueriesUiState.value val result = viewModel.recentSearchQueriesUiState.value
assertIs<Success>(result) assertIs<Success>(result)
collectJob.cancel()
} }
@Test @Test
fun searchNotReady_withNoFtsTableEntity() = runTest { fun searchNotReady_withNoFtsTableEntity() = runTest {
val collectJob = backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.searchResultUiState.collect() }
launch(UnconfinedTestDispatcher()) { viewModel.searchResultUiState.collect() }
viewModel.onSearchQueryChanged("") viewModel.onSearchQueryChanged("")
assertEquals(SearchNotReady, viewModel.searchResultUiState.value) assertEquals(SearchNotReady, viewModel.searchResultUiState.value)
collectJob.cancel()
} }
@Test @Test

@ -52,8 +52,7 @@ class SettingsViewModelTest {
@Test @Test
fun stateIsSuccessAfterUserDataLoaded() = runTest { fun stateIsSuccessAfterUserDataLoaded() = runTest {
val collectJob = backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.settingsUiState.collect() }
launch(UnconfinedTestDispatcher()) { viewModel.settingsUiState.collect() }
userDataRepository.setThemeBrand(ANDROID) userDataRepository.setThemeBrand(ANDROID)
userDataRepository.setDarkThemeConfig(DARK) userDataRepository.setDarkThemeConfig(DARK)
@ -69,6 +68,5 @@ class SettingsViewModelTest {
viewModel.settingsUiState.value, viewModel.settingsUiState.value,
) )
collectJob.cancel()
} }
} }

@ -85,7 +85,7 @@ class TopicViewModelTest {
@Test @Test
fun uiStateTopic_whenSuccess_matchesTopicFromRepository() = runTest { fun uiStateTopic_whenSuccess_matchesTopicFromRepository() = runTest {
val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.topicUiState.collect() } backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.topicUiState.collect() }
topicsRepository.sendTopics(testInputTopics.map(FollowableTopic::topic)) topicsRepository.sendTopics(testInputTopics.map(FollowableTopic::topic))
userDataRepository.setFollowedTopicIds(setOf(testInputTopics[1].topic.id)) userDataRepository.setFollowedTopicIds(setOf(testInputTopics[1].topic.id))
@ -98,7 +98,6 @@ class TopicViewModelTest {
assertEquals(topicFromRepository, item.followableTopic.topic) assertEquals(topicFromRepository, item.followableTopic.topic)
collectJob.cancel()
} }
@Test @Test
@ -113,18 +112,17 @@ class TopicViewModelTest {
@Test @Test
fun uiStateTopic_whenFollowedIdsSuccessAndTopicLoading_thenShowLoading() = runTest { fun uiStateTopic_whenFollowedIdsSuccessAndTopicLoading_thenShowLoading() = runTest {
val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.topicUiState.collect() } backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.topicUiState.collect() }
userDataRepository.setFollowedTopicIds(setOf(testInputTopics[1].topic.id)) userDataRepository.setFollowedTopicIds(setOf(testInputTopics[1].topic.id))
assertEquals(TopicUiState.Loading, viewModel.topicUiState.value) assertEquals(TopicUiState.Loading, viewModel.topicUiState.value)
collectJob.cancel()
} }
@Test @Test
fun uiStateTopic_whenFollowedIdsSuccessAndTopicSuccess_thenTopicSuccessAndNewsLoading() = fun uiStateTopic_whenFollowedIdsSuccessAndTopicSuccess_thenTopicSuccessAndNewsLoading() =
runTest { runTest {
val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.topicUiState.collect() } backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.topicUiState.collect() }
topicsRepository.sendTopics(testInputTopics.map { it.topic }) topicsRepository.sendTopics(testInputTopics.map { it.topic })
userDataRepository.setFollowedTopicIds(setOf(testInputTopics[1].topic.id)) userDataRepository.setFollowedTopicIds(setOf(testInputTopics[1].topic.id))
@ -134,13 +132,12 @@ class TopicViewModelTest {
assertIs<TopicUiState.Success>(topicUiState) assertIs<TopicUiState.Success>(topicUiState)
assertIs<NewsUiState.Loading>(newsUiState) assertIs<NewsUiState.Loading>(newsUiState)
collectJob.cancel()
} }
@Test @Test
fun uiStateTopic_whenFollowedIdsSuccessAndTopicSuccessAndNewsIsSuccess_thenAllSuccess() = fun uiStateTopic_whenFollowedIdsSuccessAndTopicSuccessAndNewsIsSuccess_thenAllSuccess() =
runTest { runTest {
val collectJob = launch(UnconfinedTestDispatcher()) { backgroundScope.launch(UnconfinedTestDispatcher()) {
combine( combine(
viewModel.topicUiState, viewModel.topicUiState,
viewModel.newsUiState, viewModel.newsUiState,
@ -156,12 +153,11 @@ class TopicViewModelTest {
assertIs<TopicUiState.Success>(topicUiState) assertIs<TopicUiState.Success>(topicUiState)
assertIs<NewsUiState.Success>(newsUiState) assertIs<NewsUiState.Success>(newsUiState)
collectJob.cancel()
} }
@Test @Test
fun uiStateTopic_whenFollowingTopic_thenShowUpdatedTopic() = runTest { fun uiStateTopic_whenFollowingTopic_thenShowUpdatedTopic() = runTest {
val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.topicUiState.collect() } backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.topicUiState.collect() }
topicsRepository.sendTopics(testInputTopics.map { it.topic }) topicsRepository.sendTopics(testInputTopics.map { it.topic })
// Set which topic IDs are followed, not including 0. // Set which topic IDs are followed, not including 0.
@ -174,7 +170,6 @@ class TopicViewModelTest {
viewModel.topicUiState.value, viewModel.topicUiState.value,
) )
collectJob.cancel()
} }
} }

Loading…
Cancel
Save