Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change collectJob to backgroundScope coroutine #1585

Merged
merged 4 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,18 @@ class BookmarksViewModelTest {

@Test
fun oneBookmark_showsInFeed() = runTest {
val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.feedUiState.collect() }
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.feedUiState.collect() }

newsRepository.sendNewsResources(newsResourcesTestData)
userDataRepository.setNewsResourceBookmarked(newsResourcesTestData[0].id, true)
val item = viewModel.feedUiState.value
assertIs<Success>(item)
assertEquals(item.feed.size, 1)

collectJob.cancel()
}

@Test
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
newsRepository.sendNewsResources(newsResourcesTestData)
// Start with the resource saved
Expand All @@ -88,7 +86,5 @@ class BookmarksViewModelTest {
val item = viewModel.feedUiState.value
assertIs<Success>(item)
assertEquals(item.feed.size, 0)

collectJob.cancel()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,8 @@ class ForYouViewModelTest {

@Test
fun stateIsLoadingWhenFollowedTopicsAreLoading() = runTest {
val collectJob1 =
launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() }
val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() }
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }

topicsRepository.sendTopics(sampleTopics)

Expand All @@ -109,31 +108,24 @@ class ForYouViewModelTest {
viewModel.onboardingUiState.value,
)
assertEquals(NewsFeedUiState.Loading, viewModel.feedState.value)

collectJob1.cancel()
collectJob2.cancel()
}

@Test
fun stateIsLoadingWhenAppIsSyncingWithNoInterests() = runTest {
syncManager.setSyncing(true)

val collectJob =
launch(UnconfinedTestDispatcher()) { viewModel.isSyncing.collect() }
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.isSyncing.collect() }

assertEquals(
true,
viewModel.isSyncing.value,
)

collectJob.cancel()
}

@Test
fun onboardingStateIsLoadingWhenTopicsAreLoading() = runTest {
val collectJob1 =
launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() }
val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() }
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }

userDataRepository.setFollowedTopicIds(emptySet())

Expand All @@ -142,16 +134,12 @@ class ForYouViewModelTest {
viewModel.onboardingUiState.value,
)
assertEquals(NewsFeedUiState.Success(emptyList()), viewModel.feedState.value)

collectJob1.cancel()
collectJob2.cancel()
}

@Test
fun onboardingIsShownWhenNewsResourcesAreLoading() = runTest {
val collectJob1 =
launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() }
val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() }
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }

topicsRepository.sendTopics(sampleTopics)
userDataRepository.setFollowedTopicIds(emptySet())
Expand Down Expand Up @@ -202,16 +190,12 @@ class ForYouViewModelTest {
),
viewModel.feedState.value,
)

collectJob1.cancel()
collectJob2.cancel()
}

@Test
fun onboardingIsShownAfterLoadingEmptyFollowedTopics() = runTest {
val collectJob1 =
launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() }
val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() }
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }

topicsRepository.sendTopics(sampleTopics)
userDataRepository.setFollowedTopicIds(emptySet())
Expand Down Expand Up @@ -263,16 +247,12 @@ class ForYouViewModelTest {
),
viewModel.feedState.value,
)

collectJob1.cancel()
collectJob2.cancel()
}

@Test
fun onboardingIsNotShownAfterUserDismissesOnboarding() = runTest {
val collectJob1 =
launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() }
val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() }
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }

topicsRepository.sendTopics(sampleTopics)

Expand All @@ -299,16 +279,12 @@ class ForYouViewModelTest {
),
viewModel.feedState.value,
)

collectJob1.cancel()
collectJob2.cancel()
}

@Test
fun topicSelectionUpdatesAfterSelectingTopic() = runTest {
val collectJob1 =
launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() }
val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() }
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }

topicsRepository.sendTopics(sampleTopics)
userDataRepository.setFollowedTopicIds(emptySet())
Expand Down Expand Up @@ -352,16 +328,12 @@ class ForYouViewModelTest {
),
viewModel.feedState.value,
)

collectJob1.cancel()
collectJob2.cancel()
}

@Test
fun topicSelectionUpdatesAfterUnselectingTopic() = runTest {
val collectJob1 =
launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() }
val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() }
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }

topicsRepository.sendTopics(sampleTopics)
userDataRepository.setFollowedTopicIds(emptySet())
Expand Down Expand Up @@ -416,16 +388,12 @@ class ForYouViewModelTest {
),
viewModel.feedState.value,
)

collectJob1.cancel()
collectJob2.cancel()
}

