Skip to content

Commit

Permalink
Update ReaderTagsFeedViewModel unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Horta committed May 21, 2024
1 parent e67a946 commit 13d03b1
Showing 1 changed file with 88 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ class ReaderTagsFeedViewModelTest : BaseUnitTest() {
)
}

@Suppress("LongMethod")
@Test
fun `given valid tags, when loaded, then UI state should update properly`() = testCollectingUiStates {
// Given
Expand Down Expand Up @@ -225,7 +224,6 @@ class ReaderTagsFeedViewModelTest : BaseUnitTest() {
)
}

@Suppress("LongMethod")
@Test
fun `given valid and invalid tags, when loaded, then UI state should update properly`() = testCollectingUiStates {
// Given
Expand Down Expand Up @@ -342,9 +340,8 @@ class ReaderTagsFeedViewModelTest : BaseUnitTest() {
assertIs<Event<ReaderNavigationEvents.ShowBlogPreview>>(readerNavigationEvents.first())
}

@Suppress("LongMethod")
@Test
fun `given tags fetched, when start again, then nothing happens`() = testCollectingUiStates {
fun `given tags fetched, when onTagsChanged again, then nothing happens`() = testCollectingUiStates {
// Given
val tag1 = ReaderTestUtils.createTag("tag1")
val tag2 = ReaderTestUtils.createTag("tag2")
Expand Down Expand Up @@ -384,19 +381,23 @@ class ReaderTagsFeedViewModelTest : BaseUnitTest() {
verifyNoInteractions(readerPostRepository)
}

@Suppress("LongMethod")
@Test
fun `given tags fetched, when start again refreshing, then move back to initial state`() = testCollectingUiStates {
fun `given new tags fetched, when onTagsChanged again, then state updates`() = testCollectingUiStates {
// Given
val tag1 = ReaderTestUtils.createTag("tag1")
val tag2 = ReaderTestUtils.createTag("tag2")
val tag1 = ReaderTestUtils.createTag("tag1") // will be present both times
val tag2 = ReaderTestUtils.createTag("tag2") // will be present only first time
val tag3 = ReaderTestUtils.createTag("tag3") // will be present only second time

val posts1 = ReaderPostList().apply {
add(ReaderPost())
}
val posts2 = ReaderPostList().apply {
add(ReaderPost())
}
whenever(networkUtilsWrapper.isNetworkAvailable()).thenReturn(true)
val posts3 = ReaderPostList().apply {
add(ReaderPost())
}

whenever(readerPostRepository.fetchNewerPostsForTag(tag1)).doSuspendableAnswer {
delay(100)
posts1
Expand All @@ -405,9 +406,15 @@ class ReaderTagsFeedViewModelTest : BaseUnitTest() {
delay(200)
posts2
}
whenever(readerPostRepository.fetchNewerPostsForTag(tag3)).doSuspendableAnswer {
delay(300)
posts3
}

mockMapInitialTagFeedItems()
mockMapLoadingTagFeedItems()
mockMapLoadedTagFeedItems()
mockMapInitialTagFeedItem()

// When
viewModel.onTagsChanged(listOf(tag1, tag2))
Expand All @@ -417,25 +424,31 @@ class ReaderTagsFeedViewModelTest : BaseUnitTest() {
viewModel.onItemEnteredView(getInitialTagFeedItem(tag2))
advanceUntilIdle()

viewModel.onRefresh()
assertThat(collectedUiStates.last()).isEqualTo(
ReaderTagsFeedViewModel.UiState.Loaded(
data = listOf(
getLoadedTagFeedItem(tag1),
getLoadedTagFeedItem(tag2),
)
)
)

// Then
viewModel.onTagsChanged(listOf(tag1, tag2))
viewModel.onTagsChanged(listOf(tag1, tag3))
advanceUntilIdle()

val loadedState = collectedUiStates.last() as ReaderTagsFeedViewModel.UiState.Loaded
assertThat(loadedState.data).isEqualTo(
listOf(
getInitialTagFeedItem(tag1),
getInitialTagFeedItem(tag2)
assertThat(collectedUiStates.last()).isEqualTo(
ReaderTagsFeedViewModel.UiState.Loaded(
data = listOf(
getLoadedTagFeedItem(tag1), // still loaded even without entering view
getInitialTagFeedItem(tag3),
)
)
)
assertThat(loadedState.isRefreshing).isFalse()
}

@Suppress("LongMethod")
@Test
fun `given no tags requested, when start, then UI state should update properly`() = testCollectingUiStates {
fun `given no tags, when onTagsChanged, then UI state should update properly`() = testCollectingUiStates {
// Given
val tags = emptyList<ReaderTag>()

Expand All @@ -447,7 +460,55 @@ class ReaderTagsFeedViewModelTest : BaseUnitTest() {
assertThat(collectedUiStates).last().isInstanceOf(ReaderTagsFeedViewModel.UiState.Empty::class.java)
}

@Suppress("LongMethod")
@Test
fun `given tags fetched, when onTagsChanged again refreshing, then move back to initial state`() =
testCollectingUiStates {
// Given
val tag1 = ReaderTestUtils.createTag("tag1")
val tag2 = ReaderTestUtils.createTag("tag2")
val posts1 = ReaderPostList().apply {
add(ReaderPost())
}
val posts2 = ReaderPostList().apply {
add(ReaderPost())
}
whenever(networkUtilsWrapper.isNetworkAvailable()).thenReturn(true)
whenever(readerPostRepository.fetchNewerPostsForTag(tag1)).doSuspendableAnswer {
delay(100)
posts1
}
whenever(readerPostRepository.fetchNewerPostsForTag(tag2)).doSuspendableAnswer {
delay(200)
posts2
}
mockMapInitialTagFeedItems()
mockMapLoadingTagFeedItems()
mockMapLoadedTagFeedItems()

// When
viewModel.onTagsChanged(listOf(tag1, tag2))
advanceUntilIdle()
viewModel.onItemEnteredView(getInitialTagFeedItem(tag1))
advanceUntilIdle()
viewModel.onItemEnteredView(getInitialTagFeedItem(tag2))
advanceUntilIdle()

viewModel.onRefresh()

// Then
viewModel.onTagsChanged(listOf(tag1, tag2))
advanceUntilIdle()

val loadedState = collectedUiStates.last() as ReaderTagsFeedViewModel.UiState.Loaded
assertThat(loadedState.data).isEqualTo(
listOf(
getInitialTagFeedItem(tag1),
getInitialTagFeedItem(tag2)
)
)
assertThat(loadedState.isRefreshing).isFalse()
}

@Test
fun `given tags fetched, when refreshing, then update isRefreshing status`() = testCollectingUiStates {
// Given
Expand Down Expand Up @@ -487,7 +548,6 @@ class ReaderTagsFeedViewModelTest : BaseUnitTest() {
assertThat(loadedState.isRefreshing).isTrue()
}

@Suppress("LongMethod")
@Test
fun `given tags fetched, when refreshing, then RefreshTagsFeed action is posted`() = testCollectingUiStates {
// Given
Expand Down Expand Up @@ -527,7 +587,6 @@ class ReaderTagsFeedViewModelTest : BaseUnitTest() {
assertThat(action).isEqualTo(ActionEvent.RefreshTags)
}

@Suppress("LongMethod")
@Test
fun `given tags fetched and no connection, when refreshing, then show error message`() = testCollectingUiStates {
// Given
Expand Down Expand Up @@ -877,6 +936,13 @@ class ReaderTagsFeedViewModelTest : BaseUnitTest() {
}
}

private fun mockMapInitialTagFeedItem() {
whenever(readerTagsFeedUiStateMapper.mapInitialTagFeedItem(any(), any(), any(), any()))
.thenAnswer {
getInitialTagFeedItem(it.getArgument(0))
}
}

private fun getInitialTagFeedItem(tag: ReaderTag) = ReaderTagsFeedViewModel.TagFeedItem(
ReaderTagsFeedViewModel.TagChip(tag, {}, {}),
ReaderTagsFeedViewModel.PostList.Initial
Expand Down

0 comments on commit 13d03b1

Please sign in to comment.