A fully functional Android app built entirely with Kotlin and Jetpack Compose
Update Main dispatcher rule, assert on StateFlow's value property
Márton Braune220c66329f7
diff --git a/core-testing/src/main/java/com/google/samples/apps/nowinandroid/core/testing/util/TestDispatcherRule.kt b/core-testing/src/main/java/com/google/samples/apps/nowinandroid/core/testing/util/MainDispatcherRule.ktsimilarity index 54%rename from core-testing/src/main/java/com/google/samples/apps/nowinandroid/core/testing/util/TestDispatcherRule.ktrename to core-testing/src/main/java/com/google/samples/apps/nowinandroid/core/testing/util/MainDispatcherRule.ktindex 789779716..5b8807b75 100644--- a/core-testing/src/main/java/com/google/samples/apps/nowinandroid/core/testing/util/TestDispatcherRule.kt+++ b/core-testing/src/main/java/com/google/samples/apps/nowinandroid/core/testing/util/MainDispatcherRule.kt@@ -16,31 +16,27 @@package com.google.samples.apps.nowinandroid.core.testing.util-import kotlinx.coroutines.CoroutineDispatcherimport kotlinx.coroutines.Dispatchers-import kotlinx.coroutines.test.StandardTestDispatcher+import kotlinx.coroutines.test.TestDispatcher+import kotlinx.coroutines.test.UnconfinedTestDispatcherimport kotlinx.coroutines.test.resetMainimport kotlinx.coroutines.test.setMainimport org.junit.rules.TestRule+import org.junit.rules.TestWatcherimport org.junit.runner.Description-import org.junit.runners.model.Statement/**- * A [TestRule] that initializes the main dispatcher to [dispatcher], which defaults to a- * [StandardTestDispatcher].+ * A JUnit [TestRule] that sets the Main dispatcher to [testDispatcher]+ * for the duration of the test.*/-class TestDispatcherRule(- private val dispatcher: CoroutineDispatcher = StandardTestDispatcher()-) : TestRule {- override fun apply(base: Statement, description: Description): Statement =- object : Statement() {- override fun evaluate() {- Dispatchers.setMain(dispatcher)- try {- base.evaluate()- } finally {- Dispatchers.resetMain()- }- }- }+class MainDispatcherRule(+ val testDispatcher: TestDispatcher = UnconfinedTestDispatcher(),+) : TestWatcher() {+ override fun starting(description: Description) {+ Dispatchers.setMain(testDispatcher)+ }++ override fun finished(description: Description) {+ Dispatchers.resetMain()+ }}diff --git a/feature-author/src/test/java/com/google/samples/apps/nowinandroid/feature/author/AuthorViewModelTest.kt b/feature-author/src/test/java/com/google/samples/apps/nowinandroid/feature/author/AuthorViewModelTest.ktindex 9ab24160b..0e1fc48d2 100644--- a/feature-author/src/test/java/com/google/samples/apps/nowinandroid/feature/author/AuthorViewModelTest.kt+++ b/feature-author/src/test/java/com/google/samples/apps/nowinandroid/feature/author/AuthorViewModelTest.kt@@ -17,7 +17,6 @@package com.google.samples.apps.nowinandroid.feature.authorimport androidx.lifecycle.SavedStateHandle-import app.cash.turbine.testimport com.google.samples.apps.nowinandroid.core.model.data.Authorimport com.google.samples.apps.nowinandroid.core.model.data.FollowableAuthorimport com.google.samples.apps.nowinandroid.core.model.data.NewsResource@@ -25,9 +24,12 @@ import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Vidimport com.google.samples.apps.nowinandroid.core.testing.repository.TestAuthorsRepositoryimport com.google.samples.apps.nowinandroid.core.testing.repository.TestNewsRepositoryimport com.google.samples.apps.nowinandroid.core.testing.repository.TestUserDataRepository-import com.google.samples.apps.nowinandroid.core.testing.util.TestDispatcherRule+import com.google.samples.apps.nowinandroid.core.testing.util.MainDispatcherRuleimport com.google.samples.apps.nowinandroid.feature.author.navigation.AuthorDestination+import kotlinx.coroutines.flow.collectimport kotlinx.coroutines.flow.first+import kotlinx.coroutines.launch+import kotlinx.coroutines.test.UnconfinedTestDispatcherimport kotlinx.coroutines.test.runTestimport kotlinx.datetime.Instantimport org.junit.Assert.assertEquals@@ -36,10 +38,14 @@ import org.junit.Beforeimport org.junit.Ruleimport org.junit.Test+/**+ * To learn more about how this test handles Flows created with stateIn, see+ * https://developer.android.com/kotlin/flow/test#statein+ */class AuthorViewModelTest {@get:Rule- val dispatcherRule = TestDispatcherRule()+ val dispatcherRule = MainDispatcherRule()private val userDataRepository = TestUserDataRepository()private val authorsRepository = TestAuthorsRepository()@@ -62,90 +68,91 @@ class AuthorViewModelTest {@Testfun uiStateAuthor_whenSuccess_matchesAuthorFromRepository() = runTest {- viewModel.uiState.test {- awaitItem()- // To make sure AuthorUiState is success- authorsRepository.sendAuthors(testInputAuthors.map(FollowableAuthor::author))- userDataRepository.setFollowedAuthorIds(setOf(testInputAuthors[1].author.id))+ val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }- val item = awaitItem()- assertTrue(item.authorState is AuthorUiState.Success)+ // To make sure AuthorUiState is success+ authorsRepository.sendAuthors(testInputAuthors.map(FollowableAuthor::author))+ userDataRepository.setFollowedAuthorIds(setOf(testInputAuthors[1].author.id))- val successAuthorUiState = item.authorState as AuthorUiState.Success- val authorFromRepository = authorsRepository.getAuthorStream(- id = testInputAuthors[0].author.id- ).first()+ val item = viewModel.uiState.value+ assertTrue(item.authorState is AuthorUiState.Success)- successAuthorUiState.followableAuthor.author- assertEquals(authorFromRepository, successAuthorUiState.followableAuthor.author)- }+ val successAuthorUiState = item.authorState as AuthorUiState.Success+ val authorFromRepository = authorsRepository.getAuthorStream(+ id = testInputAuthors[0].author.id+ ).first()++ successAuthorUiState.followableAuthor.author+ assertEquals(authorFromRepository, successAuthorUiState.followableAuthor.author)++ collectJob.cancel()}@Testfun uiStateNews_whenInitialized_thenShowLoading() = runTest {- viewModel.uiState.test {- assertEquals(NewsUiState.Loading, awaitItem().newsState)- }+ assertEquals(NewsUiState.Loading, viewModel.uiState.value.newsState)}@Testfun uiStateAuthor_whenInitialized_thenShowLoading() = runTest {- viewModel.uiState.test {- assertEquals(AuthorUiState.Loading, awaitItem().authorState)- }+ assertEquals(AuthorUiState.Loading, viewModel.uiState.value.authorState)}@Testfun uiStateAuthor_whenFollowedIdsSuccessAndAuthorLoading_thenShowLoading() = runTest {- viewModel.uiState.test {- userDataRepository.setFollowedAuthorIds(setOf(testInputAuthors[1].author.id))- assertEquals(AuthorUiState.Loading, awaitItem().authorState)- }+ val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }++ userDataRepository.setFollowedAuthorIds(setOf(testInputAuthors[1].author.id))+ assertEquals(AuthorUiState.Loading, viewModel.uiState.value.authorState)++ collectJob.cancel()}@Testfun uiStateAuthor_whenFollowedIdsSuccessAndAuthorSuccess_thenAuthorSuccessAndNewsLoading() =runTest {- viewModel.uiState.test {- awaitItem()- authorsRepository.sendAuthors(testInputAuthors.map { it.author })- userDataRepository.setFollowedAuthorIds(setOf(testInputAuthors[1].author.id))- val item = awaitItem()- assertTrue(item.authorState is AuthorUiState.Success)- assertTrue(item.newsState is NewsUiState.Loading)- }+ val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }++ authorsRepository.sendAuthors(testInputAuthors.map { it.author })+ userDataRepository.setFollowedAuthorIds(setOf(testInputAuthors[1].author.id))+ val item = viewModel.uiState.value+ assertTrue(item.authorState is AuthorUiState.Success)+ assertTrue(item.newsState is NewsUiState.Loading)++ collectJob.cancel()}@Testfun uiStateAuthor_whenFollowedIdsSuccessAndAuthorSuccessAndNewsIsSuccess_thenAllSuccess() =runTest {- viewModel.uiState.test {- awaitItem()- authorsRepository.sendAuthors(testInputAuthors.map { it.author })- userDataRepository.setFollowedAuthorIds(setOf(testInputAuthors[1].author.id))- newsRepository.sendNewsResources(sampleNewsResources)- val item = awaitItem()- assertTrue(item.authorState is AuthorUiState.Success)- assertTrue(item.newsState is NewsUiState.Success)- }+ val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }++ authorsRepository.sendAuthors(testInputAuthors.map { it.author })+ userDataRepository.setFollowedAuthorIds(setOf(testInputAuthors[1].author.id))+ newsRepository.sendNewsResources(sampleNewsResources)+ val item = viewModel.uiState.value+ assertTrue(item.authorState is AuthorUiState.Success)+ assertTrue(item.newsState is NewsUiState.Success)++ collectJob.cancel()}@Testfun uiStateAuthor_whenFollowingAuthor_thenShowUpdatedAuthor() = runTest {- viewModel.uiState- .test {- awaitItem()- authorsRepository.sendAuthors(testInputAuthors.map { it.author })- // Set which author IDs are followed, not including 0.- userDataRepository.setFollowedAuthorIds(setOf(testInputAuthors[1].author.id))-- viewModel.followAuthorToggle(true)-- assertEquals(- AuthorUiState.Success(followableAuthor = testOutputAuthors[0]),- awaitItem().authorState- )- }+ val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }++ authorsRepository.sendAuthors(testInputAuthors.map { it.author })+ // Set which author IDs are followed, not including 0.+ userDataRepository.setFollowedAuthorIds(setOf(testInputAuthors[1].author.id))++ viewModel.followAuthorToggle(true)++ assertEquals(+ AuthorUiState.Success(followableAuthor = testOutputAuthors[0]),+ viewModel.uiState.value.authorState+ )++ collectJob.cancel()}}diff --git a/feature-foryou/src/test/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModelTest.kt b/feature-foryou/src/test/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModelTest.ktindex dba50dccb..ca4ef7094 100644--- a/feature-foryou/src/test/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModelTest.kt+++ b/feature-foryou/src/test/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouViewModelTest.kt@@ -17,7 +17,6 @@package com.google.samples.apps.nowinandroid.feature.foryouimport androidx.lifecycle.SavedStateHandle-import app.cash.turbine.testimport com.google.samples.apps.nowinandroid.core.model.data.Authorimport com.google.samples.apps.nowinandroid.core.model.data.FollowableAuthorimport com.google.samples.apps.nowinandroid.core.model.data.FollowableTopic@@ -29,8 +28,10 @@ import com.google.samples.apps.nowinandroid.core.testing.repository.TestAuthorsRimport com.google.samples.apps.nowinandroid.core.testing.repository.TestNewsRepositoryimport com.google.samples.apps.nowinandroid.core.testing.repository.TestTopicsRepositoryimport com.google.samples.apps.nowinandroid.core.testing.repository.TestUserDataRepository-import com.google.samples.apps.nowinandroid.core.testing.util.TestDispatcherRule-import kotlinx.coroutines.flow.combine+import com.google.samples.apps.nowinandroid.core.testing.util.MainDispatcherRule+import kotlinx.coroutines.flow.collect+import kotlinx.coroutines.launch+import kotlinx.coroutines.test.UnconfinedTestDispatcherimport kotlinx.coroutines.test.advanceUntilIdleimport kotlinx.coroutines.test.runTestimport kotlinx.datetime.Instant@@ -39,9 +40,13 @@ import org.junit.Beforeimport org.junit.Ruleimport org.junit.Test+/**+ * To learn more about how this test handles Flows created with stateIn, see+ * https://developer.android.com/kotlin/flow/test#statein+ */class ForYouViewModelTest {@get:Rule- val dispatcherRule = TestDispatcherRule()+ val mainDispatcherRule = MainDispatcherRule()private val userDataRepository = TestUserDataRepository()private val authorsRepository = TestAuthorsRepository()@@ -60,1449 +65,1308 @@ class ForYouViewModelTest {)}- /**- * A pairing of [ForYouInterestsSelectionUiState] and [ForYouFeedUiState] for ease of testing- * state updates as a single flow.- */- private data class ForYouUiState(- val interestsSelectionState: ForYouInterestsSelectionUiState,- val feedState: ForYouFeedUiState,- )-- private val ForYouViewModel.uiState- get() =- combine(- interestsSelectionState,- feedState,- ::ForYouUiState- )-@Testfun stateIsInitiallyLoading() = runTest {- viewModel.uiState.test {- assertEquals(- ForYouUiState(- ForYouInterestsSelectionUiState.Loading,- ForYouFeedUiState.Loading- ),- awaitItem()- )- }+ assertEquals(+ ForYouInterestsSelectionUiState.Loading,+ viewModel.interestsSelectionState.value+ )+ assertEquals(ForYouFeedUiState.Loading, viewModel.feedState.value)}@Testfun stateIsLoadingWhenFollowedTopicsAreLoading() = runTest {- viewModel.uiState.test {- assertEquals(- ForYouUiState(- ForYouInterestsSelectionUiState.Loading,- ForYouFeedUiState.Loading- ),- awaitItem()- )- topicsRepository.sendTopics(sampleTopics)- }+ val collectJob1 =+ launch(UnconfinedTestDispatcher()) { viewModel.interestsSelectionState.collect() }+ val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }++ topicsRepository.sendTopics(sampleTopics)++ assertEquals(+ ForYouInterestsSelectionUiState.Loading,+ viewModel.interestsSelectionState.value+ )+ assertEquals(ForYouFeedUiState.Loading, viewModel.feedState.value)++ collectJob1.cancel()+ collectJob2.cancel()}@Testfun stateIsLoadingWhenFollowedAuthorsAreLoading() = runTest {- viewModel.uiState.test {- assertEquals(- ForYouUiState(- ForYouInterestsSelectionUiState.Loading,- ForYouFeedUiState.Loading- ),- awaitItem()- )- authorsRepository.sendAuthors(sampleAuthors)- }+ val collectJob1 =+ launch(UnconfinedTestDispatcher()) { viewModel.interestsSelectionState.collect() }+ val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }++ authorsRepository.sendAuthors(sampleAuthors)++ assertEquals(+ ForYouInterestsSelectionUiState.Loading,+ viewModel.interestsSelectionState.value+ )+ assertEquals(ForYouFeedUiState.Loading, viewModel.feedState.value)++ collectJob1.cancel()+ collectJob2.cancel()}@Testfun stateIsLoadingWhenTopicsAreLoading() = runTest {- viewModel.uiState.test {- assertEquals(- ForYouUiState(- ForYouInterestsSelectionUiState.Loading,- ForYouFeedUiState.Loading- ),- awaitItem()- )- userDataRepository.setFollowedTopicIds(emptySet())- }+ val collectJob1 =+ launch(UnconfinedTestDispatcher()) { viewModel.interestsSelectionState.collect() }+ val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }++ userDataRepository.setFollowedTopicIds(emptySet())++ assertEquals(+ ForYouInterestsSelectionUiState.Loading,+ viewModel.interestsSelectionState.value+ )+ assertEquals(ForYouFeedUiState.Success(emptyList()), viewModel.feedState.value)++ collectJob1.cancel()+ collectJob2.cancel()}@Testfun stateIsLoadingWhenAuthorsAreLoading() = runTest {- viewModel.uiState.test {- assertEquals(- ForYouUiState(- ForYouInterestsSelectionUiState.Loading,- ForYouFeedUiState.Loading- ),- awaitItem()- )- userDataRepository.setFollowedAuthorIds(emptySet())- }+ val collectJob1 =+ launch(UnconfinedTestDispatcher()) { viewModel.interestsSelectionState.collect() }+ val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }++ userDataRepository.setFollowedAuthorIds(emptySet())++ assertEquals(+ ForYouInterestsSelectionUiState.Loading,+ viewModel.interestsSelectionState.value+ )+ assertEquals(ForYouFeedUiState.Success(emptyList()), viewModel.feedState.value)++ collectJob1.cancel()+ collectJob2.cancel()}@Testfun stateIsInterestsSelectionWhenNewsResourcesAreLoading() = runTest {- viewModel.uiState.test {- advanceUntilIdle()- expectMostRecentItem()- topicsRepository.sendTopics(sampleTopics)- userDataRepository.setFollowedTopicIds(emptySet())- authorsRepository.sendAuthors(sampleAuthors)- userDataRepository.setFollowedAuthorIds(emptySet())-- advanceUntilIdle()- assertEquals(- ForYouUiState(- interestsSelectionState =- ForYouInterestsSelectionUiState.WithInterestsSelection(- 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- ),+ val collectJob1 =+ launch(UnconfinedTestDispatcher()) { viewModel.interestsSelectionState.collect() }+ val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }++ topicsRepository.sendTopics(sampleTopics)+ userDataRepository.setFollowedTopicIds(emptySet())+ authorsRepository.sendAuthors(sampleAuthors)+ userDataRepository.setFollowedAuthorIds(emptySet())++ assertEquals(+ ForYouInterestsSelectionUiState.WithInterestsSelection(+ 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",),- authors = listOf(- FollowableAuthor(- author = Author(- id = "0",- name = "Android Dev",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = false- ),- FollowableAuthor(- author = Author(- id = "1",- name = "Android Dev 2",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = false- ),- FollowableAuthor(- author = Author(- id = "2",- name = "Android Dev 3",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = false- )+ isFollowed = false+ ),+ FollowableTopic(+ topic = Topic(+ id = "2",+ name = "Tools",+ shortDescription = "",+ longDescription = "long description",+ url = "URL",+ imageUrl = "image URL",),+ isFollowed = false),- feedState = ForYouFeedUiState.Success(- feed = emptyList()+ ),+ authors = listOf(+ FollowableAuthor(+ author = Author(+ id = "0",+ name = "Android Dev",+ imageUrl = "",+ twitter = "",+ mediumPage = "",+ bio = "",+ ),+ isFollowed = false+ ),+ FollowableAuthor(+ author = Author(+ id = "1",+ name = "Android Dev 2",+ imageUrl = "",+ twitter = "",+ mediumPage = "",+ bio = "",+ ),+ isFollowed = false+ ),+ FollowableAuthor(+ author = Author(+ id = "2",+ name = "Android Dev 3",+ imageUrl = "",+ twitter = "",+ mediumPage = "",+ bio = "",+ ),+ isFollowed = false)),- expectMostRecentItem()- )- }+ ),+ viewModel.interestsSelectionState.value+ )+ assertEquals(+ ForYouFeedUiState.Success(+ feed = emptyList()+ ),+ viewModel.feedState.value+ )++ collectJob1.cancel()+ collectJob2.cancel()}@Testfun stateIsInterestsSelectionAfterLoadingEmptyFollowedTopicsAndAuthors() = runTest {- viewModel.uiState- .test {- topicsRepository.sendTopics(sampleTopics)- authorsRepository.sendAuthors(sampleAuthors)- userDataRepository.setFollowedTopicIds(emptySet())- userDataRepository.setFollowedAuthorIds(emptySet())- newsRepository.sendNewsResources(sampleNewsResources)-- advanceUntilIdle()- assertEquals(- ForYouUiState(- interestsSelectionState =- ForYouInterestsSelectionUiState.WithInterestsSelection(- 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- ),- ),- authors = listOf(- FollowableAuthor(- author = Author(- id = "0",- name = "Android Dev",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = false- ),- FollowableAuthor(- author = Author(- id = "1",- name = "Android Dev 2",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = false- ),- FollowableAuthor(- author = Author(- id = "2",- name = "Android Dev 3",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = false- )- ),+ val collectJob1 =+ launch(UnconfinedTestDispatcher()) { viewModel.interestsSelectionState.collect() }+ val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }++ topicsRepository.sendTopics(sampleTopics)+ authorsRepository.sendAuthors(sampleAuthors)+ userDataRepository.setFollowedTopicIds(emptySet())+ userDataRepository.setFollowedAuthorIds(emptySet())+ newsRepository.sendNewsResources(sampleNewsResources)++ assertEquals(+ ForYouInterestsSelectionUiState.WithInterestsSelection(+ topics = listOf(+ FollowableTopic(+ topic = Topic(+ id = "0",+ name = "Headlines",+ shortDescription = "",+ longDescription = "long description",+ url = "URL",+ imageUrl = "image URL",),- feedState = ForYouFeedUiState.Success(- feed = emptyList()- )+ isFollowed = false),- expectMostRecentItem()- )- }+ 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+ ),+ ),+ authors = listOf(+ FollowableAuthor(+ author = Author(+ id = "0",+ name = "Android Dev",+ imageUrl = "",+ twitter = "",+ mediumPage = "",+ bio = "",+ ),+ isFollowed = false+ ),+ FollowableAuthor(+ author = Author(+ id = "1",+ name = "Android Dev 2",+ imageUrl = "",+ twitter = "",+ mediumPage = "",+ bio = "",+ ),+ isFollowed = false+ ),+ FollowableAuthor(+ author = Author(+ id = "2",+ name = "Android Dev 3",+ imageUrl = "",+ twitter = "",+ mediumPage = "",+ bio = "",+ ),+ isFollowed = false+ )+ ),+ ),+ viewModel.interestsSelectionState.value+ )+ assertEquals(+ ForYouFeedUiState.Success(+ feed = emptyList()++ ),+ viewModel.feedState.value+ )++ collectJob1.cancel()+ collectJob2.cancel()}@Testfun stateIsWithoutInterestsSelectionAfterLoadingFollowedTopics() = runTest {- viewModel.uiState- .test {- advanceUntilIdle()- expectMostRecentItem()- authorsRepository.sendAuthors(sampleAuthors)- userDataRepository.setFollowedAuthorIds(emptySet())- topicsRepository.sendTopics(sampleTopics)- userDataRepository.setFollowedTopicIds(setOf("0", "1"))-- assertEquals(- ForYouUiState(- interestsSelectionState =- ForYouInterestsSelectionUiState.NoInterestsSelection,- feedState = ForYouFeedUiState.Loading- ),- awaitItem()- )+ val collectJob1 =+ launch(UnconfinedTestDispatcher()) { viewModel.interestsSelectionState.collect() }+ val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }- newsRepository.sendNewsResources(sampleNewsResources)-- assertEquals(- ForYouUiState(- interestsSelectionState =- ForYouInterestsSelectionUiState.NoInterestsSelection,- feedState = ForYouFeedUiState.Success(- feed = sampleNewsResources.map {- SaveableNewsResource(- newsResource = it,- isSaved = false- )- }- )- ),- awaitItem()- )- }+ authorsRepository.sendAuthors(sampleAuthors)+ userDataRepository.setFollowedAuthorIds(emptySet())+ topicsRepository.sendTopics(sampleTopics)+ userDataRepository.setFollowedTopicIds(setOf("0", "1"))++ assertEquals(+ ForYouInterestsSelectionUiState.NoInterestsSelection,+ viewModel.interestsSelectionState.value+ )+ assertEquals(ForYouFeedUiState.Loading, viewModel.feedState.value)++ newsRepository.sendNewsResources(sampleNewsResources)++ assertEquals(+ ForYouInterestsSelectionUiState.NoInterestsSelection,+ viewModel.interestsSelectionState.value+ )+ assertEquals(+ ForYouFeedUiState.Success(+ feed =+ sampleNewsResources.map {+ SaveableNewsResource(+ newsResource = it,+ isSaved = false+ )+ }+ ),+ viewModel.feedState.value+ )++ collectJob1.cancel()+ collectJob2.cancel()}@Testfun stateIsWithoutInterestsSelectionAfterLoadingFollowedAuthors() = runTest {- viewModel.uiState- .test {- advanceUntilIdle()- expectMostRecentItem()- authorsRepository.sendAuthors(sampleAuthors)- userDataRepository.setFollowedAuthorIds(setOf("0", "1"))- topicsRepository.sendTopics(sampleTopics)- userDataRepository.setFollowedTopicIds(emptySet())-- assertEquals(- ForYouUiState(- interestsSelectionState =- ForYouInterestsSelectionUiState.NoInterestsSelection,- feedState = ForYouFeedUiState.Loading- ),- awaitItem()- )+ val collectJob1 =+ launch(UnconfinedTestDispatcher()) { viewModel.interestsSelectionState.collect() }+ val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }- newsRepository.sendNewsResources(sampleNewsResources)-- assertEquals(- ForYouUiState(- interestsSelectionState =- ForYouInterestsSelectionUiState.NoInterestsSelection,- feedState = ForYouFeedUiState.Success(- feed = sampleNewsResources.map {- SaveableNewsResource(- newsResource = it,- isSaved = false- )- }- )- ),- awaitItem()- )- }+ authorsRepository.sendAuthors(sampleAuthors)+ userDataRepository.setFollowedAuthorIds(setOf("0", "1"))+ topicsRepository.sendTopics(sampleTopics)+ userDataRepository.setFollowedTopicIds(emptySet())++ assertEquals(+ ForYouInterestsSelectionUiState.NoInterestsSelection,+ viewModel.interestsSelectionState.value+ )+ assertEquals(+ ForYouFeedUiState.Loading,+ viewModel.feedState.value+ )++ newsRepository.sendNewsResources(sampleNewsResources)++ assertEquals(+ ForYouInterestsSelectionUiState.NoInterestsSelection,+ viewModel.interestsSelectionState.value+ )+ assertEquals(+ ForYouFeedUiState.Success(+ feed = sampleNewsResources.map {+ SaveableNewsResource(+ newsResource = it,+ isSaved = false+ )+ }+ ),+ viewModel.feedState.value+ )++ collectJob1.cancel()+ collectJob2.cancel()}@Testfun topicSelectionUpdatesAfterSelectingTopic() = runTest {- viewModel.uiState- .test {- topicsRepository.sendTopics(sampleTopics)- userDataRepository.setFollowedTopicIds(emptySet())- authorsRepository.sendAuthors(sampleAuthors)- userDataRepository.setFollowedAuthorIds(emptySet())- newsRepository.sendNewsResources(sampleNewsResources)-- advanceUntilIdle()- expectMostRecentItem()-- viewModel.updateTopicSelection("1", isChecked = true)-- assertEquals(- ForYouUiState(- interestsSelectionState =- ForYouInterestsSelectionUiState.WithInterestsSelection(- 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- )- ),- authors = listOf(- FollowableAuthor(- author = Author(- id = "0",- name = "Android Dev",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = false- ),- FollowableAuthor(- author = Author(- id = "1",- name = "Android Dev 2",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = false- ),- FollowableAuthor(- author = Author(- id = "2",- name = "Android Dev 3",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = false- )- ),+ val collectJob1 =+ launch(UnconfinedTestDispatcher()) { viewModel.interestsSelectionState.collect() }+ val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }++ topicsRepository.sendTopics(sampleTopics)+ userDataRepository.setFollowedTopicIds(emptySet())+ authorsRepository.sendAuthors(sampleAuthors)+ userDataRepository.setFollowedAuthorIds(emptySet())+ newsRepository.sendNewsResources(sampleNewsResources)++ assertEquals(+ ForYouInterestsSelectionUiState.WithInterestsSelection(+ topics = listOf(+ FollowableTopic(+ topic = Topic(+ id = "0",+ name = "Headlines",+ shortDescription = "",+ longDescription = "long description",+ url = "URL",+ imageUrl = "image URL",),- feedState = ForYouFeedUiState.Success(- feed = emptyList(),- )+ isFollowed = false),- awaitItem()- )- assertEquals(- ForYouUiState(- interestsSelectionState =- ForYouInterestsSelectionUiState.WithInterestsSelection(- 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- )- ),- authors = listOf(- FollowableAuthor(- author = Author(- id = "0",- name = "Android Dev",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = false- ),- FollowableAuthor(- author = Author(- id = "1",- name = "Android Dev 2",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = false- ),- FollowableAuthor(- author = Author(- id = "2",- name = "Android Dev 3",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = false- )- ),+ FollowableTopic(+ topic = Topic(+ id = "1",+ name = "UI",+ shortDescription = "",+ longDescription = "long description",+ url = "URL",+ imageUrl = "image URL",),- feedState = ForYouFeedUiState.Loading+ isFollowed = false),- awaitItem()- )- assertEquals(- ForYouUiState(- interestsSelectionState =- ForYouInterestsSelectionUiState.WithInterestsSelection(- 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- )- ),- authors = listOf(- FollowableAuthor(- author = Author(- id = "0",- name = "Android Dev",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = false- ),- FollowableAuthor(- author = Author(- id = "1",- name = "Android Dev 2",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = false- ),- FollowableAuthor(- author = Author(- id = "2",- name = "Android Dev 3",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = false- )- ),+ FollowableTopic(+ topic = Topic(+ id = "2",+ name = "Tools",+ shortDescription = "",+ longDescription = "long description",+ url = "URL",+ imageUrl = "image URL",+ ),+ isFollowed = false+ )+ ),+ authors = listOf(+ FollowableAuthor(+ author = Author(+ id = "0",+ name = "Android Dev",+ imageUrl = "",+ twitter = "",+ mediumPage = "",+ bio = "",),- feedState = ForYouFeedUiState.Success(- feed = listOf(- SaveableNewsResource(- newsResource = sampleNewsResources[1],- isSaved = false- ),- SaveableNewsResource(- newsResource = sampleNewsResources[2],- isSaved = false- )- )- )+ isFollowed = false),- awaitItem()+ FollowableAuthor(+ author = Author(+ id = "1",+ name = "Android Dev 2",+ imageUrl = "",+ twitter = "",+ mediumPage = "",+ bio = "",+ ),+ isFollowed = false+ ),+ FollowableAuthor(+ author = Author(+ id = "2",+ name = "Android Dev 3",+ imageUrl = "",+ twitter = "",+ mediumPage = "",+ bio = "",+ ),+ isFollowed = false+ )+ ),+ ),+ viewModel.interestsSelectionState.value+ )+ assertEquals(+ ForYouFeedUiState.Success(+ feed = emptyList(),+ ),+ viewModel.feedState.value+ )++ viewModel.updateTopicSelection("1", isChecked = true)++ assertEquals(+ ForYouInterestsSelectionUiState.WithInterestsSelection(+ 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+ )+ ),+ authors = listOf(+ FollowableAuthor(+ author = Author(+ id = "0",+ name = "Android Dev",+ imageUrl = "",+ twitter = "",+ mediumPage = "",+ bio = "",+ ),+ isFollowed = false+ ),+ FollowableAuthor(+ author = Author(+ id = "1",+ name = "Android Dev 2",+ imageUrl = "",+ twitter = "",+ mediumPage = "",+ bio = "",+ ),+ isFollowed = false+ ),+ FollowableAuthor(+ author = Author(+ id = "2",+ name = "Android Dev 3",+ imageUrl = "",+ twitter = "",+ mediumPage = "",+ bio = "",+ ),+ isFollowed = false+ )+ ),+ ),+ viewModel.interestsSelectionState.value+ )+ assertEquals(+ ForYouFeedUiState.Success(+ feed = listOf(+ SaveableNewsResource(+ newsResource = sampleNewsResources[1],+ isSaved = false+ ),+ SaveableNewsResource(+ newsResource = sampleNewsResources[2],+ isSaved = false+ ))- }+ ),+ viewModel.feedState.value+ )++ collectJob1.cancel()+ collectJob2.cancel()}@Testfun topicSelectionUpdatesAfterSelectingAuthor() = runTest {- viewModel.uiState- .test {- topicsRepository.sendTopics(sampleTopics)- userDataRepository.setFollowedTopicIds(emptySet())- authorsRepository.sendAuthors(sampleAuthors)- userDataRepository.setFollowedAuthorIds(emptySet())- newsRepository.sendNewsResources(sampleNewsResources)-- advanceUntilIdle()- expectMostRecentItem()-- viewModel.updateAuthorSelection("1", isChecked = true)-- assertEquals(- ForYouUiState(- interestsSelectionState =- ForYouInterestsSelectionUiState.WithInterestsSelection(- 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- )- ),- authors = listOf(- FollowableAuthor(- author = Author(- id = "0",- name = "Android Dev",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = false- ),- FollowableAuthor(- author = Author(- id = "1",- name = "Android Dev 2",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = true- ),- FollowableAuthor(- author = Author(- id = "2",- name = "Android Dev 3",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = false- )- ),+ val collectJob1 =+ launch(UnconfinedTestDispatcher()) { viewModel.interestsSelectionState.collect() }+ val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }++ topicsRepository.sendTopics(sampleTopics)+ userDataRepository.setFollowedTopicIds(emptySet())+ authorsRepository.sendAuthors(sampleAuthors)+ userDataRepository.setFollowedAuthorIds(emptySet())+ newsRepository.sendNewsResources(sampleNewsResources)++ assertEquals(+ ForYouInterestsSelectionUiState.WithInterestsSelection(+ topics = listOf(+ FollowableTopic(+ topic = Topic(+ id = "0",+ name = "Headlines",+ shortDescription = "",+ longDescription = "long description",+ url = "URL",+ imageUrl = "image URL",),- feedState = ForYouFeedUiState.Success(- feed = emptyList(),- )+ isFollowed = false),- awaitItem()- )- assertEquals(- ForYouUiState(- interestsSelectionState =- ForYouInterestsSelectionUiState.WithInterestsSelection(- 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- )- ),- authors = listOf(- FollowableAuthor(- author = Author(- id = "0",- name = "Android Dev",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = false- ),- FollowableAuthor(- author = Author(- id = "1",- name = "Android Dev 2",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = true- ),- FollowableAuthor(- author = Author(- id = "2",- name = "Android Dev 3",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = false- )- ),+ FollowableTopic(+ topic = Topic(+ id = "1",+ name = "UI",+ shortDescription = "",+ longDescription = "long description",+ url = "URL",+ imageUrl = "image URL",),- feedState = ForYouFeedUiState.Loading+ isFollowed = false),- awaitItem()- )- assertEquals(- ForYouUiState(- interestsSelectionState =- ForYouInterestsSelectionUiState.WithInterestsSelection(- 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- )- ),- authors = listOf(- FollowableAuthor(- author = Author(- id = "0",- name = "Android Dev",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = false- ),- FollowableAuthor(- author = Author(- id = "1",- name = "Android Dev 2",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = true- ),- FollowableAuthor(- author = Author(- id = "2",- name = "Android Dev 3",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = false- )- ),+ FollowableTopic(+ topic = Topic(+ id = "2",+ name = "Tools",+ shortDescription = "",+ longDescription = "long description",+ url = "URL",+ imageUrl = "image URL",+ ),+ isFollowed = false+ )+ ),+ authors = listOf(+ FollowableAuthor(+ author = Author(+ id = "0",+ name = "Android Dev",+ imageUrl = "",+ twitter = "",+ mediumPage = "",+ bio = "",),- feedState = ForYouFeedUiState.Success(- feed = listOf(- SaveableNewsResource(- newsResource = sampleNewsResources[1],- isSaved = false- ),- SaveableNewsResource(- newsResource = sampleNewsResources[2],- isSaved = false- )- )- )+ isFollowed = false),- awaitItem()+ FollowableAuthor(+ author = Author(+ id = "1",+ name = "Android Dev 2",+ imageUrl = "",+ twitter = "",+ mediumPage = "",+ bio = "",+ ),+ isFollowed = false+ ),+ FollowableAuthor(+ author = Author(+ id = "2",+ name = "Android Dev 3",+ imageUrl = "",+ twitter = "",+ mediumPage = "",+ bio = "",+ ),+ isFollowed = false+ )+ ),+ ),+ viewModel.interestsSelectionState.value+ )+ assertEquals(+ ForYouFeedUiState.Success(+ feed = emptyList(),+ ),+ viewModel.feedState.value+ )++ viewModel.updateAuthorSelection("1", isChecked = true)++ assertEquals(+ ForYouInterestsSelectionUiState.WithInterestsSelection(+ 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+ )+ ),+ authors = listOf(+ FollowableAuthor(+ author = Author(+ id = "0",+ name = "Android Dev",+ imageUrl = "",+ twitter = "",+ mediumPage = "",+ bio = "",+ ),+ isFollowed = false+ ),+ FollowableAuthor(+ author = Author(+ id = "1",+ name = "Android Dev 2",+ imageUrl = "",+ twitter = "",+ mediumPage = "",+ bio = "",+ ),+ isFollowed = true+ ),+ FollowableAuthor(+ author = Author(+ id = "2",+ name = "Android Dev 3",+ imageUrl = "",+ twitter = "",+ mediumPage = "",+ bio = "",+ ),+ isFollowed = false+ )+ ),+ ),+ viewModel.interestsSelectionState.value+ )+ assertEquals(+ ForYouFeedUiState.Success(+ feed = listOf(+ SaveableNewsResource(+ newsResource = sampleNewsResources[1],+ isSaved = false+ ),+ SaveableNewsResource(+ newsResource = sampleNewsResources[2],+ isSaved = false+ ))- }+ ),+ viewModel.feedState.value+ )++ collectJob1.cancel()+ collectJob2.cancel()}@Testfun topicSelectionUpdatesAfterUnselectingTopic() = runTest {- viewModel.uiState- .test {- topicsRepository.sendTopics(sampleTopics)- userDataRepository.setFollowedTopicIds(emptySet())- authorsRepository.sendAuthors(sampleAuthors)- userDataRepository.setFollowedAuthorIds(emptySet())- newsRepository.sendNewsResources(sampleNewsResources)- viewModel.updateTopicSelection("1", isChecked = true)- viewModel.updateTopicSelection("1", isChecked = false)-- advanceUntilIdle()- assertEquals(- ForYouUiState(- interestsSelectionState =- ForYouInterestsSelectionUiState.WithInterestsSelection(- 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- )- ),- authors = listOf(- FollowableAuthor(- author = Author(- id = "0",- name = "Android Dev",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = false- ),- FollowableAuthor(- author = Author(- id = "1",- name = "Android Dev 2",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = false- ),- FollowableAuthor(- author = Author(- id = "2",- name = "Android Dev 3",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = false- )- ),+ val collectJob1 =+ launch(UnconfinedTestDispatcher()) { viewModel.interestsSelectionState.collect() }+ val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }++ topicsRepository.sendTopics(sampleTopics)+ userDataRepository.setFollowedTopicIds(emptySet())+ authorsRepository.sendAuthors(sampleAuthors)+ userDataRepository.setFollowedAuthorIds(emptySet())+ newsRepository.sendNewsResources(sampleNewsResources)+ viewModel.updateTopicSelection("1", isChecked = true)+ viewModel.updateTopicSelection("1", isChecked = false)++ advanceUntilIdle()+ assertEquals(+ ForYouInterestsSelectionUiState.WithInterestsSelection(+ topics = listOf(+ FollowableTopic(+ topic = Topic(+ id = "0",+ name = "Headlines",+ shortDescription = "",+ longDescription = "long description",+ url = "URL",+ imageUrl = "image URL",),- feedState = ForYouFeedUiState.Success(- feed = emptyList()- )+ isFollowed = false),- expectMostRecentItem()- )- }+ 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+ )+ ),+ authors = listOf(+ FollowableAuthor(+ author = Author(+ id = "0",+ name = "Android Dev",+ imageUrl = "",+ twitter = "",+ mediumPage = "",+ bio = "",+ ),+ isFollowed = false+ ),+ FollowableAuthor(+ author = Author(+ id = "1",+ name = "Android Dev 2",+ imageUrl = "",+ twitter = "",+ mediumPage = "",+ bio = "",+ ),+ isFollowed = false+ ),+ FollowableAuthor(+ author = Author(+ id = "2",+ name = "Android Dev 3",+ imageUrl = "",+ twitter = "",+ mediumPage = "",+ bio = "",+ ),+ isFollowed = false+ )+ ),+ ),+ viewModel.interestsSelectionState.value+ )+ assertEquals(+ ForYouFeedUiState.Success(+ feed = emptyList()+ ),+ viewModel.feedState.value+ )++ collectJob1.cancel()+ collectJob2.cancel()}@Testfun topicSelectionUpdatesAfterUnselectingAuthor() = runTest {- viewModel.uiState- .test {- topicsRepository.sendTopics(sampleTopics)- userDataRepository.setFollowedTopicIds(emptySet())- authorsRepository.sendAuthors(sampleAuthors)- userDataRepository.setFollowedAuthorIds(emptySet())- newsRepository.sendNewsResources(sampleNewsResources)- viewModel.updateAuthorSelection("1", isChecked = true)- viewModel.updateAuthorSelection("1", isChecked = false)-- advanceUntilIdle()- assertEquals(- ForYouUiState(- interestsSelectionState =- ForYouInterestsSelectionUiState.WithInterestsSelection(- 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- )- ),- authors = listOf(- FollowableAuthor(- author = Author(- id = "0",- name = "Android Dev",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = false- ),- FollowableAuthor(- author = Author(- id = "1",- name = "Android Dev 2",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = false- ),- FollowableAuthor(- author = Author(- id = "2",- name = "Android Dev 3",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = false- )- ),+ val collectJob1 =+ launch(UnconfinedTestDispatcher()) { viewModel.interestsSelectionState.collect() }+ val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }++ topicsRepository.sendTopics(sampleTopics)+ userDataRepository.setFollowedTopicIds(emptySet())+ authorsRepository.sendAuthors(sampleAuthors)+ userDataRepository.setFollowedAuthorIds(emptySet())+ newsRepository.sendNewsResources(sampleNewsResources)+ viewModel.updateAuthorSelection("1", isChecked = true)+ viewModel.updateAuthorSelection("1", isChecked = false)++ assertEquals(++ ForYouInterestsSelectionUiState.WithInterestsSelection(+ topics = listOf(+ FollowableTopic(+ topic = Topic(+ id = "0",+ name = "Headlines",+ shortDescription = "",+ longDescription = "long description",+ url = "URL",+ imageUrl = "image URL",),- feedState = ForYouFeedUiState.Success(- feed = emptyList()- )+ isFollowed = false),- expectMostRecentItem()- )- }+ 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+ )+ ),+ authors = listOf(+ FollowableAuthor(+ author = Author(+ id = "0",+ name = "Android Dev",+ imageUrl = "",+ twitter = "",+ mediumPage = "",+ bio = "",+ ),+ isFollowed = false+ ),+ FollowableAuthor(+ author = Author(+ id = "1",+ name = "Android Dev 2",+ imageUrl = "",+ twitter = "",+ mediumPage = "",+ bio = "",+ ),+ isFollowed = false+ ),+ FollowableAuthor(+ author = Author(+ id = "2",+ name = "Android Dev 3",+ imageUrl = "",+ twitter = "",+ mediumPage = "",+ bio = "",+ ),+ isFollowed = false+ )+ ),+ ),+ viewModel.interestsSelectionState.value+ )+ assertEquals(+ ForYouFeedUiState.Success(+ feed = emptyList()+ ),+ viewModel.feedState.value+ )++ collectJob1.cancel()+ collectJob2.cancel()}@Testfun topicSelectionUpdatesAfterSavingTopicsOnly() = runTest {- viewModel.uiState- .test {- topicsRepository.sendTopics(sampleTopics)- userDataRepository.setFollowedTopicIds(emptySet())- authorsRepository.sendAuthors(sampleAuthors)- userDataRepository.setFollowedAuthorIds(emptySet())- newsRepository.sendNewsResources(sampleNewsResources)- viewModel.updateTopicSelection("1", isChecked = true)-- advanceUntilIdle()- expectMostRecentItem()-- viewModel.saveFollowedInterests()-- advanceUntilIdle()- assertEquals(- ForYouUiState(- interestsSelectionState =- ForYouInterestsSelectionUiState.NoInterestsSelection,- feedState = ForYouFeedUiState.Success(- feed = listOf(- SaveableNewsResource(- newsResource = sampleNewsResources[1],- isSaved = false,- ),- SaveableNewsResource(- newsResource = sampleNewsResources[2],- isSaved = false,- )- )- )+ val collectJob1 =+ launch(UnconfinedTestDispatcher()) { viewModel.interestsSelectionState.collect() }+ val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }++ topicsRepository.sendTopics(sampleTopics)+ userDataRepository.setFollowedTopicIds(emptySet())+ authorsRepository.sendAuthors(sampleAuthors)+ userDataRepository.setFollowedAuthorIds(emptySet())+ newsRepository.sendNewsResources(sampleNewsResources)+ viewModel.updateTopicSelection("1", isChecked = true)++ viewModel.saveFollowedInterests()++ assertEquals(+ ForYouInterestsSelectionUiState.NoInterestsSelection,+ viewModel.interestsSelectionState.value+ )+ assertEquals(+ ForYouFeedUiState.Success(+ feed = listOf(+ SaveableNewsResource(+ newsResource = sampleNewsResources[1],+ isSaved = false,),- expectMostRecentItem()+ SaveableNewsResource(+ newsResource = sampleNewsResources[2],+ isSaved = false,+ ))- assertEquals(setOf("1"), userDataRepository.getCurrentFollowedTopics())- assertEquals(emptySet<Int>(), userDataRepository.getCurrentFollowedAuthors())- }+ ),+ viewModel.feedState.value+ )+ assertEquals(setOf("1"), userDataRepository.getCurrentFollowedTopics())+ assertEquals(emptySet<Int>(), userDataRepository.getCurrentFollowedAuthors())++ collectJob1.cancel()+ collectJob2.cancel()}@Testfun topicSelectionUpdatesAfterSavingAuthorsOnly() = runTest {- viewModel.uiState- .test {- topicsRepository.sendTopics(sampleTopics)- userDataRepository.setFollowedTopicIds(emptySet())- authorsRepository.sendAuthors(sampleAuthors)- userDataRepository.setFollowedAuthorIds(emptySet())- newsRepository.sendNewsResources(sampleNewsResources)- viewModel.updateAuthorSelection("0", isChecked = true)-- advanceUntilIdle()- expectMostRecentItem()-- viewModel.saveFollowedInterests()-- advanceUntilIdle()- assertEquals(- ForYouUiState(- interestsSelectionState =- ForYouInterestsSelectionUiState.NoInterestsSelection,- feedState = ForYouFeedUiState.Success(- feed = listOf(- SaveableNewsResource(- newsResource = sampleNewsResources[0],- isSaved = false- ),- )- )+ val collectJob1 =+ launch(UnconfinedTestDispatcher()) { viewModel.interestsSelectionState.collect() }+ val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }++ topicsRepository.sendTopics(sampleTopics)+ userDataRepository.setFollowedTopicIds(emptySet())+ authorsRepository.sendAuthors(sampleAuthors)+ userDataRepository.setFollowedAuthorIds(emptySet())+ newsRepository.sendNewsResources(sampleNewsResources)+ viewModel.updateAuthorSelection("0", isChecked = true)++ viewModel.saveFollowedInterests()++ assertEquals(+ ForYouInterestsSelectionUiState.NoInterestsSelection,+ viewModel.interestsSelectionState.value+ )+ assertEquals(+ ForYouFeedUiState.Success(+ feed = listOf(+ SaveableNewsResource(+ newsResource = sampleNewsResources[0],+ isSaved = false),- expectMostRecentItem())- assertEquals(emptySet<Int>(), userDataRepository.getCurrentFollowedTopics())- assertEquals(setOf("0"), userDataRepository.getCurrentFollowedAuthors())- }+ ),+ viewModel.feedState.value+ )+ assertEquals(emptySet<Int>(), userDataRepository.getCurrentFollowedTopics())+ assertEquals(setOf("0"), userDataRepository.getCurrentFollowedAuthors())++ collectJob1.cancel()+ collectJob2.cancel()}@Testfun topicSelectionUpdatesAfterSavingAuthorsAndTopics() = runTest {- viewModel.uiState- .test {- topicsRepository.sendTopics(sampleTopics)- userDataRepository.setFollowedTopicIds(emptySet())- authorsRepository.sendAuthors(sampleAuthors)- userDataRepository.setFollowedAuthorIds(emptySet())- newsRepository.sendNewsResources(sampleNewsResources)- viewModel.updateAuthorSelection("1", isChecked = true)- viewModel.updateTopicSelection("1", isChecked = true)-- advanceUntilIdle()- expectMostRecentItem()-- viewModel.saveFollowedInterests()-- advanceUntilIdle()- assertEquals(- ForYouUiState(- interestsSelectionState =- ForYouInterestsSelectionUiState.NoInterestsSelection,- feedState = ForYouFeedUiState.Success(- feed = listOf(- SaveableNewsResource(- newsResource = sampleNewsResources[1],- isSaved = false- ),- SaveableNewsResource(- newsResource = sampleNewsResources[2],- isSaved = false- )- )- )+ val collectJob1 =+ launch(UnconfinedTestDispatcher()) { viewModel.interestsSelectionState.collect() }+ val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }++ topicsRepository.sendTopics(sampleTopics)+ userDataRepository.setFollowedTopicIds(emptySet())+ authorsRepository.sendAuthors(sampleAuthors)+ userDataRepository.setFollowedAuthorIds(emptySet())+ newsRepository.sendNewsResources(sampleNewsResources)+ viewModel.updateAuthorSelection("1", isChecked = true)+ viewModel.updateTopicSelection("1", isChecked = true)++ viewModel.saveFollowedInterests()++ assertEquals(+ ForYouInterestsSelectionUiState.NoInterestsSelection,+ viewModel.interestsSelectionState.value+ )+ assertEquals(+ ForYouFeedUiState.Success(+ feed = listOf(+ SaveableNewsResource(+ newsResource = sampleNewsResources[1],+ isSaved = false),- expectMostRecentItem()+ SaveableNewsResource(+ newsResource = sampleNewsResources[2],+ isSaved = false+ ))- assertEquals(setOf("1"), userDataRepository.getCurrentFollowedTopics())- assertEquals(setOf("1"), userDataRepository.getCurrentFollowedAuthors())- }+ ),+ viewModel.feedState.value+ )+ assertEquals(setOf("1"), userDataRepository.getCurrentFollowedTopics())+ assertEquals(setOf("1"), userDataRepository.getCurrentFollowedAuthors())++ collectJob1.cancel()+ collectJob2.cancel()}@Testfun topicSelectionIsResetAfterSavingTopicsAndRemovingThem() = runTest {- viewModel.uiState- .test {- topicsRepository.sendTopics(sampleTopics)- userDataRepository.setFollowedTopicIds(emptySet())- authorsRepository.sendAuthors(sampleAuthors)- userDataRepository.setFollowedAuthorIds(emptySet())- newsRepository.sendNewsResources(sampleNewsResources)- viewModel.updateTopicSelection("1", isChecked = true)- viewModel.saveFollowedInterests()-- advanceUntilIdle()- expectMostRecentItem()-- userDataRepository.setFollowedTopicIds(emptySet())-- advanceUntilIdle()- assertEquals(- ForYouUiState(- interestsSelectionState =- ForYouInterestsSelectionUiState.WithInterestsSelection(- 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- )- ),- authors = listOf(- FollowableAuthor(- author = Author(- id = "0",- name = "Android Dev",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = false- ),- FollowableAuthor(- author = Author(- id = "1",- name = "Android Dev 2",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = false- ),- FollowableAuthor(- author = Author(- id = "2",- name = "Android Dev 3",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = false- )- )+ val collectJob1 =+ launch(UnconfinedTestDispatcher()) { viewModel.interestsSelectionState.collect() }+ val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }++ topicsRepository.sendTopics(sampleTopics)+ userDataRepository.setFollowedTopicIds(emptySet())+ authorsRepository.sendAuthors(sampleAuthors)+ userDataRepository.setFollowedAuthorIds(emptySet())+ newsRepository.sendNewsResources(sampleNewsResources)+ viewModel.updateTopicSelection("1", isChecked = true)+ viewModel.saveFollowedInterests()++ userDataRepository.setFollowedTopicIds(emptySet())++ assertEquals(+ ForYouInterestsSelectionUiState.WithInterestsSelection(+ topics = listOf(+ FollowableTopic(+ topic = Topic(+ id = "0",+ name = "Headlines",+ shortDescription = "",+ longDescription = "long description",+ url = "URL",+ imageUrl = "image URL",),- feedState = ForYouFeedUiState.Success(- feed = emptyList()- )+ isFollowed = false),-- expectMostRecentItem()+ 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+ )+ ),+ authors = listOf(+ FollowableAuthor(+ author = Author(+ id = "0",+ name = "Android Dev",+ imageUrl = "",+ twitter = "",+ mediumPage = "",+ bio = "",+ ),+ isFollowed = false+ ),+ FollowableAuthor(+ author = Author(+ id = "1",+ name = "Android Dev 2",+ imageUrl = "",+ twitter = "",+ mediumPage = "",+ bio = "",+ ),+ isFollowed = false+ ),+ FollowableAuthor(+ author = Author(+ id = "2",+ name = "Android Dev 3",+ imageUrl = "",+ twitter = "",+ mediumPage = "",+ bio = "",+ ),+ isFollowed = false+ ))- }+ ),+ viewModel.interestsSelectionState.value+ )+ assertEquals(+ ForYouFeedUiState.Success(+ feed = emptyList()+ ),+ viewModel.feedState.value+ )++ collectJob1.cancel()+ collectJob2.cancel()}@Testfun authorSelectionIsResetAfterSavingAuthorsAndRemovingThem() = runTest {- viewModel.uiState- .test {- topicsRepository.sendTopics(sampleTopics)- userDataRepository.setFollowedTopicIds(emptySet())- authorsRepository.sendAuthors(sampleAuthors)- userDataRepository.setFollowedAuthorIds(emptySet())- newsRepository.sendNewsResources(sampleNewsResources)- viewModel.updateAuthorSelection("1", isChecked = true)- viewModel.saveFollowedInterests()-- advanceUntilIdle()- expectMostRecentItem()-- userDataRepository.setFollowedAuthorIds(emptySet())-- advanceUntilIdle()- assertEquals(- ForYouUiState(- interestsSelectionState =- ForYouInterestsSelectionUiState.WithInterestsSelection(- 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- )- ),- authors = listOf(- FollowableAuthor(- author = Author(- id = "0",- name = "Android Dev",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = false- ),- FollowableAuthor(- author = Author(- id = "1",- name = "Android Dev 2",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = false- ),- FollowableAuthor(- author = Author(- id = "2",- name = "Android Dev 3",- imageUrl = "",- twitter = "",- mediumPage = "",- bio = "",- ),- isFollowed = false- )- )+ val collectJob1 =+ launch(UnconfinedTestDispatcher()) { viewModel.interestsSelectionState.collect() }+ val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }++ topicsRepository.sendTopics(sampleTopics)+ userDataRepository.setFollowedTopicIds(emptySet())+ authorsRepository.sendAuthors(sampleAuthors)+ userDataRepository.setFollowedAuthorIds(emptySet())+ newsRepository.sendNewsResources(sampleNewsResources)+ viewModel.updateAuthorSelection("1", isChecked = true)+ viewModel.saveFollowedInterests()++ userDataRepository.setFollowedAuthorIds(emptySet())++ assertEquals(+ ForYouInterestsSelectionUiState.WithInterestsSelection(+ 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",),- feedState = ForYouFeedUiState.Success(- feed = emptyList()- )+ isFollowed = false),- expectMostRecentItem()+ FollowableTopic(+ topic = Topic(+ id = "2",+ name = "Tools",+ shortDescription = "",+ longDescription = "long description",+ url = "URL",+ imageUrl = "image URL",+ ),+ isFollowed = false+ )+ ),+ authors = listOf(+ FollowableAuthor(+ author = Author(+ id = "0",+ name = "Android Dev",+ imageUrl = "",+ twitter = "",+ mediumPage = "",+ bio = "",+ ),+ isFollowed = false+ ),+ FollowableAuthor(+ author = Author(+ id = "1",+ name = "Android Dev 2",+ imageUrl = "",+ twitter = "",+ mediumPage = "",+ bio = "",+ ),+ isFollowed = false+ ),+ FollowableAuthor(+ author = Author(+ id = "2",+ name = "Android Dev 3",+ imageUrl = "",+ twitter = "",+ mediumPage = "",+ bio = "",+ ),+ isFollowed = false+ ))- }+ ),+ viewModel.interestsSelectionState.value+ )+ assertEquals(+ ForYouFeedUiState.Success(+ feed = emptyList()+ ),+ viewModel.feedState.value+ )++ collectJob1.cancel()+ collectJob2.cancel()}@Testfun newsResourceSelectionUpdatesAfterLoadingFollowedTopics() = runTest {- viewModel.uiState- .test {- topicsRepository.sendTopics(sampleTopics)- userDataRepository.setFollowedTopicIds(setOf("1"))- authorsRepository.sendAuthors(sampleAuthors)- userDataRepository.setFollowedAuthorIds(setOf("1"))- newsRepository.sendNewsResources(sampleNewsResources)- viewModel.updateNewsResourceSaved("2", true)-- advanceUntilIdle()- assertEquals(- ForYouUiState(- interestsSelectionState =- ForYouInterestsSelectionUiState.NoInterestsSelection,- feedState = ForYouFeedUiState.Success(- feed = listOf(- SaveableNewsResource(- newsResource = sampleNewsResources[1],- isSaved = true- ),- SaveableNewsResource(- newsResource = sampleNewsResources[2],- isSaved = false- )- )- )+ val collectJob1 =+ launch(UnconfinedTestDispatcher()) { viewModel.interestsSelectionState.collect() }+ val collectJob2 = launch(UnconfinedTestDispatcher()) { viewModel.feedState.collect() }++ topicsRepository.sendTopics(sampleTopics)+ userDataRepository.setFollowedTopicIds(setOf("1"))+ authorsRepository.sendAuthors(sampleAuthors)+ userDataRepository.setFollowedAuthorIds(setOf("1"))+ newsRepository.sendNewsResources(sampleNewsResources)+ viewModel.updateNewsResourceSaved("2", true)++ assertEquals(+ ForYouInterestsSelectionUiState.NoInterestsSelection,+ viewModel.interestsSelectionState.value+ )+ assertEquals(+ ForYouFeedUiState.Success(+ feed = listOf(+ SaveableNewsResource(+ newsResource = sampleNewsResources[1],+ isSaved = true),- expectMostRecentItem()+ SaveableNewsResource(+ newsResource = sampleNewsResources[2],+ isSaved = false+ ))- }+ ),+ viewModel.feedState.value+ )++ collectJob1.cancel()+ collectJob2.cancel()}}diff --git a/feature-interests/src/test/java/com/google/samples/apps/nowinandroid/interests/InterestsViewModelTest.kt b/feature-interests/src/test/java/com/google/samples/apps/nowinandroid/interests/InterestsViewModelTest.ktindex a341f7fd6..c36625f43 100644--- a/feature-interests/src/test/java/com/google/samples/apps/nowinandroid/interests/InterestsViewModelTest.kt+++ b/feature-interests/src/test/java/com/google/samples/apps/nowinandroid/interests/InterestsViewModelTest.kt@@ -16,7 +16,6 @@package com.google.samples.apps.nowinandroid.interests-import app.cash.turbine.testimport com.google.samples.apps.nowinandroid.core.model.data.Authorimport com.google.samples.apps.nowinandroid.core.model.data.FollowableAuthorimport com.google.samples.apps.nowinandroid.core.model.data.FollowableTopic@@ -24,19 +23,26 @@ import com.google.samples.apps.nowinandroid.core.model.data.Topicimport com.google.samples.apps.nowinandroid.core.testing.repository.TestAuthorsRepositoryimport com.google.samples.apps.nowinandroid.core.testing.repository.TestTopicsRepositoryimport com.google.samples.apps.nowinandroid.core.testing.repository.TestUserDataRepository-import com.google.samples.apps.nowinandroid.core.testing.util.TestDispatcherRule+import com.google.samples.apps.nowinandroid.core.testing.util.MainDispatcherRuleimport com.google.samples.apps.nowinandroid.feature.interests.InterestsUiStateimport com.google.samples.apps.nowinandroid.feature.interests.InterestsViewModel+import kotlinx.coroutines.flow.collect+import kotlinx.coroutines.launch+import kotlinx.coroutines.test.UnconfinedTestDispatcherimport kotlinx.coroutines.test.runTestimport org.junit.Assert.assertEqualsimport org.junit.Beforeimport org.junit.Ruleimport org.junit.Test+/**+ * To learn more about how this test handles Flows created with stateIn, see+ * https://developer.android.com/kotlin/flow/test#statein+ */class InterestsViewModelTest {@get:Rule- val dispatcherRule = TestDispatcherRule()+ val mainDispatcherRule = MainDispatcherRule()private val userDataRepository = TestUserDataRepository()private val authorsRepository = TestAuthorsRepository()@@ -54,135 +60,136 @@ class InterestsViewModelTest {@Testfun uiState_whenInitialized_thenShowLoading() = runTest {- viewModel.uiState.test {- assertEquals(InterestsUiState.Loading, awaitItem())- }+ assertEquals(InterestsUiState.Loading, viewModel.uiState.value)}@Testfun uiState_whenFollowedTopicsAreLoading_thenShowLoading() = runTest {- viewModel.uiState.test {- assertEquals(InterestsUiState.Loading, awaitItem())- userDataRepository.setFollowedAuthorIds(setOf("1"))- userDataRepository.setFollowedTopicIds(emptySet())- }+ val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }++ userDataRepository.setFollowedAuthorIds(setOf("1"))+ userDataRepository.setFollowedTopicIds(emptySet())+ assertEquals(InterestsUiState.Loading, viewModel.uiState.value)++ collectJob.cancel()}@Testfun uiState_whenFollowedAuthorsAreLoading_thenShowLoading() = runTest {- viewModel.uiState.test {- assertEquals(InterestsUiState.Loading, awaitItem())- userDataRepository.setFollowedAuthorIds(emptySet())- userDataRepository.setFollowedTopicIds(setOf("1"))- }+ val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }++ userDataRepository.setFollowedAuthorIds(emptySet())+ userDataRepository.setFollowedTopicIds(setOf("1"))+ assertEquals(InterestsUiState.Loading, viewModel.uiState.value)++ collectJob.cancel()}@Testfun uiState_whenFollowingNewTopic_thenShowUpdatedTopics() = runTest {+ val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }+val toggleTopicId = testOutputTopics[1].topic.id- viewModel.uiState- .test {- awaitItem()- authorsRepository.sendAuthors(emptyList())- userDataRepository.setFollowedAuthorIds(emptySet())- topicsRepository.sendTopics(testInputTopics.map { it.topic })- userDataRepository.setFollowedTopicIds(setOf(testInputTopics[0].topic.id))-- assertEquals(- false,- (awaitItem() as InterestsUiState.Interests)- .topics.first { it.topic.id == toggleTopicId }.isFollowed- )-- viewModel.followTopic(- followedTopicId = toggleTopicId,- true- )-- assertEquals(- InterestsUiState.Interests(topics = testOutputTopics, authors = emptyList()),- awaitItem()- )- }+ authorsRepository.sendAuthors(emptyList())+ userDataRepository.setFollowedAuthorIds(emptySet())+ topicsRepository.sendTopics(testInputTopics.map { it.topic })+ userDataRepository.setFollowedTopicIds(setOf(testInputTopics[0].topic.id))++ assertEquals(+ false,+ (viewModel.uiState.value as InterestsUiState.Interests)+ .topics.first { it.topic.id == toggleTopicId }.isFollowed+ )++ viewModel.followTopic(+ followedTopicId = toggleTopicId,+ true+ )++ assertEquals(+ InterestsUiState.Interests(topics = testOutputTopics, authors = emptyList()),+ viewModel.uiState.value+ )++ collectJob.cancel()}@Testfun uiState_whenFollowingNewAuthor_thenShowUpdatedAuthors() = runTest {- viewModel.uiState- .test {- awaitItem()- authorsRepository.sendAuthors(testInputAuthors.map { it.author })- userDataRepository.setFollowedAuthorIds(setOf(testInputAuthors[0].author.id))- topicsRepository.sendTopics(listOf())- userDataRepository.setFollowedTopicIds(setOf())-- awaitItem()- viewModel.followAuthor(- followedAuthorId = testInputAuthors[1].author.id,- followed = true- )-- assertEquals(- InterestsUiState.Interests(topics = emptyList(), authors = testOutputAuthors),- awaitItem()- )- }+ val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }++ authorsRepository.sendAuthors(testInputAuthors.map { it.author })+ userDataRepository.setFollowedAuthorIds(setOf(testInputAuthors[0].author.id))+ topicsRepository.sendTopics(listOf())+ userDataRepository.setFollowedTopicIds(setOf())++ viewModel.followAuthor(+ followedAuthorId = testInputAuthors[1].author.id,+ followed = true+ )++ assertEquals(+ InterestsUiState.Interests(topics = emptyList(), authors = testOutputAuthors),+ viewModel.uiState.value+ )++ collectJob.cancel()}@Testfun uiState_whenUnfollowingTopics_thenShowUpdatedTopics() = runTest {+ val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }+val toggleTopicId = testOutputTopics[1].topic.id- viewModel.uiState- .test {- awaitItem()- authorsRepository.sendAuthors(emptyList())- userDataRepository.setFollowedAuthorIds(emptySet())- topicsRepository.sendTopics(testOutputTopics.map { it.topic })- userDataRepository.setFollowedTopicIds(- setOf(testOutputTopics[0].topic.id, testOutputTopics[1].topic.id)- )-- assertEquals(- true,- (awaitItem() as InterestsUiState.Interests)- .topics.first { it.topic.id == toggleTopicId }.isFollowed- )-- viewModel.followTopic(- followedTopicId = toggleTopicId,- false- )-- assertEquals(- InterestsUiState.Interests(topics = testInputTopics, authors = emptyList()),- awaitItem()- )- }++ authorsRepository.sendAuthors(emptyList())+ userDataRepository.setFollowedAuthorIds(emptySet())+ topicsRepository.sendTopics(testOutputTopics.map { it.topic })+ userDataRepository.setFollowedTopicIds(+ setOf(testOutputTopics[0].topic.id, testOutputTopics[1].topic.id)+ )++ assertEquals(+ true,+ (viewModel.uiState.value as InterestsUiState.Interests)+ .topics.first { it.topic.id == toggleTopicId }.isFollowed+ )++ viewModel.followTopic(+ followedTopicId = toggleTopicId,+ false+ )++ assertEquals(+ InterestsUiState.Interests(topics = testInputTopics, authors = emptyList()),+ viewModel.uiState.value+ )++ collectJob.cancel()}@Testfun uiState_whenUnfollowingAuthors_thenShowUpdatedAuthors() = runTest {- viewModel.uiState- .test {- awaitItem()- authorsRepository.sendAuthors(testOutputAuthors.map { it.author })- userDataRepository.setFollowedAuthorIds(- setOf(testOutputAuthors[0].author.id, testOutputAuthors[1].author.id)- )- topicsRepository.sendTopics(listOf())- userDataRepository.setFollowedTopicIds(setOf())-- awaitItem()- viewModel.followAuthor(- followedAuthorId = testOutputAuthors[1].author.id,- followed = false- )-- assertEquals(- InterestsUiState.Interests(topics = emptyList(), authors = testInputAuthors),- awaitItem()- )- }+ val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }++ authorsRepository.sendAuthors(testOutputAuthors.map { it.author })+ userDataRepository.setFollowedAuthorIds(+ setOf(testOutputAuthors[0].author.id, testOutputAuthors[1].author.id)+ )+ topicsRepository.sendTopics(listOf())+ userDataRepository.setFollowedTopicIds(setOf())++ viewModel.followAuthor(+ followedAuthorId = testOutputAuthors[1].author.id,+ followed = false+ )++ assertEquals(+ InterestsUiState.Interests(topics = emptyList(), authors = testInputAuthors),+ viewModel.uiState.value+ )++ collectJob.cancel()}}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.ktindex bbc183003..e29b33054 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@@ -17,7 +17,6 @@package com.google.samples.apps.nowinandroid.feature.topicimport androidx.lifecycle.SavedStateHandle-import app.cash.turbine.testimport com.google.samples.apps.nowinandroid.core.model.data.FollowableTopicimport com.google.samples.apps.nowinandroid.core.model.data.NewsResourceimport com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType.Video@@ -25,9 +24,12 @@ import com.google.samples.apps.nowinandroid.core.model.data.Topicimport com.google.samples.apps.nowinandroid.core.testing.repository.TestNewsRepositoryimport com.google.samples.apps.nowinandroid.core.testing.repository.TestTopicsRepositoryimport com.google.samples.apps.nowinandroid.core.testing.repository.TestUserDataRepository-import com.google.samples.apps.nowinandroid.core.testing.util.TestDispatcherRule+import com.google.samples.apps.nowinandroid.core.testing.util.MainDispatcherRuleimport com.google.samples.apps.nowinandroid.feature.topic.navigation.TopicDestination+import kotlinx.coroutines.flow.collectimport kotlinx.coroutines.flow.first+import kotlinx.coroutines.launch+import kotlinx.coroutines.test.UnconfinedTestDispatcherimport kotlinx.coroutines.test.runTestimport kotlinx.datetime.Instantimport org.junit.Assert.assertEquals@@ -36,10 +38,14 @@ import org.junit.Beforeimport org.junit.Ruleimport org.junit.Test+/**+ * To learn more about how this test handles Flows created with stateIn, see+ * https://developer.android.com/kotlin/flow/test#statein+ */class TopicViewModelTest {@get:Rule- val dispatcherRule = TestDispatcherRule()+ val dispatcherRule = MainDispatcherRule()private val userDataRepository = TestUserDataRepository()private val topicsRepository = TestTopicsRepository()@@ -59,87 +65,88 @@ class TopicViewModelTest {@Testfun uiStateAuthor_whenSuccess_matchesTopicFromRepository() = runTest {- viewModel.uiState.test {- awaitItem()- topicsRepository.sendTopics(testInputTopics.map(FollowableTopic::topic))- userDataRepository.setFollowedTopicIds(setOf(testInputTopics[1].topic.id))- val item = awaitItem()- assertTrue(item.topicState is TopicUiState.Success)+ val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }- val successTopicState = item.topicState as TopicUiState.Success- val topicFromRepository = topicsRepository.getTopic(- testInputTopics[0].topic.id- ).first()+ topicsRepository.sendTopics(testInputTopics.map(FollowableTopic::topic))+ userDataRepository.setFollowedTopicIds(setOf(testInputTopics[1].topic.id))+ val item = viewModel.uiState.value+ assertTrue(item.topicState is TopicUiState.Success)- assertEquals(topicFromRepository, successTopicState.followableTopic.topic)- }+ val successTopicState = item.topicState as TopicUiState.Success+ val topicFromRepository = topicsRepository.getTopic(+ testInputTopics[0].topic.id+ ).first()++ assertEquals(topicFromRepository, successTopicState.followableTopic.topic)++ collectJob.cancel()}@Testfun uiStateNews_whenInitialized_thenShowLoading() = runTest {- viewModel.uiState.test {- assertEquals(NewsUiState.Loading, awaitItem().newsState)- }+ assertEquals(NewsUiState.Loading, viewModel.uiState.value.newsState)}@Testfun uiStateTopic_whenInitialized_thenShowLoading() = runTest {- viewModel.uiState.test {- assertEquals(TopicUiState.Loading, awaitItem().topicState)- }+ assertEquals(TopicUiState.Loading, viewModel.uiState.value.topicState)}@Testfun uiStateTopic_whenFollowedIdsSuccessAndTopicLoading_thenShowLoading() = runTest {- viewModel.uiState.test {- userDataRepository.setFollowedTopicIds(setOf(testInputTopics[1].topic.id))- assertEquals(TopicUiState.Loading, awaitItem().topicState)- }+ val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }++ userDataRepository.setFollowedTopicIds(setOf(testInputTopics[1].topic.id))+ assertEquals(TopicUiState.Loading, viewModel.uiState.value.topicState)++ collectJob.cancel()}@Testfun uiStateTopic_whenFollowedIdsSuccessAndTopicSuccess_thenTopicSuccessAndNewsLoading() =runTest {- viewModel.uiState.test {- awaitItem()- topicsRepository.sendTopics(testInputTopics.map { it.topic })- userDataRepository.setFollowedTopicIds(setOf(testInputTopics[1].topic.id))- val item = awaitItem()- assertTrue(item.topicState is TopicUiState.Success)- assertTrue(item.newsState is NewsUiState.Loading)- }+ val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }++ topicsRepository.sendTopics(testInputTopics.map { it.topic })+ userDataRepository.setFollowedTopicIds(setOf(testInputTopics[1].topic.id))+ val item = viewModel.uiState.value+ assertTrue(item.topicState is TopicUiState.Success)+ assertTrue(item.newsState is NewsUiState.Loading)++ collectJob.cancel()}@Testfun uiStateTopic_whenFollowedIdsSuccessAndTopicSuccessAndNewsIsSuccess_thenAllSuccess() =runTest {- viewModel.uiState.test {- awaitItem()- topicsRepository.sendTopics(testInputTopics.map { it.topic })- userDataRepository.setFollowedTopicIds(setOf(testInputTopics[1].topic.id))- newsRepository.sendNewsResources(sampleNewsResources)- val item = awaitItem()- assertTrue(item.topicState is TopicUiState.Success)- assertTrue(item.newsState is NewsUiState.Success)- }+ val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }++ topicsRepository.sendTopics(testInputTopics.map { it.topic })+ userDataRepository.setFollowedTopicIds(setOf(testInputTopics[1].topic.id))+ newsRepository.sendNewsResources(sampleNewsResources)+ val item = viewModel.uiState.value+ assertTrue(item.topicState is TopicUiState.Success)+ assertTrue(item.newsState is NewsUiState.Success)++ collectJob.cancel()}@Testfun uiStateTopic_whenFollowingTopic_thenShowUpdatedTopic() = runTest {- viewModel.uiState- .test {- awaitItem()- topicsRepository.sendTopics(testInputTopics.map { it.topic })- // Set which topic IDs are followed, not including 0.- userDataRepository.setFollowedTopicIds(setOf(testInputTopics[1].topic.id))-- viewModel.followTopicToggle(true)-- assertEquals(- TopicUiState.Success(followableTopic = testOutputTopics[0]),- awaitItem().topicState- )- }+ val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }++ topicsRepository.sendTopics(testInputTopics.map { it.topic })+ // Set which topic IDs are followed, not including 0.+ userDataRepository.setFollowedTopicIds(setOf(testInputTopics[1].topic.id))++ viewModel.followTopicToggle(true)++ assertEquals(+ TopicUiState.Success(followableTopic = testOutputTopics[0]),+ viewModel.uiState.value.topicState+ )++ collectJob.cancel()}}