Add unit tests and comments

pull/550/head
Don Turner 3 years ago
parent a98dd001b2
commit e86cdea381

@ -63,7 +63,6 @@ class GetFollowedUserNewsResourcesUseCase @Inject constructor(
* Returns a list of UserNewsResources for topics which the user is following * Returns a list of UserNewsResources for topics which the user is following
*/ */
operator fun invoke(): Flow<List<UserNewsResource>> = operator fun invoke(): Flow<List<UserNewsResource>> =
/** /**
* This sequence of flow transformation functions does the following: * This sequence of flow transformation functions does the following:
* *
@ -72,9 +71,9 @@ class GetFollowedUserNewsResourcesUseCase @Inject constructor(
* - distinctUntilChanged: will only emit a set of followed topic IDs if it's changed. This * - distinctUntilChanged: will only emit a set of followed topic IDs if it's changed. This
* avoids calling potentially expensive operations (like setting up a new flow) when nothing * avoids calling potentially expensive operations (like setting up a new flow) when nothing
* has changed. * has changed.
* - flatMapLatest: getUserNewsResources returns a flow, so we end up with a flow inside a * - flatMapLatest: getUserNewsResources returns a flow, so we have a flow inside a
* flow. flatMapLatest solves this and cancels any previous flows created by * flow. flatMapLatest moves the inner flow (the one we want to return) to the outer flow
* getUserNewsResources. * and cancels any previous flows created by getUserNewsResources.
*/ */
userDataRepository.userData userDataRepository.userData
.map { userData -> .map { userData ->

@ -83,6 +83,55 @@ class GetUserNewsResourcesUseCaseTest {
} }
} }
class GetFollowedUserNewsResourcesUseCaseTest {
@get:Rule
val mainDispatcherRule = MainDispatcherRule()
private val newsRepository = TestNewsRepository()
private val userDataRepository = TestUserDataRepository()
private val getUserNewsResourcesUseCase =
GetUserNewsResourcesUseCase(newsRepository, userDataRepository)
val useCase =
GetFollowedUserNewsResourcesUseCase(userDataRepository, getUserNewsResourcesUseCase)
@Test
fun whenOnboardingShownAndNoTopicsFollowed_emptyListIsReturned() = runTest {
val followedNewsResources = useCase()
// Send some news resources and empty user data
newsRepository.sendNewsResources(sampleNewsResources)
userDataRepository.setUserData(emptyUserData)
// Check that an empty list is returned
assertEquals(
emptyList(),
followedNewsResources.first(),
)
}
@Test
fun whenTopicsAreFollowed_correctNewsResourcesAreReturned() = runTest {
val followedNewsResources = useCase()
// Send some news resources and user data with a followed topic
newsRepository.sendNewsResources(sampleNewsResources)
val userData = emptyUserData.copy(
followedTopics = setOf(sampleTopic1.id),
)
userDataRepository.setUserData(userData)
assertEquals(
sampleNewsResources
.filter { it.topics.contains(sampleTopic1) }
.mapToUserNewsResources(userData),
followedNewsResources.first(),
)
}
}
private val sampleTopic1 = Topic( private val sampleTopic1 = Topic(
id = "Topic1", id = "Topic1",
name = "Headlines", name = "Headlines",

Loading…
Cancel
Save