|
|
@ -16,7 +16,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
package com.google.samples.apps.nowinandroid.interests
|
|
|
|
package com.google.samples.apps.nowinandroid.interests
|
|
|
|
|
|
|
|
|
|
|
|
import app.cash.turbine.test
|
|
|
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.model.data.Author
|
|
|
|
import com.google.samples.apps.nowinandroid.core.model.data.Author
|
|
|
|
import com.google.samples.apps.nowinandroid.core.model.data.FollowableAuthor
|
|
|
|
import com.google.samples.apps.nowinandroid.core.model.data.FollowableAuthor
|
|
|
|
import com.google.samples.apps.nowinandroid.core.model.data.FollowableTopic
|
|
|
|
import com.google.samples.apps.nowinandroid.core.model.data.FollowableTopic
|
|
|
@ -24,19 +23,26 @@ import com.google.samples.apps.nowinandroid.core.model.data.Topic
|
|
|
|
import com.google.samples.apps.nowinandroid.core.testing.repository.TestAuthorsRepository
|
|
|
|
import com.google.samples.apps.nowinandroid.core.testing.repository.TestAuthorsRepository
|
|
|
|
import com.google.samples.apps.nowinandroid.core.testing.repository.TestTopicsRepository
|
|
|
|
import com.google.samples.apps.nowinandroid.core.testing.repository.TestTopicsRepository
|
|
|
|
import com.google.samples.apps.nowinandroid.core.testing.repository.TestUserDataRepository
|
|
|
|
import 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.MainDispatcherRule
|
|
|
|
import com.google.samples.apps.nowinandroid.feature.interests.InterestsUiState
|
|
|
|
import com.google.samples.apps.nowinandroid.feature.interests.InterestsUiState
|
|
|
|
import com.google.samples.apps.nowinandroid.feature.interests.InterestsViewModel
|
|
|
|
import com.google.samples.apps.nowinandroid.feature.interests.InterestsViewModel
|
|
|
|
|
|
|
|
import kotlinx.coroutines.flow.collect
|
|
|
|
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
|
|
|
|
import kotlinx.coroutines.test.UnconfinedTestDispatcher
|
|
|
|
import kotlinx.coroutines.test.runTest
|
|
|
|
import kotlinx.coroutines.test.runTest
|
|
|
|
import org.junit.Assert.assertEquals
|
|
|
|
import org.junit.Assert.assertEquals
|
|
|
|
import org.junit.Before
|
|
|
|
import org.junit.Before
|
|
|
|
import org.junit.Rule
|
|
|
|
import org.junit.Rule
|
|
|
|
import org.junit.Test
|
|
|
|
import 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 {
|
|
|
|
class InterestsViewModelTest {
|
|
|
|
|
|
|
|
|
|
|
|
@get:Rule
|
|
|
|
@get:Rule
|
|
|
|
val dispatcherRule = TestDispatcherRule()
|
|
|
|
val mainDispatcherRule = MainDispatcherRule()
|
|
|
|
|
|
|
|
|
|
|
|
private val userDataRepository = TestUserDataRepository()
|
|
|
|
private val userDataRepository = TestUserDataRepository()
|
|
|
|
private val authorsRepository = TestAuthorsRepository()
|
|
|
|
private val authorsRepository = TestAuthorsRepository()
|
|
|
@ -54,35 +60,36 @@ class InterestsViewModelTest {
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
fun uiState_whenInitialized_thenShowLoading() = runTest {
|
|
|
|
fun uiState_whenInitialized_thenShowLoading() = runTest {
|
|
|
|
viewModel.uiState.test {
|
|
|
|
assertEquals(InterestsUiState.Loading, viewModel.uiState.value)
|
|
|
|
assertEquals(InterestsUiState.Loading, awaitItem())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
fun uiState_whenFollowedTopicsAreLoading_thenShowLoading() = runTest {
|
|
|
|
fun uiState_whenFollowedTopicsAreLoading_thenShowLoading() = runTest {
|
|
|
|
viewModel.uiState.test {
|
|
|
|
val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }
|
|
|
|
assertEquals(InterestsUiState.Loading, awaitItem())
|
|
|
|
|
|
|
|
userDataRepository.setFollowedAuthorIds(setOf("1"))
|
|
|
|
userDataRepository.setFollowedAuthorIds(setOf("1"))
|
|
|
|
userDataRepository.setFollowedTopicIds(emptySet())
|
|
|
|
userDataRepository.setFollowedTopicIds(emptySet())
|
|
|
|
}
|
|
|
|
assertEquals(InterestsUiState.Loading, viewModel.uiState.value)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
collectJob.cancel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
fun uiState_whenFollowedAuthorsAreLoading_thenShowLoading() = runTest {
|
|
|
|
fun uiState_whenFollowedAuthorsAreLoading_thenShowLoading() = runTest {
|
|
|
|
viewModel.uiState.test {
|
|
|
|
val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }
|
|
|
|
assertEquals(InterestsUiState.Loading, awaitItem())
|
|
|
|
|
|
|
|
userDataRepository.setFollowedAuthorIds(emptySet())
|
|
|
|
userDataRepository.setFollowedAuthorIds(emptySet())
|
|
|
|
userDataRepository.setFollowedTopicIds(setOf("1"))
|
|
|
|
userDataRepository.setFollowedTopicIds(setOf("1"))
|
|
|
|
}
|
|
|
|
assertEquals(InterestsUiState.Loading, viewModel.uiState.value)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
collectJob.cancel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
fun uiState_whenFollowingNewTopic_thenShowUpdatedTopics() = runTest {
|
|
|
|
fun uiState_whenFollowingNewTopic_thenShowUpdatedTopics() = runTest {
|
|
|
|
|
|
|
|
val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }
|
|
|
|
|
|
|
|
|
|
|
|
val toggleTopicId = testOutputTopics[1].topic.id
|
|
|
|
val toggleTopicId = testOutputTopics[1].topic.id
|
|
|
|
viewModel.uiState
|
|
|
|
|
|
|
|
.test {
|
|
|
|
|
|
|
|
awaitItem()
|
|
|
|
|
|
|
|
authorsRepository.sendAuthors(emptyList())
|
|
|
|
authorsRepository.sendAuthors(emptyList())
|
|
|
|
userDataRepository.setFollowedAuthorIds(emptySet())
|
|
|
|
userDataRepository.setFollowedAuthorIds(emptySet())
|
|
|
|
topicsRepository.sendTopics(testInputTopics.map { it.topic })
|
|
|
|
topicsRepository.sendTopics(testInputTopics.map { it.topic })
|
|
|
@ -90,7 +97,7 @@ class InterestsViewModelTest {
|
|
|
|
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
assertEquals(
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
(awaitItem() as InterestsUiState.Interests)
|
|
|
|
(viewModel.uiState.value as InterestsUiState.Interests)
|
|
|
|
.topics.first { it.topic.id == toggleTopicId }.isFollowed
|
|
|
|
.topics.first { it.topic.id == toggleTopicId }.isFollowed
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
@ -101,22 +108,21 @@ class InterestsViewModelTest {
|
|
|
|
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
assertEquals(
|
|
|
|
InterestsUiState.Interests(topics = testOutputTopics, authors = emptyList()),
|
|
|
|
InterestsUiState.Interests(topics = testOutputTopics, authors = emptyList()),
|
|
|
|
awaitItem()
|
|
|
|
viewModel.uiState.value
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
collectJob.cancel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
fun uiState_whenFollowingNewAuthor_thenShowUpdatedAuthors() = runTest {
|
|
|
|
fun uiState_whenFollowingNewAuthor_thenShowUpdatedAuthors() = runTest {
|
|
|
|
viewModel.uiState
|
|
|
|
val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }
|
|
|
|
.test {
|
|
|
|
|
|
|
|
awaitItem()
|
|
|
|
|
|
|
|
authorsRepository.sendAuthors(testInputAuthors.map { it.author })
|
|
|
|
authorsRepository.sendAuthors(testInputAuthors.map { it.author })
|
|
|
|
userDataRepository.setFollowedAuthorIds(setOf(testInputAuthors[0].author.id))
|
|
|
|
userDataRepository.setFollowedAuthorIds(setOf(testInputAuthors[0].author.id))
|
|
|
|
topicsRepository.sendTopics(listOf())
|
|
|
|
topicsRepository.sendTopics(listOf())
|
|
|
|
userDataRepository.setFollowedTopicIds(setOf())
|
|
|
|
userDataRepository.setFollowedTopicIds(setOf())
|
|
|
|
|
|
|
|
|
|
|
|
awaitItem()
|
|
|
|
|
|
|
|
viewModel.followAuthor(
|
|
|
|
viewModel.followAuthor(
|
|
|
|
followedAuthorId = testInputAuthors[1].author.id,
|
|
|
|
followedAuthorId = testInputAuthors[1].author.id,
|
|
|
|
followed = true
|
|
|
|
followed = true
|
|
|
@ -124,17 +130,18 @@ class InterestsViewModelTest {
|
|
|
|
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
assertEquals(
|
|
|
|
InterestsUiState.Interests(topics = emptyList(), authors = testOutputAuthors),
|
|
|
|
InterestsUiState.Interests(topics = emptyList(), authors = testOutputAuthors),
|
|
|
|
awaitItem()
|
|
|
|
viewModel.uiState.value
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
collectJob.cancel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
fun uiState_whenUnfollowingTopics_thenShowUpdatedTopics() = runTest {
|
|
|
|
fun uiState_whenUnfollowingTopics_thenShowUpdatedTopics() = runTest {
|
|
|
|
|
|
|
|
val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }
|
|
|
|
|
|
|
|
|
|
|
|
val toggleTopicId = testOutputTopics[1].topic.id
|
|
|
|
val toggleTopicId = testOutputTopics[1].topic.id
|
|
|
|
viewModel.uiState
|
|
|
|
|
|
|
|
.test {
|
|
|
|
|
|
|
|
awaitItem()
|
|
|
|
|
|
|
|
authorsRepository.sendAuthors(emptyList())
|
|
|
|
authorsRepository.sendAuthors(emptyList())
|
|
|
|
userDataRepository.setFollowedAuthorIds(emptySet())
|
|
|
|
userDataRepository.setFollowedAuthorIds(emptySet())
|
|
|
|
topicsRepository.sendTopics(testOutputTopics.map { it.topic })
|
|
|
|
topicsRepository.sendTopics(testOutputTopics.map { it.topic })
|
|
|
@ -144,7 +151,7 @@ class InterestsViewModelTest {
|
|
|
|
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
assertEquals(
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
(awaitItem() as InterestsUiState.Interests)
|
|
|
|
(viewModel.uiState.value as InterestsUiState.Interests)
|
|
|
|
.topics.first { it.topic.id == toggleTopicId }.isFollowed
|
|
|
|
.topics.first { it.topic.id == toggleTopicId }.isFollowed
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
@ -155,16 +162,16 @@ class InterestsViewModelTest {
|
|
|
|
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
assertEquals(
|
|
|
|
InterestsUiState.Interests(topics = testInputTopics, authors = emptyList()),
|
|
|
|
InterestsUiState.Interests(topics = testInputTopics, authors = emptyList()),
|
|
|
|
awaitItem()
|
|
|
|
viewModel.uiState.value
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
collectJob.cancel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
fun uiState_whenUnfollowingAuthors_thenShowUpdatedAuthors() = runTest {
|
|
|
|
fun uiState_whenUnfollowingAuthors_thenShowUpdatedAuthors() = runTest {
|
|
|
|
viewModel.uiState
|
|
|
|
val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.uiState.collect() }
|
|
|
|
.test {
|
|
|
|
|
|
|
|
awaitItem()
|
|
|
|
|
|
|
|
authorsRepository.sendAuthors(testOutputAuthors.map { it.author })
|
|
|
|
authorsRepository.sendAuthors(testOutputAuthors.map { it.author })
|
|
|
|
userDataRepository.setFollowedAuthorIds(
|
|
|
|
userDataRepository.setFollowedAuthorIds(
|
|
|
|
setOf(testOutputAuthors[0].author.id, testOutputAuthors[1].author.id)
|
|
|
|
setOf(testOutputAuthors[0].author.id, testOutputAuthors[1].author.id)
|
|
|
@ -172,7 +179,6 @@ class InterestsViewModelTest {
|
|
|
|
topicsRepository.sendTopics(listOf())
|
|
|
|
topicsRepository.sendTopics(listOf())
|
|
|
|
userDataRepository.setFollowedTopicIds(setOf())
|
|
|
|
userDataRepository.setFollowedTopicIds(setOf())
|
|
|
|
|
|
|
|
|
|
|
|
awaitItem()
|
|
|
|
|
|
|
|
viewModel.followAuthor(
|
|
|
|
viewModel.followAuthor(
|
|
|
|
followedAuthorId = testOutputAuthors[1].author.id,
|
|
|
|
followedAuthorId = testOutputAuthors[1].author.id,
|
|
|
|
followed = false
|
|
|
|
followed = false
|
|
|
@ -180,9 +186,10 @@ class InterestsViewModelTest {
|
|
|
|
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
assertEquals(
|
|
|
|
InterestsUiState.Interests(topics = emptyList(), authors = testInputAuthors),
|
|
|
|
InterestsUiState.Interests(topics = emptyList(), authors = testInputAuthors),
|
|
|
|
awaitItem()
|
|
|
|
viewModel.uiState.value
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
collectJob.cancel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|