Skip to content

Commit

Permalink
Update Announcement unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Horta committed May 24, 2024
1 parent dfeaf1d commit c232b01
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 103 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package org.wordpress.android.ui.reader.repository

import org.assertj.core.api.Assertions.assertThat
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.junit.MockitoJUnitRunner
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import org.wordpress.android.R
import org.wordpress.android.analytics.AnalyticsTracker
import org.wordpress.android.ui.prefs.AppPrefsWrapper
import org.wordpress.android.ui.reader.tracker.ReaderTracker
import org.wordpress.android.util.config.ReaderAnnouncementCardFeatureConfig
import org.wordpress.android.util.config.ReaderTagsFeedFeatureConfig

@RunWith(MockitoJUnitRunner::class)
class ReaderAnnouncementRepositoryTest {
@Mock
private lateinit var readerAnnouncementCardFeatureConfig: ReaderAnnouncementCardFeatureConfig

@Mock
private lateinit var readerTagsFeedFeatureConfig: ReaderTagsFeedFeatureConfig

@Mock
private lateinit var appPrefsWrapper: AppPrefsWrapper

@Mock
private lateinit var readerTracker: ReaderTracker

private lateinit var repository: ReaderAnnouncementRepository

@Before
fun setUp() {
repository = ReaderAnnouncementRepository(
readerAnnouncementCardFeatureConfig,
readerTagsFeedFeatureConfig,
appPrefsWrapper,
readerTracker
)
}

@Test
fun `given feature config is off the hasReaderAnnouncement is false`() {
// Given
whenever(readerAnnouncementCardFeatureConfig.isEnabled()).thenReturn(false)

// When
val hasAnnouncement = repository.hasReaderAnnouncement()

// Then
assertThat(hasAnnouncement).isFalse()
}

@Test
fun `given should show announcement in prefs is false the hasReaderAnnouncement is false`() {
// Given
whenever(readerAnnouncementCardFeatureConfig.isEnabled()).thenReturn(true)
whenever(appPrefsWrapper.shouldShowReaderAnnouncementCard()).thenReturn(false)

// When
val hasAnnouncement = repository.hasReaderAnnouncement()

// Then
assertThat(hasAnnouncement).isFalse()
}

@Test
fun `given feature config is on and should show announcement in prefs is true the hasReaderAnnouncement is true`() {
// Given
whenever(readerAnnouncementCardFeatureConfig.isEnabled()).thenReturn(true)
whenever(appPrefsWrapper.shouldShowReaderAnnouncementCard()).thenReturn(true)

// When
val hasAnnouncement = repository.hasReaderAnnouncement()

// Then
assertThat(hasAnnouncement).isTrue()
}

@Test
fun `given tags feed feature is off when getReaderAnnouncementItems then return single item`() {
// Given
whenever(readerAnnouncementCardFeatureConfig.isEnabled()).thenReturn(true)
whenever(appPrefsWrapper.shouldShowReaderAnnouncementCard()).thenReturn(true)
whenever(readerTagsFeedFeatureConfig.isEnabled()).thenReturn(false)

// When
val items = repository.getReaderAnnouncementItems()

// Then
assertThat(items).hasSize(1)

val readerPreferencesItem = items[0]
assertThat(readerPreferencesItem.iconRes).isEqualTo(R.drawable.ic_reader_preferences)
assertThat(readerPreferencesItem.titleRes).isEqualTo(
R.string.reader_announcement_card_reading_preferences_title
)
assertThat(readerPreferencesItem.descriptionRes).isEqualTo(
R.string.reader_announcement_card_reading_preferences_description
)
}

@Test
fun `given tags feed feature is on when getReaderAnnouncementItems then return single item`() {
// Given
whenever(readerAnnouncementCardFeatureConfig.isEnabled()).thenReturn(true)
whenever(appPrefsWrapper.shouldShowReaderAnnouncementCard()).thenReturn(true)
whenever(readerTagsFeedFeatureConfig.isEnabled()).thenReturn(true)

// When
val items = repository.getReaderAnnouncementItems()

// Then
assertThat(items).hasSize(2)

val tagsFeedItem = items[0]
assertThat(tagsFeedItem.iconRes).isEqualTo(R.drawable.ic_reader_tag)
assertThat(tagsFeedItem.titleRes).isEqualTo(R.string.reader_announcement_card_tags_stream_title)
assertThat(tagsFeedItem.descriptionRes).isEqualTo(R.string.reader_announcement_card_tags_stream_description)

val readerPreferencesItem = items[1]
assertThat(readerPreferencesItem.iconRes).isEqualTo(R.drawable.ic_reader_preferences)
assertThat(readerPreferencesItem.titleRes).isEqualTo(
R.string.reader_announcement_card_reading_preferences_title
)
assertThat(readerPreferencesItem.descriptionRes).isEqualTo(
R.string.reader_announcement_card_reading_preferences_description
)
}

@Test
fun `when dismissReaderAnnouncement then track`() {
// When
repository.dismissReaderAnnouncement()

// Then
verify(readerTracker).track(AnalyticsTracker.Stat.READER_ANNOUNCEMENT_CARD_DISMISSED)
}

@Test
fun `when dismissReaderAnnouncement then set should show reader announcement card to false`() {
// When
repository.dismissReaderAnnouncement()

// Then
verify(appPrefsWrapper).setShouldShowReaderAnnouncementCard(false)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,9 @@ import org.wordpress.android.ui.reader.viewmodels.ReaderViewModel.TopBarUiState
import org.wordpress.android.util.JetpackBrandingUtils
import org.wordpress.android.util.SnackbarSequencer
import org.wordpress.android.util.UrlUtilsWrapper
import org.wordpress.android.util.config.ReaderAnnouncementCardFeatureConfig
import org.wordpress.android.util.config.ReaderTagsFeedFeatureConfig
import org.wordpress.android.viewmodel.Event
import java.util.Date
import kotlin.test.assertFalse
import kotlin.test.assertTrue

private const val DUMMY_CURRENT_TIME: Long = 10000000000

Expand Down Expand Up @@ -92,9 +89,6 @@ class ReaderViewModelTest : BaseUnitTest() {
@Mock
lateinit var readerTagsFeedFeatureConfig: ReaderTagsFeedFeatureConfig

@Mock
lateinit var readerAnnouncementCardFeatureConfig: ReaderAnnouncementCardFeatureConfig

private val emptyReaderTagList = ReaderTagList()
private val nonEmptyReaderTagList = createNonMockedNonEmptyReaderTagList()

Expand All @@ -118,7 +112,6 @@ class ReaderViewModelTest : BaseUnitTest() {
ReaderTopBarMenuHelper(readerTagsFeedFeatureConfig),
urlUtilsWrapper,
readerTagsFeedFeatureConfig,
readerAnnouncementCardFeatureConfig,
)

whenever(dateProvider.getCurrentDate()).thenReturn(Date(DUMMY_CURRENT_TIME))
Expand Down Expand Up @@ -509,95 +502,6 @@ class ReaderViewModelTest : BaseUnitTest() {
assertThat(showJetpackOverlayEvent.last().peekContent()).isTrue
}

@Test
fun `Should update announcement card UI correctly with tags item`() = testWithNonEmptyTags {
whenever(readerTagsFeedFeatureConfig.isEnabled()).thenReturn(true)

viewModel.onFeedContentLoaded()
val observers = initObservers()

val announcementCardUiState = observers.announcementCardStateEvents.first()

assertThat(announcementCardUiState.items).hasSize(2)

val tagsFeedItem = announcementCardUiState.items[0]
assertThat(tagsFeedItem.iconRes).isEqualTo(R.drawable.ic_reader_tag)
assertThat(tagsFeedItem.titleRes).isEqualTo(R.string.reader_announcement_card_tags_stream_title)
assertThat(tagsFeedItem.descriptionRes).isEqualTo(R.string.reader_announcement_card_tags_stream_description)

val readerPreferencesItem = announcementCardUiState.items[1]
assertThat(readerPreferencesItem.iconRes).isEqualTo(R.drawable.ic_reader_preferences)
assertThat(readerPreferencesItem.titleRes).isEqualTo(
R.string.reader_announcement_card_reading_preferences_title
)
assertThat(readerPreferencesItem.descriptionRes).isEqualTo(
R.string.reader_announcement_card_reading_preferences_description
)
}

@Test
fun `Should update announcement card UI correctly without tags item onFeedContentLoaded`() = testWithNonEmptyTags {
whenever(readerTagsFeedFeatureConfig.isEnabled()).thenReturn(false)

viewModel.onFeedContentLoaded()
val observers = initObservers()

val announcementCardUiState = observers.announcementCardStateEvents.first()

assertThat(announcementCardUiState.items).hasSize(1)

val readerPreferencesItem = announcementCardUiState.items[0]
assertThat(readerPreferencesItem.iconRes).isEqualTo(R.drawable.ic_reader_preferences)
assertThat(readerPreferencesItem.titleRes).isEqualTo(
R.string.reader_announcement_card_reading_preferences_title
)
assertThat(readerPreferencesItem.descriptionRes).isEqualTo(
R.string.reader_announcement_card_reading_preferences_description
)
}

@Test
fun `Should show announcement card if feature flag is enabled, app preference returns true and feed is Discover`() =
testWithNonEmptyTags {
val readerTag = ReaderTag("Discover", "Discover", "Discover", DISCOVER_PATH, ReaderTagType.DEFAULT)
whenever(readerAnnouncementCardFeatureConfig.isEnabled()).thenReturn(true)
whenever(appPrefsWrapper.shouldShowReaderAnnouncementCard()).thenReturn(true)
val observers = initObservers()
triggerContentDisplay()
viewModel.updateSelectedContent(readerTag)
viewModel.onFeedContentLoaded()

val announcementCardUiState = observers.announcementCardStateEvents.first()
assertTrue(announcementCardUiState.shouldShow)
}

@Test
fun `Should NOT show announcement card if feature flag is disabled`() = testWithNonEmptyTags {
val readerTag = ReaderTag("Discover", "Discover", "Discover", DISCOVER_PATH, ReaderTagType.DEFAULT)
whenever(readerAnnouncementCardFeatureConfig.isEnabled()).thenReturn(false)
val observers = initObservers()
triggerContentDisplay()
viewModel.updateSelectedContent(readerTag)
viewModel.onFeedContentLoaded()

val announcementCardUiState = observers.announcementCardStateEvents.first()
assertFalse(announcementCardUiState.shouldShow)
}

@Test
fun `Should NOT show announcement card if app preference returns false`() = testWithNonEmptyTags {
val readerTag = ReaderTag("Discover", "Discover", "Discover", DISCOVER_PATH, ReaderTagType.DEFAULT)
whenever(readerAnnouncementCardFeatureConfig.isEnabled()).thenReturn(true)
whenever(appPrefsWrapper.shouldShowReaderAnnouncementCard()).thenReturn(false)
triggerContentDisplay()
viewModel.updateSelectedContent(readerTag)
viewModel.onFeedContentLoaded()
val observers = initObservers()

val announcementCardUiState = observers.announcementCardStateEvents.first()
assertFalse(announcementCardUiState.shouldShow)
}

private fun assertQsFollowSiteTaskStarted(
observers: Observers,
isSettingsSupported: Boolean = true
Expand Down Expand Up @@ -642,19 +546,13 @@ class ReaderViewModelTest : BaseUnitTest() {
// tabNavigationEvents.add(it.peekContent())
// }

val announcementCardStateEvents = mutableListOf<ReaderViewModel.AnnouncementCardUiState>()
viewModel.announcementCardState.observeForever {
announcementCardStateEvents.add(it)
}

return Observers(uiStates, quickStartReaderPrompts, tabNavigationEvents, announcementCardStateEvents)
return Observers(uiStates, quickStartReaderPrompts, tabNavigationEvents)
}

private data class Observers(
val uiStates: List<ReaderUiState>,
val quickStartReaderPrompts: List<Event<QuickStartReaderPrompt>>,
val tabNavigationEvents: List<TabNavigation>,
val announcementCardStateEvents: List<ReaderViewModel.AnnouncementCardUiState>,
)

private fun triggerContentDisplay(
Expand Down

0 comments on commit c232b01

Please sign in to comment.