@Test
fun newsResourceSelectionUpdatesAfterLoadingFollowedTopics() = runTest {
val collectJob1 =
launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() }
val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.onboardingUiState.collect() }
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }

val followedTopicIds = setOf("1")
val userData = emptyUserData.copy(
Expand Down Expand Up @@ -460,15 +428,11 @@ class ForYouViewModelTest {
),
viewModel.feedState.value,
)

collectJob1.cancel()
collectJob2.cancel()
}

@Test
fun deepLinkedNewsResourceIsFetchedAndResetAfterViewing() = runTest {
val collectJob =
launch(UnconfinedTestDispatcher()) { viewModel.deepLinkedNewsResource.collect() }
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.deepLinkedNewsResource.collect() }

newsRepository.sendNewsResources(sampleNewsResources)
userDataRepository.setUserData(emptyUserData)
Expand Down Expand Up @@ -503,8 +467,6 @@ class ForYouViewModelTest {
),
),
)

collectJob.cancel()
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,15 @@ class InterestsViewModelTest {

@Test
fun uiState_whenFollowedTopicsAreLoading_thenShowLoading() = runTest {
val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }

userDataRepository.setFollowedTopicIds(emptySet())
assertEquals(InterestsUiState.Loading, viewModel.uiState.value)

collectJob.cancel()
}

@Test
fun uiState_whenFollowingNewTopic_thenShowUpdatedTopics() = runTest {
val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }

val toggleTopicId = testOutputTopics[1].topic.id
topicsRepository.sendTopics(testInputTopics.map { it.topic })
Expand All @@ -114,13 +112,11 @@ class InterestsViewModelTest {
),
viewModel.uiState.value,
)

collectJob.cancel()
}

@Test
fun uiState_whenUnfollowingTopics_thenShowUpdatedTopics() = runTest {
val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }

val toggleTopicId = testOutputTopics[1].topic.id

Expand All @@ -147,8 +143,6 @@ class InterestsViewModelTest {
),
viewModel.uiState.value,
)

collectJob.cancel()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,53 +85,41 @@ class SearchViewModelTest {
fun stateIsEmptyQuery_withEmptySearchQuery() = runTest {
searchContentsRepository.addNewsResources(newsResourcesTestData)
searchContentsRepository.addTopics(topicsTestData)
val collectJob =
launch(UnconfinedTestDispatcher()) { viewModel.searchResultUiState.collect() }
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.searchResultUiState.collect() }

viewModel.onSearchQueryChanged("")

assertEquals(EmptyQuery, viewModel.searchResultUiState.value)

collectJob.cancel()
}

@Test
fun emptyResultIsReturned_withNotMatchingQuery() = runTest {
val collectJob =
launch(UnconfinedTestDispatcher()) { viewModel.searchResultUiState.collect() }
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.searchResultUiState.collect() }

viewModel.onSearchQueryChanged("XXX")
searchContentsRepository.addNewsResources(newsResourcesTestData)
searchContentsRepository.addTopics(topicsTestData)

val result = viewModel.searchResultUiState.value
assertIs<SearchResultUiState.Success>(result)

collectJob.cancel()
}

@Test
fun recentSearches_verifyUiStateIsSuccess() = runTest {
val collectJob =
launch(UnconfinedTestDispatcher()) { viewModel.recentSearchQueriesUiState.collect() }
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.recentSearchQueriesUiState.collect() }
viewModel.onSearchTriggered("kotlin")

val result = viewModel.recentSearchQueriesUiState.value
assertIs<Success>(result)

collectJob.cancel()
}

@Test
fun searchNotReady_withNoFtsTableEntity() = runTest {
val collectJob =
launch(UnconfinedTestDispatcher()) { viewModel.searchResultUiState.collect() }
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.searchResultUiState.collect() }

viewModel.onSearchQueryChanged("")

assertEquals(SearchNotReady, viewModel.searchResultUiState.value)

collectJob.cancel()
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ class SettingsViewModelTest {

@Test
fun stateIsSuccessAfterUserDataLoaded() = runTest {
val collectJob =
launch(UnconfinedTestDispatcher()) { viewModel.settingsUiState.collect() }
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.settingsUiState.collect() }

userDataRepository.setThemeBrand(ANDROID)
userDataRepository.setDarkThemeConfig(DARK)
Expand All @@ -68,7 +67,5 @@ class SettingsViewModelTest {
),
viewModel.settingsUiState.value,
)

collectJob.cancel()
}
}
Loading
Loading