Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ class MySiteViewModel @Inject constructor(
as they're already built on site select. */
private var isSiteSelected = false

/* Editor capabilities rarely change, so once we've successfully fetched them for a site we
skip subsequent non-user-initiated fetches in this ViewModel session. Failed fetches do
not populate this set, so a transient network failure recovers on the next onResume.
User-initiated refreshes (e.g. pull-to-refresh) always bypass this gate. */
private val fetchedCapabilitiesForSite = mutableSetOf<Int>()

val onScrollTo: MutableLiveData<Event<Int>> = MutableLiveData()

val onSnackbarMessage = merge(
Expand Down Expand Up @@ -202,8 +208,14 @@ class MySiteViewModel @Inject constructor(
site: SiteModel,
isUserInitiated: Boolean
) {
if (site.id in fetchedCapabilitiesForSite && !isUserInitiated) {
return
}
val ok = editorSettingsRepository
.fetchEditorCapabilitiesForSite(site)
if (ok) {
fetchedCapabilitiesForSite.add(site.id)
}
val hasCache = editorSettingsRepository
.hasCachedCapabilities(site)
if (!ok && (isUserInitiated || !hasCache)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,89 @@ class MySiteViewModelTest : BaseUnitTest() {
verify(dashboardCardsViewModelSlice).clearValue()
}

@Test
fun `given selected site, when onResume invoked twice, then editor capabilities are fetched once`() = test {
initSelectedSite()

viewModel.onResume()
advanceUntilIdle()
viewModel.onResume()
advanceUntilIdle()

verify(editorSettingsRepository, times(1)).fetchEditorCapabilitiesForSite(siteTest)
}

@Test
fun `given selected site, when onResume then non-PTR refresh, then editor capabilities are fetched once`() =
test {
initSelectedSite()

viewModel.onResume()
advanceUntilIdle()
viewModel.refresh(isPullToRefresh = false)
advanceUntilIdle()

verify(editorSettingsRepository, times(1)).fetchEditorCapabilitiesForSite(siteTest)
}

@Test
fun `given selected site, when onResume then PTR refresh, then editor capabilities are fetched twice`() = test {
initSelectedSite()

viewModel.onResume()
advanceUntilIdle()
viewModel.refresh(isPullToRefresh = true)
advanceUntilIdle()

verify(editorSettingsRepository, times(2)).fetchEditorCapabilitiesForSite(siteTest)
}

@Test
fun `given PTR refresh, when onResume invoked after, then editor capabilities are not re-fetched`() = test {
initSelectedSite()

viewModel.refresh(isPullToRefresh = true)
advanceUntilIdle()
viewModel.onResume()
advanceUntilIdle()

verify(editorSettingsRepository, times(1)).fetchEditorCapabilitiesForSite(siteTest)
}

@Test
fun `given fetch failed, when onResume invoked again, then editor capabilities are re-fetched`() = test {
initSelectedSite()
whenever(editorSettingsRepository.fetchEditorCapabilitiesForSite(siteTest)).thenReturn(false, true)

viewModel.onResume()
advanceUntilIdle()
viewModel.onResume()
advanceUntilIdle()

verify(editorSettingsRepository, times(2)).fetchEditorCapabilitiesForSite(siteTest)
}

@Test
fun `given site switched, when onResume invoked, then editor capabilities are fetched for the new site`() =
test {
initSelectedSite()
val otherSite = SiteModel().apply {
id = TEST_SITE_ID + 1
url = TEST_URL
name = TEST_SITE_NAME
siteId = (TEST_SITE_ID + 1).toLong()
}

viewModel.onResume()
advanceUntilIdle()
whenever(selectedSiteRepository.getSelectedSite()).thenReturn(otherSite)
viewModel.onResume()
advanceUntilIdle()

verify(editorSettingsRepository, times(1)).fetchEditorCapabilitiesForSite(siteTest)
verify(editorSettingsRepository, times(1)).fetchEditorCapabilitiesForSite(otherSite)
}



/* LAND ON THE EDITOR A/B EXPERIMENT */
Expand Down
Loading